Debian Codegraph @

Xpjproject/pjlib/include/pj/file_io.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 /**
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. */
62 PJ_O_RDWR = 0x1103, /**< Open file for reading and writing.
63 File will be truncated. */
64 PJ_O_APPEND = 0x1108, /**< Append to existing file. */
65 PJ_O_CLOEXEC = 0x1104, /**< Enable unix close-on-exec flag. */
66
67 PJ_O_ASYNC = 0x200, /**< Open file for asynchronous
68 (OVERLAPPED) IO.
69 Currently supports on Windows
70 platform only. Otherwise is ignored. */
71 PJ_O_SEQUENTIAL = 0x400, /**< Access is intended to be sequential
72 from beginning to end. This flag
73 should not be used together with
74 PJ_O_RANDOM.
75 Currently supports on Windows
76 platform only. Otherwise is ignored. */
77 PJ_O_RANDOM = 0x800 /**< Access is intended to be random.
78 This flag should not be used together
79 with PJ_O_SEQUENTIAL.
80 Currently supports on Windows
81 platform only.Otherwise is ignored. */
82 };
83
84 /**
85 * The seek directive when setting the file position with #pj_file_setpos.
86 */
87 enum pj_file_seek_type
88 {
89 PJ_SEEK_SET = 0x1201, /**< Offset from beginning of the file. */
90 PJ_SEEK_CUR = 0x1202, /**< Offset from current position. */
91 PJ_SEEK_END = 0x1203 /**< Size of the file plus offset. */
92 };
93
94 /**
95 * Open the file as specified in \c pathname with the specified
96 * mode, and return the handle in \c fd. All files will be opened
97 * as binary.
98 *
99 * @param pool Pool to allocate memory for the new file descriptor.
100 * @param pathname The file name to open.
101 * @param flags Open flags, which is bitmask combination of
102 * #pj_file_access enum. The flag must be either
103 * PJ_O_RDONLY, PJ_O_WRONLY, or PJ_O_RDWR. When file
104 * writing is specified, existing file will be
105 * truncated unless PJ_O_APPEND is specified.
106 * @param fd The returned descriptor.
107 *
108 * @return PJ_SUCCESS or the appropriate error code on error.
109 */
110 PJ_DECL(pj_status_t) pj_file_open(pj_pool_t *pool,
111 const char *pathname,
112 unsigned flags,
113 pj_oshandle_t *fd);
114
115 /**
116 * Close an opened file descriptor.
117 *
118 * @param fd The file descriptor.
119 *
120 * @return PJ_SUCCESS or the appropriate error code on error.
121 */
122 PJ_DECL(pj_status_t) pj_file_close(pj_oshandle_t fd);
123
124 /**
125 * Write data with the specified size to an opened file.
126 *
127 * @param fd The file descriptor.
128 * @param data Data to be written to the file.
129 * @param size On input, specifies the size of data to be written.
130 * On return, it contains the number of data actually
131 * written to the file.
132 *
133 * @return PJ_SUCCESS or the appropriate error code on error.
134 */
135 PJ_DECL(pj_status_t) pj_file_write(pj_oshandle_t fd,
136 const void *data,
137 pj_ssize_t *size);
138
139 /**
140 * Read data from the specified file. When end-of-file condition is set,
141 * this function will return PJ_SUCCESS but the size will contain zero.
142 *
143 * @param fd The file descriptor.
144 * @param data Pointer to buffer to receive the data.
145 * @param size On input, specifies the maximum number of data to
146 * read from the file. On output, it contains the size
147 * of data actually read from the file. It will contain
148 * zero when EOF occurs.
149 *
150 * @return PJ_SUCCESS or the appropriate error code on error.
151 * When EOF occurs, the return is PJ_SUCCESS but size
152 * will report zero.
153 */
154 PJ_DECL(pj_status_t) pj_file_read(pj_oshandle_t fd,
155 void *data,
156 pj_ssize_t *size);
157
158 /**
159 * Set file position to new offset according to directive \c whence.
160 *
161 * @param fd The file descriptor.
162 * @param offset The new file position to set.
163 * @param whence The directive.
164 *
165 * @return PJ_SUCCESS or the appropriate error code on error.
166 */
167 PJ_DECL(pj_status_t) pj_file_setpos(pj_oshandle_t fd,
168 pj_off_t offset,
169 enum pj_file_seek_type whence);
170
171 /**
172 * Get current file position.
173 *
174 * @param fd The file descriptor.
175 * @param pos On return contains the file position as measured
176 * from the beginning of the file.
177 *
178 * @return PJ_SUCCESS or the appropriate error code on error.
179 */
180 PJ_DECL(pj_status_t) pj_file_getpos(pj_oshandle_t fd,
181 pj_off_t *pos);
182
183 /**
184 * Flush file buffers.
185 *
186 * @param fd The file descriptor.
187 *
188 * @return PJ_SUCCESS or the appropriate error code on error.
189 */
190 PJ_DECL(pj_status_t) pj_file_flush(pj_oshandle_t fd);
191
192
193 /** @} */
194
195
196 PJ_END_DECL
197
198 #endif /* __PJ_FILE_IO_H__ */
199