Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <unistd.h>
  6. #include <ctype.h>
  7. #include <errno.h>
  8.  
  9. extern int errno;
  10.  
  11. #define MAXLEN 128
  12.  
  13. struct Node
  14. {
  15. char *sentence;
  16. struct Node *leftChild;
  17. struct Node *rightChild;
  18. };
  19.  
  20. void insert(char *, struct Node **, bool);
  21. int stringInsensitiveCompare(const char *, const char *);
  22. int stringSensitiveCompare(const char *, const char *);
  23. size_t stringCopy(char *, const char *, size_t);
  24. void readFile(char *, struct Node **, bool);
  25. void writeToFile(char *, struct Node *);
  26. void inorder(struct Node *, FILE *);
  27.  
  28. void insert(char *key, struct Node **node, bool isCaseSensitive)
  29. {
  30. if(*node == NULL)
  31. {
  32. *node = malloc(sizeof(struct Node));
  33. (*node)->sentence = malloc(sizeof(char) * strlen(key) + 1);
  34. stringCopy((*node)->sentence, key, strlen(key) + 1);
  35. (*node)->leftChild = NULL;
  36. (*node)->rightChild = NULL;
  37. }
  38. else
  39. {
  40. int res;
  41.  
  42. if(isCaseSensitive)
  43. res = stringSensitiveCompare(key, (*node)->sentence);
  44. else
  45. res = stringInsensitiveCompare(key, (*node)->sentence);
  46.  
  47. if(res < 0)
  48. insert(key, &(*node)->leftChild, isCaseSensitive);
  49. else
  50. insert(key, &(*node)->rightChild, isCaseSensitive);
  51. }
  52. }
  53.  
  54. int stringSensitiveCompare(const char *str1, const char *str2)
  55. {
  56. int s1;
  57. int s2;
  58.  
  59. do
  60. {
  61. s1 = *str1++;
  62. s2 = *str2++;
  63.  
  64. if(s1 == '\0')
  65. break;
  66. }
  67. while(s1 == s2);
  68.  
  69. return (s1 < s2) ? -1 : (s1 > s2);
  70. }
  71.  
  72. int stringInsensitiveCompare(const char *str1, const char *str2)
  73. {
  74. for(;; str1++, str2++)
  75. {
  76. int res = tolower(*str1) - tolower(*str2);
  77.  
  78. if(res != 0 || !*str1)
  79. return res;
  80. }
  81. }
  82.  
  83. void inorder(struct Node *root, FILE *stream)
  84. {
  85. if(root != NULL)
  86. {
  87. inorder(root->leftChild, stream);
  88. fprintf(stream, "%s\n", root->sentence);
  89. inorder(root->rightChild, stream);
  90. }
  91. }
  92.  
  93. void deleteBST(struct Node **root)
  94. {
  95. if(*root != NULL)
  96. {
  97. deleteBST(&(*root)->leftChild);
  98. deleteBST(&(*root)->rightChild);
  99. free((*root)->sentence);
  100. free((*root));
  101. }
  102. }
  103.  
  104. size_t stringCopy(char *dst, const char *src, size_t dstSize)
  105. {
  106. size_t strLength = strlen(src);
  107.  
  108. if(dstSize > 0)
  109. {
  110. size_t blockSize = (strLength < dstSize - 1 ? strLength : dstSize - 1);
  111. ((char*)memcpy(dst, src, blockSize))[blockSize] = '\0';
  112. }
  113.  
  114. return strLength;
  115. }
  116.  
  117. void readFile(char *filename, struct Node **root, bool isCaseSensitive)
  118. {
  119. FILE *inputFile;
  120. char buffer[MAXLEN];
  121.  
  122. errno = 0;
  123.  
  124. inputFile = fopen(filename, "r");
  125.  
  126. if(inputFile != NULL)
  127. {
  128. while(fgets(buffer, MAXLEN, inputFile) != NULL)
  129. {
  130. buffer[strcspn(buffer, "\n")] = '\0';
  131. insert(buffer, root, isCaseSensitive);
  132. }
  133.  
  134. fclose(inputFile);
  135. }
  136. else
  137. fprintf(stderr, "Error opening file: %s\n", strerror(errno));
  138. }
  139.  
  140. void writeToFile(char *filename, struct Node *root)
  141. {
  142. errno = 0;
  143.  
  144. FILE *outputFile = fopen(filename, "w");
  145.  
  146. if(outputFile != NULL)
  147. {
  148. inorder(root, outputFile);
  149. fclose(outputFile);
  150. }
  151. else
  152. fprintf(stderr, "Error opening file: %s\n", strerror(errno));
  153. }
  154.  
  155. void processUserInput(struct Node **root, bool isCaseSensitive)
  156. {
  157. char sentence[MAXLEN + 1];
  158.  
  159. printf("\nEnter a sentence or simply press enter to quit: ");
  160.  
  161. while ((fgets(sentence, sizeof(sentence), stdin) != NULL) && (sentence[0] != '\n'))
  162. {
  163. sentence[strcspn(sentence, "\n")] = '\0';
  164. insert(sentence, root, isCaseSensitive);
  165. printf("\nEnter another sentence or simply press enter to quit: ");
  166. }
  167. }
  168.  
  169. void printUsage(char *executableName, char *option)
  170. {
  171. fprintf(stderr, "\nThe Option %s is not one of the expected arguments. See usage.\n", option);
  172. fprintf(stderr, "\nUsage: %s | [-c] | [-o output_file_name] | [input_file_name]\n", executableName);
  173. }
  174.  
  175. int main(int argc, char *argv[])
  176. {
  177. struct Node *root = NULL;
  178. int opt = 0;
  179. int outputFileFlag = 0;
  180. int inputFileFlag = 0;
  181. bool isCaseSensitive = false;
  182. char outputFilename[MAXLEN];
  183. char inputFilename[MAXLEN];
  184.  
  185. while ((opt = getopt(argc, argv, "-c-o:")) != -1)
  186. {
  187. switch (opt)
  188. {
  189. case 'c':
  190. isCaseSensitive = true;
  191. break;
  192. case 'o':
  193. stringCopy(outputFilename, argv[optind - 1], strlen(argv[optind - 1] - 1));
  194. outputFileFlag = 1;
  195. break;
  196. case '?':
  197. printUsage(argv[0], argv[optind - 1]);
  198. return EXIT_FAILURE;
  199. default:
  200. stringCopy(inputFilename, argv[optind - 1], strlen(argv[optind - 1] - 1));
  201. inputFileFlag = 1;
  202. }
  203. }
  204.  
  205. if(outputFileFlag && inputFileFlag)
  206. {
  207. readFile(inputFilename, &root, isCaseSensitive);
  208. writeToFile(outputFilename, root);
  209. }
  210. else if(outputFileFlag)
  211. {
  212. processUserInput(&root, isCaseSensitive);
  213. writeToFile(outputFilename, root);
  214. }
  215. else if(inputFileFlag)
  216. {
  217. readFile(inputFilename, &root, isCaseSensitive);
  218. printf("\n");
  219. inorder(root, stdout);
  220. }
  221. else
  222. {
  223. processUserInput(&root, isCaseSensitive);
  224. printf("\n");
  225. inorder(root, stdout);
  226. }
  227.  
  228. deleteBST(&root);
  229.  
  230. return EXIT_SUCCESS;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement