file_io.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_FILE_IO_H__
  20. #define __PJ_FILE_IO_H__
  21. /**
  22. * @file file_io.h
  23. * @brief Simple file I/O abstraction.
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_FILE_IO File I/O
  29. * @ingroup PJ_IO
  30. * @{
  31. *
  32. * This file contains functionalities to perform file I/O. The file
  33. * I/O can be implemented with various back-end, either using native
  34. * file API or ANSI stream.
  35. *
  36. * @section pj_file_size_limit_sec Size Limits
  37. *
  38. * There may be limitation on the size that can be handled by the
  39. * #pj_file_setpos() or #pj_file_getpos() functions. The API itself
  40. * uses 64-bit integer for the file offset/position (where available);
  41. * however some backends (such as ANSI) may only support signed 32-bit
  42. * offset resolution.
  43. *
  44. * Reading and writing operation uses signed 32-bit integer to indicate
  45. * the size.
  46. *
  47. *
  48. */
  49. /**
  50. * These enumerations are used when opening file. Values PJ_O_RDONLY,
  51. * PJ_O_WRONLY, and PJ_O_RDWR are mutually exclusive. Value PJ_O_APPEND
  52. * can only be used when the file is opened for writing.
  53. */
  54. enum pj_file_access
  55. {
  56. PJ_O_RDONLY = 0x1101, /**< Open file for reading. */
  57. PJ_O_WRONLY = 0x1102, /**< Open file for writing. */
  58. PJ_O_RDWR = 0x1103, /**< Open file for reading and writing.
  59. File will be truncated. */
  60. PJ_O_APPEND = 0x1108, /**< Append to existing file. */
  61. PJ_O_CLOEXEC = 0x1104, /**< Enable unix close-on-exec flag. */
  62. };
  63. /**
  64. * The seek directive when setting the file position with #pj_file_setpos.
  65. */
  66. enum pj_file_seek_type
  67. {
  68. PJ_SEEK_SET = 0x1201, /**< Offset from beginning of the file. */
  69. PJ_SEEK_CUR = 0x1202, /**< Offset from current position. */
  70. PJ_SEEK_END = 0x1203 /**< Size of the file plus offset. */
  71. };
  72. /**
  73. * Open the file as specified in \c pathname with the specified
  74. * mode, and return the handle in \c fd. All files will be opened
  75. * as binary.
  76. *
  77. * @param pool Pool to allocate memory for the new file descriptor.
  78. * @param pathname The file name to open.
  79. * @param flags Open flags, which is bitmask combination of
  80. * #pj_file_access enum. The flag must be either
  81. * PJ_O_RDONLY, PJ_O_WRONLY, or PJ_O_RDWR. When file
  82. * writing is specified, existing file will be
  83. * truncated unless PJ_O_APPEND is specified.
  84. * @param fd The returned descriptor.
  85. *
  86. * @return PJ_SUCCESS or the appropriate error code on error.
  87. */
  88. PJ_DECL(pj_status_t) pj_file_open(pj_pool_t *pool,
  89. const char *pathname,
  90. unsigned flags,
  91. pj_oshandle_t *fd);
  92. /**
  93. * Close an opened file descriptor.
  94. *
  95. * @param fd The file descriptor.
  96. *
  97. * @return PJ_SUCCESS or the appropriate error code on error.
  98. */
  99. PJ_DECL(pj_status_t) pj_file_close(pj_oshandle_t fd);
  100. /**
  101. * Write data with the specified size to an opened file.
  102. *
  103. * @param fd The file descriptor.
  104. * @param data Data to be written to the file.
  105. * @param size On input, specifies the size of data to be written.
  106. * On return, it contains the number of data actually
  107. * written to the file.
  108. *
  109. * @return PJ_SUCCESS or the appropriate error code on error.
  110. */
  111. PJ_DECL(pj_status_t) pj_file_write(pj_oshandle_t fd,
  112. const void *data,
  113. pj_ssize_t *size);
  114. /**
  115. * Read data from the specified file. When end-of-file condition is set,
  116. * this function will return PJ_SUCCESS but the size will contain zero.
  117. *
  118. * @param fd The file descriptor.
  119. * @param data Pointer to buffer to receive the data.
  120. * @param size On input, specifies the maximum number of data to
  121. * read from the file. On output, it contains the size
  122. * of data actually read from the file. It will contain
  123. * zero when EOF occurs.
  124. *
  125. * @return PJ_SUCCESS or the appropriate error code on error.
  126. * When EOF occurs, the return is PJ_SUCCESS but size
  127. * will report zero.
  128. */
  129. PJ_DECL(pj_status_t) pj_file_read(pj_oshandle_t fd,
  130. void *data,
  131. pj_ssize_t *size);
  132. /**
  133. * Set file position to new offset according to directive \c whence.
  134. *
  135. * @param fd The file descriptor.
  136. * @param offset The new file position to set.
  137. * @param whence The directive.
  138. *
  139. * @return PJ_SUCCESS or the appropriate error code on error.
  140. */
  141. PJ_DECL(pj_status_t) pj_file_setpos(pj_oshandle_t fd,
  142. pj_off_t offset,
  143. enum pj_file_seek_type whence);
  144. /**
  145. * Get current file position.
  146. *
  147. * @param fd The file descriptor.
  148. * @param pos On return contains the file position as measured
  149. * from the beginning of the file.
  150. *
  151. * @return PJ_SUCCESS or the appropriate error code on error.
  152. */
  153. PJ_DECL(pj_status_t) pj_file_getpos(pj_oshandle_t fd,
  154. pj_off_t *pos);
  155. /**
  156. * Flush file buffers.
  157. *
  158. * @param fd The file descriptor.
  159. *
  160. * @return PJ_SUCCESS or the appropriate error code on error.
  161. */
  162. PJ_DECL(pj_status_t) pj_file_flush(pj_oshandle_t fd);
  163. /** @} */
  164. PJ_END_DECL
  165. #endif /* __PJ_FILE_IO_H__ */