diff options
author | Benny Prijono <bennylp@teluu.com> | 2005-11-13 19:40:44 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2005-11-13 19:40:44 +0000 |
commit | a08b589d09d5197f9a76d549a189e4686bd2ca8c (patch) | |
tree | 549904e7680dfab96b3ce579b1843c5d58107100 /pjlib/src/pj/file_io_ansi.c | |
parent | 8df70c6d5fef443506618bf31b686d53fef3f259 (diff) |
Applying license to pjproject
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@49 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib/src/pj/file_io_ansi.c')
-rw-r--r-- | pjlib/src/pj/file_io_ansi.c | 285 |
1 files changed, 153 insertions, 132 deletions
diff --git a/pjlib/src/pj/file_io_ansi.c b/pjlib/src/pj/file_io_ansi.c index 0946eddc..3acb91ee 100644 --- a/pjlib/src/pj/file_io_ansi.c +++ b/pjlib/src/pj/file_io_ansi.c @@ -1,140 +1,161 @@ -/* $Id$ */ -#include <pj/file_io.h> -#include <pj/assert.h> -#include <pj/errno.h> -#include <stdio.h> -#include <errno.h> - -PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool, - const char *pathname, - unsigned flags, - pj_oshandle_t *fd) -{ - char mode[8]; - char *p = mode; - - PJ_ASSERT_RETURN(pathname && fd, PJ_EINVAL); - PJ_UNUSED_ARG(pool); - - if ((flags & PJ_O_APPEND) == PJ_O_APPEND) { - if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) { - *p++ = 'a'; - if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) - *p++ = '+'; - } else { - /* This is invalid. - * Can not specify PJ_O_RDONLY with PJ_O_APPEND! - */ - } - } else { - if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) { - *p++ = 'r'; - if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) - *p++ = '+'; - } else { - *p++ = 'w'; - } - } - - if (p==mode) - return PJ_EINVAL; - - *p++ = 'b'; - *p++ = '\0'; - - *fd = fopen(pathname, mode); - if (*fd == NULL) - return PJ_RETURN_OS_ERROR(errno); - - return PJ_SUCCESS; -} - -PJ_DEF(pj_status_t) pj_file_close(pj_oshandle_t fd) -{ - PJ_ASSERT_RETURN(fd, PJ_EINVAL); - if (fclose((FILE*)fd) != 0) - return PJ_RETURN_OS_ERROR(errno); - - return PJ_SUCCESS; -} - -PJ_DEF(pj_status_t) pj_file_write( pj_oshandle_t fd, - const void *data, - pj_ssize_t *size) -{ - size_t written; - - clearerr((FILE*)fd); - written = fwrite(data, 1, *size, (FILE*)fd); +/* $Id$ */
+/*
+ * PJLIB - PJ Foundation Library
+ * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
+ *
+ * Author:
+ * Benny Prijono <bennylp@bulukucing.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <pj/file_io.h>
+#include <pj/assert.h>
+#include <pj/errno.h>
+#include <stdio.h>
+#include <errno.h>
+
+PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool,
+ const char *pathname,
+ unsigned flags,
+ pj_oshandle_t *fd)
+{
+ char mode[8];
+ char *p = mode;
+
+ PJ_ASSERT_RETURN(pathname && fd, PJ_EINVAL);
+ PJ_UNUSED_ARG(pool);
+
+ if ((flags & PJ_O_APPEND) == PJ_O_APPEND) {
+ if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) {
+ *p++ = 'a';
+ if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY)
+ *p++ = '+';
+ } else {
+ /* This is invalid.
+ * Can not specify PJ_O_RDONLY with PJ_O_APPEND!
+ */
+ }
+ } else {
+ if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) {
+ *p++ = 'r';
+ if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY)
+ *p++ = '+';
+ } else {
+ *p++ = 'w';
+ }
+ }
+
+ if (p==mode)
+ return PJ_EINVAL;
+
+ *p++ = 'b';
+ *p++ = '\0';
+
+ *fd = fopen(pathname, mode);
+ if (*fd == NULL)
+ return PJ_RETURN_OS_ERROR(errno);
+
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pj_file_close(pj_oshandle_t fd)
+{
+ PJ_ASSERT_RETURN(fd, PJ_EINVAL);
+ if (fclose((FILE*)fd) != 0)
+ return PJ_RETURN_OS_ERROR(errno);
+
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pj_file_write( pj_oshandle_t fd,
+ const void *data,
+ pj_ssize_t *size)
+{
+ size_t written;
+
+ clearerr((FILE*)fd);
+ written = fwrite(data, 1, *size, (FILE*)fd);
if (ferror((FILE*)fd)) {
- *size = -1; - return PJ_RETURN_OS_ERROR(errno); - } - - *size = written; - return PJ_SUCCESS; -} - -PJ_DEF(pj_status_t) pj_file_read( pj_oshandle_t fd, - void *data, - pj_ssize_t *size) -{ - size_t bytes; - - clearerr((FILE*)fd); - bytes = fread(data, 1, *size, (FILE*)fd); + *size = -1;
+ return PJ_RETURN_OS_ERROR(errno);
+ }
+
+ *size = written;
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pj_file_read( pj_oshandle_t fd,
+ void *data,
+ pj_ssize_t *size)
+{
+ size_t bytes;
+
+ clearerr((FILE*)fd);
+ bytes = fread(data, 1, *size, (FILE*)fd);
if (ferror((FILE*)fd)) {
- *size = -1; - return PJ_RETURN_OS_ERROR(errno); - } - - *size = bytes; - return PJ_SUCCESS; -} + *size = -1;
+ return PJ_RETURN_OS_ERROR(errno);
+ }
+
+ *size = bytes;
+ return PJ_SUCCESS;
+}
PJ_DEF(pj_bool_t) pj_file_eof(pj_oshandle_t fd, enum pj_file_access access)
{
PJ_UNUSED_ARG(access);
return feof((FILE*)fd) ? PJ_TRUE : 0;
}
- -PJ_DEF(pj_status_t) pj_file_setpos( pj_oshandle_t fd, - pj_off_t offset, - enum pj_file_seek_type whence) -{ - int mode; - - switch (whence) { - case PJ_SEEK_SET: - mode = SEEK_SET; break; - case PJ_SEEK_CUR: - mode = SEEK_CUR; break; - case PJ_SEEK_END: - mode = SEEK_END; break; - default: - pj_assert(!"Invalid whence in file_setpos"); - return PJ_EINVAL; - } - - if (fseek((FILE*)fd, (long)offset, mode) != 0) - return PJ_RETURN_OS_ERROR(errno); - - return PJ_SUCCESS; -} - -PJ_DEF(pj_status_t) pj_file_getpos( pj_oshandle_t fd, - pj_off_t *pos) -{ - long offset; - - offset = ftell((FILE*)fd); - if (offset == -1) { - *pos = -1; - return PJ_RETURN_OS_ERROR(errno); - } - - *pos = offset; - return PJ_SUCCESS; -} - - +
+PJ_DEF(pj_status_t) pj_file_setpos( pj_oshandle_t fd,
+ pj_off_t offset,
+ enum pj_file_seek_type whence)
+{
+ int mode;
+
+ switch (whence) {
+ case PJ_SEEK_SET:
+ mode = SEEK_SET; break;
+ case PJ_SEEK_CUR:
+ mode = SEEK_CUR; break;
+ case PJ_SEEK_END:
+ mode = SEEK_END; break;
+ default:
+ pj_assert(!"Invalid whence in file_setpos");
+ return PJ_EINVAL;
+ }
+
+ if (fseek((FILE*)fd, (long)offset, mode) != 0)
+ return PJ_RETURN_OS_ERROR(errno);
+
+ return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pj_file_getpos( pj_oshandle_t fd,
+ pj_off_t *pos)
+{
+ long offset;
+
+ offset = ftell((FILE*)fd);
+ if (offset == -1) {
+ *pos = -1;
+ return PJ_RETURN_OS_ERROR(errno);
+ }
+
+ *pos = offset;
+ return PJ_SUCCESS;
+}
+
+
|