opc_proc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <opc/opc.h>
  30. #include <stdio.h>
  31. #include <time.h>
  32. #ifdef WIN32
  33. #include <crtdbg.h>
  34. #endif
  35. int main( int argc, const char* argv[] )
  36. {
  37. #ifdef WIN32
  38. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  39. #endif
  40. time_t start_time=time(NULL);
  41. opc_error_t err=OPC_ERROR_NONE;
  42. if (OPC_ERROR_NONE==opcInitLibrary() && argc>1) {
  43. opcContainer *c=NULL;
  44. if (NULL!=(c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_WRITE, NULL, NULL))) {
  45. pbool_t closed=PFALSE;
  46. for(puint32_t i=2;i<argc;i++) {
  47. if (xmlStrcmp(_X(argv[i]), _X("--dump"))==0) {
  48. opcContainerDump(c, stdout);
  49. } else if (xmlStrcmp(_X(argv[i]), _X("--create"))==0 && i+4<argc) {
  50. const xmlChar *part_name=_X(argv[i+1]);
  51. const xmlChar *part_type=_X(argv[i+2]);
  52. const puint32_t part_flags=atoi(argv[i+3]);
  53. if (xmlStrcasecmp(part_type, _X("NULL"))==0) {
  54. part_type=NULL;
  55. }
  56. opcPart part=opcPartCreate(c, part_name, part_type, part_flags);
  57. if (OPC_PART_INVALID!=part) {
  58. opcContainerOutputStream* stream = opcContainerCreateOutputStream(c, part, OPC_COMPRESSIONOPTION_NORMAL);
  59. if (stream != NULL) {
  60. const char *filename=argv[i+4];
  61. FILE *in=fopen(filename, "rb");
  62. if (NULL!=in) {
  63. int ret=0;
  64. opc_uint8_t buf[100];
  65. while((ret=fread(buf, sizeof(opc_uint8_t), sizeof(buf), in))>0) {
  66. opcContainerWriteOutputStream(stream, buf, ret);
  67. }
  68. fclose(in);
  69. }
  70. opcContainerCloseOutputStream(stream);
  71. }
  72. }
  73. i+=4;
  74. } else if (xmlStrcmp(_X(argv[i]), _X("--delete"))==0 && i+1<argc) {
  75. const xmlChar *part_name=_X(argv[i+1]);
  76. OPC_ENSURE(OPC_ERROR_NONE==opcPartDelete(c, part_name));
  77. i+=1;
  78. } else {
  79. printf("ERROR: unknown command \"%s\".\n", argv[i]);
  80. return 3;
  81. }
  82. }
  83. if (!closed) {
  84. opcContainerClose(c, OPC_CLOSE_NOW);
  85. }
  86. } else {
  87. printf("ERROR: \"%s\" could not be opened.\n", argv[1]);
  88. err=OPC_ERROR_STREAM;
  89. }
  90. opcFreeLibrary();
  91. } else if (2==argc) {
  92. printf("ERROR: initialization of libopc failed.\n");
  93. err=OPC_ERROR_STREAM;
  94. } else {
  95. printf("opc_proc FILENAME [COMMANDS].\n\n");
  96. printf("Sample: opc_proc test.docx --dump\n");
  97. }
  98. time_t end_time=time(NULL);
  99. fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
  100. #ifdef WIN32
  101. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  102. #endif
  103. return (OPC_ERROR_NONE==err?0:3);
  104. }