summaryrefslogtreecommitdiff
path: root/dict.c
diff options
context:
space:
mode:
authorb0ric <b0risov.alexandr@rambler.ru>2009-08-08 20:18:38 +0300
committerb0ric <b0risov.alexandr@rambler.ru>2009-08-08 20:18:38 +0300
commit343357ed1e7907cf4b488058053df280ae63c7bb (patch)
tree201915ed25612180af3745131be66ce19edf1a0b /dict.c
parent88db5cdceabf042f5e5d5695bf984900f8396225 (diff)
Now words are shown in alphabetical order
Diffstat (limited to 'dict.c')
-rw-r--r--dict.c50
1 files changed, 45 insertions, 5 deletions
diff --git a/dict.c b/dict.c
index 2995822..c8a9a65 100644
--- a/dict.c
+++ b/dict.c
@@ -16,18 +16,34 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <sys/stat.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "dict.h"
+#include "main.h"
-Word *load_dict(FILE *dict)
+Word *dict;
+
+static void create_dict_file(char *path);
+
+Word *load_dict()
{
+ FILE *fdict;
Word *root = NULL;
char word[WORDLENGTH] = {0};
int i;
- while (!feof(dict)) {
- fgets(word, WORDLENGTH, dict);
+ if (!(fdict = fopen(dictfile, "r"))) {
+ perror(dictfile);
+ create_dict_file(optpath);
+ if (!(fdict = fopen(dictfile, "r"))) {
+ perror(dictfile);
+ exit(1);
+ }
+ }
+ while (!feof(fdict)) {
+ fgets(word, WORDLENGTH, fdict);
if (strcmp(word, "\0") == 0) /*to not process final blank line*/
break;
word[strlen(word)-1] = '\0';
@@ -35,6 +51,7 @@ Word *load_dict(FILE *dict)
for (i = 0; i < WORDLENGTH; i++)
word[i] = 0;
}
+ fclose(fdict);
return root;
}
@@ -44,8 +61,7 @@ int is_in_dict(char *word, Word *dic_rec)
if (dic_rec == NULL)
return NOT_IN_DICT;
- else {
-
+ else {
if ((cond = strcmp(word, dic_rec->word)) == 0)
return IN_DICT;
else if (cond > 0)
@@ -55,3 +71,27 @@ int is_in_dict(char *word, Word *dic_rec)
}
}
+void create_dict_file(char *path)
+{
+ mode_t mode_0755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
+ struct stat st;
+ FILE *fdict;
+ char file[PATH_LENGTH] = {0};
+
+ if (stat(path, &st)) {
+ fprintf(stderr, "%s: Creating directory\n", path);
+ if (mkdir(path, mode_0755)) {
+ perror(path);
+ exit(1);
+ }
+ }
+ strcat(file, path);
+ strcat(file, DICT_FILE);
+ fprintf(stderr, "%s: Creating blank dictionary file\n", file);
+ if (!(fdict = fopen(file, "w"))) {
+ perror(file);
+ exit(1);
+ }
+ fclose(fdict);
+}
+