-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathquery.c
32 lines (31 loc) · 847 Bytes
/
query.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
typedef struct {
int64_t after;
uint16_t start;
uint8_t limit;
} Query;
int parse_query (Query *q, char *query) {
int state = 0, c, p; int64_t x;
if (!query) return 1;
q->start = 0; q->after = 0; q->limit = 1;
while (1) { c = *query;
switch (state) {
case 0: if (c == '\0') return 1; state++;
case 1: p = c; state++; break;
case 2: if (c != '=') return 0; state++; break;
case 3: if (digit (c)) x = (c - '0'); else return 0; state++; break;
case 4: switch (c) {
case '&': state = 0; query++;
case '\0': state++; switch (p) {
case 'a': q->after = x; break;
case 'l': q->limit = x; break;
case 's': q->start = x; break;
default: return 0;
} break;
default: if (digit (c)) x = x * 10 + (c - '0');
else return 0; query++;
} continue;
case 5: return 1;
}
query++;
}
}