opc_zipwrite.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /*
  36. This example shows how to use the low level zip functions.
  37. Ussage:
  38. opc_zipread FILENAME [--import] [--delete] [--add] [--commit] [--trim]
  39. * --import existing streams
  40. * --delete all streams
  41. * --add sample streams
  42. * --commit streams
  43. * --trim commit and trim streams
  44. Sample:
  45. opc_zipread out.zip --delete --import --add --commit
  46. */
  47. static opc_error_t addSegment(void *iocontext,
  48. void *userctx,
  49. opcZipSegmentInfo_t *info,
  50. opcZipLoaderOpenCallback *open,
  51. opcZipLoaderReadCallback *read,
  52. opcZipLoaderCloseCallback *close,
  53. opcZipLoaderSkipCallback *skip) {
  54. opcZip *zip=(opcZip *)userctx;
  55. OPC_ENSURE(0==skip(iocontext));
  56. OPC_ENSURE(-1!=opcZipLoadSegment(zip, xmlStrdup(info->name), info->rels_segment, info));
  57. return OPC_ERROR_NONE;
  58. }
  59. static opc_error_t releaseSegment(opcZip *zip, opc_uint32_t segment_id) {
  60. const xmlChar *name=NULL;
  61. OPC_ENSURE(OPC_ERROR_NONE==opcZipGetSegmentInfo(zip, segment_id, &name, NULL, NULL));
  62. OPC_ASSERT(NULL!=name);
  63. xmlFree((void*)name);
  64. return OPC_ERROR_NONE;
  65. }
  66. int main( int argc, const char* argv[] )
  67. {
  68. #ifdef WIN32
  69. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  70. #endif
  71. time_t start_time=time(NULL);
  72. opc_error_t err=OPC_ERROR_NONE;
  73. if (OPC_ERROR_NONE==(err=opcInitLibrary())) {
  74. if (argc>0) {
  75. opcIO_t io;
  76. if (OPC_ERROR_NONE==opcFileInitIOFile(&io, _X(argv[1]), OPC_FILE_READ | OPC_FILE_WRITE)) {
  77. opcZip *zip=opcZipCreate(&io);
  78. if (NULL!=zip) {
  79. for(int i=2;i<argc;i++) {
  80. if (0==strcmp(argv[i], "--import")) { // import existing segments
  81. OPC_ENSURE(OPC_ERROR_NONE==opcZipLoader(&io, zip, addSegment));
  82. } else if (0==strcmp(argv[i], "--delete")) { // delete all segments
  83. for(opc_uint32_t i=opcZipGetFirstSegmentId(zip);-1!=i;i=opcZipGetNextSegmentId(zip, i)) {
  84. opc_uint32_t first_segment=i;
  85. opc_uint32_t last_segment=i;
  86. OPC_ENSURE(opcZipSegmentDelete(zip, &first_segment, &last_segment, releaseSegment));
  87. }
  88. } else if (0==strcmp(argv[i], "--add")) { // add sample streams
  89. static char txt[]="Hello World!";
  90. opc_uint32_t segment1_id=opcZipCreateSegment(zip, xmlStrdup(_X("hello.txt")), OPC_FALSE, 47+5, 0, 0, 0);
  91. opc_uint32_t segment2_id=opcZipCreateSegment(zip, xmlStrdup(_X("stream.txt")), OPC_FALSE, 47+5, 0, 8, 6);
  92. if (-1!=segment1_id) {
  93. opcZipOutputStream *out=opcZipOpenOutputStream(zip, &segment1_id);
  94. OPC_ENSURE(opcZipWriteOutputStream(zip, out, _X(txt), strlen(txt))==strlen(txt));
  95. OPC_ENSURE(OPC_ERROR_NONE==opcZipCloseOutputStream(zip, out, &segment1_id));
  96. }
  97. if (-1!=segment2_id) {
  98. opcZipOutputStream *out=opcZipOpenOutputStream(zip, &segment2_id);
  99. OPC_ENSURE(opcZipWriteOutputStream(zip, out, _X(txt), strlen(txt))==strlen(txt));
  100. OPC_ENSURE(OPC_ERROR_NONE==opcZipCloseOutputStream(zip, out, &segment2_id));
  101. }
  102. } else if (0==strcmp(argv[i], "--commit")) { // commit
  103. OPC_ENSURE(OPC_ERROR_NONE==opcZipCommit(zip, OPC_FALSE));
  104. } else if (0==strcmp(argv[i], "--trim")) { // commit and trim
  105. OPC_ENSURE(OPC_ERROR_NONE==opcZipCommit(zip, OPC_TRUE));
  106. }
  107. }
  108. opcZipClose(zip, releaseSegment);
  109. } else {
  110. OPC_ENSURE(OPC_ERROR_NONE==opcFileCleanupIO(&io));
  111. }
  112. }
  113. }
  114. if (OPC_ERROR_NONE==err) err=opcFreeLibrary();
  115. }
  116. time_t end_time=time(NULL);
  117. fprintf(stdout, "time %.2lfsec\n", difftime(end_time, start_time));
  118. #ifdef WIN32
  119. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  120. #endif
  121. return (OPC_ERROR_NONE==err?0:3);
  122. }