-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
executable file
·122 lines (112 loc) · 2.95 KB
/
Copy pathparser.cpp
File metadata and controls
executable file
·122 lines (112 loc) · 2.95 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
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <map>
#include <vector>
#include <set>
#include <functional>
#include <sstream>
#include <cassert>
#include <cstdint>
#include <cstddef>
#include <iostream>
int demo_parser(FILE *input, int *op,
uint64_t *arg, uint64_t *arg2,
char *new_val)
{
int ret;
char command[64];
//char new_val[64];
ret = fscanf(input, "%s %ld", command, arg);
if (ret == EOF)
return EOF;
else if (ret != 2) {
fprintf(stderr, "Parse error\n");
exit(3);
}
if (strcmp(command, "Insert") == 0) {
*op = 0;
if (1 != fscanf(input, " %s", new_val)) {
fprintf(stderr, "Parse error\n");
exit(3);
}
} else if (strcmp(command, "Update") == 0) {
*op = 1;
if (1 != fscanf(input, " %s", new_val)) {
fprintf(stderr, "Parse error\n");
exit(3);
}
} else if (strcmp(command, "Delete") == 0) {
*op = 2;
} else if (strcmp(command, "Query") == 0) {
*op = 3;
} else if (strcmp(command, "Range_query") == 0){ //3
*op = 4;
if (1 != fscanf(input, " %ld", arg2)){
fprintf(stderr, "Parse error\n");
exit(3);
}
} else {
fprintf(stderr, "Unknown command: %s\n", command);
exit(1);
}
return 0;
}
int main()
{
char script_infile[64] = "parser_in.txt"; //NULL;
char script_outfile[64] = "parser_out.txt"; //NULL;
FILE *script_input;
FILE *script_output;
if (script_infile) {
script_input = fopen(script_infile, "r");
if (script_input == NULL) {
perror("Couldn't open input file");
exit(1);
}
}
if (script_outfile) {
script_output = fopen(script_outfile, "w");
if (script_output == NULL) {
perror("Couldn't open output file");
exit(1);
}
}
int op;
uint64_t target, ed;
char new_val[10];
while (1) {
int r = demo_parser(script_input, &op, &target, &ed, new_val);
if (r == EOF)
exit(0);
if (r < 0)
exit(4);
switch (op)
{
case 0:
fprintf(script_output, "inserting %ld with val %s\n", target, new_val);
// do insert
break;
case 1:
fprintf(script_output, "updating %ld with val %s\n", target, new_val);
// do update
break;
case 2:
fprintf(script_output, "Deleting %ld\n", target);
// do insert
break;
case 3:
// do query
fprintf(script_output, "Query %ld\n", target);
break;
case 4:
// do range query
fprintf(script_output, "Range_query from %ld to %ld\n", target, ed);
break;
default:
fprintf(script_output, "err~ \n");
break;
}
}
}