mcepp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <stdio.h>
  30. #include <time.h>
  31. #include <mce/textreader.h>
  32. #include <libxml/xmlwriter.h>
  33. #include <libxml/xmlreader.h>
  34. #ifdef WIN32
  35. #include <crtdbg.h>
  36. #endif
  37. int main( int argc, const char* argv[] )
  38. {
  39. #ifdef WIN32
  40. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  41. #endif
  42. int ret=-1;
  43. time_t start_time=time(NULL);
  44. FILE *file=NULL;
  45. int writer_indent=0;
  46. pbool_t reader_mce=PTRUE;
  47. const char *fileName=NULL;
  48. for(int i=1;i<argc;i++) {
  49. if ((0==xmlStrcmp(_X("--understands"), _X(argv[i])) || 0==xmlStrcmp(_X("-u"), _X(argv[i]))) && i+1<argc) {
  50. i++; // skip namespace, registered later when parser was created.
  51. } else if ((0==xmlStrcmp(_X("--out"), _X(argv[i])) || 0==xmlStrcmp(_X("--out"), _X(argv[i]))) && i+1<argc && NULL==file) {
  52. const char *filename=argv[++i];
  53. file=fopen(filename, "w");
  54. } else if (0==xmlStrcmp(_X("--indent"), _X(argv[i]))) {
  55. writer_indent=1;
  56. } else if (0==xmlStrcmp(_X("--raw"), _X(argv[i]))) {
  57. reader_mce=PFALSE;
  58. } else if (NULL==fileName) {
  59. fileName=argv[i];
  60. } else {
  61. fprintf(stderr, "IGNORED: %s\n", argv[i]);
  62. }
  63. }
  64. xmlTextWriter *writer=xmlNewTextWriterFile(file);
  65. if (NULL==fileName || NULL==writer) {
  66. printf("mcepp [--understands NAMESPACE] [--out FILENAME] [--indent] [--raw] [FILENAME | - ]\n\n");
  67. printf("Sample: mcepp sample.xml\n");
  68. } else {
  69. xmlInitParser();
  70. xmlTextWriterSetIndent(writer, writer_indent);
  71. mceTextReader_t mceTextReader;
  72. mceTextReaderInit(&mceTextReader, ('-'==fileName[0] && 0==fileName[1]?xmlReaderForFd(0, NULL, NULL, 0):xmlReaderForFile(fileName, NULL, 0)));
  73. mceTextReaderDisableMCE(&mceTextReader, !reader_mce);
  74. for(int i=1;i<argc;i++) {
  75. if ((0==xmlStrcmp(_X("--understands"), _X(argv[i])) || 0==xmlStrcmp(_X("-u"), _X(argv[i]))) && i+1<argc) {
  76. const xmlChar *ns=_X(argv[++i]);
  77. mceTextReaderUnderstandsNamespace(&mceTextReader, ns);
  78. }
  79. }
  80. if (-1==mceTextReaderDump(&mceTextReader, writer, PFALSE)) {
  81. ret=mceTextReaderGetError(&mceTextReader);
  82. } else {
  83. ret=0;
  84. }
  85. mceTextReaderCleanup(&mceTextReader);
  86. xmlCleanupParser();
  87. }
  88. if (NULL!=writer) xmlFreeTextWriter(writer);
  89. if (NULL!=file) fclose(file);
  90. time_t end_time=time(NULL);
  91. fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
  92. #ifdef WIN32
  93. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  94. #endif
  95. return ret;
  96. }