8 #include "hash_index.h"
11 static inline int is_ss_char(char c
)
13 if ((c
>= 'A' && c
<= 'Z') ||
14 (c
>= 'a' && c
<= 'z') ||
15 (c
>= '0' && c
<= '9'))
21 static inline int is_space(char c
)
26 static char word
[128];
28 * Get a word from _p_, skip the none-ss-char first,
29 * then returen the begining of the word in _word_,
30 * and last, return the begining of the next word.
32 static char *get_word(char *str
)
36 while (*str
&& !is_ss_char(*str
))
42 while (is_ss_char(*str
) || *str
== '.')
49 static void tolower_str(char *str
)
52 if (*str
>= 'A' && *str
<= 'Z')
59 * Do the boring but Cool thing
61 static void parse_word(void)
63 int len
= strlen(word
);
64 char *p
= word
+ len
- 1;
66 /* Drop the last period mark */
73 /* 'ies' and 'ied' to 'y' */
74 if (!strncmp(p
- 2, "ies", 3) ||
75 !strncmp(p
- 2, "ied", 3)) {
82 * Drop the trailing 'e', making keywords like 'waiting' and 'waite'
91 * treat the word with 's' ending as plural,
92 * even it may be not true
98 if (!strncmp(p
- 2, "ing", 3)) {
104 if (!strncmp(p
- 1, "er", 2)) {
110 if (!strncmp(p
- 1, "ed", 2)) {
119 static void install_word(char *dir
, char *word
,
120 const char *file
, uint32_t file_pos
)
125 struct key_words key
;
127 strcpy(hash_file
, dir
);
128 strcat(hash_file
, "/");
129 strcat(hash_file
, word
);
131 if (access(hash_file
, F_OK
) < 0)
133 fd
= open(hash_file
, O_WRONLY
| O_APPEND
| flag
, 0644);
135 printf("ERROR: open hash file %s error!\n", hash_file
);
137 memset(&key
, 0, sizeof key
);
138 key
.file_pos
= file_pos
;
139 strcpy(key
.file
, file
);
140 if ((write(fd
, &key
, sizeof key
)) < 0)
141 printf("EROOR: write hash file %s error!\n", hash_file
);
145 static void hash_word(char *word
, const char *file
, uint32_t file_pos
)
148 char hash_dir
[HASH_DIR_LEN
+ 3]= HASH_DIR
;
150 hash
= do_hash(word
);
152 sprintf(hash_dir
+ HASH_DIR_LEN
, "%02x", hash
);
153 if (access(hash_dir
, F_OK
) < 0) {
154 if (mkdir(hash_dir
, 0700) < 0) {
155 printf("mkdir: %s error!\n", hash_dir
);
159 install_word(hash_dir
, word
, file
, file_pos
);
162 static void do_parse(char *buf
, const char *file
, uint32_t file_pos
)
173 hash_word(word
, file
, file_pos
+ p
- buf
- strlen(word
));
178 static void parse_file(const char *file
)
181 uint32_t file_pos
= 0;
184 fd
= open(file
, O_RDONLY
);
186 printf("Open file _%s_ error!\n");
190 while((read(fd
, buf
, sizeof buf
)) > 0) {
191 do_parse(buf
, file
, file_pos
);
192 file_pos
+= sizeof buf
;
206 printf("open current directory error!\n");
210 while((de
= readdir(dir
))) {
212 * we do nothing to these files started '.',
213 * "." and ".." included.
215 if (!strcmp(de
->d_name
, "a.out") ||
219 printf("Parsing file %s\n", de
->d_name
);
221 parse_file(de
->d_name
);