-
Notifications
You must be signed in to change notification settings - Fork 38
/
parsed_xpath.c
168 lines (152 loc) · 3.55 KB
/
parsed_xpath.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
// typedef struct __pxpath_node {
// int type;
// char* value;
// __pxpath_node * next;
// __pxpath_node * child;
// } pxpath_node;
//
// typedef pxpath_node pxpathPtr;
//
// enum {
// PXPATH_FUNCTION,
// PXPATH_PATH
// };
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "parsed_xpath.h"
#include <json/printbuf.h>
pxpathPtr pxpath_new(int type, char* value) {
pxpathPtr ptr = (pxpathPtr) calloc(sizeof(pxpath_node), 1);
ptr->value = strdup(value);
ptr->type = type;
ptr->next = NULL;
ptr->child = NULL;
return ptr;
}
// pxpathPtr pxpath_cat_paths(int n, va_list) {
//
// }
// pxpathPtr pxpath_new_paths(int n, va_list) {
//
// }
static void
_pxpath_to_string(pxpathPtr ptr, struct printbuf *buf) {
if(ptr == NULL) return;
if(ptr->type == PXPATH_FUNCTION) {
sprintbuf(buf, "%s(", ptr->value);
pxpathPtr next = ptr->child;
while(next != NULL) {
char* comma = (next->next == NULL) ? "" : ", ";
_pxpath_to_string(next, buf);
sprintbuf(buf, "%s", comma);
next = next->next;
}
sprintbuf(buf, "%s", ")");
} else {
sprintbuf(buf, "%s", ptr->value);
}
}
static char *
format_n(int n) {
char * out = calloc(2 * n + 1, 1);
for(int i =0; i < n; i++) {
strcat(out, "%s");
}
}
char* pxpath_to_string(pxpathPtr ptr) {
if(ptr == NULL) return NULL;
struct printbuf *buf = printbuf_new();
_pxpath_to_string(ptr, buf);
char *out = strdup(buf->buf);
free(buf);
return out;
}
pxpathPtr pxpath_dup(pxpathPtr p) {
if(p == NULL) return NULL;
pxpathPtr dup = (pxpathPtr) calloc(sizeof(pxpath_node), 1);
dup->type = p->type;
dup->value = strdup(p->value);
dup->next = pxpath_dup(p->next);
dup->child = pxpath_dup(p->child);
return dup;
}
void pxpath_free(pxpathPtr ptr) {
if(ptr == NULL) return;
free(ptr->value);
pxpath_free(ptr->next);
pxpath_free(ptr->child);
free(ptr);
}
pxpathPtr pxpath_new_func(char* value, pxpathPtr child) {
pxpathPtr ptr = pxpath_new(PXPATH_FUNCTION, value);
ptr->child = child;
return ptr;
}
pxpathPtr pxpath_cat_things(int n, va_list va) {
struct printbuf *buf = printbuf_new();
pxpathPtr last;
pxpathPtr first;
for(int i = 0; i < n; i++) {
pxpathPtr ptr = va_arg(va, pxpathPtr);
sprintbuf(buf, "%s", pxpath_to_string(ptr));
if(i == 0) first = ptr;
if(i != 0) last->next = ptr;
last = ptr;
}
pxpathPtr out = pxpath_new(PXPATH_PATH, buf->buf);
printbuf_free(buf);
out->child = first;
return out;
}
pxpathPtr pxpath_cat_literals(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr out = pxpath_cat_things(n, va);
va_end(va);
out->type = PXPATH_LIT_EXPR;
return out;
}
pxpathPtr pxpath_cat_paths(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr out = pxpath_cat_things(n, va);
va_end(va);
out->type = PXPATH_PATH;
return out;
}
static pxpathPtr
pxpath_new_thing(int n, va_list va) {
struct printbuf *buf = printbuf_new();
for(int i = 0; i < n; i++) {
sprintbuf(buf, "%s", va_arg(va, char *));
}
pxpathPtr ptr = pxpath_new(PXPATH_PATH, buf->buf);
printbuf_free(buf);
return ptr;
}
pxpathPtr pxpath_new_path(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr ptr = pxpath_new_thing(n, va);
va_end(va);
ptr->type = PXPATH_PATH;
return ptr;
}
pxpathPtr pxpath_new_literal(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr ptr = pxpath_new_thing(n, va);
va_end(va);
ptr->type = PXPATH_LITERAL;
return ptr;
}
pxpathPtr pxpath_new_operator(int n, ...) {
va_list va;
va_start(va, n);
pxpathPtr ptr = pxpath_new_thing(n, va);
va_end(va);
ptr->type = PXPATH_OPERATOR;
return ptr;
}