-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
275 lines (234 loc) · 8.59 KB
/
main.c
File metadata and controls
275 lines (234 loc) · 8.59 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
// Kuba Czech & Adam Wilczyński
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
char c;
int i, start_size, separator_index;
struct {
char *char_array;
int size;
} typedef str;
bool is_string_numerical(str s) {
for (i = 0; i < s.size - 1; ++i) {
if (!isdigit(s.char_array[i])) {
return false;
}
}
return true;
}
bool is_proper_identifier(str s) {
for (i = 0; i < s.size - 1; ++i) {
if (!isalnum(s.char_array[i]) & s.char_array[i] != '-') {
return false;
}
}
return true;
}
int first_index(const str s, const char target) {
for (i = 0; i < strlen(s.char_array); ++i) {
if (s.char_array[i] == target) {
return i;
}
}
return s.size;
}
bool is_key_value_pair(const str line) {
return first_index(line, '=') != line.size;
};
str extract_str(str s, int start, int end) {
str result;
result.size = end - start + 1;
result.char_array = malloc(sizeof(char) * result.size); // on assumption that a space separates '=' from both sides
strncpy(result.char_array, s.char_array + start, result.size - 1);
result.char_array[result.size - 1] = '\0';
return result;
}
bool is_header(const str line) {
return line.char_array[0] == '[';
}
str get_header(const str line) {
str header;
header.size = strlen(line.char_array) - 2 + 1;
header.char_array = malloc(sizeof(char) * header.size);
for (i = 1; i < strlen(line.char_array) - 1; ++i) {
header.char_array[i - 1] = line.char_array[i];
}
header.char_array[i - 1] = '\0';
return header;
}
struct {
str section;
str key;
str value;
} typedef item;
void print_item(const item item_to_print) {
printf("[%s] %s = %s\n", item_to_print.section.char_array, item_to_print.key.char_array, item_to_print.value.char_array);
}
struct {
item *item_array;
int size;
} typedef dynamic_item_array;
int find_first_item_with_section(dynamic_item_array database, str section) {
item current_item;
for (i = 0; i < database.size; ++i) {
current_item = database.item_array[i];
if (strcmp(current_item.section.char_array, section.char_array) == 0) {
return i;
}
}
return database.size;
}
int find_item_index_by_key_and_value(dynamic_item_array database, int section_start_index, str key) {
item current_item = database.item_array[section_start_index];
str start_section = current_item.section;
for (i = section_start_index; i < database.size & strcmp(current_item.section.char_array, start_section.char_array) == 0; ++i) {
current_item = database.item_array[i];
if (strcmp(current_item.key.char_array, key.char_array) == 0) {
return i;
}
}
return database.size;
}
int find_item_index(str selector, dynamic_item_array database) {
separator_index = first_index(selector, '.');
str section = extract_str(selector, 0, separator_index);
str key = extract_str(selector, separator_index + 1, selector.size);
int section_index = find_first_item_with_section(database, section);
if (section_index == database.size) {
printf("Failed to find section [%s]\n", section.char_array);
return -1;
}
int item_index = find_item_index_by_key_and_value(database, section_index, key);
if (item_index == database.size) {
printf("Failed to find key \"%s\" in section [%s]\n", key.char_array, section.char_array);
return -2;
}
return item_index;
}
str process_expression(str a, str b, char operator) {
str result;
if (is_string_numerical(a) & is_string_numerical(b)) {
long long int a_numerical, b_numerical;
sscanf(a.char_array, "%lld", &a_numerical);
sscanf(b.char_array, "%lld", &b_numerical);
switch (operator) {
case '+':
sprintf(result.char_array, "%lli", a_numerical + b_numerical);
break;
case '-':
sprintf(result.char_array, "%lli", a_numerical - b_numerical);
break;
case '*':
sprintf(result.char_array, "%lli", a_numerical * b_numerical);
break;
case '/':
sprintf(result.char_array, "%lli", a_numerical / b_numerical);
break;
default:
printf("Invalid operator %c", operator);
result.size = -1;
return result;
}
result.size = strlen(result.char_array);
return result;
}
if ((operator == '+') & !is_string_numerical(a) & !is_string_numerical(b)) {
result.size = a.size + b.size - 1;
result.char_array = malloc(result.size);
strncpy(result.char_array , a.char_array, a.size - 1);
strncpy(result.char_array + a.size - 1, b.char_array, b.size);
return result;
}
printf("Invalid operation: %s %c %s", a.char_array, operator, b.char_array);
}
int main(int argc, char *argv[]) {
FILE *file;
file = fopen(argv[1], "r");
if (file == NULL) {
printf("Error occurred when opening a file: %s", argv[1]);
return 1;
}
str line;
start_size = 1;
line.char_array = malloc(sizeof(char) * start_size);
line.char_array[0] = '\0';
line.size = start_size;
dynamic_item_array database;
database.item_array = malloc(sizeof(item));
database.size = 0;
str current_header;
item current_line_item;
while ((c = fgetc(file)) != EOF) {
if (c == '\n') {
// printf("%d %s\n", is_header(line), line.char_array);
if (is_header(line)) {
current_header = get_header(line);
} else if (is_key_value_pair(line)) {
separator_index = first_index(line, '=');
current_line_item.key = extract_str(line, 0, first_index(line, '=') - 1);
current_line_item.value = extract_str(line, separator_index + 2, strlen(line.char_array));
current_line_item.section = current_header;
if (!is_proper_identifier(current_line_item.key)) {
printf("%s is an invalid identifier", current_line_item.key.char_array);
return -1;
} else if (!is_proper_identifier(current_line_item.value)) {
printf("%s is an invalid identifier", current_line_item.value.char_array);
return -1;
} else if (!is_proper_identifier(current_line_item.section)) {
printf("%s is an invalid identifier", current_line_item.section.char_array);
return -1;
}
database.item_array = realloc(database.item_array, sizeof(item) * ++database.size);
database.item_array[database.size - 1] = current_line_item;
}
// printf("%s\n",line.char_array);
line.size = 1;
line.char_array[line.size - 1] = '\0';
} else if (strlen(line.char_array) + 2 >= line.size) {
line.char_array = realloc(line.char_array, sizeof(char) * ++line.size);
line.char_array[line.size - 2] = c;
line.char_array[line.size - 1] = '\0';
}
}
if (strcmp(argv[2], "expression") != 0) {
// Search Query
str selector;
selector.char_array = argv[2];
selector.size = strlen(selector.char_array) + 1;
int item_index = find_item_index(selector, database);
if (item_index < 0) {
return -1;
}
print_item(database.item_array[item_index]);
} else {
// Query
str query, a, b;
query.char_array = argv[3];
query.size = strlen(argv[3]);
separator_index = first_index(query, ' ');
if (separator_index == query.size) {
printf("Invalid expression");
return -1;
}
a = extract_str(query, 0, separator_index);
char operator = query.char_array[separator_index + 1];
b = extract_str(query, separator_index + 3, query.size);
int a_index, b_index;
a_index = find_item_index(a, database);
b_index = find_item_index(b, database);
if (a_index < 0 | b_index < 0) {
return -1;
}
str a_value, b_value;
a_value = database.item_array[a_index].value;
b_value = database.item_array[b_index].value;
str result = process_expression(a_value, b_value, operator);
if (result.size < 0) {
return -1;
}
printf("%s %c %s = %s", a_value.char_array, operator, b_value.char_array, result.char_array);
}
return 0;
}