opc_extract.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. Extract a part from an OPC containers.
  31. Ussage:
  32. opc_extract FILENAME PARTNAME
  33. Sample:
  34. opc_extract OOXMLI1.docx "word/document.xml"
  35. */
  36. #include <opc/opc.h>
  37. #include <stdio.h>
  38. #ifdef WIN32
  39. #include <io.h>
  40. #include <fcntl.h>
  41. #endif
  42. #ifdef WIN32
  43. #include <crtdbg.h>
  44. #endif
  45. int main( int argc, const char* argv[] )
  46. {
  47. #ifdef WIN32
  48. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  49. #endif
  50. #ifdef WIN32
  51. _setmode( _fileno( stdout ), _O_BINARY ); // make sure LF are not translated to CR LF on windows...
  52. #endif
  53. if (OPC_ERROR_NONE==opcInitLibrary() && 3==argc) {
  54. opcContainer *c=NULL;
  55. if (NULL!=(c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_ONLY, NULL, NULL))) {
  56. opcPart part=OPC_PART_INVALID;
  57. if ((part=opcPartFind(c, _X(argv[2]), NULL, 0))!=OPC_PART_INVALID) {
  58. opcContainerInputStream *stream=NULL;
  59. if ((stream=opcContainerOpenInputStream(c, part))!=NULL){
  60. opc_uint8_t buf[100];
  61. opc_uint32_t len=0;
  62. while((len=opcContainerReadInputStream(stream, buf, sizeof(buf)))>0) {
  63. fwrite(buf, sizeof(opc_uint8_t), len, stdout);
  64. }
  65. opcContainerCloseInputStream(stream);
  66. } else {
  67. fprintf(stderr, "ERROR: part \"%s\" could not be opened for reading.\n", argv[2]);
  68. }
  69. } else {
  70. fprintf(stderr, "ERROR: part \"%s\" could not be opened in \"%s\".\n", argv[2], argv[1]);
  71. }
  72. opcContainerClose(c, OPC_CLOSE_NOW);
  73. } else {
  74. fprintf(stderr, "ERROR: file \"%s\" could not be opened.\n", argv[1]);
  75. }
  76. opcFreeLibrary();
  77. } else if (2==argc) {
  78. fprintf(stderr, "ERROR: initialization of libopc failed.\n");
  79. } else {
  80. fprintf(stderr, "opc_extract FILENAME PART\n\n");
  81. fprintf(stderr, "Sample: opc_extract test.docx word/document.xml\n");
  82. }
  83. #ifdef WIN32
  84. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  85. #endif
  86. return 0;
  87. }