1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #include "wql.h"
26 #include <base/buf.h>
|
27 krisbash 1.3 #include <pal/strings.h>
28 #include <pal/format.h>
|
29 mike 1.1
30 #define STRLIT(STR) STR, (sizeof(STR)-1)
31
|
32 krisbash 1.3 static void _ZApp(Buf* out, const ZChar* s, size_t n)
|
33 mike 1.1 {
34 while (n--)
35 {
36 char c = (char)(*s++);
37 Buf_App(out, &c, 1);
38 }
39 }
40
41 static void _Indent(size_t nindent, Buf* out)
42 {
43 size_t i;
44
45 for (i = 0; i < nindent; i++)
46 Buf_App(out, " ", 1);
47 }
48
49 int WQL_Define(const WQL* self, Buf* out, size_t nindent)
50 {
51 char buf[128];
52 int len;
53 size_t i;
54 mike 1.1
55 if (!self)
56 return -1;
57
58 /* Print structure header */
59 _Indent(nindent, out);
60 Buf_App(out, STRLIT("static const WQL _wql =\n"));
61 _Indent(nindent, out);
62 Buf_App(out, STRLIT("{\n"));
63
64 /* Print properties */
65 {
66 _Indent(nindent, out);
67 Buf_App(out, STRLIT(" /* properties */\n"));
68 _Indent(nindent, out);
69 Buf_App(out, STRLIT(" {\n"));
70
71 for (i = 0; i < self->nproperties; i++)
72 {
73 _Indent(nindent, out);
74 Buf_App(out, STRLIT(" \""));
|
75 krisbash 1.3
76 _ZApp(out, self->properties[i], Tcslen(self->properties[i]));
|
77 mike 1.1 Buf_App(out, STRLIT("\",\n"));
78 }
79
80 _Indent(nindent, out);
81 Buf_App(out, STRLIT(" },\n"));
82
83 _Indent(nindent, out);
84 len = Snprintf(buf, sizeof(buf), " %u,\n", (int)self->nproperties);
85 Buf_App(out, buf, len);
86 }
87
88 /* Print class name */
89 _Indent(nindent, out);
90 Buf_App(out, STRLIT(" /* className */\n"));
91 _Indent(nindent, out);
92 Buf_App(out, STRLIT(" \""));
|
93 krisbash 1.3 _ZApp(out, self->className, Tcslen(self->className));
|
94 mike 1.1 Buf_App(out, STRLIT("\",\n"));
95
96 /* Print symbols */
97 {
98 _Indent(nindent, out);
99 Buf_App(out, STRLIT(" /* symbols */\n"));
100 _Indent(nindent, out);
101 Buf_App(out, STRLIT(" {\n"));
102
103 for (i = 0; i < self->nsymbols; i++)
104 {
105 const WQL_Symbol* sym = &self->symbols[i];
106
107 _Indent(nindent, out);
108 Buf_App(out, STRLIT(" { "));
109
110 switch (sym->type)
111 {
112 case WQL_TYPE_OR:
113 Buf_App(out, STRLIT("WQL_TYPE_OR"));
114 break;
115 mike 1.1 case WQL_TYPE_AND:
116 Buf_App(out, STRLIT("WQL_TYPE_AND"));
117 break;
118 case WQL_TYPE_NOT:
119 Buf_App(out, STRLIT("WQL_TYPE_NOT"));
120 break;
121 case WQL_TYPE_EQ:
122 Buf_App(out, STRLIT("WQL_TYPE_EQ"));
123 break;
124 case WQL_TYPE_NE:
125 Buf_App(out, STRLIT("WQL_TYPE_NE"));
126 break;
127 case WQL_TYPE_LT:
128 Buf_App(out, STRLIT("WQL_TYPE_LT"));
129 break;
130 case WQL_TYPE_LE:
131 Buf_App(out, STRLIT("WQL_TYPE_LE"));
132 break;
133 case WQL_TYPE_GT:
134 Buf_App(out, STRLIT("WQL_TYPE_GT"));
135 break;
136 mike 1.1 case WQL_TYPE_GE:
137 Buf_App(out, STRLIT("WQL_TYPE_GE"));
138 break;
139 case WQL_TYPE_IDENTIFIER:
140 {
141 Buf_App(out, STRLIT("WQL_TYPE_IDENTIFIER, "));
142 Buf_App(out, STRLIT("WQL_VALUE_STRING(\""));
|
143 krisbash 1.3 _ZApp(out, sym->value.string, Tcslen(sym->value.string));
|
144 mike 1.1 Buf_App(out, STRLIT("\")"));
145 break;
146 }
147 case WQL_TYPE_BOOLEAN:
148 Buf_App(out, STRLIT("WQL_TYPE_BOOLEAN, "));
149 Buf_App(out, STRLIT("WQL_VALUE_BOOLEAN("));
150 len = Snprintf(buf, sizeof(buf), "%u", sym->value.boolean);
151 Buf_App(out, buf, len);
152 Buf_App(out, STRLIT(")"));
153 break;
154 case WQL_TYPE_INTEGER:
155 Buf_App(out, STRLIT("WQL_TYPE_INTEGER, "));
156 Buf_App(out, STRLIT("WQL_VALUE_INTEGER("));
157 len = Snprintf(buf, sizeof(buf), SINT64_FMT,
158 sym->value.integer);
159 Buf_App(out, buf, len);
160 Buf_App(out, STRLIT(")"));
161 break;
162 case WQL_TYPE_REAL:
163 Buf_App(out, STRLIT("WQL_TYPE_REAL, "));
164 Buf_App(out, STRLIT("WQL_VALUE_REAL("));
165 mike 1.1 len = Snprintf(buf, sizeof(buf), "%lf", sym->value.real);
166 Buf_App(out, buf, len);
167 Buf_App(out, STRLIT(")"));
168 break;
169 case WQL_TYPE_NULL:
170 Buf_App(out, STRLIT("WQL_TYPE_NULL"));
171 break;
172 case WQL_TYPE_STRING:
173 {
|
174 krisbash 1.3 ZChar* p;
|
175 mike 1.1 Buf_App(out, STRLIT("WQL_TYPE_STRING, "));
176 Buf_App(out, STRLIT("WQL_VALUE_STRING(\""));
177
178 for (p = sym->value.string; *p; p++)
179 {
180 char c = (char)*p;
181
182 switch (c)
183 {
184 case '"':
185 {
186 char c1 = '\\';
187 char c2 = '"';
188 Buf_App(out, &c1, 1);
189 Buf_App(out, &c2, 1);
190 break;
191 }
192 case '\n':
193 {
194 char c1 = '\\';
195 char c2 = 'n';
196 mike 1.1 Buf_App(out, &c1, 1);
197 Buf_App(out, &c2, 1);
198 break;
199 }
200 case '\r':
201 {
202 char c1 = '\\';
203 char c2 = 'r';
204 Buf_App(out, &c1, 1);
205 Buf_App(out, &c2, 1);
206 break;
207 }
208 case '\f':
209 {
210 char c1 = '\\';
211 char c2 = 'f';
212 Buf_App(out, &c1, 1);
213 Buf_App(out, &c2, 1);
214 break;
215 }
216 case '\\':
217 mike 1.1 {
218 char c1 = '\\';
219 char c2 = '\\';
220 Buf_App(out, &c1, 1);
221 Buf_App(out, &c2, 1);
222 break;
223 }
224 default:
225 Buf_App(out, &c, 1);
226 }
227 }
228 Buf_App(out, STRLIT("\")"));
229 break;
230 }
231 default:
232 return -1;
233 }
234
235 Buf_App(out, STRLIT(" },\n"));
236 }
237
238 mike 1.1 _Indent(nindent, out);
239 Buf_App(out, STRLIT(" },\n"));
240
241 len = Snprintf(buf, sizeof(buf), " %u,\n", (int)self->nsymbols);
242 _Indent(nindent, out);
243 Buf_App(out, buf, len);
244 }
245
246 /* Print structure trailer */
247 _Indent(nindent, out);
248 Buf_App(out, STRLIT("};\n"));
249
250 /* Null terminate buffer */
251 Buf_App(out, "\0", 1);
252
253 return 0;
254 }
255
256 int WQL_Dump(const WQL* self, size_t nindent)
257 {
258 Buf buf = BUF_INITIALIZER;
259 mike 1.1
260 if (WQL_Define(self, &buf, nindent) != 0)
261 return -1;
262
|
263 krisbash 1.3 Tprintf(MI_T("%s"), scs((const char*)buf.data));
|
264 mike 1.1 Buf_Destroy(&buf);
265
266 return 0;
267 }
|