File: io.c

package info (click to toggle)
libopenraw 0.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,964 kB
  • ctags: 1,936
  • sloc: sh: 10,199; cpp: 9,279; ansic: 1,466; makefile: 544; xml: 465; perl: 42
file content (154 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * libopenraw - io.c
 *
 * Copyright (C) 2005-2007 Hubert Figuiere
 *
 * 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 3 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, see
 * <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <errno.h>

#include "libopenraw/io.h"
#include "io_private.h"
#include "posix_io.h"
#include "or_debug.h"

#ifdef __cplusplus
extern "C" {
#endif

/** check pointer validity */
#define CHECK_PTR(p,r) \
	if(p == NULL) { return r; }

/** get the default io methods instance 

  @fixme currently hardcoded to POSIX
  @return the default io_methods instance, currentlty posix_io_methods
*/
struct io_methods* get_default_io_methods(void)
{
	return &posix_io_methods;
}

/** open a file
  @param methods the io_methods instance to use
  @param path the file path
  @param mode the POSIX file mode
 */
IOFileRef raw_open(struct io_methods * methods, const char *path, int mode)
{
	CHECK_PTR(methods, NULL);
	return methods->open(path, mode);
}

/** close the file

  @param f the file to close

  The implement should free the private data is at will free the file ref. 
  f will be invalid on return

  @return -1 if error.
 */
int raw_close(IOFileRef f)
{
	int retval;
	CHECK_PTR(f,-1);
	retval = f->methods->close(f);
	free(f);
	return retval;
}


/** seek in the file
  @param f the file to seek
  @param offset the offset to seek
  @param whence the the directive for seek. See lseek(2) man page

  @return -1 if error
 */
int raw_seek(IOFileRef f, off_t offset, int whence)
{
	CHECK_PTR(f,-1);
	return f->methods->seek(f, offset, whence);
}


/** read in the file
  @param f the file to readk
  @param buf the buffer to read in
  @param count the number of byte to read

  @return -1 if error
*/
int raw_read(IOFileRef f, void *buf, size_t count)
{
	CHECK_PTR(f,-1);
	return f->methods->read(f, buf, count);
}

off_t raw_filesize(IOFileRef f)
{
	CHECK_PTR(f,0);
	return f->methods->filesize(f);
}

void *raw_mmap(IOFileRef f, size_t l, off_t offset)
{
	CHECK_PTR(f,NULL);
	return f->methods->mmap(f, l, offset);
}


int raw_munmap(IOFileRef f, void *addr, size_t l)
{
	CHECK_PTR(f,-1);
	return f->methods->munmap(f, addr, l);
}


/** get the error for the file

  @param f the file 
  @return the errno code
*/
int raw_get_error(IOFileRef f)
{
	CHECK_PTR(f,EFAULT);
	return f->error;
}


/** get the real path of the file

  This function is needed because libtiff needs a pathname to 
  open TIFF files.

  @param f the file
  @return the pathname
*/
char *raw_get_path(IOFileRef f)
{
	CHECK_PTR(f,NULL);
	return f->path;
}


#ifdef __cplusplus
}
#endif