exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
exipd.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 
18 #include "decodeTestEXI.h"
19 #include "grammarGenerator.h"
20 
21 #define MAX_XSD_FILES_COUNT 10 // up to 10 XSD files
22 
23 static void printfHelp();
24 static void parseSchema(char* xsdList, EXIPSchema* schema);
25 static void parseOpsMask(char* mask, EXIOptions* ops);
26 
27 size_t readFileInputStream(void* buf, size_t readSize, void* stream);
28 
29 int main(int argc, char *argv[])
30 {
31  FILE *infile = stdin;
32  char sourceFileName[500];
33  EXIPSchema schema;
34  EXIPSchema* schemaPtr = NULL;
35  EXIOptions ops;
36  EXIOptions* opsPtr = NULL;
37  boolean outOfBandOpts = FALSE;
38  unsigned char outFlag = OUT_EXI; // Default output option
39  unsigned int argIndex = 1;
40  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
41 
42  strcpy(sourceFileName, "stdin");
43  makeDefaultOpts(&ops);
44 
45  if(argc > 1)
46  {
47  if(strcmp(argv[argIndex], "-help") == 0)
48  {
49  printfHelp();
50  return 0;
51  }
52  else if(strcmp(argv[argIndex], "-exi") == 0)
53  {
54  outFlag = OUT_EXI;
55  if(argc == 2)
56  {
57  printfHelp();
58  return 0;
59  }
60 
61  argIndex += 1;
62  }
63  else if(strcmp(argv[argIndex], "-xml") == 0)
64  {
65  outFlag = OUT_XML;
66  if(argc == 2)
67  {
68  printfHelp();
69  return 0;
70  }
71 
72  argIndex += 1;
73  }
74 
75  if(strstr(argv[argIndex], "-ops") != NULL)
76  {
77  char *mask = argv[argIndex] + 4;
78  outOfBandOpts = TRUE;
79 
80  parseOpsMask(mask, &ops);
81  opsPtr = &ops;
82 
83  argIndex += 1;
84  }
85 
86  if(strstr(argv[argIndex], "-schema") != NULL)
87  {
88  char *xsdList = argv[argIndex] + 7;
89 
90  parseSchema(xsdList, &schema);
91  schemaPtr = &schema;
92 
93  argIndex += 1;
94  }
95  }
96 
97  if(argIndex < argc)
98  {
99  strcpy(sourceFileName, argv[argIndex]);
100 
101  infile = fopen(sourceFileName, "rb" );
102  if(!infile)
103  {
104  fprintf(stderr, "Unable to open file %s\n", sourceFileName);
105  exit(1);
106  }
107  }
108 
109  tmp_err_code = decode(schemaPtr, outFlag, infile, outOfBandOpts, opsPtr, readFileInputStream);
110 
111  if(schemaPtr != NULL)
112  destroySchema(schemaPtr);
113  fclose(infile);
114 
115  if(tmp_err_code != EXIP_OK)
116  {
117  printf("\nError (code: %d) during parsing of the EXI stream: %s\n", tmp_err_code, sourceFileName);
118  return 1;
119  }
120  else
121  {
122  printf("\nSuccessful parsing of the EXI stream: %s\n", sourceFileName);
123  return 0;
124  }
125 }
126 
127 static void printfHelp()
128 {
129  printf("\n" );
130  printf(" EXIP Copyright (c) 2010 - 2012, EISLAB - LuleĆ„ University of Technology Version 0.4 \n");
131  printf(" Author: Rumen Kyusakov\n");
132  printf(" Usage: exipd [options] [exi_in]\n\n");
133  printf(" Options: [-help | [ -xml | -exi ] [-ops=<ops_mask>] -schema=<xsd_in>] \n");
134  printf(" -schema : uses schema defined in <xsd_in> for decoding. All referenced schema files should be included in <xsd_in>\n");
135  printf(" <xsd_in>: Comma-separated list of schema documents encoded in EXI with Preserve.prefixes. The first schema is the\n");
136  printf(" main one and the rest are schemas that are referenced from the main one through the <xs:import> statement.\n");
137  printf(" ops_mask: Specify out-of-band options used for encoding. Fields are delimited by %%.\n");
138  printf(" Use this argument only for specifying out-of-band options. That is if no options are specified in the header of the <exi_in>\n");
139  printf(" The format is: <1>%%<2>%%<3>%%<4>%%<5> where:\n");
140  printf(" <1> - Preservation Options: c - comments, d - dtds, l - lexicalvalues, p - pis, x- prefixes. If none set then \"-\" \n");
141  printf(" <2> - Other options: v - strict interpretation of schema, f - fragments\n");
142  printf(" r - selfContained, c - compression, p - pre-compression, a - aligned to bytes. If none set then \"-\"\n");
143  printf(" <3> - valuePartitionCapacity. If not set then \"-\"\n");
144  printf(" <4> - valueMaxLength. If not set then \"-\"\n");
145  printf(" <5> - blockSize. If not set then \"-\"\n");
146  printf(" For example: cx%%v%%0%%-%%- sets the folowing options: Preservation of comments and prefixes, strict schema informed\n");
147  printf(" and valuePartitionCapacity = 0\n");
148  printf(" -exi : EXI formated output [default]\n");
149  printf(" -xml : XML formated output\n");
150  printf(" -help : Prints this help message\n\n");
151  printf(" exi_in : input file containing the EXI stream (stdin if none specified)\n\n");
152  printf(" Purpose: This program tests the EXIP decoding functionality. The result is printed on the stdout.\n");
153  printf("\n" );
154 }
155 
156 size_t readFileInputStream(void* buf, size_t readSize, void* stream)
157 {
158  FILE *infile = (FILE*) stream;
159  return fread(buf, 1, readSize, infile);
160 }
161 
162 static void parseSchema(char* xsdList, EXIPSchema* schema)
163 {
164  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
165  FILE *schemaFile;
166  BinaryBuffer buffer[MAX_XSD_FILES_COUNT]; // up to 10 XSD files
167  char schemaFileName[500];
168  unsigned int schemaFilesCount = 0;
169  unsigned int i;
170  char *token;
171 
172  for (token = strtok(xsdList, "=,"), i = 0; token != NULL; token = strtok(NULL, "=,"), i++)
173  {
174  schemaFilesCount++;
175  if(schemaFilesCount > MAX_XSD_FILES_COUNT)
176  {
177  fprintf(stderr, "Too many xsd files given as an input: %d", schemaFilesCount);
178  exit(1);
179  }
180 
181  strcpy(schemaFileName, token);
182  schemaFile = fopen(schemaFileName, "rb" );
183  if(!schemaFile)
184  {
185  fprintf(stderr, "Unable to open file %s", schemaFileName);
186  exit(1);
187  }
188  else
189  {
190  //Get file length
191  fseek(schemaFile, 0, SEEK_END);
192  buffer[i].bufLen = ftell(schemaFile) + 1;
193  fseek(schemaFile, 0, SEEK_SET);
194 
195  //Allocate memory
196  buffer[i].buf = (char *) malloc(buffer[i].bufLen);
197  if (!buffer[i].buf)
198  {
199  fprintf(stderr, "Memory allocation error!");
200  fclose(schemaFile);
201  exit(1);
202  }
203 
204  //Read file contents into buffer
205  fread(buffer[i].buf, buffer[i].bufLen, 1, schemaFile);
206  fclose(schemaFile);
207 
208  buffer[i].bufContent = buffer[i].bufLen;
209  buffer[i].ioStrm.readWriteToStream = NULL;
210  buffer[i].ioStrm.stream = NULL;
211  }
212  }
213 
214  // Generate the EXI grammars based on the schema information
215  tmp_err_code = generateSchemaInformedGrammars(buffer, schemaFilesCount, SCHEMA_FORMAT_XSD_EXI, NULL, schema, NULL);
216 
217  for(i = 0; i < schemaFilesCount; i++)
218  {
219  free(buffer[i].buf);
220  }
221 
222  if(tmp_err_code != EXIP_OK)
223  {
224  printf("\nGrammar generation error occurred: %d", tmp_err_code);
225  exit(1);
226  }
227 }
228 
229 static void parseOpsMask(char* mask, EXIOptions* ops)
230 {
231  unsigned int i;
232  char *token;
233 
234  for (token = strtok(mask, "=%"), i = 0; token != NULL; token = strtok(NULL, "=%"), i++)
235  {
236  switch(i)
237  {
238  case 0:
239  if(strcmp(token, "-"))
240  {
241  // Preservation Options: c - comments, d - dtds, l - lexicalvalues, p - pis, x- prefixes
242  if(strstr(token, "c") != NULL)
244  if(strstr(token, "d") != NULL)
246  if(strstr(token, "l") != NULL)
248  if(strstr(token, "p") != NULL)
250  if(strstr(token, "x") != NULL)
252  }
253  break;
254  case 1:
255  if(strcmp(token, "-"))
256  {
257  // Other options: v - strict interpretation of schema, f - fragments
258  // r - selfContained, c - compression, p - pre-compression, a - aligned to bytes\n");
259  if(strstr(token, "v") != NULL)
260  SET_STRICT(ops->enumOpt);
261  if(strstr(token, "f") != NULL)
262  SET_FRAGMENT(ops->enumOpt);
263  if(strstr(token, "r") != NULL)
265  if(strstr(token, "c") != NULL)
266  SET_COMPRESSION(ops->enumOpt);
267  if(strstr(token, "p") != NULL)
269  else if(strstr(token, "a") != NULL)
271  }
272  break;
273  case 2:
274  if(strcmp(token, "-"))
275  {
276  // valuePartitionCapacity
277  ops->valuePartitionCapacity = (Index) strtol(token, NULL, 10);
278  }
279  break;
280  case 3:
281  if(strcmp(token, "-"))
282  {
283  // valueMaxLength
284  ops->valueMaxLength = (Index) strtol(token, NULL, 10);
285  }
286  break;
287  case 4:
288  if(strcmp(token, "-"))
289  {
290  // blockSize
291  ops->blockSize = (uint32_t) strtol(token, NULL, 10);
292  }
293  break;
294  default:
295  {
296  fprintf(stderr, "Wrong options mask: %s", mask);
297  exit(1);
298  }
299  }
300  }
301 }