-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdb.c
350 lines (274 loc) · 7.32 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
342
343
344
345
346
347
348
349
350
//#define FOUND 0
//#define 3
//#define 4
/*
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
// 0 if ,
//
// 1) opens the database
// 2) gets a line
// 3) extracts the key
// 4) checks the value to see if it's the one we're looking for
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, ':');
// 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;
}
//--------------------------------
// returns
// 0 if not found
// id number if found
//
int db_get_id(char* firstname)
{
int i;
char id_string[20];
char db_name[20];
char string[20];
char key[80];
int result;
for(i=1; i<100; i++)
{
// look up name, retreive id
// make id_string
// strcpy(id_string,"#");
// itoa(i, string,10); // integer to string. integer, string, base
// strcat(id_string, string );
sprintf(clipboard, "#%d", i); // make id string
//printf("D119");
// make key
sprintf(key, "%s > firstname", clipboard);
//printf("D122");
result = db_lookup(key, db_name);
//printf("D124");
if(result == 0)continue;
if(strcmp(db_name, firstname) == 0){
return i;
}
}
return 0; // not in the database
}
//--------------------------------------------------------
//
// 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];
char string[20];
int result;
char key[20];
char id_string[20];
for(i=1; i<100; i++){
// look for id, first_name
//printf("I: %d", i);
//strcpy(clipboard, "#"); printf("N220");
//itoa(i, string,10);
//strcat(clipboard, string);
sprintf(clipboard, "#%d", i); // make id string
strcpy(key, clipboard);
strcat(key, " > class");
//strcpy(key,"#");
//strcat(key, id_string);
//strcat(key, " > class");
result = db_lookup(key,value);
if(result != FOUND){
// printf("%d, %s ", i, id_string);
//sscanf(id_string, "%d", i);
//strcpy();
//printf(" S: %s", id_string);
return i;
}
// printf("N245");
}
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 i;
for(i=0; i<80; i++){
if (from[i] == delimiter){
to[i] = NULL; //
return i + 1;
}
else{
to[i] = from[i];
//i++;
}
}
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++){
strcpy(key, subject); // prepare to lookup
strcat(key," > class"); // prepare to lookup
result = db_lookup(key, value); // lookup
//printf("\nSW:%s LF:%s SUB:%s K:%s V:%s R:%d ", startingwith, lookingfor, subject, key, value, result);
if(result == NOT_FOUND) return NOT_FOUND; // if not in database at all, exit
if(strcmp(value,lookingfor)==0)return FOUND; // is it what we're looking for?
// if(strcmp(value,"root")==0)return FOUND; // has it reached root?
if(strcmp(value,"root")==0)break; // has it reached root?
strcpy(subject,value); // if no luck so far, the value becomes the subject
}
return NOT_FOUND; // not found
}