Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <ctype.h>
- #include <errno.h>
- extern int errno;
- #define MAXLEN 128
- struct Node
- {
- char *sentence;
- struct Node *leftChild;
- struct Node *rightChild;
- };
- void insert(char *, struct Node **, bool);
- int stringInsensitiveCompare(const char *, const char *);
- int stringSensitiveCompare(const char *, const char *);
- size_t stringCopy(char *, const char *, size_t);
- void readFile(char *, struct Node **, bool);
- void writeToFile(char *, struct Node *);
- void inorder(struct Node *, FILE *);
- void insert(char *key, struct Node **node, bool isCaseSensitive)
- {
- if(*node == NULL)
- {
- *node = malloc(sizeof(struct Node));
- (*node)->sentence = malloc(sizeof(char) * strlen(key) + 1);
- stringCopy((*node)->sentence, key, strlen(key) + 1);
- (*node)->leftChild = NULL;
- (*node)->rightChild = NULL;
- }
- else
- {
- int res;
- if(isCaseSensitive)
- res = stringSensitiveCompare(key, (*node)->sentence);
- else
- res = stringInsensitiveCompare(key, (*node)->sentence);
- if(res < 0)
- insert(key, &(*node)->leftChild, isCaseSensitive);
- else
- insert(key, &(*node)->rightChild, isCaseSensitive);
- }
- }
- int stringSensitiveCompare(const char *str1, const char *str2)
- {
- int s1;
- int s2;
- do
- {
- s1 = *str1++;
- s2 = *str2++;
- if(s1 == '\0')
- break;
- }
- while(s1 == s2);
- return (s1 < s2) ? -1 : (s1 > s2);
- }
- int stringInsensitiveCompare(const char *str1, const char *str2)
- {
- for(;; str1++, str2++)
- {
- int res = tolower(*str1) - tolower(*str2);
- if(res != 0 || !*str1)
- return res;
- }
- }
- void inorder(struct Node *root, FILE *stream)
- {
- if(root != NULL)
- {
- inorder(root->leftChild, stream);
- fprintf(stream, "%s\n", root->sentence);
- inorder(root->rightChild, stream);
- }
- }
- void deleteBST(struct Node **root)
- {
- if(*root != NULL)
- {
- deleteBST(&(*root)->leftChild);
- deleteBST(&(*root)->rightChild);
- free((*root)->sentence);
- free((*root));
- }
- }
- size_t stringCopy(char *dst, const char *src, size_t dstSize)
- {
- size_t strLength = strlen(src);
- if(dstSize > 0)
- {
- size_t blockSize = (strLength < dstSize - 1 ? strLength : dstSize - 1);
- ((char*)memcpy(dst, src, blockSize))[blockSize] = '\0';
- }
- return strLength;
- }
- void readFile(char *filename, struct Node **root, bool isCaseSensitive)
- {
- FILE *inputFile;
- char buffer[MAXLEN];
- errno = 0;
- inputFile = fopen(filename, "r");
- if(inputFile != NULL)
- {
- while(fgets(buffer, MAXLEN, inputFile) != NULL)
- {
- buffer[strcspn(buffer, "\n")] = '\0';
- insert(buffer, root, isCaseSensitive);
- }
- fclose(inputFile);
- }
- else
- fprintf(stderr, "Error opening file: %s\n", strerror(errno));
- }
- void writeToFile(char *filename, struct Node *root)
- {
- errno = 0;
- FILE *outputFile = fopen(filename, "w");
- if(outputFile != NULL)
- {
- inorder(root, outputFile);
- fclose(outputFile);
- }
- else
- fprintf(stderr, "Error opening file: %s\n", strerror(errno));
- }
- void processUserInput(struct Node **root, bool isCaseSensitive)
- {
- char sentence[MAXLEN + 1];
- printf("\nEnter a sentence or simply press enter to quit: ");
- while ((fgets(sentence, sizeof(sentence), stdin) != NULL) && (sentence[0] != '\n'))
- {
- sentence[strcspn(sentence, "\n")] = '\0';
- insert(sentence, root, isCaseSensitive);
- printf("\nEnter another sentence or simply press enter to quit: ");
- }
- }
- void printUsage(char *executableName, char *option)
- {
- fprintf(stderr, "\nThe Option %s is not one of the expected arguments. See usage.\n", option);
- fprintf(stderr, "\nUsage: %s | [-c] | [-o output_file_name] | [input_file_name]\n", executableName);
- }
- int main(int argc, char *argv[])
- {
- struct Node *root = NULL;
- int opt = 0;
- int outputFileFlag = 0;
- int inputFileFlag = 0;
- bool isCaseSensitive = false;
- char outputFilename[MAXLEN];
- char inputFilename[MAXLEN];
- while ((opt = getopt(argc, argv, "-c-o:")) != -1)
- {
- switch (opt)
- {
- case 'c':
- isCaseSensitive = true;
- break;
- case 'o':
- stringCopy(outputFilename, argv[optind - 1], strlen(argv[optind - 1] - 1));
- outputFileFlag = 1;
- break;
- case '?':
- printUsage(argv[0], argv[optind - 1]);
- return EXIT_FAILURE;
- default:
- stringCopy(inputFilename, argv[optind - 1], strlen(argv[optind - 1] - 1));
- inputFileFlag = 1;
- }
- }
- if(outputFileFlag && inputFileFlag)
- {
- readFile(inputFilename, &root, isCaseSensitive);
- writeToFile(outputFilename, root);
- }
- else if(outputFileFlag)
- {
- processUserInput(&root, isCaseSensitive);
- writeToFile(outputFilename, root);
- }
- else if(inputFileFlag)
- {
- readFile(inputFilename, &root, isCaseSensitive);
- printf("\n");
- inorder(root, stdout);
- }
- else
- {
- processUserInput(&root, isCaseSensitive);
- printf("\n");
- inorder(root, stdout);
- }
- deleteBST(&root);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement