-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
195 lines (176 loc) · 5.66 KB
/
search.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @file search.c
* @author Mathieu Allory
* @date February 2018
* @brief Derrick DFS: deep file search and indexing library
* @ref https://github.com/thew44/derrick
*
* @details Sample program that uses Derrick DFS to do indexed of plain-text search
*
* @license MIT License
*/
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "derrick.h"
#define CMD_QUIT "quit"
#define CMD_INDEX "index"
#define CMD_LIST "list"
#define CMD_PRINT "print"
#define CMD_FIND "find"
#define CMD_DFS "search"
#define CMD_BASE "base"
#define CMD_COUNT "count"
int Callback_Exclude(void* ctx, const char* file)
{
// exclude files containing ".git" in their name
if (strstr(file, ".git") != NULL) return 1;
// some other rules
if (strstr(file, ".sqlite") != NULL) return 1;
// OK keep it
return 0;
}
void Callback_Found(void* ctx, const char* in, const char* where)
{
if (where)
printf("%s\n[%s]\n", in, where);
else
printf("%s\n", in);
}
void PrintLastErrorMessage()
{
//Get the error message, if any.
DWORD errorMessageID = GetLastError();
if(errorMessageID == 0)
return ;
LPSTR messageBuffer = 0;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
printf("%s\n", messageBuffer);
//Free the buffer.
LocalFree(messageBuffer);
}
static int getLine (char *prmpt, char *buff, size_t sz)
{
int ch, extra;
// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf ("%s", prmpt);
fflush (stdout);
}
if (fgets (buff, (int)sz, stdin) == NULL)
return DERRICK_NO_INPUT;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff)-1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? DERRICK_TOO_LONG : DERRICK_OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff)-1] = '\0';
return DERRICK_OK;
}
int main(int argc, char *argv[])
{
int rc;
char buff[100];
char* base = 0;
DerrickIndex pIndexBuffer = 0;
// Main command loop
while (strcmp(buff, CMD_QUIT))
{
rc = getLine ("search> ", buff, sizeof(buff));
if (rc == DERRICK_NO_INPUT)
{
printf ("\nNo input\n");
return 1;
}
if (rc == DERRICK_TOO_LONG)
{
printf ("Input too long [%s]\n", buff);
return 1;
}
if (strlen(buff) == 0)
{}
else if (strlen(buff) >= strlen(CMD_INDEX) && !strncmp(buff, CMD_INDEX, strlen(CMD_INDEX)))
{
if (base != 0)
{
derrick_index_build(&pIndexBuffer, base);
}
}
else if (strlen(buff) >= strlen(CMD_PRINT) && !strncmp(buff, CMD_PRINT, strlen(CMD_PRINT)))
{
size_t offset = atoi(buff + strlen(CMD_PRINT) + 1);
char cbuffer[1000];
memcpy(cbuffer, pIndexBuffer + offset, 1000);
printf("%s\n", cbuffer);
}
else if (strlen(buff) >= strlen(CMD_LIST) && !strncmp(buff, CMD_LIST, strlen(CMD_LIST)))
{
if (pIndexBuffer != 0)
{
derrick_index_list(pIndexBuffer);
}
}
else if (strlen(buff) >= strlen(CMD_FIND) && !strncmp(buff, CMD_FIND, strlen(CMD_FIND)))
{
const char* needle = buff + strlen(CMD_FIND) + 1;
if (needle != 0)
{
struct Derrick_Parameters_s cb;
cb.cb_exclude = &Callback_Exclude;
cb.cd_found = &Callback_Found;
cb.ctx_exclude = 0;
cb.ctx_found = 0;
derrick_index_search(pIndexBuffer, needle, &cb);
}
}
else if (strlen(buff) >= strlen(CMD_BASE) && !strncmp(buff, CMD_BASE, strlen(CMD_BASE)))
{
if (base)
{
free(base);
base = 0;
}
const char* param = buff + strlen(CMD_BASE) + 1;
if (param)
{
base = _strdup(buff + strlen(CMD_BASE) + 1);
printf("Base directory [%s]\n", base);
}
}
else if (strlen(buff) >= strlen(CMD_DFS) && !strncmp(buff, CMD_DFS, strlen(CMD_DFS)))
{
const char* needle = buff + strlen(CMD_DFS) + 1;
if (needle != 0 && base !=0)
{
struct Derrick_Parameters_s cb;
cb.cb_exclude = &Callback_Exclude;
cb.cd_found = &Callback_Found;
cb.ctx_exclude = 0;
cb.ctx_found = 0;
derrick_deep_search(needle, base, &cb);
}
}
else if (strlen(buff) >= strlen(CMD_COUNT) && !strncmp(buff, CMD_COUNT, strlen(CMD_COUNT)))
{
if (base !=0)
{
struct Derrick_Parameters_s cb;
cb.cb_exclude = &Callback_Exclude;
cb.cd_found = &Callback_Found;
cb.ctx_exclude = 0;
cb.ctx_found = 0;
printf("Number of files: %d\n", derrick_count_files(base, &cb));
}
}
else if (!(strlen(buff) >= strlen(CMD_QUIT) && !strncmp(buff, CMD_QUIT, strlen(CMD_QUIT))))
{
printf ("UNKNOWN [%s]\n", buff);
}
}
return 0;
}