forked from micro222/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.c
345 lines (275 loc) · 7.3 KB
/
db.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
lookup value (key, value)
add pair (key, value)
change value (key, value)
remove pair (key)
*/
#include "db.h"
//--------------------------------------------------
//
// GET VALUE FROM GENERAL DATABASE
//
// inputs
// key, value
//
// returns
// FOUND or NOT_FOUND
//
// 1) opens the database
// 2) gets a line
// 3) extracts the key
// 4) checks the key to see if it's the one we're looking for
// 5) goes to step 2 if not
// 6) returns value
//
int db_lookup(char*target_key, char*db_value){
FILE *general;
int linepos;
char *status;
char line[80];
char db_key[60];
// open general knowlege database
general = fopen("general.txt","r");
if(general == NULL) return CANT_OPEN_FILE;
// search for key
while(1){
// get a line
status = fgets(line,80,general);
if (status==0){
fclose(general);
return NOT_FOUND;
}
// get the key
linepos = copy_to_delimiter(line, db_key, ':', 0);
// is it the key we're looking for?
// if not, get another line
if(strcmp(target_key, db_key) != 0) continue;
break;
} // end of key search
// get value
db_copy_word(line, linepos, db_value);
fclose(general);
return FOUND; // (found)
}
//-------------------------------------------------------------------
//
// example: db_add_pair("grass > color","green");
//
//
int db_add_pair(char*key, char*value){
FILE *general;
//open
general = fopen("general.txt","a");
if(general == NULL) {printf("fopen failed while trying to open general.txt");}
//add key and value
fprintf(general, "%s:%s\n", key, value);
// close
fclose(general);
return 1;
}
//--------------------------------
// look up a first name and return the id number
// returns 0 if error
//
//
int db_get_id(char* firstname)
{
int id;
char id_string[20], name[20];
char key[80];
int result;
for(id=1; id<1000; id++)
{
snprintf (id_string, sizeof(id_string), "%d",id); // convert id number from integer to string (integer, string, base)
sprintf(key, "#%s > firstname", id_string);
result = db_lookup(key, name);
// look up the first name
if(result==FOUND)
{
result = strcmp(name, firstname);
if(result==0)
{
// That's the entity we're looking for
return id;
}
// That's not the entity we're looking for, so try again
else {
continue;
}
}
}
return 0; // database size limit reached (DGI)
}
//--------------------------------------------------------
//
// looks for the specified key and changes its value
//
//
int db_change_value(char*key, char*value){
FILE *general;
FILE *temp;
char *status;
char line[80];
char db_key[60];
//open files
general = fopen("general.txt","r");
if(general == NULL) {
printf("DB: can't open general.txt");
return 2;
}
temp = fopen("temp.txt","w");
if(temp == NULL) {
printf("DB: can't open temp.txt");
fclose(general);
return 2;
}
// search for a key, copy each line
while(1){
// get a line
status = fgets(line,80,general);
if (status==0){
fclose(general);
fclose(temp);
return 1; //(key not found)
}
db_copy_word(line,1, db_key);
// is it the key we're looking for?
// if not, get another line
if(strcmp(key, db_key)==0) {
fprintf(temp, "%s", line); // copy line to temp file
break;
}
} // end of subject search
//add key and value
fprintf(temp, "%s: %s\n", key, value);
// copy remainder of file
// fprintf(temp, "%s", line); // copy line to temp file
while(1){
// get another line
status = fgets(line,80,general);
if (status==0) break;
fprintf(temp, "%s", line); // copy line to temp file
}
// close
fclose(general);
fclose(temp);
if(remove("general.txt") != 0) {printf("oops, file error ""%s"" \n", strerror(1));}
if(rename("temp.txt","general.txt") != 0) {printf("file error ""%s"" \n", strerror(1));}
return 0; // (success)
}
//--------------------------------------------------------
int db_next_available_id(void){
int i;
char value[20];
int result;
char key[20], id_string[20];
//id[0]=0;
for(i=1; i<1000; i++){
// look for id, first_name
snprintf (id_string, sizeof(id_string), "%d",i);
strcpy(key,"#");
strcat(key, id_string);
strcat(key, " > class");
result = db_lookup(key,value);
if(result != FOUND){
// printf("%d, %s ", i, id_string);
return i;
}
}
printf("error in function DNAI");
return 0;
}
//----------------------------------------------------------
//
//
//
//
//
int db_copy_word(char* line, int position, char* value)
{
int i;
for(i=0; i<MAX_LETTERS-2; i++)
{
//skip over leading spaces and tabs
if(line[position]==' ') position++;
if(line[position]==' ') position++;
if(line[position]==' ') position++;
if(line[position]=='\t') position++;
if(line[position]=='\t') position++;
if(line[position]=='\t') position++;
if ((isalpha(line[position])==0) && (isdigit(line[position])==0))
{
value[i]=0;
return position;
}
else
{
value[i]=line[position];
position++;
}
}
return position;
}
//------------------------------
//
// COPIES A STRING UP TO THE DELIMITER
//
// parameters: from, to
//
// returns: the position after the delimiter
//
// usualy the copy then gets compared to a key that's being searched for
//
int copy_to_delimiter(char* from, char* to, char delimiter, int start){
int i;
for(i=0; i<80; i++){
if (from[i+start] == delimiter){
to[i] = NULL; //
return i + 1 + start;
}
else if (from[i+start] == 10 || from[i+start] == 13){
to[i] = NULL; //
return 0;
}
else{
to[i] = from[i+start];
}
}
i++;
to[i+start] = NULL;
return i;
}
//-----------------------------------------------------------------------
//
// ex: is cat a creature?
// db_root_check("cat", "creature");
//
int db_root_check(char* startingwith, char* lookingfor){
// animal creature
int result;
int n;
char subject[60];
char key[60];
char value[20];
strcpy(subject, startingwith);
for(n=0; n<5; n++){
sprintf(key, "%s > class", subject);
result = db_lookup(key, value); // lookup
if(result == NOT_FOUND) return NOT_FOUND; // if not in database at all, exit
if(strcmp(value,lookingfor)==0)return MATCH; // is it what we're looking for?
if(strcmp(value,"root")==0)return NO_MATCH; // has it reached to root?
strcpy(subject,value); // if no luck so far, the value becomes the subject
}
return NO_MATCH; // not found
}
//-----------------------------------------------------------------------
//
// ex:
// db_check("cat");
//
int db_check(char* subject){
char key[60];
char value[20];
sprintf(key, "%s > class", subject);
return db_lookup(key, value); // lookup
}