(file) Return to output.c CVS log (file) (dir) Up to [OMI] / omi / wql

  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           #include <base/strings.h>
 28           #include <base/io.h>
 29           
 30           #define STRLIT(STR) STR, (sizeof(STR)-1)
 31           
 32           static void _ZApp(Buf* out, const MI_Char* s, size_t n)
 33           {
 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 mike  1.1     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           
 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 mike  1.1     /* 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                       _ZApp(out, self->properties[i], Zlen(self->properties[i]));
 76                       Buf_App(out, STRLIT("\",\n"));
 77                   }
 78           
 79                   _Indent(nindent, out);
 80                   Buf_App(out, STRLIT("    },\n"));
 81           
 82                   _Indent(nindent, out);
 83                   len = Snprintf(buf, sizeof(buf), "    %u,\n", (int)self->nproperties);
 84                   Buf_App(out, buf, len);
 85 mike  1.1     }
 86           
 87               /* Print class name */
 88               _Indent(nindent, out);
 89               Buf_App(out, STRLIT("    /* className */\n"));
 90               _Indent(nindent, out);
 91               Buf_App(out, STRLIT("    \""));
 92               _ZApp(out, self->className, Zlen(self->className));
 93               Buf_App(out, STRLIT("\",\n"));
 94           
 95               /* Print symbols */
 96               {
 97                   _Indent(nindent, out);
 98                   Buf_App(out, STRLIT("    /* symbols */\n"));
 99                   _Indent(nindent, out);
100                   Buf_App(out, STRLIT("    {\n"));
101           
102                   for (i = 0; i < self->nsymbols; i++)
103                   {
104                       const WQL_Symbol* sym = &self->symbols[i];
105           
106 mike  1.1             _Indent(nindent, out);
107                       Buf_App(out, STRLIT("        { "));
108           
109                       switch (sym->type)
110                       {
111                           case WQL_TYPE_OR:
112                               Buf_App(out, STRLIT("WQL_TYPE_OR"));
113                               break;
114                           case WQL_TYPE_AND:
115                               Buf_App(out, STRLIT("WQL_TYPE_AND"));
116                               break;
117                           case WQL_TYPE_NOT:
118                               Buf_App(out, STRLIT("WQL_TYPE_NOT"));
119                               break;
120                           case WQL_TYPE_EQ:
121                               Buf_App(out, STRLIT("WQL_TYPE_EQ"));
122                               break;
123                           case WQL_TYPE_NE:
124                               Buf_App(out, STRLIT("WQL_TYPE_NE"));
125                               break;
126                           case WQL_TYPE_LT:
127 mike  1.1                     Buf_App(out, STRLIT("WQL_TYPE_LT"));
128                               break;
129                           case WQL_TYPE_LE:
130                               Buf_App(out, STRLIT("WQL_TYPE_LE"));
131                               break;
132                           case WQL_TYPE_GT:
133                               Buf_App(out, STRLIT("WQL_TYPE_GT"));
134                               break;
135                           case WQL_TYPE_GE:
136                               Buf_App(out, STRLIT("WQL_TYPE_GE"));
137                               break;
138                           case WQL_TYPE_IDENTIFIER:
139                           {
140                               Buf_App(out, STRLIT("WQL_TYPE_IDENTIFIER, "));
141                               Buf_App(out, STRLIT("WQL_VALUE_STRING(\""));
142                               _ZApp(out, sym->value.string, Zlen(sym->value.string));
143                               Buf_App(out, STRLIT("\")"));
144                               break;
145                           }
146                           case WQL_TYPE_BOOLEAN:
147                               Buf_App(out, STRLIT("WQL_TYPE_BOOLEAN, "));
148 mike  1.1                     Buf_App(out, STRLIT("WQL_VALUE_BOOLEAN("));
149                               len = Snprintf(buf, sizeof(buf), "%u", sym->value.boolean);
150                               Buf_App(out, buf, len);
151                               Buf_App(out, STRLIT(")"));
152                               break;
153                           case WQL_TYPE_INTEGER:
154                               Buf_App(out, STRLIT("WQL_TYPE_INTEGER, "));
155                               Buf_App(out, STRLIT("WQL_VALUE_INTEGER("));
156                               len = Snprintf(buf, sizeof(buf), SINT64_FMT, 
157                                   sym->value.integer);
158                               Buf_App(out, buf, len);
159                               Buf_App(out, STRLIT(")"));
160                               break;
161                           case WQL_TYPE_REAL:
162                               Buf_App(out, STRLIT("WQL_TYPE_REAL, "));
163                               Buf_App(out, STRLIT("WQL_VALUE_REAL("));
164                               len = Snprintf(buf, sizeof(buf), "%lf", sym->value.real);
165                               Buf_App(out, buf, len);
166                               Buf_App(out, STRLIT(")"));
167                               break;
168                           case WQL_TYPE_NULL:
169 mike  1.1                     Buf_App(out, STRLIT("WQL_TYPE_NULL"));
170                               break;
171                           case WQL_TYPE_STRING:
172                           {
173                               MI_Char* p;
174                               Buf_App(out, STRLIT("WQL_TYPE_STRING, "));
175                               Buf_App(out, STRLIT("WQL_VALUE_STRING(\""));
176           
177                               for (p = sym->value.string; *p; p++)
178                               {
179                                   char c = (char)*p;
180           
181                                   switch (c)
182                                   {
183                                       case '"':
184                                       {
185                                           char c1 = '\\';
186                                           char c2 = '"';
187                                           Buf_App(out, &c1, 1);
188                                           Buf_App(out, &c2, 1);
189                                           break;
190 mike  1.1                             }
191                                       case '\n':
192                                       {
193                                           char c1 = '\\';
194                                           char c2 = 'n';
195                                           Buf_App(out, &c1, 1);
196                                           Buf_App(out, &c2, 1);
197                                           break;
198                                       }
199                                       case '\r':
200                                       {
201                                           char c1 = '\\';
202                                           char c2 = 'r';
203                                           Buf_App(out, &c1, 1);
204                                           Buf_App(out, &c2, 1);
205                                           break;
206                                       }
207                                       case '\f':
208                                       {
209                                           char c1 = '\\';
210                                           char c2 = 'f';
211 mike  1.1                                 Buf_App(out, &c1, 1);
212                                           Buf_App(out, &c2, 1);
213                                           break;
214                                       }
215                                       case '\\':
216                                       {
217                                           char c1 = '\\';
218                                           char c2 = '\\';
219                                           Buf_App(out, &c1, 1);
220                                           Buf_App(out, &c2, 1);
221                                           break;
222                                       }
223                                       default:
224                                           Buf_App(out, &c, 1);
225                                   }
226                               }
227                               Buf_App(out, STRLIT("\")"));
228                               break;
229                           }
230                           default:
231                               return -1;
232 mike  1.1             }
233           
234                       Buf_App(out, STRLIT(" },\n"));
235                   }
236           
237                   _Indent(nindent, out);
238                   Buf_App(out, STRLIT("    },\n"));
239           
240                   len = Snprintf(buf, sizeof(buf), "    %u,\n", (int)self->nsymbols);
241                   _Indent(nindent, out);
242                   Buf_App(out, buf, len);
243               }
244           
245               /* Print structure trailer */
246               _Indent(nindent, out);
247               Buf_App(out, STRLIT("};\n"));
248           
249               /* Null terminate buffer */
250               Buf_App(out, "\0", 1);
251           
252               return 0;
253 mike  1.1 }
254           
255           int WQL_Dump(const WQL* self, size_t nindent)
256           {
257               Buf buf = BUF_INITIALIZER;
258           
259               if (WQL_Define(self, &buf, nindent) != 0)
260                   return -1;
261           
262               printf("%s", (const char*)buf.data);
263               Buf_Destroy(&buf);
264           
265               return 0;
266           }

ViewCVS 0.9.2