-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
97 lines (92 loc) · 1.64 KB
/
main.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
92
93
94
95
96
97
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <spelling/dict.h>
#include <spelling/suggestion.h>
/* Input buffer */
char buf[BUFSIZ];
char outbuf[BUFSIZ];
/* Suggestion buffer */
struct word sug[10];
int app_print(const char *fmt,...)
{
va_list vlist;
va_start(vlist,fmt);
vsprintf(outbuf,fmt,vlist);
va_end(vlist);
return printf(APPNAME ": %s\n",outbuf);
}
int do_word(char *word)
{
char flag=0;
int i,len;
len=strlen(word);
char tmp[len];
/* Convert into lower cases */
for(i=0;i<len;i++){
tmp[i]=tolower(word[i]);
}
tmp[i]='\0';
if(dict_check(tmp)<0){
flag=1;
len=provide_suggestions(tmp,sug,10);
printf("%s: ",word);
if(len==0){
printf("Unknown word\nAdd to dictionary [y/n]");
gets(buf);
if(tolower(buf[0])=='y'){
/* Add the word to the dictionary */
dict_add(tmp);
}
return flag;
}
printf("Do you mean:");
for(i=0;i<len;i++){
printf("\t%d. %s",i+1,sug[i].name);
}
printf("\nOr add to dictionary [y/n]");
gets(buf);
if(tolower(buf[0])=='y'){
dict_add(tmp);
}
}
return flag;
}
int main(int argc,char **argv)
{
int i,len,flag;
char *tmp=NULL;
dict_init(DICT_FILENAME);
/* If there are words in the argument */
if(argc>1){
for(i=1;i<argc;i++){
flag=do_word(argv[i]);
}
if(!flag){
puts("No problem");
}
return 0;
}
check_words:
printf("> ");
gets(buf);
if(buf[0]=='#'){
goto bye;
}
if(buf[0]=='!'){
dict_delete(&buf[1]);
goto check_words;
}
len=strlen(buf);
tmp=strtok(buf," ");
if(tmp==NULL){
goto check_words;
}
do{
do_word(tmp);
}while((tmp=strtok(NULL," "))!=NULL);
goto check_words;
bye: dict_close();
return 0;
}