libopenraw  0.3.7
io.c
1 /*
2  * libopenraw - io.c
3  *
4  * Copyright (C) 2005-2020 Hubert Figuière
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 
40 {
41  return &posix_io_methods;
42 }
43 
49 IOFileRef raw_open(struct io_methods * methods, const char *path, int mode)
50 {
51  CHECK_PTR(methods, NULL);
52  return methods->open(path, mode);
53 }
54 
65 {
66  int retval;
67  CHECK_PTR(f,-1);
68  retval = f->methods->close(f);
69  free(f);
70  return retval;
71 }
72 
73 
81 int raw_seek(IOFileRef f, off_t offset, int whence)
82 {
83  CHECK_PTR(f,-1);
84  return f->methods->seek(f, offset, whence);
85 }
86 
87 
95 int raw_read(IOFileRef f, void *buf, size_t count)
96 {
97  CHECK_PTR(f,-1);
98  return f->methods->read(f, buf, count);
99 }
100 
101 off_t raw_filesize(IOFileRef f)
102 {
103  CHECK_PTR(f,0);
104  return f->methods->filesize(f);
105 }
106 
107 void *raw_mmap(IOFileRef f, size_t l, off_t offset)
108 {
109  CHECK_PTR(f,NULL);
110  return f->methods->mmap(f, l, offset);
111 }
112 
113 
114 int raw_munmap(IOFileRef f, void *addr, size_t l)
115 {
116  CHECK_PTR(f,-1);
117  return f->methods->munmap(f, addr, l);
118 }
119 
120 
127 {
128  CHECK_PTR(f,EFAULT);
129  return f->error;
130 }
131 
132 
142 {
143  CHECK_PTR(f,NULL);
144  return f->path;
145 }
146 
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 
IOFileRef raw_open(struct io_methods *methods, const char *path, int mode)
Raw open function.
Definition: io.c:49
int raw_close(IOFileRef f)
Definition: io.c:64
int raw_get_error(IOFileRef f)
Definition: io.c:126
char * raw_get_path(IOFileRef f)
Definition: io.c:141
struct io_methods * get_default_io_methods(void)
Get the default IO methods.
Definition: io.c:39
int raw_seek(IOFileRef f, off_t offset, int whence)
Definition: io.c:81
int raw_read(IOFileRef f, void *buf, size_t count)
Definition: io.c:95
int error
Definition: io_private.h:34
struct io_methods * methods
Definition: io_private.h:28
char * path
Definition: io_private.h:32
IO methods for the IO subsystem.
Definition: io.h:51
off_t(* filesize)(IOFileRef f)
filesize method
Definition: io.h:64
int(* seek)(IOFileRef f, off_t offset, int whence)
seek in the file
Definition: io.h:59
IOFileRef(* open)(const char *path, int mode)
open method
Definition: io.h:55
int(* close)(IOFileRef f)
close method
Definition: io.h:57
int(* read)(IOFileRef f, void *buf, size_t count)
read method
Definition: io.h:61
int(* munmap)(IOFileRef f, void *addr, size_t l)
munmap method
Definition: io.h:68
void *(* mmap)(IOFileRef f, size_t l, off_t offset)
mmap method
Definition: io.h:66