opc_mem.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Dump all information about an OPC container via the memory-based OPC container interface.
  31. Ussage:
  32. opc_mem FILENAME
  33. Sample:
  34. opc_mem OOXMLI1.docx
  35. */
  36. #include <opc/opc.h>
  37. #include <stdio.h>
  38. #include <time.h>
  39. #ifdef WIN32
  40. #include <crtdbg.h>
  41. #endif
  42. int main( int argc, const char* argv[] )
  43. {
  44. #ifdef WIN32
  45. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  46. #endif
  47. time_t start_time=time(NULL);
  48. opc_error_t err=OPC_ERROR_NONE;
  49. if (OPC_ERROR_NONE==opcInitLibrary() && 2==argc) {
  50. opcContainer *c=NULL;
  51. FILE *f=fopen(argv[1], "rb");
  52. fseek(f, 0, SEEK_END);
  53. opc_uint32_t data_len=ftell(f);
  54. fseek(f, 0, SEEK_SET);
  55. opc_uint8_t *data=(opc_uint8_t *)xmlMalloc(data_len);
  56. size_t fread_len=fread(data, 1, data_len, f);
  57. fclose(f);
  58. if (data_len!=fread_len) {
  59. printf("ERROR: reading file.\n");
  60. } else if (NULL!=(c=opcContainerOpenMem(data, data_len, OPC_OPEN_READ_ONLY, NULL))) {
  61. opcContainerDump(c, stdout);
  62. opcContainerClose(c, OPC_CLOSE_NOW);
  63. } else {
  64. printf("ERROR: \"%s\" could not be opened.\n", argv[1]);
  65. err=OPC_ERROR_STREAM;
  66. }
  67. xmlFree(data);
  68. opcFreeLibrary();
  69. } else if (2==argc) {
  70. printf("ERROR: initialization of libopc failed.\n");
  71. err=OPC_ERROR_STREAM;
  72. } else {
  73. printf("opc_dump FILENAME.\n\n");
  74. printf("Sample: opc_dump test.docx\n");
  75. }
  76. time_t end_time=time(NULL);
  77. fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
  78. #ifdef WIN32
  79. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  80. #endif
  81. return (OPC_ERROR_NONE==err?0:3);
  82. }