opc_generate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. Generates "C" code from an OPC container template. The generated "C" code will then prgrammatically generate the container again.
  31. Very useful for generating boilerplate code.
  32. Ussage:
  33. opc_generate FILENAME OUTNAME
  34. Sample:
  35. opc_generate helloworld.pptx helloworld.c
  36. */
  37. #include <opc/opc.h>
  38. #include <stdio.h>
  39. #include <time.h>
  40. #include <libxml/xmlsave.h>
  41. #ifdef WIN32
  42. #include <crtdbg.h>
  43. #endif
  44. static void add_visited_part(opcPart **visited_parts_array, opc_uint32_t *visited_parts_count, opcPart part) {
  45. *visited_parts_array=(opcPart*)xmlRealloc(*visited_parts_array, sizeof(opcPart)*(*visited_parts_count+1));
  46. (*visited_parts_array)[(*visited_parts_count)++]=part;
  47. }
  48. static opc_bool_t is_visited_part(opcPart *visited_parts_array, opc_uint32_t visited_parts_count, opcPart part) {
  49. for(opc_uint32_t i=0;i<visited_parts_count;i++) {
  50. if (visited_parts_array[i]==part) {
  51. return OPC_TRUE;
  52. }
  53. }
  54. return OPC_FALSE;
  55. }
  56. static void normalize_name(char *dest, const xmlChar *src, opc_uint32_t len) {
  57. opc_uint32_t i=0;
  58. opc_uint32_t j=0;
  59. while(j<len && 0!=src[i]) {
  60. if (src[i]=='/' || src[i]=='.' || src[i]=='-' || src[i]=='_' || src[i]=='[' || src[i]==']') {
  61. dest[j++]='_'; i++;
  62. } else if ((src[i]>='A' && src[i]<='Z') || (src[i]>='a' && src[i]<='z') || (src[i]>='0' && src[i]<='9')) {
  63. dest[j++]=src[i++];
  64. } else {
  65. j+=snprintf(dest+j, len-j, "x%02X", src[i++]);
  66. }
  67. }
  68. if (j<len) dest[j]=0; else if (j>0) dest[j-1]=0;
  69. }
  70. static xmlChar *xmlStrEscape(const xmlChar *str) {
  71. opc_uint32_t i=0;
  72. opc_uint32_t a=0;
  73. for(;str[i]!=0;i++) {
  74. if ('\\'==str[i]) {
  75. a++;
  76. }
  77. }
  78. if (0==a) {
  79. return xmlStrdup(str);
  80. } else {
  81. xmlChar *ret = (xmlChar *) xmlMalloc((i + a + 1) * sizeof(xmlChar));
  82. for(opc_uint32_t j=0, k=0;j<=i;j++) {
  83. if ('\\'==str[j]) { ret[k++]='\\'; ret[k++]='\\';} else ret[k++]=str[j];
  84. }
  85. return ret;
  86. }
  87. }
  88. static void generate_relations(opcContainer *c, FILE *out, opcPart root) {
  89. for(opcRelation rel=opcRelationFirst(c, root)
  90. ;OPC_RELATION_INVALID!=rel
  91. ;rel=opcRelationNext(c, root, rel)) {
  92. const xmlChar *prefix=NULL;
  93. opc_uint32_t counter=-1;
  94. const xmlChar *type=NULL;
  95. opcRelationGetInformation(c, root, rel, &prefix, &counter, &type);
  96. char buf[20]="";
  97. if (-1!=counter) {
  98. sprintf(buf, "%i", counter);
  99. }
  100. opcPart internal_target=opcRelationGetInternalTarget(c, root, rel);
  101. if (OPC_PART_INVALID!=internal_target) {
  102. char part[OPC_MAX_PATH]="";
  103. normalize_name(part, internal_target, sizeof(part));
  104. fprintf(out, " %sopcRelationAdd(c, %s, _X(\"%s%s\"), create_%s(c), _X(\"%s\"));\n",
  105. (OPC_PART_INVALID==root?"":" "),
  106. (OPC_PART_INVALID==root?"OPC_PART_INVALID":"ret"),
  107. prefix, buf, part, type);
  108. } else {
  109. xmlChar *external_target=xmlStrEscape(opcRelationGetExternalTarget(c, root, rel));
  110. if (NULL!=external_target) {
  111. fprintf(out, " %sopcRelationAddExternal(c, %s, _X(\"%s%s\"), _X(\"%s\"), _X(\"%s\"));\n",
  112. (OPC_PART_INVALID==root?"":" "),
  113. (OPC_PART_INVALID==root?"OPC_PART_INVALID":"ret"),
  114. prefix, buf,
  115. external_target,
  116. type);
  117. }
  118. xmlFree(external_target);
  119. }
  120. }
  121. }
  122. static void generate_binary_data(opcContainer *c, FILE *out, opcContainerInputStream *in) {
  123. fprintf(out, " static opc_uint8_t data[]={\n");
  124. opc_uint8_t buf[100];
  125. opc_uint32_t len=0;
  126. char cont=' ';
  127. while((len=opcContainerReadInputStream(in, buf, sizeof(buf)))>0) {
  128. fprintf(out, " ");
  129. for(opc_uint32_t i=0;i<len;i++) {
  130. fprintf(out, "%c 0x%02X", cont, buf[i]);
  131. cont=',';
  132. }
  133. fprintf(out, "\n");
  134. }
  135. fprintf(out, " };\n");
  136. }
  137. static int xmlOutputWrite(void * context, const char * buffer, int len) {
  138. FILE *out=(FILE*)context;
  139. for(int i=0;i<len;i++) {
  140. switch(buffer[i]) {
  141. case '\n':
  142. fprintf(out, "\\n\");\n writes(out, \"");
  143. case '\r':
  144. break;
  145. case '"':
  146. fprintf(out, "\\\"");
  147. break;
  148. case '\\':
  149. fprintf(out, "\\\\");
  150. break;
  151. default:
  152. putc(buffer[i], out);
  153. }
  154. }
  155. return len;
  156. }
  157. static int xmlOutputClose(void * context) {
  158. return 0;
  159. }
  160. static void generate_xml_data(opcContainer *c, FILE *out, opcPart part) {
  161. fprintf(out, " writes(out, \"");
  162. xmlDocPtr doc=opcXmlReaderReadDoc(c, part, NULL, NULL, 0);
  163. if (NULL!=doc) {
  164. xmlSaveCtxtPtr save=xmlSaveToIO(xmlOutputWrite, xmlOutputClose, out, NULL, XML_SAVE_FORMAT | XML_SAVE_NO_DECL);
  165. if (NULL!=save) {
  166. xmlSaveDoc(save, doc);
  167. xmlSaveClose(save);
  168. }
  169. xmlFreeDoc(doc);
  170. }
  171. fprintf(out, "\");\n");
  172. }
  173. static void generate_parts(opcContainer *c, FILE *out) {
  174. for(opcPart part=opcPartGetFirst(c);OPC_PART_INVALID!=part;part=opcPartGetNext(c, part)) {
  175. char norm_part[OPC_MAX_PATH]="";
  176. normalize_name(norm_part, part, sizeof(norm_part));
  177. fprintf(out, "static opcPart create_%s(opcContainer *c);\n", norm_part);
  178. }
  179. fprintf(out, "\n");
  180. for(opcPart part=opcPartGetFirst(c);OPC_PART_INVALID!=part;part=opcPartGetNext(c, part)) {
  181. char norm_part[OPC_MAX_PATH]="";
  182. normalize_name(norm_part, part, sizeof(norm_part));
  183. const xmlChar *override_type=opcPartGetTypeEx(c, part, OPC_TRUE);
  184. fprintf(out, "static opcPart create_%s(opcContainer *c) {\n", norm_part);
  185. if (NULL!=override_type) {
  186. fprintf(out, " opcPart ret=opcPartFind(c, _X(\"%s\"), _X(\"%s\"), 0);\n", part, override_type);
  187. } else {
  188. fprintf(out, " opcPart ret=opcPartFind(c, _X(\"%s\"), NULL, 0);\n", part);
  189. }
  190. if (NULL!=override_type) {
  191. fprintf(out, " if (OPC_PART_INVALID==ret && OPC_PART_INVALID!=(ret=opcPartCreate(c, _X(\"%s\"), _X(\"%s\"), 0))) {\n", part, override_type);
  192. } else {
  193. fprintf(out, " if (OPC_PART_INVALID==ret && OPC_PART_INVALID!=(ret=opcPartCreate(c, _X(\"%s\"), NULL, 0))) {\n", part);
  194. }
  195. fprintf(out, " //adding content\n");
  196. fprintf(out, " opcContainerOutputStream *out=opcContainerCreateOutputStream(c, ret, OPC_COMPRESSIONOPTION_NORMAL);\n");
  197. fprintf(out, " if (NULL!=out) {\n");
  198. const xmlChar *type=opcPartGetType(c, part);
  199. opc_uint32_t type_len=(NULL!=type?xmlStrlen(type):0);
  200. if (type_len>0 && type[type_len-3]=='x' && type[type_len-2]=='m' && type[type_len-1]=='l') {
  201. generate_xml_data(c, out, part);
  202. } else {
  203. if (opcPartGetSize(c, part)>0) {
  204. opcContainerInputStream *in=opcContainerOpenInputStream(c, part);
  205. if (NULL!=in) {
  206. generate_binary_data(c, out, in);
  207. fprintf(out, " opcContainerWriteOutputStream(out, (const opc_uint8_t*)data, sizeof(data));\n");
  208. }
  209. opcContainerCloseInputStream(in);
  210. }
  211. }
  212. fprintf(out, " opcContainerCloseOutputStream(out);\n");
  213. fprintf(out, " }\n");
  214. fprintf(out, " // adding relations\n");
  215. generate_relations(c, out, part);
  216. fprintf(out, " }\n");
  217. fprintf(out, " return ret;\n");
  218. fprintf(out, "}\n");
  219. fprintf(out, "\n");
  220. }
  221. }
  222. static void visit_all_parts(opcContainer *c, opcPart root, opcPart **visited_parts_array, opc_uint32_t *visited_parts_count) {
  223. for(opcRelation rel=opcRelationFirst(c, root)
  224. ;OPC_RELATION_INVALID!=rel
  225. ;rel=opcRelationNext(c, root, rel)) {
  226. opcPart part=opcRelationGetInternalTarget(c, root, rel);
  227. if (OPC_PART_INVALID!=part) {
  228. if (!is_visited_part(*visited_parts_array, *visited_parts_count, part)) {
  229. add_visited_part(visited_parts_array, visited_parts_count, part);
  230. visit_all_parts(c, part, visited_parts_array, visited_parts_count);
  231. }
  232. }
  233. }
  234. }
  235. static void generate_weak_parts(opcContainer *c, FILE *out) {
  236. opcPart *visited_parts_array=NULL;
  237. opc_uint32_t visited_parts_count=0;
  238. visit_all_parts(c, OPC_PART_INVALID, &visited_parts_array, &visited_parts_count);
  239. for(opcPart part=opcPartGetFirst(c);OPC_PART_INVALID!=part;part=opcPartGetNext(c, part)) {
  240. if (!is_visited_part(visited_parts_array, visited_parts_count, part)) {
  241. char _part[OPC_MAX_PATH]="";
  242. normalize_name(_part, part, sizeof(_part));
  243. fprintf(out, " create_%s(c);\n", _part);
  244. }
  245. }
  246. if (NULL!=visited_parts_array) {
  247. xmlFree(visited_parts_array);
  248. }
  249. }
  250. static void generate(opcContainer *c, FILE *out, const char *template_name, const char *out_name) {
  251. fprintf(out, "// Automatically generated by opc_generate \"%s\" \"%s\".\n", template_name, out_name);
  252. fprintf(out, "\n");
  253. fprintf(out, "#include <opc/opc.h>\n");
  254. fprintf(out, "#ifdef WIN32\n");
  255. fprintf(out, "#include <crtdbg.h>\n");
  256. fprintf(out, "#endif\n");
  257. fprintf(out, "\n");
  258. fprintf(out, "void writef(opcContainerOutputStream* stream, const char *s, ...) {\n");
  259. fprintf(out, " va_list ap;\n");
  260. fprintf(out, " va_start(ap, s);\n");
  261. fprintf(out, " char buf[1024];\n");
  262. fprintf(out, " int len=vsnprintf(buf, sizeof(buf), s, ap);\n");
  263. fprintf(out, " opcContainerWriteOutputStream(stream, (const opc_uint8_t *)buf, len);\n");
  264. fprintf(out, " va_end(ap);\n");
  265. fprintf(out, "}\n");
  266. fprintf(out, "\n");
  267. fprintf(out, "void writes(opcContainerOutputStream* stream, const char *s) {\n");
  268. fprintf(out, " int const len=strlen(s);\n");
  269. fprintf(out, " opcContainerWriteOutputStream(stream, (const opc_uint8_t *)s, len);\n");
  270. fprintf(out, "}\n");
  271. fprintf(out, "\n");
  272. generate_parts(c, out);
  273. fprintf(out, "void generate(opcContainer *c, FILE *out) {\n");
  274. fprintf(out, " // adding registered extensions\n");
  275. for(const xmlChar *ext=opcExtensionFirst(c);NULL!=ext;ext=opcExtensionNext(c, ext)) {
  276. const xmlChar *type=opcExtensionGetType(c, ext);
  277. fprintf(out, " opcExtensionRegister(c, _X(\"%s\"), _X(\"%s\"));\n", ext, type);
  278. }
  279. fprintf(out, " // adding root relations\n");
  280. generate_relations(c, out, OPC_PART_INVALID);
  281. fprintf(out, " // adding weak parts, i.e. parts which are not referenced by a relation\n");
  282. generate_weak_parts(c, out);
  283. fprintf(out, "}\n");
  284. fprintf(out, "\n");
  285. fprintf(out, "int main( int argc, const char* argv[] ) {\n");
  286. fprintf(out, "#ifdef WIN32\n");
  287. fprintf(out, " _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);\n");
  288. fprintf(out, "#endif\n");
  289. fprintf(out, " if (OPC_ERROR_NONE==opcInitLibrary() && 2==argc) {\n");
  290. fprintf(out, " opcContainer *c=NULL;\n");
  291. fprintf(out, " if (NULL!=(c=opcContainerOpen(_X(argv[1]), OPC_OPEN_WRITE_ONLY, NULL, NULL))) {\n");
  292. fprintf(out, " generate(c, stdout);\n");
  293. fprintf(out, " opcContainerClose(c, OPC_CLOSE_NOW);\n");
  294. fprintf(out, " }\n");
  295. fprintf(out, " } else if (argc!=2) {\n");
  296. int ofs=strlen(template_name); while(ofs>0 && template_name[ofs-1]!='/' && template_name[ofs-1]!='\\') ofs--;
  297. fprintf(out, " printf(\"target file needed!\\n E.g. %%s \\\"%s\\\"\", argv[0]);\n", template_name+ofs);
  298. fprintf(out, " }\n");
  299. fprintf(out, " opcFreeLibrary();\n");
  300. fprintf(out, "#ifdef WIN32\n");
  301. fprintf(out, " OPC_ASSERT(!_CrtDumpMemoryLeaks());\n");
  302. fprintf(out, "#endif\n");
  303. fprintf(out, "}\n");
  304. }
  305. int main( int argc, const char* argv[] )
  306. {
  307. #ifdef WIN32
  308. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  309. #endif
  310. time_t start_time=time(NULL);
  311. opc_error_t err=OPC_ERROR_NONE;
  312. if (OPC_ERROR_NONE==opcInitLibrary() && argc>=2) {
  313. opcContainer *c=NULL;
  314. if (NULL!=(c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_ONLY, NULL, NULL))) {
  315. FILE *out=stdout;
  316. if (argc>=3) {
  317. out=fopen(argv[2], "w");
  318. }
  319. generate(c, out, argv[1], argv[2]);
  320. if (stdout!=out) {
  321. fclose(out);
  322. }
  323. opcContainerClose(c, OPC_CLOSE_NOW);
  324. } else {
  325. printf("ERROR: \"%s\" could not be opened.\n", argv[1]);
  326. err=OPC_ERROR_STREAM;
  327. }
  328. opcFreeLibrary();
  329. } else if (2==argc) {
  330. printf("ERROR: initialization of libopc failed.\n");
  331. err=OPC_ERROR_STREAM;
  332. } else {
  333. printf("opc_generate CONTAINERNAME CFILENAME\n\n");
  334. printf("Sample: opc_generate test.docx test.c\n");
  335. }
  336. time_t end_time=time(NULL);
  337. fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
  338. #ifdef WIN32
  339. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  340. #endif
  341. return (OPC_ERROR_NONE==err?0:3);
  342. }