-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_config.c
More file actions
315 lines (286 loc) · 8.31 KB
/
database_config.c
File metadata and controls
315 lines (286 loc) · 8.31 KB
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
#include "database.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include "dialog.h"
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int catValue(const char *filepath, const DBRecord *record){
/*returns index of DBRecord with matching key & cat */
int read_fd,re,start = 0;
char keyBuffer[DB_KEYLEN], catBuffer[DB_CATLEN];
read_fd = open(filepath,O_RDONLY);
if(read_fd < 0){
perror("Error while Reading");
return -42;
}
while(1){
if(record->key == NULL || record->cat == NULL){
break;
}
re = read(read_fd,keyBuffer,DB_KEYLEN);
if(re == 0){
return -1;
}
if(strcmp(record->key,keyBuffer)==0){
re = read(read_fd,catBuffer,DB_CATLEN);
if(strcmp(record->cat,catBuffer)==0){
break;
}
}
start += 1;
lseek(read_fd,(start)*sizeof(DBRecord),SEEK_SET);
}
close(read_fd);
return start;
}
int db_search(const char *filepath, int start, DBRecord *record){
int read_fd, re;
int idx = sizeof(DBRecord) * start;
char keyBuffer[DB_KEYLEN], catBuffer[DB_CATLEN];
read_fd = open(filepath,O_RDONLY);
if(read_fd < 0){
perror("Error while Reading");
return -42;
}
idx = lseek(read_fd,idx,SEEK_SET);
if(idx < 0){
perror("Error while setting Index");
return -42;
}
while(1){
if(record->key == NULL || record->cat == NULL){
break;
}
re = read(read_fd,keyBuffer,DB_KEYLEN);
if(re == 0){
return -1;
}
if(strcmp(record->key,keyBuffer)==0){
break;
}
re = read(read_fd,catBuffer,DB_CATLEN);
if(strcmp(record->cat,catBuffer)==0){
break;
}
start += 1;
idx = lseek(read_fd,(start)*sizeof(DBRecord),SEEK_SET);
}
close(read_fd);
return start;
}
int db_get(const char *filepath, int index, DBRecord *result){
int read_fd,re;
int idx = index * sizeof(DBRecord);
char keyBuffer[DB_KEYLEN], catBuffer[DB_CATLEN], valBuffer[DB_VALLEN];
read_fd = open(filepath,O_RDONLY);
if(read_fd < 0){
return -1;
}
idx = lseek(read_fd,idx,SEEK_SET);
re = read(read_fd,keyBuffer,DB_KEYLEN);
if(re == 0){
perror("Error while Reading Key");
return -1;
}
re = read(read_fd,catBuffer,DB_CATLEN);
if(re == 0){
perror("Error while Reading Cat");
return -1;
}
re = read(read_fd,valBuffer,DB_VALLEN);
if(re == 0){
perror("Error while Reading Value");
return -1;
}
strcpy(result->key,keyBuffer);
strcpy(result->cat, catBuffer);
strcpy(result->value,valBuffer);
return 0;
}
int db_size(const char *filepath){
int read_fd, re,i;
char keyBuffer[DB_KEYLEN], catBuffer[DB_CATLEN],valBuffer[DB_VALLEN];
char *buffer[3];
int counter = 0,bufferSize []= {DB_KEYLEN,DB_CATLEN,DB_VALLEN};
buffer[0] = keyBuffer;
buffer[1] = catBuffer;
buffer[2] = valBuffer;
/*vorher geschriebene Datei oeffnen*/
read_fd = open(filepath,O_RDONLY);
if(read_fd < 0){
perror("error while writing");
exit(3);
}
/* w = write(outfd,*/
while(1){
/*Buffersize variiert fuer key, cat und Inhalt*/
for(i = 0; i < 3; i++){
re = read(read_fd,buffer[i],bufferSize[i]);
if(re == 0){
return counter;
}else if(re < 0){
perror("Error while reading");
break;
}
}
counter ++;
}
close(read_fd);
return -1;
}
int db_put(const char *filepath, int index, const DBRecord *record){
int read_fd,wr;
int idx = index * sizeof(DBRecord);
/*1. pruefen ob record ans ende gehört - wenn j amit Append öffenen*/
if(idx > 0 && idx < db_size(filepath)){
read_fd = open(filepath,O_RDWR );
}else{
read_fd = open(filepath,O_RDWR|O_APPEND );
}
if(read_fd < 0){
perror("Error while Reading");
return -1;
}
if(idx >= 0){
idx = lseek(read_fd,idx,SEEK_SET);
}
wr = write(read_fd,record,sizeof(DBRecord));
if(wr < 0){
perror("Error while Writing");
return -1;
}
close(read_fd);
return 0;
}
int db_update(const char *filepath, const DBRecord *new){
int read_fd,wr;
int idx = 0;
idx = catValue(filepath,new);
if(idx < 0){
read_fd = open(filepath,O_RDWR|O_APPEND );
wr = write(read_fd,new,sizeof(DBRecord));
if(wr < 0){
perror("Error while writing Record");
return -1;
}
close(read_fd);
return db_size(filepath);
}
read_fd = open(filepath,O_RDWR );
idx = idx * sizeof(DBRecord) + DB_KEYLEN + DB_CATLEN;
lseek(read_fd,idx,SEEK_SET);
wr = write(read_fd,new->value,DB_VALLEN);
if(wr < 0){
perror("Error while writing Record");
return -1;
}
return 0;
}
int db_del(const char *filepath, int index){
int read_fd, fd_write,wr,re, dbIndex = 0;
char *tmpFile = "temp";
char DBBuffer[sizeof(DBRecord)];
/*Index nicht vorhanden */
if(index < 0 || index > db_size(filepath)){
perror("No Record with Index");
return -1;
}
fd_write = open(tmpFile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
read_fd = open(filepath, O_RDONLY);
while(1){
if(dbIndex != index){
re = read(read_fd,DBBuffer,sizeof(DBRecord));
if(re == 0){
break;
}else if(re < 0){
perror("Error while reading");
break;
}
wr = write(fd_write,DBBuffer,sizeof(DBRecord));
if( wr < 0){
perror("Error while writing");
}
}else{
lseek(read_fd,sizeof(DBRecord),SEEK_CUR);
}
dbIndex += 1;
}
close(read_fd);
close(fd_write);
rename(tmpFile,filepath);
return 0;
}
void writeFailure(int wr){
if( wr < 0){
perror("Error while writing");
exit(4);
}
}
/*0 = stdin, 1 = stdout, 2 = stder*/
int db_list(const char *path, int outfd, int (*filter)(DBRecord *rec, const void *data), const void *data){
int read_fd, re,wr,j, valid = 1;
DBRecord *akt = malloc(sizeof(DBRecord));
char keyBuffer[DB_KEYLEN], catBuffer[DB_CATLEN],valBuffer[DB_VALLEN];
int counter = 0;
/*vorher geschriebene Datei oeffnen*/
read_fd = open(path,O_RDONLY);
if(read_fd < 0){
perror("error while writing");
exit(3);
}
/* w = write(outfd,*/
while(1){
re = read(read_fd,keyBuffer,DB_KEYLEN);
if(re == 0){
return counter;
}else if(re < 0){
perror("Error while reading");
break;
}
re = read(read_fd,catBuffer,DB_CATLEN);
if(re == 0){
return counter;
}else if(re < 0){
perror("Error while reading");
break;
}
re = read(read_fd,valBuffer,DB_VALLEN);
if(re == 0){
return counter;
}else if(re < 0){
perror("Error while reading");
break;
}
strcpy(akt->key, keyBuffer);
strcpy(akt->cat, catBuffer);
strcpy(akt->value, valBuffer);
if(filter != NULL){
valid = filter(akt, data);
}
if(valid){
wr = write(outfd, keyBuffer, DB_KEYLEN);
writeFailure(wr);
for(j = 0; j < DB_KEYLEN-strlen(keyBuffer); j++){
wr = write(outfd," ", 1);
}
writeFailure(wr);
wr = write(outfd,"|",1);
wr = write(outfd, catBuffer, DB_CATLEN);
writeFailure(wr);
for(j = 0; j < DB_CATLEN-strlen(catBuffer); j++){
wr = write(outfd," ", 1);
}
wr = write(outfd,"|",1);
wr = write(outfd, valBuffer, DB_VALLEN);
writeFailure(wr);
wr = write(outfd,"\n",1);
}
counter ++;
}
close(read_fd);
close(outfd);
return -1;
}