|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2015 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <string.h> |
|
32 |
|
33 #include <ucx/utils.h> |
|
34 |
|
35 #include "davqlexec.h" |
|
36 |
|
37 DavQLResult* dav_statement_exec(DavSession *sn, DavQLStatement *st, ...) { |
|
38 va_list ap; |
|
39 va_start(ap, st); |
|
40 DavQLResult *result = dav_statement_execv(sn, st, ap); |
|
41 va_end(ap); |
|
42 return result; |
|
43 } |
|
44 |
|
45 DavQLResult* dav_statement_execv(DavSession *sn, DavQLStatement *st, va_list ap) { |
|
46 DavQLResult *result = dav_session_malloc(sn, sizeof(DavQLResult)); |
|
47 result->result = NULL; |
|
48 result->status = 1; |
|
49 |
|
50 // make sure the statement was successfully parsed |
|
51 if(st->type == DAVQL_ERROR) { |
|
52 return result; |
|
53 } |
|
54 |
|
55 // get path string |
|
56 davqlerror_t error; |
|
57 UcxBuffer *path = dav_path_string(st->path, ap, &error); |
|
58 |
|
59 if(st->type == DAVQL_GET) { |
|
60 dav_exec_get(sn, st, path->space, ap); |
|
61 } else { |
|
62 // TODO |
|
63 } |
|
64 |
|
65 return result; |
|
66 } |
|
67 |
|
68 UcxBuffer* dav_path_string(sstr_t pathfmt, va_list ap, davqlerror_t *error) { |
|
69 UcxBuffer *path = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); |
|
70 |
|
71 int placeholder = 0; |
|
72 for(int i=0;i<pathfmt.length;i++) { |
|
73 char c = pathfmt.ptr[i]; |
|
74 if(placeholder) { |
|
75 if(c == '%') { |
|
76 // no placeholder, %% transposes to % |
|
77 ucx_buffer_putc(path, c); |
|
78 } else { |
|
79 // detect placeholder type and insert arg |
|
80 int err = 0; |
|
81 switch(c) { |
|
82 case 's': { |
|
83 char *arg = va_arg(ap, char*); |
|
84 ucx_buffer_puts(path, arg); |
|
85 break; |
|
86 } |
|
87 case 'd': { |
|
88 int arg = va_arg(ap, int); |
|
89 ucx_bprintf(path, "%d", arg); |
|
90 break; |
|
91 } |
|
92 case 'u': { |
|
93 unsigned int arg = va_arg(ap, unsigned int); |
|
94 ucx_bprintf(path, "%u", arg); |
|
95 break; |
|
96 } |
|
97 case 't': { |
|
98 // time arguments doesn't make any sense in a path |
|
99 *error = DAVQL_UNSUPPORTED_FORMATCHAR; |
|
100 err = 1; |
|
101 break; |
|
102 } |
|
103 default: { |
|
104 *error = DAVQL_UNKNOWN_FORMATCHAR; |
|
105 err = 1; |
|
106 } |
|
107 } |
|
108 if(err) { |
|
109 ucx_buffer_free(path); |
|
110 return NULL; |
|
111 } |
|
112 } |
|
113 placeholder = 0; |
|
114 } else { |
|
115 if(c == '%') { |
|
116 placeholder = 1; |
|
117 } else { |
|
118 ucx_buffer_putc(path, c); |
|
119 } |
|
120 } |
|
121 } |
|
122 ucx_buffer_putc(path, '\0'); |
|
123 *error = DAVQL_OK; |
|
124 return path; |
|
125 } |
|
126 |
|
127 void dav_exec_get(DavSession *sn, DavQLStatement *st, char* path, va_list ap) { |
|
128 // execute a davql get statement |
|
129 |
|
130 // TODO: get property list |
|
131 |
|
132 |
|
133 } |
|
134 |
|
135 int dav_eval_lexpr(DavResource *res, DavQLExpression *lexpr) { |
|
136 |
|
137 } |