container.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /** @file opc/container.h
  30. The container.h module has the fundamental methods for dealing with ZIP-based OPC container.
  31. OPC container can be opened in READ-ONLY mode, WRITE-ONLY mode, READ/WRITE mode, TEMPLATE mode and TRANSITION mode.
  32. The most notable mode is the READ/WRITE mode, which gives you concurrent stream-based READ and WRITE access to a
  33. single ZIP-based OPC container. This is achieved without the use of temporary files by taking advantage of the
  34. OPC specific “interleave” mode. \see http://standards.iso.org/ittf/PubliclyAvailableStandards/c051459_ISOIEC_29500-2_2008(E).zip
  35. The TEMPLATE mode allows very fast customized "cloning" of ZIP-based OPC container by using "RAW access" to the ZIP streams.
  36. The TRANSITION mode is a special version of the TEMPLATE mode, which allows transition-based READ/WRITE access to the
  37. ZIP-based OPC container using a temporary file.
  38. */
  39. #include <opc/config.h>
  40. #include <opc/file.h>
  41. #ifndef OPC_CONTAINER_H
  42. #define OPC_CONTAINER_H
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. Handle to an OPC container created by \ref opcContainerOpen.
  48. \see opcContainerOpen.
  49. */
  50. typedef struct OPC_CONTAINER_STRUCT opcContainer;
  51. /**
  52. Modes for opcContainerOpen();
  53. \see opcContainerOpen
  54. */
  55. typedef enum {
  56. /**
  57. Opens the OPC container denoted by \a fileName in READ-ONLY mode. The \a destName parameter must be \a NULL.
  58. \hideinitializer
  59. */
  60. OPC_OPEN_READ_ONLY=0,
  61. /**
  62. Opens the OPC container denoted by \a fileName in WRITE-ONLY mode. The \a destName parameter must be \a NULL.
  63. \hideinitializer
  64. */
  65. OPC_OPEN_WRITE_ONLY=1,
  66. /**
  67. Opens the OPC container denoted by \a fileName in READ/WRITE mode. The \a destName parameter must be \a NULL.
  68. \hideinitializer
  69. */
  70. OPC_OPEN_READ_WRITE=2,
  71. /**
  72. This mode will open the container denoted by \a fileName in READ-ONLY mode and the container denoted by
  73. \a destName in write-only mode. Any modifications will be written to the container denoted by \a destName
  74. and the unmodified streams from \a fileName will be written to \a destName on closing.
  75. \warning Currently not implemented.
  76. \hideinitializer
  77. */
  78. OPC_OPEN_TEMPLATE=3,
  79. /**
  80. Like the OPC_OPEN_TEMPLATE mode, but the \a destName will be renamed to the \a fileName on closing. If \a destName
  81. is \a NULL, then the name of the temporary file will be generated automatically.
  82. \warning Currently not implemented.
  83. \hideinitializer
  84. */
  85. OPC_OPEN_TRANSITION=4
  86. } opcContainerOpenMode;
  87. /** Modes for opcContainerClose.
  88. \see opcContainerClose.
  89. */
  90. typedef enum {
  91. /**
  92. Close the OPC container without any further postprocessing.
  93. \hideinitializer
  94. */
  95. OPC_CLOSE_NOW = 0,
  96. /**
  97. Close the OPC container and trim the file by removing unused fragments like e.g.
  98. deleted parts.
  99. \hideinitializer
  100. */
  101. OPC_CLOSE_TRIM = 1,
  102. /**
  103. Close the OPC container like in \a OPC_CLOSE_TRIM mode, but additionally remove any
  104. "interleaved" parts by reordering them.
  105. \warning Currently not implemented. Same semantic as OPC_CLOSE_TRIM.
  106. \hideinitializer
  107. */
  108. OPC_CLOSE_DEFRAG = 2
  109. } opcContainerCloseMode;
  110. /**
  111. Opens a ZIP-based OPC container.
  112. @param[in] fileName. For more details see \ref opcContainerOpenMode.
  113. @param[in] mode. For more details see \ref opcContainerOpenMode.
  114. @param[in] userContext. Will not be modified by libopc. Can be used to e.g. store the "this" pointer for C++ bindings.
  115. @param[in] destName. For more details see \ref opcContainerOpenMode.
  116. @return \a NULL if failed.
  117. \see opcContainerOpenMode
  118. \see opcContainerDump
  119. */
  120. opcContainer* opcContainerOpen(const xmlChar *fileName,
  121. opcContainerOpenMode mode,
  122. void *userContext,
  123. const xmlChar *destName);
  124. /**
  125. Opens a ZIP-based OPC container from memory.
  126. @param[in] data.
  127. @param[in] data_len.
  128. @param[in] userContext. Will not be modified by libopc. Can be used to e.g. store the "this" pointer for C++ bindings.
  129. @param[in] mode. For more details see \ref opcContainerOpenMode.
  130. @return \a NULL if failed.
  131. */
  132. opcContainer* opcContainerOpenMem(const opc_uint8_t *data, opc_uint32_t data_len,
  133. opcContainerOpenMode mode,
  134. void *userContext);
  135. /**
  136. Opens a ZIP-based OPC container from memory.
  137. @param[in] ioread.
  138. @param[in] iowrite.
  139. @param[in] ioclose.
  140. @param[in] ioseek.
  141. @param[in] iotrim.
  142. @param[in] ioflush.
  143. @param[in] iocontext.
  144. @param[in] file_size.
  145. @param[in] userContext. Will not be modified by libopc. Can be used to e.g. store the "this" pointer for C++ bindings.
  146. @param[in] mode. For more details see \ref opcContainerOpenMode.
  147. @return \a NULL if failed.
  148. */
  149. opcContainer* opcContainerOpenIO(opcFileReadCallback *ioread,
  150. opcFileWriteCallback *iowrite,
  151. opcFileCloseCallback *ioclose,
  152. opcFileSeekCallback *ioseek,
  153. opcFileTrimCallback *iotrim,
  154. opcFileFlushCallback *ioflush,
  155. void *iocontext,
  156. pofs_t file_size,
  157. opcContainerOpenMode mode,
  158. void *userContext);
  159. /**
  160. Close an OPC container.
  161. @param[in] c. \ref opcContainer openered by \ref opcContainerOpen.
  162. @param[in] mode. For more information see \ref opcContainerCloseMode.
  163. @return Non-zero if successful.
  164. \see opcContainerOpen
  165. \see opcContainerCloseMode
  166. */
  167. opc_error_t opcContainerClose(opcContainer *c, opcContainerCloseMode mode);
  168. /**
  169. Returns the unmodified user context passed to \ref opcContainerOpen.
  170. \see opcContainerOpen
  171. */
  172. void *opcContainerGetUserContext(opcContainer *c);
  173. /**
  174. List all types, relations and parts of the container \a c to \a out.
  175. \par Sample:
  176. \include opc_dump.c
  177. */
  178. opc_error_t opcContainerDump(opcContainer *c, FILE *out);
  179. /**
  180. Exports the OPC container to "Flat OPC" (http://blogs.msdn.com/b/ericwhite/archive/2008/09/29/the-flat-opc-format.aspx).
  181. The flat versions of an OPC file are very important when dealing with e.g XSL(T)-based or Javascript-based transformations.
  182. \see opcContainerFlatImport.
  183. \todo Implementation needed.
  184. */
  185. int opcContainerFlatExport(opcContainer *c, const xmlChar *fileName);
  186. /**
  187. Imports the flat version of an OPC container.
  188. \see opcContainerFlatExport.
  189. \todo Implementation needed.
  190. */
  191. int opcContainerFlatImport(opcContainer *c, const xmlChar *fileName);
  192. /**
  193. Iterate all types.
  194. \code
  195. for(xmlChar *type=opcContentTypeFirst(c);
  196. NULL!=type;
  197. type=opcContentTypeNext(c, type)) {
  198. printf("%s\n", type);
  199. }
  200. \endcode
  201. */
  202. const xmlChar *opcContentTypeFirst(opcContainer *container);
  203. /**
  204. \see opcContentTypeNext()
  205. */
  206. const xmlChar *opcContentTypeNext(opcContainer *container, const xmlChar *type);
  207. /**
  208. Iterate extensions.
  209. \code
  210. for(const xmlChar *ext=opcExtensionFirst(c);
  211. NULL!=ext;
  212. ext=opcExtensionNext(ext)) {
  213. printf("%s\n", ext);
  214. }
  215. \endcode
  216. */
  217. const xmlChar *opcExtensionFirst(opcContainer *container);
  218. /**
  219. \see opcExtensionFirst()
  220. */
  221. const xmlChar *opcExtensionNext(opcContainer *container, const xmlChar *ext);
  222. /**
  223. Get registered type for extension.
  224. \see opcExtensionRegister()
  225. */
  226. const xmlChar *opcExtensionGetType(opcContainer *container, const xmlChar *ext);
  227. /**
  228. Register a mime-type and and extension.
  229. \see opcExtensionGetType()
  230. */
  231. const xmlChar *opcExtensionRegister(opcContainer *container, const xmlChar *ext, const xmlChar *type);
  232. /**
  233. Iterator through all relation types of the container:
  234. \code
  235. for(xmlChar *type=opcRelationTypeFirst(c);
  236. NULL!=type;
  237. type=opcRelationTypeNext(c, type)) {
  238. printf("%s\n", type);
  239. }
  240. \endcode
  241. */
  242. const xmlChar *opcRelationTypeFirst(opcContainer *container);
  243. /**
  244. \see opcRelationTypeFirst()
  245. */
  246. const xmlChar *opcRelationTypeNext(opcContainer *container, const xmlChar *type);
  247. /**
  248. Iterator through all relation types of the container:
  249. \code
  250. for(xmlChar *target=opcExternalTargetFirst(c);
  251. NULL!=target;
  252. type=opcExternalTargetNext(c, target)) {
  253. printf("%s\n", target);
  254. }
  255. \endcode
  256. */
  257. const xmlChar *opcExternalTargetFirst(opcContainer *container);
  258. /**
  259. \see opcExternalTargetFirst()
  260. */
  261. const xmlChar *opcExternalTargetNext(opcContainer *container, const xmlChar *target);
  262. #ifdef __cplusplus
  263. } /* extern "C" */
  264. #endif
  265. #endif /* OPC_CONTAINER_H */