-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.c
81 lines (65 loc) · 1.86 KB
/
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
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
#include <stdio.h>
#include <stdlib.h>
#include "query.h"
Query *initQuery(enum QueryType type) {
Query *query = malloc(sizeof(Query));
query->type = type;
query->key = NULL;
query->value = NULL;
query->next = NULL;
query->prev = NULL;
query->comparator = NULL;
query->subQueries = NULL;
query->subQueryCount = 0;
return query;
}
void freeQuery(Query *query) {
if (query->value)
freeString(query->value);
if (query->key)
freeString(query->key);
if (query->comparator)
freeString(query->comparator);
if (query->next)
freeQuery(query->next);
if (query->subQueries) {
for (unsigned int i = 0; i < query->subQueryCount; i++)
freeQuery(query->subQueries[i]);
free(query->subQueries);
}
free(query);
}
void printQuery(Query *query, unsigned int depth, bool printNext) {
for (unsigned int i = 0; i < depth; i++)
fprintf(stderr, " ");
if (query->type == ElementName) {
if (query->nesting == NESTING_DIRECT)
printf("/");
else if (query->nesting == NESTING_INDIRECT)
printf("//");
} else if (query->type == ElementAttribute)
printf("@");
else if (query->type == FunctionFilter)
printf("F:");
if (query->key)
printf("%s", query->key->value);
if (query->comparator)
printf("%s", query->comparator->value);
if (query->value)
printf("%s", query->value->value);
printf("\n");
if (query->subQueries)
for (unsigned int i = 0; i < query->subQueryCount; i++)
printQuery(query->subQueries[i], depth + 1, printNext);
if (printNext && query->next)
printQuery(query->next, depth, printNext);
}
bool queryIsEmpty(Query *query) {
if (query->type == ElementName)
return !query->value;
if (query->type == ElementAttribute)
return !query->key;
if (query->type == FunctionFilter)
return !query->key || !query->comparator || !query->value;
return false;
}