-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.c
91 lines (83 loc) · 1.34 KB
/
dict.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <string.h>
#include <stdlib.h>
#include <spelling/dict.h>
/* Dictionary */
struct word dict[DICT_WORDS];
/* The logical length for dictionary */
int dicts=0;
static char filename[FILENAME_MAX];
int dict_init(char *f)
{
strncpy(filename,f,FILENAME_MAX);
char buf[WORD_LENGTH];
FILE *file=fopen(filename,"r");
if(file==NULL){
app_print("Dictionary `%s' not found, creating...",filename);
if((file=fopen(filename,"w"))==NULL){
perror(filename);
exit(1);
}
goto bye;
}
scan_dict:
if(dicts>DICT_WORDS){
goto bye;
}
fscanf(file,"%s",buf);
if(buf[0]!='#'){
strcpy(dict[dicts++].name,buf);
goto scan_dict;
}
bye:
fclose(file);
return 0;
}
int dict_delete(char *word)
{
int i=dict_check(word);
if(i>=0){
dict[i].name[0]='\0';
return 0;
}
return -1;
}
int dict_check(char *word)
{
int i;
for(i=0;i<dicts;i++){
if(dict[i].name[0]=='\0'){
continue;
}
if(!strcmp(dict[i].name,word)){
return i;
}
}
return -1;
}
int dict_close(void)
{
int i;
FILE *file=fopen(filename,"w");
if(file==NULL){
perror(filename);
exit(1);
}
for(i=0;i<dicts;i++){
if(dict[i].name[0]=='\0'){
continue;
}
fprintf(file,"%s\n",dict[i].name);
}
fprintf(file,"#\n");
fflush(file);
fclose(file);
return 0;
}
int dict_add(char *word)
{
if(dicts<DICT_WORDS){
strcpy(dict[dicts++].name,word);
return 0;
}
return 1;
}