source: roaraudio/libroar/vio.c @ 1253:b3dc719b129f

Last change on this file since 1253:b3dc719b129f was 1253:b3dc719b129f, checked in by phi, 15 years ago

done a converter to let you use an FILE* (stdio) stream as an VIO object

File size: 9.6 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_vio_init_calls (struct roar_vio_calls * calls) {
38 if ( calls == NULL )
39  return -1;
40
41 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
42
43/*
44 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
45 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
46 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
47*/
48
49 calls->read     = roar_vio_basic_read;
50 calls->write    = roar_vio_basic_write;
51 calls->lseek    = roar_vio_basic_lseek;
52 calls->nonblock = roar_vio_basic_nonblock;
53 calls->sync     = roar_vio_basic_sync;
54 calls->close    = roar_vio_basic_close;
55
56 return 0;
57}
58
59int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
60 if ( vio == NULL )
61  return -1;
62
63 vio->inst = inst;
64
65 return 0;
66}
67
68int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
69 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
70}
71
72int roar_vio_get_fh   (struct roar_vio_calls * vio) {
73 if ( vio == NULL )
74  return -1;
75
76 return ((int)(ROAR_INSTINT)vio->inst) - 1;
77}
78
79
80ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
81 if ( vio == NULL )
82  return -1;
83
84 if ( vio->read == NULL )
85  return -1;
86
87 return vio->read(vio, buf, count);
88}
89
90ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
91 if ( vio == NULL )
92  return -1;
93
94 if ( vio->write == NULL )
95  return -1;
96
97 return vio->write(vio, buf, count);
98}
99
100off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
101 if ( vio == NULL )
102  return -1;
103
104 if ( vio->lseek == NULL )
105  return -1;
106
107 return vio->lseek(vio, offset, whence);
108}
109
110int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
111 if ( vio == NULL )
112  return -1;
113
114 if ( vio->nonblock == NULL )
115  return -1;
116
117 return vio->nonblock(vio, state);
118}
119
120int     roar_vio_sync    (struct roar_vio_calls * vio) {
121 if ( vio == NULL )
122  return -1;
123
124 if ( vio->sync == NULL )
125  return -1;
126
127 return vio->sync(vio);
128}
129
130int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
131 if ( vio == NULL )
132  return -1;
133
134 if ( vio->ctl == NULL )
135  return -1;
136
137 return vio->ctl(vio, cmd, data);
138}
139
140int     roar_vio_close    (struct roar_vio_calls * vio) {
141 if ( vio == NULL )
142  return -1;
143
144 if ( vio->sync == NULL )
145  return -1;
146
147 return vio->close(vio);
148}
149
150// converters:
151int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
152 int fh;
153
154 if ( calls == NULL || filename == NULL )
155  return -1;
156
157 if ( (fh = open(filename, flags, mode)) == -1 )
158  return -1;
159
160 if ( roar_vio_open_fh(calls, fh) == -1 ) {
161  close(fh);
162  return -1;
163 }
164
165 return 0;
166}
167
168int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
169 if ( calls == NULL )
170  return -1;
171
172 if ( roar_vio_init_calls(calls) == -1 )
173  return -1;
174
175 return roar_vio_set_fh(calls, fh);
176}
177
178int     roar_vio_open_stdio    (struct roar_vio_calls * calls, FILE * dst) {
179 if ( calls == NULL || dst == NULL )
180  return -1;
181
182 memset(calls, 0, sizeof(struct roar_vio_calls));
183
184 calls->read  = roar_vio_stdio_read;
185 calls->write = roar_vio_stdio_write;
186 calls->lseek = roar_vio_stdio_lseek;
187 calls->sync  = roar_vio_stdio_sync;
188 calls->close = roar_vio_stdio_close;
189
190 calls->inst  = dst;
191
192 return 0;
193}
194
195FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
196#ifdef ROAR_HAVE_FOPENCOOKIE
197 cookie_io_functions_t foc_funcs;
198#endif
199
200 if ( calls == NULL )
201  return NULL;
202
203#if defined(ROAR_HAVE_FOPENCOOKIE)
204 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
205
206 return fopencookie((void*) calls, "rw", foc_funcs);
207#elif defined(ROAR_HAVE_FUNOPEN)
208 return funopen((void*) calls, NULL /* read */, NULL /* write */, NULL /* lseek */, NULL /* close */);
209#else
210 return NULL;
211#endif
212}
213
214// VIOs:
215
216// basic
217ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
218 return read(roar_vio_get_fh(vio), buf, count);
219}
220
221ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
222 return write(roar_vio_get_fh(vio), buf, count);
223}
224
225off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
226 return lseek(roar_vio_get_fh(vio), offset, whence);
227}
228
229int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
230 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
231  return -1;
232
233 if ( state == ROAR_SOCKET_NONBLOCK )
234  return 0;
235
236 roar_vio_sync(vio);
237
238 return 0;
239}
240
241int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
242 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
243}
244
245int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
246 return close(roar_vio_get_fh(vio));
247}
248
249// null
250ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
251 if ( vio == NULL || buf == NULL )
252  return -1;
253
254 return 0;
255}
256
257// pass
258int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
259 if ( calls == NULL )
260  return -1;
261
262 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
263
264 calls->read     = roar_vio_pass_read;
265 calls->write    = roar_vio_pass_write;
266 calls->lseek    = roar_vio_pass_lseek;
267 calls->nonblock = roar_vio_pass_nonblock;
268 calls->sync     = roar_vio_pass_sync;
269 calls->close    = roar_vio_pass_close;
270
271 calls->inst     = dst;
272
273 return 0;
274}
275
276ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
277 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
278}
279
280ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
281 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
282}
283
284off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
285 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
286}
287
288int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
289 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
290}
291
292int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
293 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
294}
295
296int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
297 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
298}
299
300int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
301 return roar_vio_close((struct roar_vio_calls *) vio->inst);
302}
303
304
305// re
306int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
307 if ( roar_vio_open_pass(calls, dst) == -1 )
308  return -1;
309
310 calls->read  = roar_vio_re_read;
311 calls->write = roar_vio_re_write;
312 calls->lseek = roar_vio_re_lseek;
313
314 return 0;
315}
316ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
317  size_t len =  0;
318 ssize_t r   = -1;
319
320 if ( vio == NULL )
321  return -1;
322
323 if ( vio->inst == NULL )
324  return -1;
325
326 errno = 0;
327
328 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
329  len   += r;
330  buf   += r;
331  count -= r;
332  if ( count == 0 )
333   break;
334 }
335
336 if ( len == 0 && r == -1 )
337  return -1;
338
339 return len;
340}
341
342ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
343  size_t len =  0;
344 ssize_t r   = -1;
345
346 if ( vio == NULL )
347  return -1;
348
349 if ( vio->inst == NULL )
350  return -1;
351
352 errno = 0;
353
354 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
355  len   += r;
356  buf   += r;
357  count -= r;
358  if ( count == 0 )
359   break;
360 }
361
362 if ( len == 0 && r == -1 )
363  return -1;
364
365 return len;
366}
367
368// TODO: we should do a some more intelgent thing here.
369off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
370 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
371}
372
373// stdio:
374ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
375 return fread(buf, 1, count, (FILE*)(vio->inst));
376}
377
378ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
379 return fwrite(buf, 1, count, (FILE*)(vio->inst));
380}
381
382off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
383 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
384  return -1;
385
386 return ftell((FILE*)(vio->inst));
387}
388
389int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
390 return fflush((FILE*)(vio->inst));
391}
392
393int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
394 return fclose((FILE*)(vio->inst));
395}
396
397//ll
Note: See TracBrowser for help on using the repository browser.