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