source: roaraudio/libroar/vio.c @ 1376:f8598fd4ec5c

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

don't produce invalide code if wether fopencookie() nor funopen() was found, make stdio part optional

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