source: roaraudio/libroar/vio.c @ 1293:8e2deef687c3

Last change on this file since 1293:8e2deef687c3 was 1293:8e2deef687c3, checked in by phi, 15 years ago

added a lot calls for vio -> stdio

File size: 11.9 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009
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->close == 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_fh_socket(struct roar_vio_calls * calls, int fh) {
179 if ( calls == NULL )
180  return -1;
181
182 return roar_vio_open_fh(calls, fh);
183}
184
185int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
186 int fh;
187
188 if ( calls == NULL )
189  return -1;
190
191 if ( (fh = roar_socket_connect(host, port)) == -1 )
192  return -1;
193
194 return roar_vio_open_fh_socket(calls, fh);
195}
196
197int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
198 int fh;
199
200 if ( calls == NULL )
201  return -1;
202
203 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
204  return -1;
205
206 return roar_vio_open_fh_socket(calls, fh);
207}
208
209int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
210                                                               char * server, int dir, char * name) {
211 int fh;
212
213 if ( calls == NULL )
214  return -1;
215
216 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
217  return -1;
218
219 return roar_vio_open_fh_socket(calls, fh);
220}
221
222
223int     roar_vio_open_stdio    (struct roar_vio_calls * calls, FILE * dst) {
224 if ( calls == NULL || dst == NULL )
225  return -1;
226
227 memset(calls, 0, sizeof(struct roar_vio_calls));
228
229 calls->read  = roar_vio_stdio_read;
230 calls->write = roar_vio_stdio_write;
231 calls->lseek = roar_vio_stdio_lseek;
232 calls->sync  = roar_vio_stdio_sync;
233 calls->close = roar_vio_stdio_close;
234
235 calls->inst  = dst;
236
237 return 0;
238}
239
240FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
241#ifdef ROAR_HAVE_FOPENCOOKIE
242 cookie_io_functions_t foc_funcs;
243#endif
244
245 if ( calls == NULL )
246  return NULL;
247
248#if defined(ROAR_HAVE_FOPENCOOKIE)
249 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
250
251 foc_funcs.close = roar_vio_to_stdio_close;
252 foc_funcs.read  = roar_vio_to_stdio_read;
253 foc_funcs.write = roar_vio_to_stdio_write;
254
255 return fopencookie((void*) calls, "rw", foc_funcs);
256#elif defined(ROAR_HAVE_FUNOPEN)
257 return funopen((void*) calls, roar_vio_to_stdio_read,  roar_vio_to_stdio_write,
258                               roar_vio_to_stdio_lseek, roar_vio_to_stdio_close);
259#else
260 return NULL;
261#endif
262}
263
264#if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN)
265int roar_vio_to_stdio_close (void *__cookie) {
266 return roar_vio_close((struct roar_vio_calls *) __cookie);
267}
268#endif
269
270#if defined(ROAR_HAVE_FOPENCOOKIE)
271__ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
272#elif defined(ROAR_HAVE_FUNOPEN)
273int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) {
274#endif
275 return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes);
276}
277
278#if defined(ROAR_HAVE_FOPENCOOKIE)
279__ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
280#elif defined(ROAR_HAVE_FUNOPEN)
281int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
282#endif
283 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
284}
285
286#if defined(ROAR_HAVE_FOPENCOOKIE)
287int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
288#elif defined(ROAR_HAVE_FUNOPEN)
289fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
290 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
291}
292#endif
293
294// VIOs:
295
296// basic
297ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
298 return read(roar_vio_get_fh(vio), buf, count);
299}
300
301ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
302 return write(roar_vio_get_fh(vio), buf, count);
303}
304
305off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
306 return lseek(roar_vio_get_fh(vio), offset, whence);
307}
308
309int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
310 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
311  return -1;
312
313 if ( state == ROAR_SOCKET_NONBLOCK )
314  return 0;
315
316 roar_vio_sync(vio);
317
318 return 0;
319}
320
321int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
322 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
323}
324
325int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
326 return close(roar_vio_get_fh(vio));
327}
328
329// null
330ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
331 if ( vio == NULL || buf == NULL )
332  return -1;
333
334 return 0;
335}
336
337// pass
338int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
339 if ( calls == NULL )
340  return -1;
341
342 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
343
344 calls->read     = roar_vio_pass_read;
345 calls->write    = roar_vio_pass_write;
346 calls->lseek    = roar_vio_pass_lseek;
347 calls->nonblock = roar_vio_pass_nonblock;
348 calls->sync     = roar_vio_pass_sync;
349 calls->close    = roar_vio_pass_close;
350
351 calls->inst     = dst;
352
353 return 0;
354}
355
356ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
357 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
358}
359
360ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
361 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
362}
363
364off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
365 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
366}
367
368int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
369 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
370}
371
372int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
373 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
374}
375
376int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
377 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
378}
379
380int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
381 return roar_vio_close((struct roar_vio_calls *) vio->inst);
382}
383
384
385// re
386int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
387 if ( roar_vio_open_pass(calls, dst) == -1 )
388  return -1;
389
390 calls->read  = roar_vio_re_read;
391 calls->write = roar_vio_re_write;
392 calls->lseek = roar_vio_re_lseek;
393
394 return 0;
395}
396ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
397  size_t len =  0;
398 ssize_t r   = -1;
399
400 if ( vio == NULL )
401  return -1;
402
403 if ( vio->inst == NULL )
404  return -1;
405
406 errno = 0;
407
408 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
409  len   += r;
410  buf   += r;
411  count -= r;
412  if ( count == 0 )
413   break;
414 }
415
416 if ( len == 0 && r == -1 )
417  return -1;
418
419 return len;
420}
421
422ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
423  size_t len =  0;
424 ssize_t r   = -1;
425
426 if ( vio == NULL )
427  return -1;
428
429 if ( vio->inst == NULL )
430  return -1;
431
432 errno = 0;
433
434 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
435  len   += r;
436  buf   += r;
437  count -= r;
438  if ( count == 0 )
439   break;
440 }
441
442 if ( len == 0 && r == -1 )
443  return -1;
444
445 return len;
446}
447
448// TODO: we should do a some more intelgent thing here.
449off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
450 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
451}
452
453// stdio:
454ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
455 return fread(buf, 1, count, (FILE*)(vio->inst));
456}
457
458ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
459 return fwrite(buf, 1, count, (FILE*)(vio->inst));
460}
461
462off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
463 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
464  return -1;
465
466 return ftell((FILE*)(vio->inst));
467}
468
469int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
470 return fflush((FILE*)(vio->inst));
471}
472
473int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
474 return fclose((FILE*)(vio->inst));
475}
476
477//ll
Note: See TracBrowser for help on using the repository browser.