summaryrefslogtreecommitdiff
path: root/word.c
diff options
context:
space:
mode:
Diffstat (limited to 'word.c')
-rw-r--r--word.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/word.c b/word.c
new file mode 100644
index 0000000..e6570c7
--- /dev/null
+++ b/word.c
@@ -0,0 +1,87 @@
+/* This file is a part of WordExtract project
+ *
+ * Copyright (C) 2009 Borisov Alexandr
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "word.h"
+#include "dict.h"
+
+inline char *wordcpy(char *word);
+inline void free_word_record(Word *record);
+
+int to_list(char *word)
+{
+ if (!is_in_dict(word, dict)) {
+ words = add_word_record(words, word);
+ }
+ return 0;
+}
+
+Word *add_word_record(Word *root, char *word)
+{
+ int cond;
+
+ if (root == NULL) {
+ root = malloc(sizeof(Word));
+ root->word = wordcpy(word);
+ root->lsibl = root->rsibl = NULL;
+ }
+ else {
+ cond = strcmp(word, root->word);
+ if (cond > 0)
+ root->rsibl = add_word_record(root->rsibl, word);
+ else if (cond < 0)
+ root->lsibl = add_word_record(root->lsibl, word);
+ }
+ return root;
+}
+
+void free_words(Word *root)
+{
+ if (root->lsibl != NULL)
+ free_words(root->lsibl);
+ if (root->rsibl != NULL)
+ free_words(root->rsibl);
+ free_word_record(root);
+}
+
+void print_words(Word *root)
+{
+ if (root != NULL) {
+ print_words(root->lsibl);
+ print_words(root->rsibl);
+ printf("%s\n", root->word);
+ }
+}
+
+inline void free_word_record(Word *record)
+{
+ free(record->word);
+ free(record);
+}
+
+inline char *wordcpy(char *word)
+{
+ char *p;
+ p = malloc(strlen(word)+1);
+ if (p != NULL)
+ strcpy(p, word);
+ return p;
+}
+