This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
litedb.cpp
2500 lines (2015 loc) · 68.1 KB
/
litedb.cpp
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Created by Zijie Wu on 2019-03-28.
* The entire Database assignment in one GIANT source file, just to make compiling easier
*
* It comprises of several components: data loader, parser, catalog, optimizer and execution engine
*
* Its mainly in C99, but to use map, I have to introduce c++11 under optimizer section.
*/
#ifndef LITE_DB_LITEDB_C
#define LITE_DB_LITEDB_C
//#define DEBUG_PROFILING 1
//#define DEBUG_STACK_SIZE 1
#ifdef DEBUG_PROFILING
#include <time.h>
static long time_query = 0;
static long time_total = 0;
// number of times buffer hit during a query
static long count_buffer_hit_query = 0;
static long count_buffer_total_query = 0;
// number of times buffer hit throughout entire program
static long count_buffer_hit_total = 0;
static long count_buffer_total = 0;
#endif
/////////////////
// C++ library //
/////////////////
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#include <ctype.h>
// runtime assert
#define ASSERT(val) \
do {\
if (!(val)) {\
fprintf(stderr, "%s:%d: runtime assert failed\n", __FILE__, __LINE__);\
exit(99);\
}\
} while(0)
/*
_______
/ \
$$$$$$$ | ______ ______ _______ ______ ______
$$ |__$$ |/ \ / \ / | / \ / \
$$ $$/ $$$$$$ |/$$$$$$ |/$$$$$$$/ /$$$$$$ |/$$$$$$ |
$$$$$$$/ / $$ |$$ | $$/ $$ \ $$ $$ |$$ | $$/
$$ | /$$$$$$$ |$$ | $$$$$$ |$$$$$$$$/ $$ |
$$ | $$ $$ |$$ | / $$/ $$ |$$ |
$$/ $$$$$$$/ $$/ $$$$$$$/ $$$$$$$/ $$/
*/
#ifndef PARSE_STACK_INIT_SIZE
#define PARSE_STACK_INIT_SIZE 256
#endif
#define EXPECT(c, ch) do { ASSERT(c->input[0] == (ch)); } while(0)
#define EXPECT_ALPHABET(c) do { \
ASSERT(('a' <= c->input[0] && c->input[0] <= 'z') || ('A' <= c->input[0] && c->input[0] <= 'Z')); } while(0)
#define IS_ALPHABET_OR_NUMERIC(ch) ((ch) >= '0' && (ch) <= '9' || (ch) >= 'a' && (ch) <= 'z' || (ch) >= 'A' && (ch) <= 'Z')
#define IS_NUMERIC(ch) ((ch) >= '0' && (ch) <= '9')
//////////
// enum //
//////////
// this is the type of operations involved in the query predicates
// =, <, >
typedef enum {
EQUAL,
LESS_THAN,
GREATER_THAN
} enum_operator;
typedef enum {
PARSE_OK = 0,
PARSE_FAILED
} enum_return_value;
////////////
// struct //
////////////
typedef struct {
// always inited with 26 element
// 2D array of path to each csv file
char **files;
// number of files that are actually there
size_t length;
} struct_input_files;
// SUM(D.c0)
typedef struct {
// single char, represents the name of relation
char relation;
// int, represents the name of column
int column;
} struct_relation_column;
// SELECT SUM(D.c0), SUM(D.c4), SUM(C.c1)
typedef struct {
struct_relation_column *sums;
// size of above array
// number of SUM queries
size_t length;
} struct_first_line;
// FROM A, B, C, D
typedef struct {
char *relations;
// length of string, not including \0
size_t length;
} struct_second_line;
// A.c1 = B.c0
typedef struct {
// length will be 2, lhs and rhs
struct_relation_column lhs;
struct_relation_column rhs;
} struct_join;
// WHERE A.c1 = B.c0 AND A.c3 = D.c0 AND C.c2 = D.c2
typedef struct {
struct_join *joins;
// number of join operations
size_t length;
} struct_third_line;
// D.c3 < -9496
typedef struct {
struct_relation_column lhs;
enum_operator op;
int rhs;
} struct_predicate;
// AND C.c2 = 2247;
typedef struct {
struct_predicate *predicates;
// number of predicates
size_t length;
} struct_fourth_line;
/*
SELECT SUM(D.c0), SUM(D.c4), SUM(C.c1)
FROM A, B, C, D
WHERE A.c1 = B.c0 AND A.c3 = D.c0 AND C.c2 = D.c2
AND D.c3 = -9496;
*/
typedef struct {
struct_first_line first;
struct_second_line second;
struct_third_line third;
struct_fourth_line fourth;
} struct_query;
typedef struct {
struct_query *queries;
size_t length;
} struct_queries;
// this struct stores information during parsing
typedef struct {
const char *input;
char *stack;
size_t size, top;
} struct_parse_context;
/*
* pop the stack by moving the top index of the stack down
* must memcpy the memory because they may be occupied by future stack push
*/
static void *context_pop(struct_parse_context *c, size_t size) {
ASSERT(c->top >= size && size != 0);
c->top -= size;
return c->stack + c->top;
}
/*
* push the stack by moving the top index of the stack up
* return a pointer to the allocated memory where the pushed object should store
*/
static void *context_push(struct_parse_context *c, size_t size) {
ASSERT(size > 0);
void *ret;
// expand 1.5 times if smaller than whats required
if (c->top + size >= c->size) {
if (c->size == 0) {
c->size = PARSE_STACK_INIT_SIZE;
}
while (c->top + size >= c->size) {
// expand the size to 1.5 times the original size
// size += size / 2
c->size += c->size >> 1;
#if DEBUG_STACK_SIZE
printf("stack size: %zu KB\n", c->size / 1024);
#endif
}
c->stack = (char *) realloc(c->stack, c->size);
}
ret = c->stack + c->top;
c->top += size;
return ret;
}
////////////////////////////
// Init & Free of structs //
////////////////////////////
static void init_struct_input_files(struct_input_files *files) {
files->length = 0;
files->files = (char **) malloc(26 * sizeof(char *));
for (int i = 0; i < 26; i++) {
files->files[i] = NULL;
}
}
static void free_struct_input_files(struct_input_files *files) {
for (int i = 0; i < files->length; i++) {
free(files->files[i]);
}
free(files->files);
files->files = NULL;
files->length = 0;
}
static void free_struct_first_line(struct_first_line *fl) {
free(fl->sums);
fl->sums = NULL;
fl->length = 0;
}
static void free_struct_second_line(struct_second_line *sl) {
free(sl->relations);
sl->relations = NULL;
sl->length = 0;
}
static void free_struct_third_line(struct_third_line *tl) {
free(tl->joins);
tl->joins = NULL;
tl->length = 0;
}
static void free_struct_fourth_line(struct_fourth_line *fl) {
// this line may be empty
if (fl->length == 0) {
return;
}
free(fl->predicates);
fl->predicates = NULL;
fl->length = 0;
}
static void free_struct_query(struct_query *query) {
free_struct_first_line(&query->first);
free_struct_second_line(&query->second);
free_struct_third_line(&query->third);
free_struct_fourth_line(&query->fourth);
}
static void free_struct_queries(struct_queries *queries) {
for (int i = 0; i < queries->length; i++) {
free_struct_query(&queries->queries[i]);
}
free(queries->queries);
queries->queries = NULL;
queries->length = 0;
}
static void init_struct_parse_context(struct_parse_context *c, const char *input) {
c->input = input;
c->stack = NULL;
c->size = c->top = 0;
}
static void free_struct_parse_context(struct_parse_context *c) {
free(c->stack);
c->stack = NULL;
c->top = c->size = 0;
}
///////////
// parse //
///////////
// skip one whitespace if there is one
void parse_whitespace(struct_parse_context *c) {
while (c->input[0] == '\n' || c->input[0] == ' ') {
c->input++;
}
}
/*
* Literal SELECT
* Match string SELECT
*/
void parse_first_line_select(struct_parse_context *c) {
ASSERT(0 == strncmp(c->input, "SELECT", strlen("SELECT")));
c->input += strlen("SELECT");
}
/*
* Match Relation and Column
* CFG:
* relation.column
*/
int parse_relation_column(struct_parse_context *c, struct_relation_column *relation_column) {
// Should start with alphabet
EXPECT_ALPHABET(c);
size_t head = c->top;
const char *tmp_p = c->input;
// parse relation
while (1) {
char ch = tmp_p[0];
// end of relation
if (ch == '.') {
size_t len = c->top - head;
const char *relation = (char *) context_pop(c, len);
// copy this relation
relation_column->relation = *relation;
c->input = tmp_p;
break;
}
*(char *) context_push(c, sizeof(char)) = ch;
tmp_p++;
}
// skip '.'
c->input++;
// parse column
// parse 'C' at the beginning of column
c->input++;
head = c->top;
tmp_p = c->input;
while (1) {
char ch = tmp_p[0];
// if its not number, then its end of column
if (!IS_NUMERIC(ch)) {
size_t len = c->top - head;
// string of number, no terminal
const char *str = (char *) context_pop(c, len);
char *tmp_str = (char *) malloc(len + 1);
memcpy(tmp_str, str, len);
tmp_str[len] = '\0';
// parse int
relation_column->column = strtol(tmp_str, NULL, 0);
// clean up
free(tmp_str);
// move head to this non numeric char
c->input = tmp_p;
break;
}
*(char *) context_push(c, sizeof(char)) = ch;
tmp_p++;
}
return PARSE_OK;
}
/*
* Match relation and column
* CFG:
* SUM(relation.column)
*/
int parse_first_line_sum(struct_parse_context *c, struct_relation_column *relation_column) {
EXPECT(c, 'S');
// move pointer to relation
c->input += strlen("SUM(");
parse_relation_column(c, relation_column);
// move pointer, skip )
c->input++;
return PARSE_OK;
}
/*
* List of sums
* CFG:
* Sums -> Sum | Sum,WhitespaceSums
*/
int parse_first_line_sums(struct_parse_context *c, struct_first_line *v) {
// Should start with SUM
EXPECT(c, 'S');
struct_relation_column *rc = NULL;
size_t head = c->top;
rc = (struct_relation_column *) malloc(sizeof(struct_relation_column));
parse_first_line_sum(c, rc);
// push into context
*(struct_relation_column *) context_push(c, sizeof(struct_relation_column)) = *rc;
free(rc);
while (c->input[0] == ',') {
c->input++;
parse_whitespace(c);
rc = (struct_relation_column *) malloc(sizeof(struct_relation_column));
parse_first_line_sum(c, rc);
// push into context
*(struct_relation_column *) context_push(c, sizeof(struct_relation_column)) = *rc;
free(rc);
}
// pop
size_t len = c->top - head;
const struct_relation_column *rcs = (struct_relation_column *) context_pop(c, len);
v->sums = (struct_relation_column *) malloc(len);
memcpy(v->sums, rcs, len);
v->length = len / sizeof(struct_relation_column);
return PARSE_OK;
}
/*
* First line of SQL query
* CFG:
* FirstLine -> SELECTWhitespaceSums
*/
int parse_first_line(struct_parse_context *c, struct_first_line *v) {
// should start with SELECT
EXPECT(c, 'S');
parse_first_line_select(c);
parse_whitespace(c);
return parse_first_line_sums(c, v);
}
/*
* Match the beginning of second line of SQL query
* literal FROM
*/
void parse_second_line_from(struct_parse_context *c) {
ASSERT(0 == strncmp(c->input, "FROM", strlen("FROM")));
c->input += strlen("FROM");
}
/*
* Match the relation given on second line of SQL query
* single alphabet
*/
int parse_second_line_relation(struct_parse_context *c, char *ch) {
EXPECT_ALPHABET(c);
// match single alphabet
*ch = c->input[0];
c->input++;
return PARSE_OK;
}
/*
* Match a list of relations
* CFG:
* Relation | Relation,WhitespaceRelations
*/
int parse_second_line_relations(struct_parse_context *c, struct_second_line *v) {
EXPECT_ALPHABET(c);
size_t head = c->top;
char ch = 0;
// parse first relation
parse_second_line_relation(c, &ch);
// push into stack
*(char *) context_push(c, sizeof(char)) = ch;
while (c->input[0] == ',') {
c->input++;
parse_whitespace(c);
// parse following relation
parse_second_line_relation(c, &ch);
*(char *) context_push(c, sizeof(char)) = ch;
}
size_t len = c->top - head;
const char *relations = (char *) context_pop(c, len);
// len = number of alphabets
v->relations = (char *) malloc(len + 1);
memcpy(v->relations, relations, len);
// add ending \0 to the string
v->relations[len] = 0;
// size of actual elements
v->length = len / sizeof(char);
return PARSE_OK;
}
/*
* Match the second line of SQL query
* CFG:
* SecondLine -> FROMWhitespaceRelations
*/
int parse_second_line(struct_parse_context *c, struct_second_line *v) {
// FROM
EXPECT(c, 'F');
parse_second_line_from(c);
parse_whitespace(c);
return parse_second_line_relations(c, v);
}
/*
* Match literal WHERE at the beginning of third line of SQL query
*/
void parse_third_line_where(struct_parse_context *c) {
ASSERT(0 == strncmp(c->input, "WHERE", strlen("WHERE")));
c->input += strlen("WHERE");
}
/*
* Math JOIN
* CFG:
* Relation.ColumnWhitespace=WhitespaceRelation.Column
*/
int parse_third_line_join(struct_parse_context *c, struct_join *join) {
EXPECT_ALPHABET(c);
parse_relation_column(c, &join->lhs);
parse_whitespace(c);
// skip = sign
EXPECT(c, '=');
c->input++;
parse_whitespace(c);
parse_relation_column(c, &join->rhs);
return PARSE_OK;
}
/*
* Match literal AND
*/
void parse_and(struct_parse_context *c) {
ASSERT(0 == strncmp(c->input, "AND", strlen("AND")));
c->input += strlen("AND");
}
/*
* Match list of JOINs
* CFG:
* Join | JoinWhitespaceANDWhitespaceJoins
*/
int parse_third_line_joins(struct_parse_context *c, struct_third_line *tl) {
EXPECT_ALPHABET(c);
struct_join *join = NULL;
size_t head = c->top;
// first join
join = (struct_join *) malloc(sizeof(struct_join));
parse_third_line_join(c, join);
// push into context
*(struct_join *) context_push(c, sizeof(struct_join)) = *join;
free(join);
// will end because third line ends with \n
while (c->input[0] == ' ') {
// skip ws AND ws
parse_whitespace(c);
parse_and(c);
parse_whitespace(c);
// following joins
join = (struct_join *) malloc(sizeof(struct_join));
parse_third_line_join(c, join);
// push into context
*(struct_join *) context_push(c, sizeof(struct_join)) = *join;
free(join);
}
size_t len = c->top - head;
const struct_join *joins = (struct_join *) context_pop(c, len);
tl->joins = (struct_join *) malloc(len);
memcpy(tl->joins, joins, len);
tl->length = len / sizeof(struct_join);
return PARSE_OK;
}
/*
* Match third line of SQL query
* CFG:
* WHEREWhitespaceJoins
*/
int parse_third_line(struct_parse_context *c, struct_third_line *v) {
// WHERE
EXPECT(c, 'W');
parse_third_line_where(c);
parse_whitespace(c);
return parse_third_line_joins(c, v);
}
/*
* Match comparision operator
* =, > or <
*/
int parse_operator(struct_parse_context *c, enum_operator *op) {
switch (c->input[0]) {
case '=':
*op = EQUAL;
break;
case '>':
*op = GREATER_THAN;
break;
case '<':
*op = LESS_THAN;
break;
default:
ASSERT(0);
return PARSE_FAILED;
}
c->input++;
return PARSE_OK;
}
/*
* Match an Integer
*/
int parse_number(struct_parse_context *c, int *num) {
char *tail = NULL;
*num = strtol(c->input, &tail, 0);
c->input = tail;
return PARSE_OK;
}
/*
* Match single predicate
* CFG:
* Relation.ColumnWhitespaceOperatorWhitespaceNumber
*/
int parse_fourth_line_predicate(struct_parse_context *c, struct_predicate *p) {
EXPECT_ALPHABET(c);
parse_relation_column(c, &p->lhs);
// skip ws Op ws
parse_whitespace(c);
parse_operator(c, &p->op);
parse_whitespace(c);
parse_number(c, &p->rhs);
return PARSE_OK;
}
/*
* Match one or more predicates
* CFG:
* Predicate | PredicateWhitespaceANDWhitespacePredicates
*/
int parse_fourth_line_predicates(struct_parse_context *c, struct_fourth_line *fl) {
EXPECT_ALPHABET(c);
struct_predicate *p = NULL;
size_t head = c->top;
// first predicate
p = (struct_predicate *) malloc(sizeof(struct_predicate));
parse_fourth_line_predicate(c, p);
// push into context
*(struct_predicate *) context_push(c, sizeof(struct_predicate)) = *p;
free(p);
while (c->input[0] == ' ') {
parse_whitespace(c);
parse_and(c);
parse_whitespace(c);
// following predicates
p = (struct_predicate *) malloc(sizeof(struct_predicate));
parse_fourth_line_predicate(c, p);
// push into context
*(struct_predicate *) context_push(c, sizeof(struct_predicate)) = *p;
free(p);
}
size_t len = c->top - head;
const struct_predicate *predicates = (struct_predicate *) context_pop(c, len);
fl->predicates = (struct_predicate *) malloc(len);
memcpy(fl->predicates, predicates, len);
fl->length = len / sizeof(struct_predicate);
return PARSE_OK;
}
/*
* Match fourth line of SQL query
* CFG:
* ANDWhitespacePredicates; | ANDWhitespaces;
*/
int parse_fourth_line(struct_parse_context *c, struct_fourth_line *v) {
// AND or ; (empty)
ASSERT(c->input[0] == 'A' || c->input[0] == ';');
if (c->input[0] == ';') {
c->input++;
v->predicates = NULL;
v->length = 0;
return PARSE_OK;
}
// AND
parse_and(c);
// whitespace
parse_whitespace(c);
// no predicates after AND
if (c->input[0] == ';') {
c->input++;
v->length = 0;
v->predicates = NULL;
return PARSE_OK;
}
// Predicates
int ret = parse_fourth_line_predicates(c, v);
// parse ';'
ASSERT(c->input[0] == ';');
c->input++;
return ret;
}
/*
* Match one SQL query
* CFG:
* FirstLine Newline SecondLine Newline ThirdLine Newline FourthLine
*/
int parse_query(struct_parse_context *c, struct_query *v) {
parse_first_line(c, &v->first);
parse_whitespace(c);
parse_second_line(c, &v->second);
parse_whitespace(c);
parse_third_line(c, &v->third);
parse_whitespace(c);
parse_fourth_line(c, &v->fourth);
parse_whitespace(c);
return PARSE_OK;
}
/*
* Match one or many queries, separated by newline
* CFG:
* Query | QueryNewlinesQueries
*/
int parse_queries(struct_parse_context *c, struct_queries *queries) {
// begin with SELECT
EXPECT(c, 'S');
struct_query *query = NULL;
size_t head = c->top;
query = (struct_query *) malloc(sizeof(struct_query));
parse_query(c, query);
// push into context
*(struct_query *) context_push(c, sizeof(struct_query)) = *query;
free(query);
// THere are more queries
while (c->input[0] == 'S') {
parse_whitespace(c);
query = (struct_query *) malloc(sizeof(struct_query));
parse_query(c, query);
// push into context
*(struct_query *) context_push(c, sizeof(struct_query)) = *query;
free(query);
}
size_t len = c->top - head;
const struct_query *tmp_queries = (struct_query *) context_pop(c, len);
queries->queries = (struct_query *) malloc(len);
memcpy(queries->queries, tmp_queries, len);
queries->length = len / sizeof(struct_query);
return PARSE_OK;
}
/*
* Match second part of the input given to the program
* CFG:
* Number Newlines Queries
*/
int parse_second_part(struct_queries *queries, const char *input) {
ASSERT(queries != NULL && input != NULL);
struct_parse_context c;
init_struct_parse_context(&c, input);
// expect number
char head = c.input[0];
ASSERT('0' <= head && head <= '9');
int count_query = 0;
// we don't really need this number
parse_number(&c, &count_query);
parse_whitespace(&c);
parse_queries(&c, queries);
free_struct_parse_context(&c);
return PARSE_OK;
}
/**
* CFG:
* Path
*/
int parse_path(struct_parse_context *c, struct_input_files *files) {
const char *point = c->input;
while (!(*point == 0 || *point == '\n') && *point != ',') {
point++;
}
char relation = *(point - 5);
// copy string into files
int length = point - c->input + 1;
files->files[relation - 'A'] = (char *) malloc(length * sizeof(char));
strncpy((files->files[relation - 'A']), c->input, length - 1);
files->files[relation - 'A'][length - 1] = '\0';
files->length++;
// move input forward
c->input = point;
return PARSE_OK;
}
/**
* CFG:
* Path | Path,WhitespacesPaths
*/
int parse_paths(struct_parse_context *c, struct_input_files *files) {
ASSERT(c->input[0] != 0);
parse_path(c, files);
parse_whitespace(c);
// there is more
while (c->input[0] == ',') {
// parse ,
c->input++;
parse_whitespace(c);
parse_path(c, files);
parse_whitespace(c);
}
return PARSE_OK;
}
/**
* Parse name of relations from input string
*/
int parse_first_part(struct_input_files *const files, const char *const input) {
ASSERT(input != NULL);
struct_parse_context c;
init_struct_parse_context(&c, input);
// parse paths
parse_paths(&c, files);
free_struct_parse_context(&c);
return PARSE_OK;
}
/**
* Read one line from stdin and assign it to *input
* @param input: the pointer to the pointer to char array
*/
void read_first_part_from_stdin(char **input) {
ASSERT(input != NULL);
size_t size = 0;
getline(input, &size, stdin);
}
/**
* Read string from stdin and assign the value to *input
* @param input: pointer to a pointer to the char array. The value will be modified by this function to point to a pointer of char array created by malloc, so please free it after use.
*/
void read_second_part_from_stdin(char **input) {
ASSERT(input != NULL);
struct_parse_context c;
// we only need the stack, so no input given
init_struct_parse_context(&c, NULL);
char *line = NULL;
// no idea what is this for
size_t size = 0;
// read first number
getline(&line, &size, stdin);
strncpy((char *) context_push(&c, strlen(line)), line, strlen(line));
// how many queries are there
int count = strtol(line, NULL, 0);
// how many lines should I read
// each query consists of 4 lines, and blank line in the middle
count = count * 4 + count - 1;
// read line by line, push them into stack
while (count > 0) {
getline(&line, &size, stdin);
if (strlen(line) != 0) {
strncpy((char *) context_push(&c, strlen(line)), line, strlen(line));
}
count--;
}
// free line