source: roaraudio/libroar/vio.c @ 1615:1f2b8a6cbd7a

Last change on this file since 1615:1f2b8a6cbd7a was 1615:1f2b8a6cbd7a, checked in by phi, 15 years ago

got HTTP support working in cf source

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