1
/* $Id: file_io_ansi.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pj/file_io.h>
21
#include <pj/assert.h>
26
PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool,
34
PJ_ASSERT_RETURN(pathname && fd, PJ_EINVAL);
37
if ((flags & PJ_O_APPEND) == PJ_O_APPEND) {
38
if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) {
40
if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY)
44
* Can not specify PJ_O_RDONLY with PJ_O_APPEND!
48
if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) {
50
if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY)
63
*fd = fopen(pathname, mode);
65
return PJ_RETURN_OS_ERROR(errno);
70
PJ_DEF(pj_status_t) pj_file_close(pj_oshandle_t fd)
72
PJ_ASSERT_RETURN(fd, PJ_EINVAL);
73
if (fclose((FILE*)fd) != 0)
74
return PJ_RETURN_OS_ERROR(errno);
79
PJ_DEF(pj_status_t) pj_file_write( pj_oshandle_t fd,
86
written = fwrite(data, 1, *size, (FILE*)fd);
87
if (ferror((FILE*)fd)) {
89
return PJ_RETURN_OS_ERROR(errno);
96
PJ_DEF(pj_status_t) pj_file_read( pj_oshandle_t fd,
103
bytes = fread(data, 1, *size, (FILE*)fd);
104
if (ferror((FILE*)fd)) {
106
return PJ_RETURN_OS_ERROR(errno);
114
PJ_DEF(pj_bool_t) pj_file_eof(pj_oshandle_t fd, enum pj_file_access access)
116
PJ_UNUSED_ARG(access);
117
return feof((FILE*)fd) ? PJ_TRUE : 0;
121
PJ_DEF(pj_status_t) pj_file_setpos( pj_oshandle_t fd,
123
enum pj_file_seek_type whence)
129
mode = SEEK_SET; break;
131
mode = SEEK_CUR; break;
133
mode = SEEK_END; break;
135
pj_assert(!"Invalid whence in file_setpos");
139
if (fseek((FILE*)fd, (long)offset, mode) != 0)
140
return PJ_RETURN_OS_ERROR(errno);
145
PJ_DEF(pj_status_t) pj_file_getpos( pj_oshandle_t fd,
150
offset = ftell((FILE*)fd);
153
return PJ_RETURN_OS_ERROR(errno);
160
PJ_DEF(pj_status_t) pj_file_flush(pj_oshandle_t fd)
164
rc = fflush((FILE*)fd);
166
return PJ_RETURN_OS_ERROR(errno);