source: roaraudio/libroar/vio.c @ 1314:7fb2fa50a1b1

Last change on this file since 1314:7fb2fa50a1b1 was 1314:7fb2fa50a1b1, checked in by phi, 15 years ago

added roar_vio_puts(), roar_vio_putc(), roar_vio_getc()

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