libopenraw
io.c
00001 /*
00002  * libopenraw - io.c
00003  *
00004  * Copyright (C) 2005-2007 Hubert Figuiere
00005  *
00006  * This library is free software: you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation, either version 3 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library.  If not, see
00018  * <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #include <stdlib.h>
00022 #include <errno.h>
00023 
00024 #include "libopenraw/io.h"
00025 #include "io_private.h"
00026 #include "posix_io.h"
00027 #include "or_debug.h"
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00034 #define CHECK_PTR(p,r) \
00035     if(p == NULL) { return r; }
00036 
00042 struct io_methods* get_default_io_methods(void)
00043 {
00044     return &posix_io_methods;
00045 }
00046 
00052 IOFileRef raw_open(struct io_methods * methods, const char *path, int mode)
00053 {
00054     CHECK_PTR(methods, NULL);
00055     return methods->open(path, mode);
00056 }
00057 
00067 int raw_close(IOFileRef f)
00068 {
00069     int retval;
00070     CHECK_PTR(f,-1);
00071     retval = f->methods->close(f);
00072     free(f);
00073     return retval;
00074 }
00075 
00076 
00084 int raw_seek(IOFileRef f, off_t offset, int whence)
00085 {
00086     CHECK_PTR(f,-1);
00087     return f->methods->seek(f, offset, whence);
00088 }
00089 
00090 
00098 int raw_read(IOFileRef f, void *buf, size_t count)
00099 {
00100     CHECK_PTR(f,-1);
00101     return f->methods->read(f, buf, count);
00102 }
00103 
00104 off_t raw_filesize(IOFileRef f)
00105 {
00106     CHECK_PTR(f,0);
00107     return f->methods->filesize(f);
00108 }
00109 
00110 void *raw_mmap(IOFileRef f, size_t l, off_t offset)
00111 {
00112     CHECK_PTR(f,NULL);
00113     return f->methods->mmap(f, l, offset);
00114 }
00115 
00116 
00117 int raw_munmap(IOFileRef f, void *addr, size_t l)
00118 {
00119     CHECK_PTR(f,-1);
00120     return f->methods->munmap(f, addr, l);
00121 }
00122 
00123 
00129 int raw_get_error(IOFileRef f)
00130 {
00131     CHECK_PTR(f,EFAULT);
00132     return f->error;
00133 }
00134 
00135 
00144 char *raw_get_path(IOFileRef f)
00145 {
00146     CHECK_PTR(f,NULL);
00147     return f->path;
00148 }
00149 
00150 
00151 #ifdef __cplusplus
00152 }
00153 #endif
00154