blob: e66c2cc32475d6a691fba171032e8f4553dcde2a [file] [log] [blame]
Benny Prijonoe7224612005-11-13 19:40:44 +00001/* $Id$ */
2/*
Benny Prijonoe0312a72005-11-18 00:16:43 +00003 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
Benny Prijonoe7224612005-11-13 19:40:44 +00004 *
Benny Prijonoe0312a72005-11-18 00:16:43 +00005 * 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 Prijonoe7224612005-11-13 19:40:44 +00009 *
Benny Prijonoe0312a72005-11-18 00:16:43 +000010 * This program is distributed in the hope that it will be useful,
Benny Prijonoe7224612005-11-13 19:40:44 +000011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Benny Prijonoe0312a72005-11-18 00:16:43 +000012 * 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 Prijonoe7224612005-11-13 19:40:44 +000018 */
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
28PJ_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 */
58enum pj_file_access
59{
60 PJ_O_RDONLY = 0x1101, /**< Open file for reading. */
61 PJ_O_WRONLY = 0x1102, /**< Open file for writing. */
Benny Prijonoa7f64a32005-11-07 15:47:28 +000062 PJ_O_RDWR = 0x1103, /**< Open file for reading and writing.
Benny Prijonoe7224612005-11-13 19:40:44 +000063 File will be truncated. */
Benny Prijonoa7f64a32005-11-07 15:47:28 +000064 PJ_O_APPEND = 0x1108, /**< Append to existing file. */
Benny Prijonoe7224612005-11-13 19:40:44 +000065};
66
67/**
68 * The seek directive when setting the file position with #pj_file_setpos.
69 */
70enum 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 Prijonoa7f64a32005-11-07 15:47:28 +000088 * truncated unless PJ_O_APPEND is specified.
Benny Prijonoe7224612005-11-13 19:40:44 +000089 * @param fd The returned descriptor.
90 *
91 * @return PJ_SUCCESS or the appropriate error code on error.
92 */
93PJ_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 */
105PJ_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 */
118PJ_DECL(pj_status_t) pj_file_write(pj_oshandle_t fd,
119 const void *data,
120 pj_ssize_t *size);
121
122/**
Benny Prijonoa7f64a32005-11-07 15:47:28 +0000123 * Read data from the specified file. When end-of-file condition is set,
Benny Prijonoe7224612005-11-13 19:40:44 +0000124 * 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 Prijonoa7f64a32005-11-07 15:47:28 +0000130 * of data actually read from the file. It will contain
Benny Prijonoe7224612005-11-13 19:40:44 +0000131 * zero when EOF occurs.
132 *
Benny Prijonoa7f64a32005-11-07 15:47:28 +0000133 * @return PJ_SUCCESS or the appropriate error code on error.
134 * When EOF occurs, the return is PJ_SUCCESS but size
Benny Prijonoe7224612005-11-13 19:40:44 +0000135 * will report zero.
136 */
137PJ_DECL(pj_status_t) pj_file_read(pj_oshandle_t fd,
138 void *data,
139 pj_ssize_t *size);
Benny Prijonoa7f64a32005-11-07 15:47:28 +0000140
Benny Prijonoe7224612005-11-13 19:40:44 +0000141/**
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 */
150PJ_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 */
163PJ_DECL(pj_status_t) pj_file_getpos(pj_oshandle_t fd,
164 pj_off_t *pos);
165
166/** @} */
167
168
169PJ_END_DECL
170
171#endif /* __PJ_FILE_IO_H__ */
172