-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.c
More file actions
335 lines (281 loc) · 6.8 KB
/
Copy pathdb.c
File metadata and controls
335 lines (281 loc) · 6.8 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
typedef enum { TYPE_NUM, TYPE_STR, TYPE_BOOL, TYPE_BYTES } Type;
typedef struct {
Type type;
char *name;
} Col;
typedef struct {
Col *schema;
char *name;
uint32_t rows;
uint32_t cols;
char *data;
} Table;
Table **tables = NULL;
int tables_count = 0;
char *
tokenise (char *str, char ***tokens, int *token_count)
{
char c;
int i = 0, in_quotes = 0, done = 0, token_index = 0;
*token_count = 0;
for (i = 0; !done; i++) {
c = str[i];
switch (c) {
case '\0':
(*token_count)++;
done = 1;
case ' ': /* fallthrough */
(*token_count)++;
break;
}
}
*tokens = calloc(*token_count, sizeof **tokens);
*token_count = 0, done = 0;
for (i = 0; !done; i++) {
c = str[i];
switch (c) {
case '\0':
if (in_quotes) return "tokenise: Unclosed string";
(*tokens)[*token_count] = realloc((*tokens)[*token_count], token_index + 1);
(*tokens)[*token_count][token_index] = '\0';
(*token_count)++;
done = 1;
break;
case ' ':
if (in_quotes) {
(*tokens)[*token_count] = realloc((*tokens)[*token_count], token_index + 1);
(*tokens)[*token_count][token_index] = ' ';
token_index++;
} else {
(*tokens)[*token_count] = realloc((*tokens)[*token_count], token_index + 1);
(*tokens)[*token_count][token_index] = '\0';
token_index = 0;
(*token_count)++;
}
break;
case '"':
if (i > 0 && str[i - 1] == '\\') {
(*tokens)[*token_count] = realloc((*tokens)[*token_count], token_index + 1);
(*tokens)[*token_count][token_index] = c;
token_index++;
} else {
in_quotes = !in_quotes;
}
break;
case '\\':
if (i > 0 && str[i - 1] == '\\') {
(*tokens)[*token_count] = realloc((*tokens)[*token_count], token_index + 1);
(*tokens)[*token_count][token_index] = c;
token_index++;
}
break;
default:
(*tokens)[*token_count] = realloc((*tokens)[*token_count], token_index + 1);
(*tokens)[*token_count][token_index] = c;
token_index++;
}
}
return NULL;
}
char *
tableNew(Table *table, char *name, char *text[], int cols) {
if (cols % 2 != 0)
return "Invalid Schema";
*table = (Table) {
.name = strdup(name),
.rows = 0,
.cols = cols / 2,
.schema = malloc(sizeof *table->schema * cols / 2),
.data = NULL
};
for (int i = 0; i < cols; i += 2) {
table->schema[i / 2].name = strdup(text[i]);
switch (text[i + 1][0]) {
case 'n':
table->schema[i / 2].type = TYPE_NUM;
break;
case 's':
table->schema[i / 2].type = TYPE_STR;
break;
case 'b':
table->schema[i / 2].type = TYPE_BOOL;
break;
case 'x':
table->schema[i / 2].type = TYPE_BYTES;
break;
}
}
return NULL;
}
char *
commandNew(char *text)
{
char **tokens = NULL, *err;
int i;
if ((err = tokenise(text, &tokens, &i)))
return err;
Table *t = malloc(sizeof *t);
if ((err = tableNew(t, tokens[0], &tokens[1], i - 1)))
return err;
tables = realloc(tables, sizeof *tables * (tables_count + 1));
tables[tables_count] = t;
tables_count++;
return "new: Success\n";
}
size_t
typeSize(Type t) {
switch (t) {
case TYPE_BOOL:
return sizeof(int);
case TYPE_BYTES:
case TYPE_STR:
return sizeof(char *);
case TYPE_NUM:
return sizeof(double);
}
}
size_t
tableRowSize(Table *t)
{
size_t row_size = 0;
for (uint32_t i = 0; i < t->cols; i++) {
row_size += typeSize(t->schema[i].type);
}
return row_size;
}
char *
tableInsert(Table *t, char **items, uint32_t items_count)
{
if (items_count % t->cols != 0)
return "ins: Incorrect number of columns\n";
uint32_t new_rows = items_count / t->cols;
size_t row_size = tableRowSize(t);
t->data = realloc(t->data, row_size * (t->rows + new_rows));
char *insert = &t->data[t->rows * row_size];
uint32_t item_idx = 0;
for (uint32_t r = 0; r < new_rows; r++) {
for (uint32_t i = 0; i < t->cols; i++, item_idx++) {
switch (t->schema[i].type) {
case TYPE_BOOL:
*(int *)insert = (items[item_idx][0] == 't');
insert += sizeof(int);
break;
case TYPE_BYTES:
case TYPE_STR:
*(char **)insert = strdup(items[item_idx]);
insert += sizeof(char *);
break;
case TYPE_NUM:
*(double *)insert = atof(items[item_idx]);
insert += sizeof(double);
break;
}
}
}
t->rows += new_rows;
return "ins: Success\n";
}
char *
commandInsert(char *row)
{
char **tokens = NULL, *err;
int i, j;
if ((err = tokenise(row, &tokens, &i)))
return err;
Table *t = NULL;
for (j = 0; j < tables_count; j++)
if (strcmp(tables[j]->name, tokens[0]) == 0)
t = tables[j];
if (!t) {
char *err = malloc(sizeof("ins: No table named ") + strlen(tokens[0])); /* sizeof includes null at end */
strcpy(err, "ins: No table named ");
strcat(err, tokens[0]);
return err;
}
return tableInsert(t, &tokens[1], i - 1);
}
char *
tableGet(Table *table, char **criteria, int criteria_count)
{
size_t row_size = tableRowSize(table);
for (uint32_t i = 0; i < table->rows; i++) {
int passed = 1;
for (int j = 0; j < criteria_count; j++) {
char *col = strdup(criteria[j]);
char *val = strchr(col, '=');
if (!val) return "get: Invalid criteria";
*val = '\0';
val++;
int found = 0;
char *current_cell = table->data + row_size * i;
for (uint32_t k = 0; k < table->cols; k++) {
char *cell = current_cell;
current_cell += typeSize(table->schema[k].type);
if (strcmp(table->schema[k].name, col) != 0)
continue;
found = 1;
switch (table->schema[k].type) {
case TYPE_STR:
case TYPE_BYTES:
printf("%d %s != %s\n", i, *(char **)cell, val);
if (strcmp(*(char **)cell, val) != 0)
passed = 0;
break;
case TYPE_NUM:
printf("num %lf != %lf\n", *(double *)cell, strtod(val, NULL));
if (*(double *)cell != strtod(val, NULL)) {
passed = 0;
}
break;
case TYPE_BOOL:
if (*(int *)cell != (val[0] == 't'))
passed = 0;
break;
}
break;
}
free(col);
if (!found)
return "get: No column matching criteria";
if (!passed)
break;
}
/* todo: return all rows */
if (passed) {
char ret[256];
snprintf(ret, 256, "get: index %d\n", i);
return strdup(ret);
}
}
return "";
}
char *
commandGet(char *cmd)
{
char **tokens;
int token_count, j;
tokenise(cmd, &tokens, &token_count);
Table *t = NULL;
for (j = 0; j < tables_count; j++) {
if (strcmp(tables[j]->name, tokens[0]) == 0)
t = tables[j];
}
if (!t) {
char *err = malloc(sizeof("get: No table named ") + strlen(tokens[0])); /* sizeof includes null at end */
strcpy(err, "get: No table named ");
strcat(err, tokens[0]);
return err;
}
return tableGet(t, &tokens[1], token_count - 1);
}
int main(void) {
printf("%s", commandNew(strdup("ages age n name s")));
printf("%s", commandInsert(strdup("ages 21 \"joe smith\" 10 bob")));
printf("%s", commandGet(strdup("ages name=bob age=10")));
return 0;
}