exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
exipe.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 "encodeTestEXI.h"
19 #include "grammarGenerator.h"
20 
21 #define MAX_XSD_FILES_COUNT 10 // up to 10 XSD files
22 
23 static void printfHelp();
24 size_t writeFileOutputStream(void* buf, size_t readSize, void* stream);
25 static void parseSchema(char* xsdList, EXIPSchema* schema);
26 
27 int main(int argc, char *argv[])
28 {
29  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
30  FILE *outfile = stdout; // Default is the standard output
31  char sourceFile[500];
32  EXIPSchema schema;
33  EXIPSchema* schemaPtr = NULL;
34  int argIndex = 1;
35 
36  strcpy(sourceFile, "stdout");
37 
38  if(argc >= 2)
39  {
40  if(strcmp(argv[argIndex], "-help") == 0)
41  {
42  printfHelp();
43  return 0;
44  }
45  else if(strstr(argv[argIndex], "-schema") != NULL)
46  {
47  // Schema enabled encoding is requested.
48  // All the xsd files should be passed as comma-separated list
49  char* xsdList = argv[argIndex] + 7;
50 
51  parseSchema(xsdList, &schema);
52 
53  schemaPtr = &schema;
54  argIndex++;
55  }
56  }
57 
58  if(argIndex < argc)
59  {
60  strcpy(sourceFile, argv[argIndex]);
61 
62  outfile = fopen(sourceFile, "wb" );
63  if(!outfile)
64  {
65  fprintf(stderr, "Unable to open file %s", sourceFile);
66  return 1;
67  }
68  }
69 
70  tmp_err_code = encode(schemaPtr, outfile, writeFileOutputStream);
71 
72  if(schemaPtr != NULL)
73  destroySchema(schemaPtr);
74 
75  if(tmp_err_code != EXIP_OK)
76  printf("\nError (code: %d) during encoding of: %s\n", tmp_err_code, sourceFile);
77  else
78  printf("\nSuccessful encoding in %s\n", sourceFile);
79 
80  fclose(outfile);
81 
82  return 0;
83 }
84 
85 static void printfHelp()
86 {
87  printf("\n" );
88  printf(" EXIP Copyright (c) 2010 - 2012, EISLAB - LuleĆ„ University of Technology Version 0.4 \n");
89  printf(" Author: Rumen Kyusakov\n");
90  printf(" Usage: exipe [options] [exi_out]\n\n");
91  printf(" Options: [-help | -schema=<xsd_in>] \n");
92  printf(" -schema : uses schema defined in <xsd_in> for encoding. All referenced schema files should be included in <xsd_in>\n");
93  printf(" <xsd_in>: Comma-separated list of schema documents encoded in EXI with Preserve.prefixes. The first schema is the\n");
94  printf(" main one and the rest are schemas that are referenced from the main one through the <xs:import> statement.\n");
95  printf(" -help : Prints this help message\n\n");
96  printf(" exi_out : output file for the EXI stream (stdout if none specified)\n\n");
97  printf(" Purpose: This program tests the EXIP encoding functionality\n");
98  printf("\n" );
99 }
100 
101 size_t writeFileOutputStream(void* buf, size_t readSize, void* stream)
102 {
103  FILE *outfile = (FILE*) stream;
104  return fwrite(buf, 1, readSize, outfile);
105 }
106 
107 static void parseSchema(char* xsdList, EXIPSchema* schema)
108 {
109  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
110  FILE *schemaFile;
111  BinaryBuffer buffer[MAX_XSD_FILES_COUNT]; // up to 10 XSD files
112  char schemaFileName[500];
113  unsigned int schemaFilesCount = 0;
114  unsigned int i;
115  char *token;
116 
117  for (token = strtok(xsdList, "=,"), i = 0; token != NULL; token = strtok(NULL, "=,"), i++)
118  {
119  schemaFilesCount++;
120  if(schemaFilesCount > MAX_XSD_FILES_COUNT)
121  {
122  fprintf(stderr, "Too many xsd files given as an input: %d", schemaFilesCount);
123  exit(1);
124  }
125 
126  strcpy(schemaFileName, token);
127  schemaFile = fopen(schemaFileName, "rb" );
128  if(!schemaFile)
129  {
130  fprintf(stderr, "Unable to open file %s", schemaFileName);
131  exit(1);
132  }
133  else
134  {
135  //Get file length
136  fseek(schemaFile, 0, SEEK_END);
137  buffer[i].bufLen = ftell(schemaFile) + 1;
138  fseek(schemaFile, 0, SEEK_SET);
139 
140  //Allocate memory
141  buffer[i].buf = (char *) malloc(buffer[i].bufLen);
142  if (!buffer[i].buf)
143  {
144  fprintf(stderr, "Memory allocation error!");
145  fclose(schemaFile);
146  exit(1);
147  }
148 
149  //Read file contents into buffer
150  fread(buffer[i].buf, buffer[i].bufLen, 1, schemaFile);
151  fclose(schemaFile);
152 
153  buffer[i].bufContent = buffer[i].bufLen;
154  buffer[i].ioStrm.readWriteToStream = NULL;
155  buffer[i].ioStrm.stream = NULL;
156  }
157  }
158 
159  // Generate the EXI grammars based on the schema information
160  tmp_err_code = generateSchemaInformedGrammars(buffer, schemaFilesCount, SCHEMA_FORMAT_XSD_EXI, NULL, schema, NULL);
161 
162  for(i = 0; i < schemaFilesCount; i++)
163  {
164  free(buffer[i].buf);
165  }
166 
167  if(tmp_err_code != EXIP_OK)
168  {
169  printf("\nGrammar generation error occurred: %d", tmp_err_code);
170  exit(1);
171  }
172 }