file.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/file.h
  30. The opc module contains the file library functions.
  31. */
  32. #include <opc/config.h>
  33. #ifndef OPC_FILE_H
  34. #define OPC_FILE_H
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. Flag for READ access.
  40. \hideinitializer
  41. */
  42. #define OPC_FILE_READ (1<<0)
  43. /**
  44. Flag for WRITE access.
  45. \hideinitializer
  46. */
  47. #define OPC_FILE_WRITE (1<<1)
  48. /**
  49. Flag indicates that file will be truncated when opened.
  50. \hideinitializer
  51. */
  52. #define OPC_FILE_TRUNC (1<<2)
  53. /**
  54. Abstraction for see modes.
  55. */
  56. typedef enum OPC_FILESEEKMODE_ENUM {
  57. opcFileSeekSet = SEEK_SET,
  58. opcFileSeekCur = SEEK_CUR,
  59. opcFileSeekEnd = SEEK_END
  60. } opcFileSeekMode;
  61. /**
  62. Callback to read a file. E.g. for a FILE * context this can be implemented as
  63. \code
  64. static int opcFileRead(void *iocontext, char *buffer, int len) {
  65. return fread(buffer, sizeof(char), len, (FILE*)iocontext);
  66. }
  67. \endcode
  68. */
  69. typedef int opcFileReadCallback(void *iocontext, char *buffer, int len);
  70. /**
  71. Callback to write a file. E.g. for a FILE * context this can be implemented as
  72. \code
  73. static int opcFileWrite(void *iocontext, const char *buffer, int len) {
  74. return fwrite(buffer, sizeof(char), len, (FILE*)iocontext);
  75. }
  76. \endcode
  77. */
  78. typedef int opcFileWriteCallback(void *iocontext, const char *buffer, int len);
  79. /**
  80. Callback to close a file. E.g. for a FILE * context this can be implemented as
  81. \code
  82. static int opcFileClose(void *iocontext) {
  83. return fclose((FILE*)iocontext);
  84. }
  85. \endcode
  86. */
  87. typedef int opcFileCloseCallback(void *iocontext);
  88. /**
  89. Callback to seek a file. E.g. for a FILE * context this can be implemented as
  90. \code
  91. static opc_ofs_t opcFileSeek(void *iocontext, opc_ofs_t ofs) {
  92. int ret=fseek((FILE*)iocontext, ofs, SEEK_SET);
  93. if (ret>=0) {
  94. return ftell((FILE*)iocontext);
  95. } else {
  96. return ret;
  97. }
  98. }
  99. \endcode
  100. */
  101. typedef opc_ofs_t opcFileSeekCallback(void *iocontext, opc_ofs_t ofs);
  102. /**
  103. Callback to trim a file. E.g. for a FILE * context this can be implemented as
  104. \code
  105. static int opcFileTrim(void *iocontext, opc_ofs_t new_size) {
  106. #ifdef WIN32
  107. return _chsize(fileno((FILE*)iocontext), new_size);
  108. #else
  109. return ftruncate(fileno((FILE*)iocontext), new_size);
  110. #endif
  111. }
  112. \endcode
  113. */
  114. typedef int opcFileTrimCallback(void *iocontext, opc_ofs_t new_size);
  115. /**
  116. Callback to flush a file. E.g. for a FILE * context this can be implemented as
  117. \code
  118. static int opcFileFlush(void *iocontext) {
  119. return fflush((FILE*)iocontext);
  120. }
  121. \endcode
  122. */
  123. typedef int opcFileFlushCallback(void *iocontext);
  124. /**
  125. Represents a state of a file, i.e. file position (buf_pos) and error status (err).
  126. */
  127. typedef struct OPC_FILERAWSTATE_STRUCT {
  128. opc_error_t err;
  129. opc_ofs_t buf_pos; // current pos in file
  130. } opcFileRawState;
  131. /**
  132. File IO context.
  133. */
  134. typedef struct OPC_IO_STRUCT {
  135. opcFileReadCallback *_ioread;
  136. opcFileWriteCallback *_iowrite;
  137. opcFileCloseCallback *_ioclose;
  138. opcFileSeekCallback *_ioseek;
  139. opcFileTrimCallback *_iotrim;
  140. opcFileFlushCallback *_ioflush;
  141. void *iocontext;
  142. int flags;
  143. opcFileRawState state;
  144. opc_ofs_t file_size;
  145. } opcIO_t;
  146. /**
  147. Initialize an IO context.
  148. */
  149. opc_error_t opcFileInitIO(opcIO_t *io,
  150. opcFileReadCallback *ioread,
  151. opcFileWriteCallback *iowrite,
  152. opcFileCloseCallback *ioclose,
  153. opcFileSeekCallback *ioseek,
  154. opcFileTrimCallback *iotrim,
  155. opcFileFlushCallback *ioflush,
  156. void *iocontext,
  157. pofs_t file_size,
  158. int flags);
  159. /**
  160. Initialize an IO context for a file.
  161. */
  162. opc_error_t opcFileInitIOFile(opcIO_t *io, const xmlChar *filename, int flags);
  163. /**
  164. Initialize an IO for memory.
  165. \warning Currently supports READ-ONLY file access.
  166. */
  167. opc_error_t opcFileInitIOMemory(opcIO_t *io, const opc_uint8_t *data, opc_uint32_t data_len, int flags);
  168. /**
  169. Cleanup an IO context, i.e. release all system resources.
  170. */
  171. opc_error_t opcFileCleanupIO(opcIO_t *io);
  172. #ifdef __cplusplus
  173. } /* extern "C" */
  174. #endif
  175. #endif /* OPC_FILE_H */