-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
2138 lines (1712 loc) · 46.8 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bson.h>
/*
* Current tokens for MongoDB :
* - special characters : . || ( || ) || { || } || " || ' || ,
* - logical operators :( $or || $and || $nor ) ==> logic_oper()
* - reserves_keyWords :
* 1 - functions(reserved_words) : (find_token , update_token , use_token , db_token , remove_token) ==> db_functions()
* use : ==> use_function()
* 2 - symbols :
* - the identifiers || strings !!
* - Numbers : number_token ==> number()
*/
// Declare the structure of our LinkedList
typedef struct list_bson
{
char key[20];
char value[20];
struct list * next ;
}bson_list;
// Declare the structure of our LinkedList
typedef struct list
{
char key[20];
char value[20];
struct list * next ;
}find_list;
// Declare the head of the LinkedList
find_list * head;
// Those varibales will help to save the current key and it's value , in order to pass it as parameters to insetToList function !
char Aux_key[20] ;
char Aux_value[20] ;
//char Aux_value_int ;
find_list * create_node(char key[20] , char value[20])
{
find_list * curr = (find_list * )malloc(sizeof(find_list)) ;
strcpy(curr->key , key) ;
strcpy(curr->value , value);
curr->next = NULL ;
return curr ;
}
void insertToList(char key[20] , char value[20])
{
find_list * prec = (find_list * )malloc(sizeof(find_list)) ;
if(head == NULL)
{
head = (find_list * )malloc(sizeof(find_list)) ;
strcpy(head->key , key) ;
strcpy(head->value , value);
head->next = NULL ;
}
else
{
find_list * curr = (find_list * )malloc(sizeof(find_list));
curr = head;
while(curr->next != NULL)
{
curr = curr->next;
}
curr->next = create_node(key , value);
}
}
void explore_list(find_list * hd)
{
if(hd == NULL )
{
printf(" Your LinkedList is Empty \n");
}
else
{
find_list * curr = (find_list * )malloc(sizeof(find_list)) ;
curr = hd ;
while(curr != NULL)
{ printf("In Loop ");
//printf("%s :: %s\n",curr->key,curr->value);
curr = curr->next ;
}
}
}
int getListLength()
{
find_list * curr = (find_list * ) malloc(sizeof(find_list));
curr = head ;
int length = 0 ;
while(curr != NULL)
{
length++;
curr = curr->next ;
}
return length ;
}
typedef enum{
db_token , find_token , insert_token ,update_token , use_token , remove_token, collection_idf_token , dot_token , opened_bracket_token , closed_bracket_token , comma_token , opened_brace_token
, closed_brace_token , quotation_marks_token , apostrophe_token , colon_token , number_token , id_token , string_token , opened_box_brackets , closed_box_brackets , eq_token , gte_token , gt_token , lt_token , lte_token ,
or_token , nor_token , ne_token , in_token , nin_token , not_token , show_token , dbs_token
}Tokens;
typedef enum{
special_char_error , id_missing, opened_box_brackets_missing , closed_box_brackets_missing , colon_missing , closed_bracket_missing
, opened_brace_missing , closed_brace_missing , db_keyWord_missing , dot_missing , opened_bracket_missing , undefined_function , comma_missing , $in_Or_$nin_missing
,invalid_number , dbs_token_missing
}error_tokens;
typedef struct
{
Tokens keyWordToken ; char keyWord[8];
}KeyWords ;
find_list * update_lst ;
int cdts_length ;
char * query;
char current_char ;
int current_index ;
int query_length ;
char current_word[15] ;
Tokens current_token ;
char * json ;
int jsonLength ;
FILE * dataBases_store ;
/** Some flags to know wich function used in the query
if insert = 1 : we the fct use the insert function , if = 0 the query use another fct thant insert
**/
int insert , find , update , delete , use , show ;
/** variables to stock the db name , and the collection name **/
char * db_name ;
char collection_name[15];
KeyWords key_Words[18] =
{{db_token , "db"} , {find_token , "find"} , {show_token , "show"} , {dbs_token , "dbs"} , {insert_token , "insert"} , {update_token , "update"} , {use_token , "use"} , {remove_token , "remove"} , {or_token , "$or"}, {nor_token , "$nor"} , {gt_token ,"$gt"},{gte_token ,"$gte"}
,{lt_token ,"$lt"},{lte_token ,"$lte"} ,{ne_token ,"$ne"} , {in_token , "$in" } , {nin_token , "$nin" } , {not_token , "$not"}};
void print_token(Tokens token)
{
switch( token )
{
case db_token:
printf("db_token\n");
break ;
case show_token:
printf("show_token\n");
break ;
case dbs_token:
printf("dbs_token\n");
break ;
case remove_token :
printf("remove_token\n");
case use_token:
printf("use_token\n");
break ;
case find_token:
printf("find_token\n");
break ;
case insert_token:
printf("insert_token\n");
break ;
case update_token:
printf("update_token\n") ;
break ;
case number_token :
printf("number_token\n");
break ;
case comma_token:
printf("comma_token\n");
break ;
case quotation_marks_token :
printf("quotation_marks_token\n");
break ;
case apostrophe_token :
printf("apostrophe_token\n");
break ;
case opened_bracket_token:
printf("opened_bracket_token\n");
break ;
case closed_bracket_token:
printf("closed_bracket_token\n");
break ;
case opened_brace_token:
printf("opened_brace_token\n");
break ;
case closed_brace_token:
printf("closed_brace_token\n");
break ;
case dot_token:
printf("dot_token\n");
break ;
case colon_token :
printf("colon_token\n");
break ;
case id_token :
printf("id_token\n");
break ;
case string_token:
printf("string_token\n");
break ;
case opened_box_brackets :
printf("opened_box_brackets\n");
break ;
case closed_box_brackets :
printf("closed_box_brackets\n");
break ;
case gte_token :
printf("gte_token\n");
break;
case gt_token :
printf("gt_token\n");
break;
case lt_token :
printf("lt_token\n");
break ;
case lte_token :
printf("lte_token\n");
break ;
case eq_token :
printf("eq_token\n");
break ;
case or_token :
printf("or_token\n");
break ;
case nor_token:
printf("nor_token\n");
break ;
case ne_token :
printf("ne_token\n");
break ;
case in_token :
printf("in_token\n");
break ;
case nin_token :
printf("nin_token\n");
break ;
case not_token :
printf("not_token\n");
break ;
}
}
Tokens look_for_token(Tokens key_word)
{
switch( key_word )
{
case db_token:
print_token(db_token);
return db_token ;
case show_token:
print_token(show_token);
return show_token ;
case dbs_token:
print_token(dbs_token);
return dbs_token ;
case use_token:
print_token(use_token);
return use_token ;
case find_token:
print_token(find_token);
return find_token ;
case insert_token:
print_token(insert_token);
return insert_token ;
case update_token:
print_token(update_token);
return update_token ;
case remove_token :
print_token(remove_token);
return remove_token ;
case number_token :
print_token(number_token);
return number_token ;
case comma_token:
print_token(comma_token);
return comma_token ;
case quotation_marks_token :
print_token(quotation_marks_token);
return quotation_marks_token ;
case apostrophe_token :
print_token(apostrophe_token);
return apostrophe_token ;
case opened_bracket_token:
print_token(opened_bracket_token);
return opened_bracket_token ;
case closed_bracket_token:
print_token(closed_bracket_token);
return closed_bracket_token ;
case opened_brace_token:
print_token(opened_brace_token);
return opened_brace_token ;
case closed_brace_token:
print_token(closed_brace_token);
return closed_brace_token ;
case dot_token:
print_token(dot_token);
return dot_token ;
case colon_token :
print_token(colon_token);
return colon_token ;
case id_token :
print_token(id_token);
return id_token ;
case string_token :
print_token(string_token);
return string_token ;
case opened_box_brackets :
print_token(opened_box_brackets);
return opened_box_brackets ;
case closed_box_brackets :
print_token(closed_box_brackets);
return closed_box_brackets ;
case gte_token :
print_token(gte_token);
return gte_token ;
case gt_token :
print_token(gt_token);
return gt_token ;
case lt_token :
print_token(lt_token);
return lt_token ;
case lte_token :
print_token(lte_token);
return lte_token ;
case eq_token :
print_token(eq_token);
return eq_token ;
case or_token :
print_token(or_token);
return or_token ;
case nor_token:
print_token(nor_token);
return nor_token ;
case ne_token :
print_token(ne_token);
return ne_token ;
case in_token :
print_token(in_token);
return in_token ;
case nin_token :
print_token(nin_token);
return nin_token ;
case not_token :
print_token(not_token);
return not_token ;
}
}
void print_error_token(error_tokens key_word)
{
switch (key_word)
{
case opened_brace_missing :
printf("opened_brace_missing\n");
break ;
case invalid_number :
printf("invalid_number");
break ;
case closed_brace_missing :
printf("closed_brace_missing\n");
break ;
case special_char_error :
printf("special_char_error\n");
break ;
case dbs_token_missing :
printf("dbs_token_missing\n");
break ;
case id_missing :
printf("id_missing\n");
break ;
case opened_box_brackets_missing :
printf("opened_box_brackets_missing\n");
break ;
case closed_box_brackets_missing :
printf("closed_box_brackets_missing\n");
break ;
case opened_bracket_missing :
printf("opened_bracket_missing\n");
break ;
case closed_bracket_missing :
printf("closed_bracket_missing\n");
break ;
case colon_missing :
printf("colon_missing\n");
break ;
case dot_missing :
printf("dot_missing\n");
break ;
case undefined_function :
printf("undefined_function\n");
break ;
case comma_missing :
printf("comma_missing\n");
break ;
case $in_Or_$nin_missing:
printf("$in Or $nin operator is missing");
}
}
Tokens isKeyWord()
{
int i = 0 ;
while( i < 19)
{
if(strcmp(current_word ,key_Words[i].keyWord) == 0)
{
return look_for_token(key_Words[i].keyWordToken);
}
else
i++ ;
}
return look_for_token(id_token);
}
Tokens special_char_function()
{
switch (current_char)
{
case ',':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(comma_token);
case ':':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(colon_token);
case '"' :
{
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
int i = 0 ; // as index
while(1)
{
if(current_char == 34)
{
if(json != NULL && json[0] != '\0' )
{
current_word[i] = current_char ;
i++ ;
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
current_word[i-1] = '\0';
return look_for_token(string_token);
}
else
{
if(json != NULL && json[0] != '\0' )
{
current_word[i] = current_char ;
i++ ;
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
}
}
}
case '\'' :
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(apostrophe_token);
case '(':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(opened_bracket_token);
case ')':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(closed_bracket_token);
case '{':
if(json != NULL && json[0] != '\0' && strcmp(json , "{") != 0)
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(opened_brace_token);
case '}':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(closed_brace_token);
case '.':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(dot_token);
case '[':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(opened_box_brackets);
case ']':
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++;
current_char = query[current_index];
return look_for_token(closed_box_brackets);
}
}
Tokens numbrer_function()
{
int i = 0 ;
if(json != NULL && json[0] != '\0' )
{ current_word[i] = current_char ;
i++ ;
json[jsonLength] = current_char;
jsonLength++ ;
}
current_index++ ;
current_char = query[current_index];
while(current_char >= 48 && current_char <= 57)
{
if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_word[i] = current_char ;
i++ ;
current_index++ ;
current_char = query[current_index];
} current_word[i] = '\0' ;
if( current_char == ' ' || current_char == ',' || current_char == '}')
{ return look_for_token(number_token);
}
/** Error Case **/
else
{
print_error_token(invalid_number);
exit(1);
}
}
Tokens string_function()
{
int i = 0 ;
while(current_char == '$' ||current_char >= 97 && current_char <= 122 || current_char >= 65 && current_char <= 90 || current_char >= 48 && current_char <= 57 || current_char == '_')
{ if(json != NULL && json[0] != '\0' )
{
json[jsonLength] = current_char;
jsonLength++ ;
}
current_word[i] = current_char ;
current_index++ ;
current_char = query[current_index];
i++ ;
}
current_word[i] = '\0';
return isKeyWord();
}
Tokens read_char()
{
while(current_char == ' ' || current_char == '\t')
{
current_index++;
current_char = query[current_index];
}
if( current_char == '$' || current_char >= 97 && current_char <= 122 || current_char >= 65 && current_char <= 90)
{
return string_function();
}
if(current_char >= 48 && current_char <= 57)
{
return numbrer_function();
}
/*
if(current_char == 36)
{
ligical_oper_function();
}*/
if( current_char == '[' || current_char == ']' ||current_char == ':' || current_char == '.' || current_char == ',' || current_char == '{' || current_char == '}' || current_char == '(' || current_char == ')' || current_char == '"' || current_char == '\'')
{
return special_char_function();
}
}
void verify_token(Tokens expected_token , error_tokens error)
{
if( current_token == expected_token)
{
current_token = read_char();
}
else
{
print_error_token(error);
exit(1);
}
}
/** <leaf-inter> ::= , <leaf-value-list> | epsilon **/
void leaf_inter()
{
switch(current_token)
{
case comma_token :
current_token = read_char();
leaf_value_list();
break ;
case closed_box_brackets :
break ;
}
}
/** <leaf-value-list> ::= <leaf-value> <leaf-inter> **/
void leaf_value_list()
{
leaf_value();
leaf_inter();
}
/** <array> ::= [ <leaf-value-list> ] **/
void array()
{
verify_token(opened_box_brackets , opened_box_brackets_missing);
leaf_value_list();
verify_token(closed_box_brackets , closed_box_brackets_missing);
}
/** <key> ::= <id-token> **/
void key()
{
verify_token(id_token , id_missing);
}
/** <array-operator> ::= <in-operator> | <nin-operator> **/
void array_operator()
{
switch(current_token)
{
case in_token :
current_token = read_char();
break ;
case nin_token :
current_token = read_char();
break ;
}
}
/** <attributs-inter> ::= , <attributs-list> | epsilon **/
void attributs_inter()
{
switch(current_token)
{
case comma_token :
current_token = read_char();
attributs_list();
break ;
case closed_brace_token:
break ;
}
}
/** <attributs> ::= <key> : <leaf-value> **/
void attributs()
{
key();
verify_token(colon_token , colon_missing);
leaf_value();
}
/** <attributs-list> ::= <attributs> <attributs-inter> **/
void attributs_list()
{
attributs();
attributs_inter();
}
/** <document> ::= <attributs-list> } **/
void document()
{
attributs_list();
verify_token(closed_brace_token , closed_brace_missing);
}
/** <operator-inter> ::= , <key> : { <operator-list> | epsilon **/
void operator_inter()
{
switch(current_token)
{
case comma_token :
current_token = read_char();
key();
verify_token(colon_token , colon_missing);
verify_token(opened_brace_token , closed_brace_missing);
operator_list();
break ;
case closed_brace_token:
break ;
default :
printf("Error on operator_inter !");
exit(1);
}
}
/** <value-operator> ::= <gt-operator> |
<gte-operator> |
<lt-operator> |
<lte-operator> |
<eq-operator> |
<ne-operator> |
<type-operator> |
<size-operator> |
<regex-operator> |
<exists-operator> **/
void value_operator()
{
switch(current_token)
{
case gt_token :
current_token = read_char();
break ;
case gte_token:
current_token = read_char();
break ;
case lt_token :
current_token = read_char();
break ;
case lte_token :
current_token = read_char();
break ;
case eq_token:
current_token = read_char();
break ;
}
}
/** <operators> ::= <value-operator> : <leaf-value> } | <array-operator> : [ <leaf-value-list> ] } | <not-operator> : <operator> } | <elemmatch-operator> : <expression> } **/
void operators()
{
switch(current_token)
{
case gt_token: /** <value-operator> case **/
{
value_operator();
verify_token(colon_token , colon_missing);
leaf_value();
verify_token(closed_brace_token , closed_brace_missing);
break ;
}
case lt_token: /** <value-operator> case **/
{
value_operator();
verify_token(colon_token , colon_missing);
leaf_value();
verify_token(closed_brace_token , closed_brace_missing);
break ;
}
case in_token: /** <array-operator> : [ <leaf-value-list> ] **/
array_operator();
verify_token(colon_token , colon_missing);
verify_token(opened_box_brackets , opened_box_brackets_missing);
leaf_value_list();
verify_token(closed_box_brackets , closed_box_brackets_missing);
break ;
case nin_token: /** <array-operator> : [ <leaf-value-list> ] **/
array_operator();
verify_token(colon_token , colon_missing);
verify_token(opened_box_brackets , opened_box_brackets_missing);
leaf_value_list();
verify_token(closed_box_brackets , closed_box_brackets_missing);
break ;
case not_token : /** <not-operator> : <operators> **/
current_token = read_char();
verify_token(colon_token , colon_missing);
operators();
break ;
default :
printf("Oops , Error : Invalid Operator !");
exit(1);
}
}
/** <operator-list> ::= { <operators> <operator-inter> **/
void operator_list()
{
operators();
operator_inter();
}
/** <operator-object> ::= <operator-list> **/
void operator_object()
{
operator_list();
}
/** <aux> ::= <operator-object> || <document> **/
void aux()
{ //print_token(current_token);
switch (current_token)
{
case id_token:
document();
break ;
case gt_token:
if(insert == 1 )
{
printf("Oops , Error operation $gt not permitted !");
exit(1);
}
operator_object();
break ;
case lt_token:
if(insert == 1 )
{
printf("Oops , Error : operation $lt not permitted !");
exit(1);
}
operator_object();
break ;