exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
staticOutputUtils.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
17 #include "schemaOutputUtils.h"
18 #include "hashtable.h"
19 #include "sTables.h"
20 
21 static void setProdStrings(IndexStrings *indexStrings, Production *prod)
22 {
23  char *indexMaxStr = "INDEX_MAX";
24  char *uriMaxStr = "URI_MAX";
25  char *lnMaxStr = "LN_MAX";
26 
27  if (prod->typeId == INDEX_MAX)
28  {
29  strcpy(indexStrings->typeIdStr, indexMaxStr);
30  }
31  else
32  {
33  sprintf(indexStrings->typeIdStr, "%u", (unsigned int) prod->typeId);
34  }
35  if (prod->qnameId.uriId == URI_MAX)
36  {
37  strcpy(indexStrings->uriIdStr, uriMaxStr);
38  }
39  else
40  {
41  sprintf(indexStrings->uriIdStr, "%u", (unsigned int) prod->qnameId.uriId);
42  }
43  if (prod->qnameId.lnId == LN_MAX)
44  {
45  strcpy(indexStrings->lnIdStr, lnMaxStr);
46  }
47  else
48  {
49  sprintf(indexStrings->lnIdStr, "%u", (unsigned int) prod->qnameId.lnId);
50  }
51 }
52 
53 void staticStringDefOutput(String* str, char* varName, FILE* out)
54 {
55  Index charIter, charMax;
56  char displayStr[VAR_BUFFER_MAX_LENGTH];
57 
58  charMax = str->length;
59  if(charMax > 0)
60  {
61  fprintf(out, "CONST CharType %s[] = {", varName);
62  for(charIter = 0; charIter < charMax; charIter++)
63  {
64  fprintf(out, "0x%x", str->str[charIter]);
65  if(charIter < charMax - 1)
66  fprintf(out, ", ");
67  }
68  strncpy(displayStr, str->str, str->length);
69  displayStr[str->length] = '\0';
70  fprintf(out, "}; /* %s */\n", displayStr);
71  }
72 }
73 
74 void staticStringTblDefsOutput(UriTable* uriTbl, char* prefix, FILE* out)
75 {
76  Index uriIter, pfxIter, lnIter;
77  char varName[VAR_BUFFER_MAX_LENGTH];
78 
79  fprintf(out, "/** START_STRINGS_DEFINITONS */\n\n");
80 
81  for(uriIter = 0; uriIter < uriTbl->count; uriIter++)
82  {
83  // Printing of a uri string
84  sprintf(varName, "%sURI_%u", prefix, (unsigned int) uriIter);
85  staticStringDefOutput(&uriTbl->uri[uriIter].uriStr, varName, out);
86 
87  // Printing of a pfx strings if any
88  for(pfxIter = 0; pfxIter < uriTbl->uri[uriIter].pfxTable.count; pfxIter++)
89  {
90  sprintf(varName, "%sPFX_%u_%u", prefix, (unsigned int) uriIter, (unsigned int) pfxIter);
91  staticStringDefOutput(&uriTbl->uri[uriIter].pfxTable.pfx[pfxIter], varName, out);
92  }
93 
94  // Printing of all local names for that uri
95  for(lnIter = 0; lnIter < uriTbl->uri[uriIter].lnTable.count; lnIter++)
96  {
97  sprintf(varName, "%sLN_%u_%u", prefix, (unsigned int) uriIter, (unsigned int) lnIter);
98  staticStringDefOutput(&uriTbl->uri[uriIter].lnTable.ln[lnIter].lnStr, varName, out);
99  }
100  }
101 
102  fprintf(out, "\n/** END_STRINGS_DEFINITONS */\n\n");
103 }
104 
105 void staticProductionsOutput(EXIGrammar* gr, char* prefix, Index grId, FILE* out)
106 {
107  Index ruleIter;
108  char varName[VAR_BUFFER_MAX_LENGTH];
109  Index prodIter;
110  IndexStrings indexStrings;
111 
112  for(ruleIter = 0; ruleIter < gr->count; ruleIter++)
113  {
114  if (gr->rule[ruleIter].pCount)
115  {
116  // Printing of the Production variable string
117  sprintf(varName, "%sprod_%u_%u", prefix, (unsigned int) grId, (unsigned int) ruleIter);
118 
119  fprintf(out, "static CONST Production %s[%u] =\n{\n", varName, (unsigned int) gr->rule[ruleIter].pCount);
120 
121  for(prodIter = 0; prodIter < gr->rule[ruleIter].pCount; prodIter++)
122  {
123  setProdStrings(&indexStrings, &gr->rule[ruleIter].production[prodIter]);
124  fprintf(out,
125  " {\n %u, %s,\n {%s, %s}}%s",
126  gr->rule[ruleIter].production[prodIter].content,
127  indexStrings.typeIdStr,
128  indexStrings.uriIdStr,
129  indexStrings.lnIdStr,
130  prodIter==(gr->rule[ruleIter].pCount - 1) ? "\n};\n\n" : ",\n");
131  }
132  }
133  }
134 }
135 
136 void staticRulesOutput(EXIGrammar* gr, char* prefix, Index grId, FILE* out)
137 {
138  Index ruleIter;
139 
140  fprintf(out,
141  "static CONST GrammarRule %srule_%u[%u] =\n{",
142  prefix,
143  (unsigned int) grId,
144  (unsigned int) gr->count);
145 
146  for(ruleIter = 0; ruleIter < gr->count; ruleIter++)
147  {
148  fprintf(out, "\n {");
149  if (gr->rule[ruleIter].pCount > 0)
150  {
151  fprintf(out,
152  "%sprod_%u_%u, ",
153  prefix,
154  (unsigned int) grId,
155  (unsigned int) ruleIter);
156  }
157  else
158  fprintf(out, "NULL, ");
159 
160  fprintf(out, "%u, ", (unsigned int) gr->rule[ruleIter].pCount);
161  fprintf(out, "%u", (unsigned int) gr->rule[ruleIter].meta);
162  fprintf(out, "}%s", ruleIter != (gr->count-1)?",":"");
163 
164  }
165 
166  fprintf(out, "\n};\n\n");
167 }
168 
169 void staticDocGrammarOutput(EXIGrammar* docGr, char* prefix, FILE* out)
170 {
171  char varNameContent[VAR_BUFFER_MAX_LENGTH];
172  char varNameEnd[VAR_BUFFER_MAX_LENGTH];
173  Index prodIter;
174  IndexStrings indexStrings;
175 
176  // Printing of the Production variable string
177  sprintf(varNameContent, "%sprod_doc_content", prefix);
178 
179  /* Build the document grammar, DocContent productions */
180 
181  fprintf(out, "static CONST Production %s[%u] =\n{\n", varNameContent, (unsigned int) docGr->rule[GR_DOC_CONTENT].pCount);
182 
183  for(prodIter = 0; prodIter < docGr->rule[GR_DOC_CONTENT].pCount; prodIter++)
184  {
185  setProdStrings(&indexStrings, &docGr->rule[GR_DOC_CONTENT].production[prodIter]);
186  fprintf(out,
187  " {\n %u, %s,\n {%s, %s}}%s",
188  (unsigned int) docGr->rule[GR_DOC_CONTENT].production[prodIter].content,
189  indexStrings.typeIdStr,
190  indexStrings.uriIdStr,
191  indexStrings.lnIdStr,
192  prodIter==(docGr->rule[GR_DOC_CONTENT].pCount - 1) ? "\n};\n\n" : ",\n");
193  }
194 
195  // Printing of the Production variable string
196  sprintf(varNameEnd, "%sprod_doc_end", prefix);
197 
198  /* Build the document grammar, DocEnd productions */
199 
200  fprintf(out, "static CONST Production %s[%u] =\n{\n", varNameEnd, 1);
201  fprintf(out," {\n 0xAFFFFFF, INDEX_MAX,\n {URI_MAX, LN_MAX}}\n};\n\n");
202 
203  /* Build the document grammar rules */
204  fprintf(out, "static CONST GrammarRule %sdocGrammarRule[2] =\n{\n", prefix);
205  fprintf(out, " {%s, %u, 0},\n\
206  {%s, 1, 0}\n};\n\n", varNameContent, (unsigned int) docGr->rule[GR_DOC_CONTENT].pCount, varNameEnd);
207 }
208 
209 void staticPrefixOutput(PfxTable* pfxTbl, char* prefix, Index uriId, Deviations dvis, FILE* out)
210 {
211  Index pfxIter;
212  if(pfxTbl->count > 0)
213  {
214  fprintf(out, "static CONST String %sPfxEntry_%u[%u] =\n{\n", prefix, (unsigned int) uriId, (unsigned int) pfxTbl->count + dvis.pfx);
215 
216  for(pfxIter = 0; pfxIter < pfxTbl->count; pfxIter++)
217  {
218  if(pfxTbl->pfx[pfxIter].length > 0)
219  fprintf(out, " {%sPFX_%u_%u, %u}%s", prefix, (unsigned int) uriId, (unsigned int) pfxIter, (unsigned int) pfxTbl->pfx[pfxIter].length,
220  pfxIter==pfxTbl->count + dvis.pfx - 1?"\n};\n\n" : ",\n");
221  else
222  fprintf(out, " {NULL, 0}%s", pfxIter==pfxTbl->count + dvis.pfx - 1?"\n};\n\n" : ",\n");
223  }
224 
225  for(pfxIter = 0; pfxIter < dvis.pfx; pfxIter++)
226  {
227  fprintf(out, " {NULL, 0}%s", pfxIter==dvis.pfx - 1?"\n};\n\n" : ",\n");
228  }
229  }
230 }
231 
232 void staticLnEntriesOutput(LnTable* lnTbl, char* prefix, Index uriId, Deviations dvis, FILE* out)
233 {
234  Index lnIter;
235  char elemGrammar[20];
236  char typeGrammar[20];
237 
238  if(lnTbl->count + dvis.ln> 0)
239  {
240  fprintf(out, "static CONST LnEntry %sLnEntry_%u[%u] =\n{\n", prefix, (unsigned int) uriId, (unsigned int) lnTbl->count + dvis.ln);
241 
242  for(lnIter = 0; lnIter < lnTbl->count; lnIter++)
243  {
244  if(lnTbl->ln[lnIter].elemGrammar == INDEX_MAX)
245  strcpy(elemGrammar, "INDEX_MAX");
246  else
247  sprintf(elemGrammar, "%u", (unsigned int) lnTbl->ln[lnIter].elemGrammar);
248 
249  if(lnTbl->ln[lnIter].typeGrammar == INDEX_MAX)
250  strcpy(typeGrammar, "INDEX_MAX");
251  else
252  sprintf(typeGrammar, "%u", (unsigned int) lnTbl->ln[lnIter].typeGrammar);
253 
254  fprintf(out, " {\n#if VALUE_CROSSTABLE_USE\n NULL,\n#endif\n");
255  if(lnTbl->ln[lnIter].lnStr.length > 0)
256  fprintf(out, " {%sLN_%u_%u, %u},\n %s, %s\n", prefix, (unsigned int) uriId, (unsigned int) lnIter, (unsigned int) lnTbl->ln[lnIter].lnStr.length, elemGrammar, typeGrammar);
257  else
258  fprintf(out, " {NULL, 0},\n %s, %s\n", elemGrammar, typeGrammar);
259  fprintf(out, "%s", lnIter==(lnTbl->count-1) + dvis.ln?" }\n};\n\n":" },\n");
260  }
261 
262  strcpy(elemGrammar, "INDEX_MAX");
263  strcpy(typeGrammar, "INDEX_MAX");
264 
265  for(lnIter = 0; lnIter < dvis.ln; lnIter++)
266  {
267  fprintf(out, " {\n#if VALUE_CROSSTABLE_USE\n NULL,\n#endif\n");
268  fprintf(out, " {NULL, 0},\n %s, %s\n", elemGrammar, typeGrammar);
269  fprintf(out, "%s", lnIter==dvis.ln - 1?" }\n};\n\n":" },\n");
270  }
271  } /* END if(lnTableSize > 0) */
272 }
273 
274 void staticUriTableOutput(UriTable* uriTbl, char* prefix, Deviations dvis, FILE* out)
275 {
276  Index uriIter;
277  fprintf(out, "static CONST UriEntry %suriEntry[%u] =\n{\n", prefix, (unsigned int) uriTbl->count + dvis.url);
278 
279  for(uriIter = 0; uriIter < uriTbl->count; uriIter++)
280  {
281  if(uriTbl->uri[uriIter].lnTable.count > 0)
282  {
283  fprintf(out,
284  " {\n {{sizeof(LnEntry), %u, %u}, %sLnEntry_%u, %u},\n",
285  (unsigned int) uriTbl->uri[uriIter].lnTable.count,
286  (unsigned int) uriTbl->uri[uriIter].lnTable.count + dvis.ln,
287  prefix,
288  (unsigned int) uriIter,
289  (unsigned int) uriTbl->uri[uriIter].lnTable.count);
290  }
291  else
292  {
293  fprintf(out, " {\n {{sizeof(LnEntry), %d, %d}, NULL, 0},\n", 0, 0);
294  }
295 
296  if(uriTbl->uri[uriIter].pfxTable.count > 0)
297  {
298  fprintf(out, " {{sizeof(String), %u, %u}, %sPfxEntry_%u, %u},\n",
299  (unsigned int) uriTbl->uri[uriIter].pfxTable.count,
300  (unsigned int) uriTbl->uri[uriIter].pfxTable.count + dvis.pfx,
301  prefix,
302  (unsigned int) uriIter,
303  (unsigned int) uriTbl->uri[uriIter].pfxTable.count);
304  }
305  else
306  {
307  fprintf(out, " {{sizeof(String), %d, %d}, NULL, 0},\n", 0, 0);
308  }
309 
310  if(uriTbl->uri[uriIter].uriStr.length > 0)
311  fprintf(out, " {%sURI_%u, %u}%s", prefix, (unsigned int) uriIter, (unsigned int) uriTbl->uri[uriIter].uriStr.length,
312  uriIter==(uriTbl->count-1) + dvis.url?"\n }\n};\n\n":"\n },\n");
313  else
314  fprintf(out, " {NULL, 0}%s", uriIter==(uriTbl->count-1) + dvis.url?"\n }\n};\n\n":"\n },\n");
315  }
316 
317  for(uriIter = 0; uriIter < dvis.url; uriIter++)
318  {
319  fprintf(out, " {\n {{sizeof(LnEntry), %d, %d}, NULL, 0},\n", dvis.ln, dvis.ln);
320  fprintf(out, " {{sizeof(String), %d, %d}, NULL, 0},\n", 0, 0);
321  fprintf(out, " {NULL, 0}%s", uriIter==dvis.url - 1?"\n }\n};\n\n":"\n },\n");
322  }
323 }
324 
325 void staticEnumTableOutput(EXIPSchema* schema, char* prefix, FILE* out)
326 {
327  EnumDefinition* tmpDef;
328  char varName[VAR_BUFFER_MAX_LENGTH];
329  Index i, j;
330 
331  if(schema->enumTable.count == 0)
332  return;
333 
334  for(i = 0; i < schema->enumTable.count; i++)
335  {
336  tmpDef = &schema->enumTable.enumDef[i];
337  switch(GET_EXI_TYPE(schema->simpleTypeTable.sType[tmpDef->typeId].content))
338  {
339  case VALUE_TYPE_STRING:
340  {
341  String* tmpStr;
342  for(j = 0; j < tmpDef->count; j++)
343  {
344  tmpStr = &((String*) tmpDef->values)[j];
345  sprintf(varName, "%sENUM_%u_%u", prefix, (unsigned int) i, (unsigned int) j);
346  staticStringDefOutput(tmpStr, varName, out);
347  }
348  fprintf(out, "\nstatic CONST String %senumValues_%u[%u] = { \n", prefix, (unsigned int) i, (unsigned int) tmpDef->count);
349  for(j = 0; j < tmpDef->count; j++)
350  {
351  tmpStr = &((String*) tmpDef->values)[j];
352  if(tmpStr->str != NULL)
353  fprintf(out, " {%sENUM_%u_%u, %u}", prefix, (unsigned int) i, (unsigned int) j, (unsigned int) tmpStr->length);
354  else
355  fprintf(out, " {NULL, 0}");
356 
357  if(j < tmpDef->count - 1)
358  fprintf(out, ",\n");
359  else
360  fprintf(out, "\n};\n\n");
361  }
362  } break;
363  case VALUE_TYPE_BOOLEAN:
364  // NOT_IMPLEMENTED
365  assert(FALSE);
366  break;
368  case VALUE_TYPE_YEAR:
369  case VALUE_TYPE_DATE:
370  case VALUE_TYPE_MONTH:
371  case VALUE_TYPE_TIME:
372  // NOT_IMPLEMENTED
373  assert(FALSE);
374  break;
375  case VALUE_TYPE_DECIMAL:
376  // NOT_IMPLEMENTED
377  assert(FALSE);
378  break;
379  case VALUE_TYPE_FLOAT:
380  // NOT_IMPLEMENTED
381  assert(FALSE);
382  break;
383  case VALUE_TYPE_INTEGER:
384  // NOT_IMPLEMENTED
385  assert(FALSE);
386  break;
388  // NOT_IMPLEMENTED
389  assert(FALSE);
390  break;
392  fprintf(out, "\nstatic CONST UnsignedInteger %senumValues_%u[%u] = { \n", prefix, (unsigned int) i, (unsigned int) tmpDef->count);
393  for(j = 0; j < tmpDef->count; j++)
394  {
395  fprintf(out, " 0x%016lX", (long unsigned) ((UnsignedInteger*) tmpDef->values)[j]);
396 
397  if(j < tmpDef->count - 1)
398  fprintf(out, ",\n");
399  else
400  fprintf(out, "\n};\n\n");
401  }
402  break;
403  }
404  }
405 
406  fprintf(out, "static CONST EnumDefinition %senumTable[%u] = { \n", prefix, (unsigned int) schema->enumTable.count);
407  for(i = 0; i < schema->enumTable.count; i++)
408  {
409  tmpDef = &schema->enumTable.enumDef[i];
410  fprintf(out, " {%u, %senumValues_%u, %u}", (unsigned int) tmpDef->typeId, prefix, (unsigned int) i, (unsigned int) tmpDef->count);
411 
412  if(i < schema->enumTable.count - 1)
413  fprintf(out, ",\n");
414  else
415  fprintf(out, "\n};\n\n");
416  }
417 }