libopenraw
io.c
1 /*
2  * libopenraw - io.c
3  *
4  * Copyright (C) 2005-2014 Hubert Figuiere
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <stdlib.h>
22 #include <errno.h>
23 
24 #include <libopenraw/io.h>
25 #include "io_private.h"
26 #include "posix_io.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
33 #define CHECK_PTR(p,r) \
34  if(p == NULL) { return r; }
35 
41 struct io_methods* get_default_io_methods(void)
42 {
43  return &posix_io_methods;
44 }
45 
51 IOFileRef raw_open(struct io_methods * methods, const char *path, int mode)
52 {
53  CHECK_PTR(methods, NULL);
54  return methods->open(path, mode);
55 }
56 
66 int raw_close(IOFileRef f)
67 {
68  int retval;
69  CHECK_PTR(f,-1);
70  retval = f->methods->close(f);
71  free(f);
72  return retval;
73 }
74 
75 
83 int raw_seek(IOFileRef f, off_t offset, int whence)
84 {
85  CHECK_PTR(f,-1);
86  return f->methods->seek(f, offset, whence);
87 }
88 
89 
97 int raw_read(IOFileRef f, void *buf, size_t count)
98 {
99  CHECK_PTR(f,-1);
100  return f->methods->read(f, buf, count);
101 }
102 
103 off_t raw_filesize(IOFileRef f)
104 {
105  CHECK_PTR(f,0);
106  return f->methods->filesize(f);
107 }
108 
109 void *raw_mmap(IOFileRef f, size_t l, off_t offset)
110 {
111  CHECK_PTR(f,NULL);
112  return f->methods->mmap(f, l, offset);
113 }
114 
115 
116 int raw_munmap(IOFileRef f, void *addr, size_t l)
117 {
118  CHECK_PTR(f,-1);
119  return f->methods->munmap(f, addr, l);
120 }
121 
122 
128 int raw_get_error(IOFileRef f)
129 {
130  CHECK_PTR(f,EFAULT);
131  return f->error;
132 }
133 
134 
143 char *raw_get_path(IOFileRef f)
144 {
145  CHECK_PTR(f,NULL);
146  return f->path;
147 }
148 
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
int error
Definition: io_private.h:34
IOFileRef(* open)(const char *path, int mode)
Definition: io.h:42
struct io_methods * methods
Definition: io_private.h:28
int(* close)(IOFileRef f)
Definition: io.h:44
int(* read)(IOFileRef f, void *buf, size_t count)
Definition: io.h:48
int(* seek)(IOFileRef f, off_t offset, int whence)
Definition: io.h:46
Definition: io.h:38
char * path
Definition: io_private.h:32