source: roaraudio/libroar/vio.c @ 1351:f6b4d7e85459

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

added roar_vio_printf()

File size: 12.5 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 if ( calls == NULL || dst == NULL )
252  return -1;
253
254 memset(calls, 0, sizeof(struct roar_vio_calls));
255
256 calls->read  = roar_vio_stdio_read;
257 calls->write = roar_vio_stdio_write;
258 calls->lseek = roar_vio_stdio_lseek;
259 calls->sync  = roar_vio_stdio_sync;
260 calls->close = roar_vio_stdio_close;
261
262 calls->inst  = dst;
263
264 return 0;
265}
266
267FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
268#ifdef ROAR_HAVE_FOPENCOOKIE
269 cookie_io_functions_t foc_funcs;
270#endif
271
272 if ( calls == NULL )
273  return NULL;
274
275#if defined(ROAR_HAVE_FOPENCOOKIE)
276 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
277
278 foc_funcs.close = roar_vio_to_stdio_close;
279 foc_funcs.read  = roar_vio_to_stdio_read;
280 foc_funcs.write = roar_vio_to_stdio_write;
281
282 return fopencookie((void*) calls, "rw", foc_funcs);
283#elif defined(ROAR_HAVE_FUNOPEN)
284 return funopen((void*) calls, roar_vio_to_stdio_read,  roar_vio_to_stdio_write,
285                               roar_vio_to_stdio_lseek, roar_vio_to_stdio_close);
286#else
287 return NULL;
288#endif
289}
290
291#if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN)
292int roar_vio_to_stdio_close (void *__cookie) {
293 return roar_vio_close((struct roar_vio_calls *) __cookie);
294}
295#endif
296
297#if defined(ROAR_HAVE_FOPENCOOKIE)
298__ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
299#elif defined(ROAR_HAVE_FUNOPEN)
300int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) {
301#endif
302 return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes);
303}
304
305#if defined(ROAR_HAVE_FOPENCOOKIE)
306__ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
307#elif defined(ROAR_HAVE_FUNOPEN)
308int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
309#endif
310 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
311}
312
313#if defined(ROAR_HAVE_FOPENCOOKIE)
314int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
315#elif defined(ROAR_HAVE_FUNOPEN)
316fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
317 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
318}
319#endif
320
321// VIOs:
322
323// basic
324ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
325 return read(roar_vio_get_fh(vio), buf, count);
326}
327
328ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
329 return write(roar_vio_get_fh(vio), buf, count);
330}
331
332off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
333 return lseek(roar_vio_get_fh(vio), offset, whence);
334}
335
336int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
337 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
338  return -1;
339
340 if ( state == ROAR_SOCKET_NONBLOCK )
341  return 0;
342
343 roar_vio_sync(vio);
344
345 return 0;
346}
347
348int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
349 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
350}
351
352int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
353 if ( roar_vio_get_fh(vio) != -1 )
354  return close(roar_vio_get_fh(vio));
355
356 return 0;
357}
358
359// null
360ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
361 if ( vio == NULL || buf == NULL )
362  return -1;
363
364 return 0;
365}
366
367// pass
368int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
369 if ( calls == NULL )
370  return -1;
371
372 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
373
374 calls->read     = roar_vio_pass_read;
375 calls->write    = roar_vio_pass_write;
376 calls->lseek    = roar_vio_pass_lseek;
377 calls->nonblock = roar_vio_pass_nonblock;
378 calls->sync     = roar_vio_pass_sync;
379 calls->close    = roar_vio_pass_close;
380
381 calls->inst     = dst;
382
383 return 0;
384}
385
386ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
387 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
388}
389
390ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
391 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
392}
393
394off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
395 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
396}
397
398int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
399 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
400}
401
402int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
403 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
404}
405
406int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
407 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
408}
409
410int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
411 return roar_vio_close((struct roar_vio_calls *) vio->inst);
412}
413
414
415// re
416int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
417 if ( roar_vio_open_pass(calls, dst) == -1 )
418  return -1;
419
420 calls->read  = roar_vio_re_read;
421 calls->write = roar_vio_re_write;
422 calls->lseek = roar_vio_re_lseek;
423
424 return 0;
425}
426ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
427  size_t len =  0;
428 ssize_t r   = -1;
429
430 if ( vio == NULL )
431  return -1;
432
433 if ( vio->inst == NULL )
434  return -1;
435
436 errno = 0;
437
438 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
439  len   += r;
440  buf   += r;
441  count -= r;
442  if ( count == 0 )
443   break;
444 }
445
446 if ( len == 0 && r == -1 )
447  return -1;
448
449 return len;
450}
451
452ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
453  size_t len =  0;
454 ssize_t r   = -1;
455
456 if ( vio == NULL )
457  return -1;
458
459 if ( vio->inst == NULL )
460  return -1;
461
462 errno = 0;
463
464 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
465  len   += r;
466  buf   += r;
467  count -= r;
468  if ( count == 0 )
469   break;
470 }
471
472 if ( len == 0 && r == -1 )
473  return -1;
474
475 return len;
476}
477
478// TODO: we should do a some more intelgent thing here.
479off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
480 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
481}
482
483// stdio:
484ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
485 return fread(buf, 1, count, (FILE*)(vio->inst));
486}
487
488ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
489 return fwrite(buf, 1, count, (FILE*)(vio->inst));
490}
491
492off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
493 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
494  return -1;
495
496 return ftell((FILE*)(vio->inst));
497}
498
499int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
500 return fflush((FILE*)(vio->inst));
501}
502
503int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
504 return fclose((FILE*)(vio->inst));
505}
506
507//ll
Note: See TracBrowser for help on using the repository browser.