source: roaraudio/libroar/vio.c @ 1474:0a0bf0ac011b

Last change on this file since 1474:0a0bf0ac011b was 1474:0a0bf0ac011b, checked in by phi, 15 years ago

only use POSIX file IO if we have POSIX file IO ;)

File size: 13.0 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
37#ifdef ROAR_HAVE_IO_POSIX
38#define _CAN_OPERATE
39#endif
40
41int roar_vio_init_calls (struct roar_vio_calls * calls) {
42#ifdef _CAN_OPERATE
43 if ( calls == NULL )
44  return -1;
45
46 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
47
48/*
49 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
50 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
51 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
52*/
53
54 calls->read     = roar_vio_basic_read;
55 calls->write    = roar_vio_basic_write;
56 calls->lseek    = roar_vio_basic_lseek;
57 calls->nonblock = roar_vio_basic_nonblock;
58 calls->sync     = roar_vio_basic_sync;
59 calls->close    = roar_vio_basic_close;
60
61 return 0;
62#else
63 return -1;
64#endif
65}
66
67int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
68 if ( vio == NULL )
69  return -1;
70
71 vio->inst = inst;
72
73 return 0;
74}
75
76int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
77 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
78}
79
80int roar_vio_get_fh   (struct roar_vio_calls * vio) {
81 if ( vio == NULL )
82  return -1;
83
84 return ((int)(ROAR_INSTINT)vio->inst) - 1;
85}
86
87
88ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
89 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
90
91 if ( vio == NULL )
92  return -1;
93
94 if ( vio->read == NULL )
95  return -1;
96
97 return vio->read(vio, buf, count);
98}
99
100ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
101 if ( vio == NULL )
102  return -1;
103
104 if ( vio->write == NULL )
105  return -1;
106
107 return vio->write(vio, buf, count);
108}
109
110off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
111 if ( vio == NULL )
112  return -1;
113
114 if ( vio->lseek == NULL )
115  return -1;
116
117 return vio->lseek(vio, offset, whence);
118}
119
120int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
121 if ( vio == NULL )
122  return -1;
123
124 if ( vio->nonblock == NULL )
125  return -1;
126
127 return vio->nonblock(vio, state);
128}
129
130int     roar_vio_sync    (struct roar_vio_calls * vio) {
131 if ( vio == NULL )
132  return -1;
133
134 if ( vio->sync == NULL )
135  return -1;
136
137 return vio->sync(vio);
138}
139
140int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
141 if ( vio == NULL )
142  return -1;
143
144 if ( vio->ctl == NULL )
145  return -1;
146
147 return vio->ctl(vio, cmd, data);
148}
149
150int     roar_vio_close    (struct roar_vio_calls * vio) {
151 if ( vio == NULL )
152  return -1;
153
154 if ( vio->close == NULL )
155  return -1;
156
157 return vio->close(vio);
158}
159
160int     roar_vio_putc    (struct roar_vio_calls * vio, char c) {
161 return roar_vio_write(vio, &c, 1);
162}
163
164int     roar_vio_getc    (struct roar_vio_calls * vio) {
165 unsigned char c;
166
167 if ( roar_vio_read(vio, &c, 1) != 1 )
168  return EOF;
169
170 return c;
171}
172
173int     roar_vio_printf(struct roar_vio_calls * vio, const char *format, ...) {
174 va_list ap;
175 int ret;
176 char buf[8192];
177
178 va_start(ap, format);
179 ret = vsnprintf(buf, 8192, format, ap);
180 va_end(ap);
181
182 return roar_vio_write(vio, buf, ret);
183}
184
185// converters:
186int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
187#ifdef _CAN_OPERATE
188 int fh;
189
190 if ( calls == NULL || filename == NULL )
191  return -1;
192
193 if ( (fh = open(filename, flags, mode)) == -1 )
194  return -1;
195
196 if ( roar_vio_open_fh(calls, fh) == -1 ) {
197  close(fh);
198  return -1;
199 }
200
201 return 0;
202#else
203 return -1;
204#endif
205}
206
207int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
208 if ( calls == NULL )
209  return -1;
210
211 if ( roar_vio_init_calls(calls) == -1 )
212  return -1;
213
214 return roar_vio_set_fh(calls, fh);
215}
216
217int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
218 if ( calls == NULL )
219  return -1;
220
221 return roar_vio_open_fh(calls, fh);
222}
223
224int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
225 int fh;
226
227 if ( calls == NULL )
228  return -1;
229
230 if ( (fh = roar_socket_connect(host, port)) == -1 )
231  return -1;
232
233 return roar_vio_open_fh_socket(calls, fh);
234}
235
236int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
237 int fh;
238
239 if ( calls == NULL )
240  return -1;
241
242 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
243  return -1;
244
245 return roar_vio_open_fh_socket(calls, fh);
246}
247
248int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
249                                                               char * server, int dir, char * name) {
250 int fh;
251
252 if ( calls == NULL )
253  return -1;
254
255 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
256  return -1;
257
258 return roar_vio_open_fh_socket(calls, fh);
259}
260
261
262int     roar_vio_open_stdio    (struct roar_vio_calls * calls, FILE * dst) {
263#ifndef ROAR_WITHOUT_VIO_STDIO
264 if ( calls == NULL || dst == NULL )
265  return -1;
266
267 memset(calls, 0, sizeof(struct roar_vio_calls));
268
269 calls->read  = roar_vio_stdio_read;
270 calls->write = roar_vio_stdio_write;
271 calls->lseek = roar_vio_stdio_lseek;
272 calls->sync  = roar_vio_stdio_sync;
273 calls->close = roar_vio_stdio_close;
274
275 calls->inst  = dst;
276
277 return 0;
278#else
279 return -1;
280#endif
281}
282
283FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
284#ifdef ROAR_HAVE_FOPENCOOKIE
285 cookie_io_functions_t foc_funcs;
286#endif
287
288 if ( calls == NULL )
289  return NULL;
290
291#if defined(ROAR_HAVE_FOPENCOOKIE)
292 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
293
294 foc_funcs.close = roar_vio_to_stdio_close;
295 foc_funcs.read  = roar_vio_to_stdio_read;
296 foc_funcs.write = roar_vio_to_stdio_write;
297
298 return fopencookie((void*) calls, "rw", foc_funcs);
299#elif defined(ROAR_HAVE_FUNOPEN)
300 return funopen((void*) calls, roar_vio_to_stdio_read,  roar_vio_to_stdio_write,
301                               roar_vio_to_stdio_lseek, roar_vio_to_stdio_close);
302#else
303 return NULL;
304#endif
305}
306
307#if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN)
308int roar_vio_to_stdio_close (void *__cookie) {
309 return roar_vio_close((struct roar_vio_calls *) __cookie);
310}
311
312#if defined(ROAR_HAVE_FOPENCOOKIE)
313__ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
314#elif defined(ROAR_HAVE_FUNOPEN)
315int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) {
316#endif
317 return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes);
318}
319
320#if defined(ROAR_HAVE_FOPENCOOKIE)
321__ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
322#elif defined(ROAR_HAVE_FUNOPEN)
323int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
324#endif
325 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
326}
327
328#if defined(ROAR_HAVE_FOPENCOOKIE)
329int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
330#elif defined(ROAR_HAVE_FUNOPEN)
331fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
332 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
333}
334#endif
335#endif
336
337// VIOs:
338
339// basic
340ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
341#ifdef _CAN_OPERATE
342 return read(roar_vio_get_fh(vio), buf, count);
343#else
344 return -1;
345#endif
346}
347
348ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
349#ifdef _CAN_OPERATE
350 return write(roar_vio_get_fh(vio), buf, count);
351#else
352 return -1;
353#endif
354}
355
356off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
357#ifdef _CAN_OPERATE
358 return lseek(roar_vio_get_fh(vio), offset, whence);
359#else
360 return -1;
361#endif
362}
363
364int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
365 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
366  return -1;
367
368 if ( state == ROAR_SOCKET_NONBLOCK )
369  return 0;
370
371 roar_vio_sync(vio);
372
373 return 0;
374}
375
376int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
377#ifdef ROAR_FDATASYNC
378 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
379#else
380 return 0;
381#endif
382}
383
384int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
385#ifdef _CAN_OPERATE
386 if ( roar_vio_get_fh(vio) != -1 )
387  return close(roar_vio_get_fh(vio));
388
389 return 0;
390#else
391 return -1;
392#endif
393}
394
395// null
396ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
397 if ( vio == NULL || buf == NULL )
398  return -1;
399
400 return 0;
401}
402
403// pass
404int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
405 if ( calls == NULL )
406  return -1;
407
408 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
409
410 calls->read     = roar_vio_pass_read;
411 calls->write    = roar_vio_pass_write;
412 calls->lseek    = roar_vio_pass_lseek;
413 calls->nonblock = roar_vio_pass_nonblock;
414 calls->sync     = roar_vio_pass_sync;
415 calls->close    = roar_vio_pass_close;
416
417 calls->inst     = dst;
418
419 return 0;
420}
421
422ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
423 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
424}
425
426ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
427 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
428}
429
430off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
431 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
432}
433
434int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
435 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
436}
437
438int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
439 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
440}
441
442int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
443 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
444}
445
446int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
447 return roar_vio_close((struct roar_vio_calls *) vio->inst);
448}
449
450
451// re
452int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
453 if ( roar_vio_open_pass(calls, dst) == -1 )
454  return -1;
455
456 calls->read  = roar_vio_re_read;
457 calls->write = roar_vio_re_write;
458 calls->lseek = roar_vio_re_lseek;
459
460 return 0;
461}
462ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
463  size_t len =  0;
464 ssize_t r   = -1;
465
466 if ( vio == NULL )
467  return -1;
468
469 if ( vio->inst == NULL )
470  return -1;
471
472 errno = 0;
473
474 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
475  len   += r;
476  buf   += r;
477  count -= r;
478  if ( count == 0 )
479   break;
480 }
481
482 if ( len == 0 && r == -1 )
483  return -1;
484
485 return len;
486}
487
488ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
489  size_t len =  0;
490 ssize_t r   = -1;
491
492 if ( vio == NULL )
493  return -1;
494
495 if ( vio->inst == NULL )
496  return -1;
497
498 errno = 0;
499
500 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
501  len   += r;
502  buf   += r;
503  count -= r;
504  if ( count == 0 )
505   break;
506 }
507
508 if ( len == 0 && r == -1 )
509  return -1;
510
511 return len;
512}
513
514// TODO: we should do a some more intelgent thing here.
515off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
516 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
517}
518
519// stdio:
520#ifndef ROAR_WITHOUT_VIO_STDIO
521ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
522 return fread(buf, 1, count, (FILE*)(vio->inst));
523}
524
525ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
526 return fwrite(buf, 1, count, (FILE*)(vio->inst));
527}
528
529off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
530 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
531  return -1;
532
533 return ftell((FILE*)(vio->inst));
534}
535
536int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
537 return fflush((FILE*)(vio->inst));
538}
539
540int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
541 return fclose((FILE*)(vio->inst));
542}
543#endif
544
545//ll
Note: See TracBrowser for help on using the repository browser.