Benny Prijono | a7f64a3 | 2005-11-07 15:47:28 +0000 | [diff] [blame^] | 1 | /* $Id$ */ |
| 2 | #ifndef __PJ_FILE_IO_H__ |
| 3 | #define __PJ_FILE_IO_H__ |
| 4 | |
| 5 | /** |
| 6 | * @file file_io.h |
| 7 | * @brief Simple file I/O abstraction. |
| 8 | */ |
| 9 | #include <pj/types.h> |
| 10 | |
| 11 | PJ_BEGIN_DECL |
| 12 | |
| 13 | /** |
| 14 | * @defgroup PJ_FILE_IO File I/O |
| 15 | * @ingroup PJ_IO |
| 16 | * @{ |
| 17 | * |
| 18 | * This file contains functionalities to perform file I/O. The file |
| 19 | * I/O can be implemented with various back-end, either using native |
| 20 | * file API or ANSI stream. |
| 21 | * |
| 22 | * @section pj_file_size_limit_sec Size Limits |
| 23 | * |
| 24 | * There may be limitation on the size that can be handled by the |
| 25 | * #pj_file_setpos() or #pj_file_getpos() functions. The API itself |
| 26 | * uses 64-bit integer for the file offset/position (where available); |
| 27 | * however some backends (such as ANSI) may only support signed 32-bit |
| 28 | * offset resolution. |
| 29 | * |
| 30 | * Reading and writing operation uses signed 32-bit integer to indicate |
| 31 | * the size. |
| 32 | * |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * These enumerations are used when opening file. Values PJ_O_RDONLY, |
| 38 | * PJ_O_WRONLY, and PJ_O_RDWR are mutually exclusive. Value PJ_O_APPEND |
| 39 | * can only be used when the file is opened for writing. |
| 40 | */ |
| 41 | enum pj_file_access |
| 42 | { |
| 43 | PJ_O_RDONLY = 0x1101, /**< Open file for reading. */ |
| 44 | PJ_O_WRONLY = 0x1102, /**< Open file for writing. */ |
| 45 | PJ_O_RDWR = 0x1103, /**< Open file for reading and writing.
|
| 46 | File will be truncated. */ |
| 47 | PJ_O_APPEND = 0x1108, /**< Append to existing file. */
|
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * The seek directive when setting the file position with #pj_file_setpos. |
| 52 | */ |
| 53 | enum pj_file_seek_type |
| 54 | { |
| 55 | PJ_SEEK_SET = 0x1201, /**< Offset from beginning of the file. */ |
| 56 | PJ_SEEK_CUR = 0x1202, /**< Offset from current position. */ |
| 57 | PJ_SEEK_END = 0x1203, /**< Size of the file plus offset. */ |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Open the file as specified in \c pathname with the specified |
| 62 | * mode, and return the handle in \c fd. All files will be opened |
| 63 | * as binary. |
| 64 | * |
| 65 | * @param pool Pool to allocate memory for the new file descriptor. |
| 66 | * @param pathname The file name to open. |
| 67 | * @param flags Open flags, which is bitmask combination of |
| 68 | * #pj_file_access enum. The flag must be either |
| 69 | * PJ_O_RDONLY, PJ_O_WRONLY, or PJ_O_RDWR. When file |
| 70 | * writing is specified, existing file will be |
| 71 | * truncated unless PJ_O_APPEND is specified.
|
| 72 | * @param fd The returned descriptor. |
| 73 | * |
| 74 | * @return PJ_SUCCESS or the appropriate error code on error. |
| 75 | */ |
| 76 | PJ_DECL(pj_status_t) pj_file_open(pj_pool_t *pool, |
| 77 | const char *pathname, |
| 78 | unsigned flags, |
| 79 | pj_oshandle_t *fd); |
| 80 | |
| 81 | /** |
| 82 | * Close an opened file descriptor. |
| 83 | * |
| 84 | * @param fd The file descriptor. |
| 85 | * |
| 86 | * @return PJ_SUCCESS or the appropriate error code on error. |
| 87 | */ |
| 88 | PJ_DECL(pj_status_t) pj_file_close(pj_oshandle_t fd); |
| 89 | |
| 90 | /** |
| 91 | * Write data with the specified size to an opened file. |
| 92 | * |
| 93 | * @param fd The file descriptor. |
| 94 | * @param data Data to be written to the file. |
| 95 | * @param size On input, specifies the size of data to be written. |
| 96 | * On return, it contains the number of data actually |
| 97 | * written to the file. |
| 98 | * |
| 99 | * @return PJ_SUCCESS or the appropriate error code on error. |
| 100 | */ |
| 101 | PJ_DECL(pj_status_t) pj_file_write(pj_oshandle_t fd, |
| 102 | const void *data, |
| 103 | pj_ssize_t *size); |
| 104 | |
| 105 | /** |
| 106 | * Read data from the specified file. When end-of-file condition is set,
|
| 107 | * this function will return PJ_SUCCESS but the size will contain zero. |
| 108 | * |
| 109 | * @param fd The file descriptor. |
| 110 | * @param data Pointer to buffer to receive the data. |
| 111 | * @param size On input, specifies the maximum number of data to |
| 112 | * read from the file. On output, it contains the size |
| 113 | * of data actually read from the file. It will contain
|
| 114 | * zero when EOF occurs. |
| 115 | * |
| 116 | * @return PJ_SUCCESS or the appropriate error code on error.
|
| 117 | * When EOF occurs, the return is PJ_SUCCESS but size
|
| 118 | * will report zero. |
| 119 | */ |
| 120 | PJ_DECL(pj_status_t) pj_file_read(pj_oshandle_t fd, |
| 121 | void *data, |
| 122 | pj_ssize_t *size); |
| 123 |
|
| 124 | /** |
| 125 | * Set file position to new offset according to directive \c whence. |
| 126 | * |
| 127 | * @param fd The file descriptor. |
| 128 | * @param offset The new file position to set. |
| 129 | * @param whence The directive. |
| 130 | * |
| 131 | * @return PJ_SUCCESS or the appropriate error code on error. |
| 132 | */ |
| 133 | PJ_DECL(pj_status_t) pj_file_setpos(pj_oshandle_t fd, |
| 134 | pj_off_t offset, |
| 135 | enum pj_file_seek_type whence); |
| 136 | |
| 137 | /** |
| 138 | * Get current file position. |
| 139 | * |
| 140 | * @param fd The file descriptor. |
| 141 | * @param pos On return contains the file position as measured |
| 142 | * from the beginning of the file. |
| 143 | * |
| 144 | * @return PJ_SUCCESS or the appropriate error code on error. |
| 145 | */ |
| 146 | PJ_DECL(pj_status_t) pj_file_getpos(pj_oshandle_t fd, |
| 147 | pj_off_t *pos); |
| 148 | |
| 149 | /** @} */ |
| 150 | |
| 151 | |
| 152 | PJ_END_DECL |
| 153 | |
| 154 | #endif /* __PJ_FILE_IO_H__ */ |
| 155 | |