exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dynOutputUtils.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 "stringManipulate.h"
20 
21 #define MAX_GRAMMARS_COUNT 5000
22 
23 //TODO: split this code into separate functions
24 errorCode dynExipSchemaOutput(EXIPSchema* schema, char* prefix, unsigned char mask_specified,
25  unsigned char mask_strict, unsigned char mask_sc, unsigned char mask_preserve, FILE* out)
26 {
27 
28  // TODO: must be fixed!
29 #if 0
30  Index uriIter, lnIter, pfxIter, ruleIter, partIter, prodIter;
31  EXIGrammar* tmpGrammar;
32  uintptr_t grammarPointer = 0;
33  String hashKey;
34  Index typeGrammarID;
35  struct hashtable *typeGrammarsHash;
36  unsigned int tgCount = 0;
37  QNameID typeGrammars[MAX_GRAMMARS_COUNT];
38 
39  typeGrammarsHash = create_hashtable(1000, djbHash, stringEqual);
40 
41  // TODO: needs to be fixed
42 
43  fprintf(out, "errorCode get_%sSchema(EXIPSchema* schema);\n\n", prefix);
44  fprintf(out, "errorCode get_%sSchema(EXIPSchema* schema)\n{\n\t errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;\n\t\n\t", prefix);
45  fprintf(out, "if(schema == NULL)\n\t return EXIP_NULL_POINTER_REF;\n\t");
46 
47  fprintf(out, "tmp_err_code = initAllocList(&schema->memList);\n\t");
48  fprintf(out, "if(tmp_err_code != EXIP_OK)\n\t return tmp_err_code;\n\t");
49 
50  for(uriIter = 0; uriIter < schema->uriTable.count; uriIter++)
51  {
52  if(schema->uriTable.uri[uriIter].pfxTable != NULL)
53  {
54  fprintf(out, "PfxTable* pfxTable_%d = memManagedAllocate(&schema->memList, sizeof(PfxTable));\n\t", uriIter);
55  fprintf(out, "if(pTable_%d == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t", uriIter);
56  fprintf(out, "pTable_%d->rowCount = %d;\n\t", uriIter, schema->uriTable.uri[uriIter].pfxTable->count);
57 
58  for(pfxIter = 0; pfxIter < schema->uriTable.uri[uriIter].pfxTable->count; pfxIter++)
59  {
60  fprintf(out, "tmp_err_code += asciiToString(\"");
61  fwrite(schema->uriTable.uri[uriIter].pfxTable->pfxStr[pfxIter].str, sizeof(CharType), schema->uriTable.uri[uriIter].pfxTable->pfxStr[pfxIter].length, out);
62  fprintf(out, "\", &pTable_%d->string_val[%d], &schema->memList, TRUE);\n\t", uriIter, pfxIter);
63  }
64  for(; pfxIter < MAXIMUM_NUMBER_OF_PREFIXES_PER_URI; pfxIter++)
65  {
66  fprintf(out, "getEmptyString(&pTable_%d->string_val[%d]);\n\t", uriIter, pfxIter);
67  }
68  fprintf(out, "if(tmp_err_code != EXIP_OK)\n\t return EXIP_UNEXPECTED_ERROR;\n\t");
69  }
70 
71  for(lnIter = 0; lnIter < schema->uriTable.uri[uriIter].lnTable.count; lnIter++)
72  {
73  tmpGrammar = schema->uriTable.uri[uriIter].lnTable.ln[lnIter].typeGrammar;
74  grammarPointer = (uintptr_t) tmpGrammar;
75  hashKey.str = (CharType*) &grammarPointer;
76  hashKey.length = sizeof(grammarPointer)/sizeof(CharType);
77  typeGrammarID = hashtable_search(typeGrammarsHash, &hashKey);
78  if(tmpGrammar != NULL && typeGrammarID == INDEX_MAX)
79  {
80  if(mask_specified == TRUE)
81  {
82  if(EXIP_OK != addUndeclaredProductions(&schema->memList, mask_strict, mask_sc, mask_preserve, tmpGrammar, &schema->simpleTypeTable))
83  {
84  printf("\n ERROR: OUT_SRC_DYN output format!");
85  exit(1);
86  }
87  }
88 
89  for(ruleIter = 0; ruleIter < tmpGrammar->count; ruleIter++)
90  {
91  for(partIter = 0; partIter < 3; partIter++)
92  {
93  if(tmpGrammar->rule[ruleIter].part[partIter].count > 0)
94  {
95  fprintf(out, "Production* prod_%d_%d_%d_part%d = memManagedAllocate(&schema->memList, %d * sizeof(Production));\n\t", uriIter, lnIter, ruleIter, partIter, tmpGrammar->rule[ruleIter].part[partIter].count);
96  fprintf(out, "if(prod_%d_%d_%d_part%d == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t", uriIter, lnIter, ruleIter, partIter);
97 
98  for(prodIter = 0; prodIter < tmpGrammar->rule[ruleIter].part[partIter].count; prodIter++)
99  {
100  fprintf(out, "prod_%d_%d_%d_part%d[%d].evnt.eventType = %d;\n\t", uriIter, lnIter, ruleIter, partIter, prodIter, tmpGrammar->rule[ruleIter].part[partIter].prod[prodIter].evnt.eventType);
101  fprintf(out, "prod_%d_%d_%d_part%d[%d].evnt.valueType.exiType = %d;\n\t", uriIter, lnIter, ruleIter, partIter, prodIter, tmpGrammar->rule[ruleIter].part[partIter].prod[prodIter].evnt.valueType.exiType);
102  fprintf(out, "prod_%d_%d_%d_part%d[%d].evnt.valueType.simpleTypeId = %d;\n\t", uriIter, lnIter, ruleIter, partIter, prodIter, tmpGrammar->rule[ruleIter].part[partIter].prod[prodIter].evnt.valueType.simpleTypeId);
103  fprintf(out, "prod_%d_%d_%d_part%d[%d].nonTermID = %d;\n\t", uriIter, lnIter, ruleIter, partIter, prodIter, tmpGrammar->rule[ruleIter].part[partIter].prod[prodIter].nonTermID);
104  fprintf(out, "prod_%d_%d_%d_part%d[%d].qname.uriId = %d;\n\t", uriIter, lnIter, ruleIter, partIter, prodIter, tmpGrammar->rule[ruleIter].part[partIter].prod[prodIter].qname.uriId);
105  fprintf(out, "prod_%d_%d_%d_part%d[%d].qname.lnId = %d;\n\t", uriIter, lnIter, ruleIter, partIter, prodIter, tmpGrammar->rule[ruleIter].part[partIter].prod[prodIter].qname.lnId);
106 
107  }
108  }
109  }
110  }
111  fprintf(out, "GrammarRule* rule_%d_%d = memManagedAllocate(&schema->memList, %d * sizeof(GrammarRule));\n\t", uriIter, lnIter, tmpGrammar->count);
112  fprintf(out, "if(rule_%d_%d == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t", uriIter, lnIter);
113 
114  for(ruleIter = 0; ruleIter < tmpGrammar->count; ruleIter++)
115  {
116  for(partIter = 0; partIter < 3; partIter++)
117  {
118  if(tmpGrammar->rule[ruleIter].part[partIter].count > 0)
119  fprintf(out, "rule_%d_%d[%d].part[%d] = prod_%d_%d_%d_part%d;\n\t", uriIter, lnIter, ruleIter, partIter, uriIter, lnIter, ruleIter, partIter);
120  else
121  fprintf(out, "rule_%d_%d[%d].part[%d].prod = NULL;\n\t", uriIter, lnIter, ruleIter, partIter);
122 
123  fprintf(out, "rule_%d_%d[%d].part[%d].count = %d;\n\t", uriIter, lnIter, ruleIter, partIter, tmpGrammar->rule[ruleIter].part[partIter].count);
124  fprintf(out, "rule_%d_%d[%d].part[%d].bits = %d;\n\t", uriIter, lnIter, ruleIter, partIter, tmpGrammar->rule[ruleIter].part[partIter].bits);
125 
126  }
127  }
128 
129  fprintf(out, "EXIGrammar* grammar_%d_%d = memManagedAllocate(&schema->memList, sizeof(EXIGrammar));\n\t", uriIter, lnIter);
130  fprintf(out, "if(grammar_%d_%d == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t", uriIter, lnIter);
131 
132  fprintf(out, "grammar_%d_%d->contentIndex = %d;\n\t", uriIter, lnIter, tmpGrammar->contentIndex);
133  fprintf(out, "grammar_%d_%d->props = 0x%02x;\n\t", uriIter, lnIter, tmpGrammar->props);
134  fprintf(out, "grammar_%d_%d->count = %d;\n\t", uriIter, lnIter, tmpGrammar->count);
135  fprintf(out, "grammar_%d_%d->rule = rule_%d_%d;\n\t", uriIter, lnIter, uriIter, lnIter);
136 
137  if(tgCount >= MAX_GRAMMARS_COUNT)
138  {
139  printf("\n ERROR: MAX_GRAMMARS_COUNT reached!");
140  exit(1);
141  }
142 
143  if(EXIP_OK != hashtable_insert(typeGrammarsHash, &hashKey, tgCount))
144  {
145  printf("\n ERROR: OUT_SRC_DYN output format!");
146  exit(1);
147  }
148  typeGrammars[tgCount].uriId = uriIter;
149  typeGrammars[tgCount].lnId = lnIter;
150  tgCount++;
151  }
152  }
153  fprintf(out, "struct LocalNamesRow* LNrows_%d = memManagedAllocate(&schema->memList, %d * sizeof(struct LocalNamesRow));\n\t", uriIter, schema->uriTable.uri[uriIter].lnTable.count);
154  fprintf(out, "if(LNrows_%d == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t", uriIter);
155 
156  for(lnIter = 0; lnIter < schema->uriTable.uri[uriIter].lnTable.count; lnIter++)
157  {
158  tmpGrammar = schema->uriTable.uri[uriIter].lnTable.ln[lnIter].typeGrammar;
159  grammarPointer = (uintptr_t) tmpGrammar;
160  hashKey.str = (CharType*) &grammarPointer;
161  hashKey.length = sizeof(grammarPointer)/sizeof(CharType);
162  typeGrammarID = hashtable_search(typeGrammarsHash, &hashKey);
163 
164  if(tmpGrammar != NULL)
165  fprintf(out, "LNrows_%d[%d].typeGrammar = grammar_%d_%d;\n\t", uriIter, lnIter, typeGrammars[typeGrammarID].uriId, typeGrammars[typeGrammarID].lnId);
166  else
167  fprintf(out, "LNrows_%d[%d].typeGrammar = NULL;\n\t", uriIter, lnIter);
168 
169  fprintf(out, "LNrows_%d[%d].string_val.str = \"", uriIter, lnIter);
170  fwrite(schema->uriTable.uri[uriIter].lnTable.ln[lnIter].lnStr.str, sizeof(CharType), schema->uriTable.uri[uriIter].lnTable.ln[lnIter].lnStr.length, out);
171  fprintf(out, "\";\n\t");
172  fprintf(out, "LNrows_%d[%d].string_val.length = %d;\n\t", uriIter, lnIter, schema->uriTable.uri[uriIter].lnTable.ln[lnIter].lnStr.length);
173  fprintf(out, "LNrows_%d[%d].vCrossTable = NULL;\n\t", uriIter, lnIter);
174 
175  }
176 
177  fprintf(out, "LocalNamesTable* lTable_%d = memManagedAllocate(&schema->memList, sizeof(LocalNamesTable));\n\t", uriIter);
178  fprintf(out, "if(lTable_%d == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t", uriIter);
179 
180  fprintf(out, "lTable_%d->arrayDimension = %d;\n\t", uriIter, schema->uriTable.uri[uriIter].lnTable.count);
181  fprintf(out, "lTable_%d->rowCount = %d;\n\t", uriIter, schema->uriTable.uri[uriIter].lnTable.count);
182  fprintf(out, "lTable_%d->rows = LNrows_%d;\n\t", uriIter, uriIter);
183  fprintf(out, "lTable_%d->memPair.memBlock = NULL;\n\t", uriIter); // TO BE fixed!
184  fprintf(out, "lTable_%d->memPair.allocIndx = 0;\n\t", uriIter); // TO BE fixed!
185  }
186 
187  fprintf(out, "struct URIRow* uriRows = memManagedAllocate(&schema->memList, %d * sizeof(struct URIRow));\n\t", schema->uriTable.count);
188 
189  fprintf(out, "if(uriRows == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t");
190 
191 
192  for(uriIter = 0; uriIter < schema->uriTable.count; uriIter++)
193  {
194  if(schema->uriTable.uri[uriIter].pfxTable != NULL)
195  fprintf(out, "uriRows[%d].pTable = pTable_%d;\n\t", uriIter, uriIter);
196  else
197  fprintf(out, "uriRows[%d].pTable = NULL;\n\t", uriIter);
198  fprintf(out, "uriRows[%d].lTable = lTable_%d;\n\t", uriIter, uriIter);
199  fprintf(out, "uriRows[%d].string_val.str = \"", uriIter);
200  fwrite(schema->uriTable.uri[uriIter].uriStr.str, sizeof(CharType), schema->uriTable.uri[uriIter].uriStr.length, out);
201  fprintf(out, "\";\n\t");
202  fprintf(out, "uriRows[%d].string_val.length = %d;\n\t", uriIter, schema->uriTable.uri[uriIter].uriStr.length);
203 
204  }
205 
206  fprintf(out, "URITable* uriTbl = memManagedAllocate(&schema->memList, sizeof(URITable));\n\t");
207  fprintf(out, "if(uriTbl == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t");
208  fprintf(out, "uriTbl->arrayDimension = %d;\n\t", schema->uriTable.dynArray.arrayEntries);
209  fprintf(out, "uriTbl->rowCount = %d;\n\t", schema->uriTable.count);
210  fprintf(out, "uriTbl->rows = uriRows;\n\t");
211  fprintf(out, "uriTbl->memPair.memBlock = NULL;\n\t"); // TO BE fixed!
212  fprintf(out, "uriTbl->memPair.allocIndx = 0;\n\t"); // TO BE fixed!
213  fprintf(out, "QNameID* qnames = memManagedAllocate(&schema->memList, %d * sizeof(QNameID));\n\t", schema->globalElemGrammarTable.count);
214  fprintf(out, "if(qnames == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t");
215 
216  for(uriIter = 0; uriIter < schema->globalElemGrammarTable.count; uriIter++)
217  {
218  fprintf(out, "qnames[%d].uriId = %d;\n\t", uriIter, schema->globalElemGrammarTable.qname[uriIter].uriId);
219  fprintf(out, "qnames[%d].lnId = %d;\n\t", uriIter, schema->globalElemGrammarTable.qname[uriIter].lnId);
220 
221  }
222 
223  fprintf(out, "SimpleType* sTypes = memManagedAllocate(&schema->memList, %d * sizeof(SimpleType));\n\t", schema->simpleTypeTable.count);
224  fprintf(out, "if(sTypes == NULL)\n\t return EXIP_MEMORY_ALLOCATION_ERROR;\n\t");
225 
226  for(uriIter = 0; uriIter < schema->simpleTypeTable.count; uriIter++)
227  {
228  fprintf(out, "sTypes[%d].facetPresenceMask = %d;\n\t", uriIter, schema->simpleTypeTable.sType[uriIter].facetPresenceMask);
229  fprintf(out, "sTypes[%d].maxInclusive = %d;\n\t", uriIter, schema->simpleTypeTable.sType[uriIter].maxInclusive);
230  fprintf(out, "sTypes[%d].minInclusive = %d;\n\t", uriIter, schema->simpleTypeTable.sType[uriIter].minInclusive);
231  fprintf(out, "sTypes[%d].maxLength = %d;\n\t", uriIter, schema->simpleTypeTable.sType[uriIter].maxLength);
232 
233  }
234 
235  fprintf(out, "schema->globalElemGrammars = qnames;\n\t");
236  fprintf(out, "schema->globalElemGrammarTable.count = %d;\n\t", schema->globalElemGrammarTable.count);
237  fprintf(out, "schema->uriTable = uriTbl;\n\t");
238  fprintf(out, "schema->simpleTypeArray = sTypes;\n\t");
239  fprintf(out, "schema->simpleTypeTable.count = %d;\n\t", schema->simpleTypeTable.count);
240  fprintf(out, "schema->isAugmented = %d;\n\t", mask_specified);
241  fprintf(out, "schema->isStatic = FALSE;\n\t");
242  fprintf(out, "return EXIP_OK;\n}");
243 
244  hashtable_destroy(typeGrammarsHash);
245 #endif
246  return EXIP_OK;
247 }