8 #include "hash_index.h"
12 #define SETCOLOR_GREEN "\033[1;32m"
13 #define SETCOLOR_RED "\033[1;31m"
14 #define SETCOLOR_NORMAL "\033[0;39m"
17 void print_green(char c
)
20 printf("%s%c%s", SETCOLOR_GREEN
, c
, SETCOLOR_NORMAL
);
25 static char *get_file(char *keyword
)
27 static char hash_file
[512] = HASH_DIR
;
29 sprintf(hash_file
+ HASH_DIR_LEN
, "%02x/", do_hash(keyword
));
30 strcat(hash_file
, keyword
);
34 static void print_3_line(char *data
, char *word
, uint32_t file_pos
)
37 char *p
= data
+ file_pos
;
75 * count: is the keywords count.
76 * keywords: The address of the keywords strings.
78 static void query(int count
, char *keywords
[])
87 for (i
= 0; i
< count
; i
++) {
89 hash_file
= get_file(key
);
90 fd1
= open(hash_file
, O_RDONLY
);
93 while(read(fd1
, &kw
, sizeof kw
) > 0) {
94 fd2
= open(kw
.file
, O_RDONLY
);
96 printf("Open file %s error!\n", kw
.file
);
99 read(fd2
, buf
, sizeof buf
);
100 print_3_line(buf
, key
, kw
.file_pos
);
101 printf("FROM file: %s\n\n", kw
.file
);
112 static void usage(void)
114 printf("USAGE: query keywords ....\n");
118 int main(int argc
, char *argv
[])
125 query(argc
- 1, &argv
[1]);