70 sstr_t property_query = q; |
70 sstr_t property_query = q; |
71 q = util_getsubstr_until_token(q, S("from"), &property_query); |
71 q = util_getsubstr_until_token(q, S("from"), &property_query); |
72 |
72 |
73 sstr_t from_query = q; |
73 sstr_t from_query = q; |
74 sstr_t cond = util_getsubstr_until_token(q, S("where"), &from_query); |
74 sstr_t cond = util_getsubstr_until_token(q, S("where"), &from_query); |
75 |
75 sstr_t with = util_getsubstr_until_token(cond, S("with"), &cond); |
|
76 int depth = 1; |
|
77 |
76 // insert variable values |
78 // insert variable values |
77 UcxBuffer *fbuf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); |
79 UcxBuffer *fbuf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); |
78 int var = 0; |
80 int var = 0; |
79 for(int i=0;i<from_query.length;i++) { |
81 for(int i=0;i<from_query.length;i++) { |
80 char c = from_query.ptr[i]; |
82 char c = from_query.ptr[i]; |
122 l++; |
124 l++; |
123 } |
125 } |
124 ucx_list_free(ops); |
126 ucx_list_free(ops); |
125 } |
127 } |
126 |
128 |
|
129 // with |
|
130 if(with.ptr) { |
|
131 if(dav_parse_with(with, &depth, ap)) { |
|
132 // TODO: error |
|
133 printf("parse error\n"); |
|
134 return NULL; |
|
135 } |
|
136 } |
|
137 |
127 DavGetQuery *getquery = malloc(sizeof(DavGetQuery)); |
138 DavGetQuery *getquery = malloc(sizeof(DavGetQuery)); |
128 getquery->properties = sstrdup(property_query); |
139 getquery->properties = sstrdup(property_query); |
129 getquery->from = sstrn(fbuf->space, fbuf->pos); |
140 getquery->from = sstrn(fbuf->space, fbuf->pos); |
|
141 getquery->depth = depth; |
130 if(condition) { |
142 if(condition) { |
131 getquery->condition = condition; |
143 getquery->condition = condition; |
132 getquery->condlen = oplen; |
144 getquery->condlen = oplen; |
133 } else { |
145 } else { |
134 getquery->condition = NULL; |
146 getquery->condition = NULL; |
159 *depth = -1; |
171 *depth = -1; |
160 *path = sstrdup(sstrsubsl(query, 0, query.length-1)).ptr; |
172 *path = sstrdup(sstrsubsl(query, 0, query.length-1)).ptr; |
161 } else { |
173 } else { |
162 *path = sstrdup(query).ptr; |
174 *path = sstrdup(query).ptr; |
163 *depth = 1; |
175 *depth = 1; |
|
176 } |
|
177 |
|
178 return 0; |
|
179 } |
|
180 |
|
181 static int dav_str2depth(sstr_t str, int *depth) { |
|
182 if(!sstrcmp(str, S("infinity"))) { |
|
183 *depth = -1; |
|
184 } else { |
|
185 sstr_t cp = sstrdup(str); // terminate |
|
186 *depth = atoi(cp.ptr); |
|
187 free(cp.ptr); |
|
188 } |
|
189 return 0; |
|
190 } |
|
191 |
|
192 int dav_parse_with(sstr_t with, int *depth, va_list ap) { |
|
193 int i; |
|
194 for(i=0;i<with.length;i++) { |
|
195 if(with.ptr[i] == ' ') { |
|
196 break; |
|
197 } |
|
198 } |
|
199 |
|
200 sstr_t name = sstrsubsl(with, 0, i); |
|
201 sstr_t value = sstrtrim(sstrsubs(with, i)); |
|
202 //printf("with {%.*s} {%.*s}\n", name.length, name.ptr, value.length, value.ptr); |
|
203 |
|
204 if(!sstrcmp(name, S("depth"))) { |
|
205 if(value.length == 0) { |
|
206 return 1; |
|
207 } else if(value.ptr[0] == '%') { |
|
208 switch(value.ptr[1]) { |
|
209 default: return 1; |
|
210 case 's': { |
|
211 sstr_t v = sstr(va_arg(ap, char*)); |
|
212 if(dav_str2depth(value, depth)) { |
|
213 return 1; |
|
214 } |
|
215 break; |
|
216 } |
|
217 case 'd': { |
|
218 *depth = va_arg(ap, int); |
|
219 break; |
|
220 } |
|
221 } |
|
222 } else { |
|
223 if(dav_str2depth(value, depth)) { |
|
224 return 1; |
|
225 } |
|
226 } |
164 } |
227 } |
165 |
228 |
166 return 0; |
229 return 0; |
167 } |
230 } |
168 |
231 |