source: roaraudio/libroar/vio_stdio.c @ 5353:de96f27919bf

Last change on this file since 5353:de96f27919bf was 5278:b3e0dd3f3141, checked in by phi, 12 years ago

last parts of merging _nonblock into _ctl and fixed sizeof(cmd) of _ctls

File size: 5.2 KB
Line 
1//vio_stdio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38int     roar_vio_open_stdio    (struct roar_vio_calls * calls, FILE * dst) {
39#ifndef ROAR_WITHOUT_VIO_STDIO
40 if ( calls == NULL || dst == NULL )
41  return -1;
42
43 memset(calls, 0, sizeof(struct roar_vio_calls));
44
45 calls->read  = roar_vio_stdio_read;
46 calls->write = roar_vio_stdio_write;
47 calls->lseek = roar_vio_stdio_lseek;
48 calls->sync  = roar_vio_stdio_sync;
49 calls->close = roar_vio_stdio_close;
50
51 calls->inst  = dst;
52
53 return 0;
54#else
55 return -1;
56#endif
57}
58
59FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
60#ifdef ROAR_HAVE_FOPENCOOKIE
61 cookie_io_functions_t foc_funcs;
62#endif
63
64 // (void)flags is the the individual sections because later added APIs
65 // may require those flags.
66
67 if ( calls == NULL )
68  return NULL;
69
70#if defined(ROAR_HAVE_FOPENCOOKIE)
71 (void)flags;
72
73 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
74
75 foc_funcs.close = roar_vio_to_stdio_close;
76 foc_funcs.read  = roar_vio_to_stdio_read;
77 foc_funcs.write = roar_vio_to_stdio_write;
78
79 return fopencookie((void*) calls, "rw", foc_funcs);
80#elif defined(ROAR_HAVE_FUNOPEN)
81 (void)flags;
82
83 return funopen((void*) calls, roar_vio_to_stdio_read,  roar_vio_to_stdio_write,
84                               roar_vio_to_stdio_lseek, roar_vio_to_stdio_close);
85#else
86 return NULL;
87#endif
88}
89
90#if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN)
91int roar_vio_to_stdio_close (void *__cookie) {
92 return roar_vio_close((struct roar_vio_calls *) __cookie);
93}
94
95#if defined(ROAR_HAVE_FOPENCOOKIE)
96ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
97#elif defined(ROAR_HAVE_FUNOPEN)
98int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) {
99#endif
100 return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes);
101}
102
103#if defined(ROAR_HAVE_FOPENCOOKIE)
104ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
105#elif defined(ROAR_HAVE_FUNOPEN)
106int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
107#endif
108 ROAR_DBG("roar_vio_to_stdio_write(*) = ?");
109 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
110}
111
112#if defined(ROAR_HAVE_FOPENCOOKIE)
113int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
114#elif defined(ROAR_HAVE_FUNOPEN)
115fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
116 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
117}
118#endif
119#endif
120
121// VIOs:
122
123// stdio:
124#ifndef ROAR_WITHOUT_VIO_STDIO
125ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
126 return fread(buf, 1, count, (FILE*)(vio->inst));
127}
128
129ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
130 return fwrite(buf, 1, count, (FILE*)(vio->inst));
131}
132
133roar_off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
134#if defined(ROAR_HAVE_FSEEK) && defined(ROAR_HAVE_FTELL)
135 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
136  return -1;
137
138 return ftell((FILE*)(vio->inst));
139#else
140 return (roar_off_t)-1;
141#endif
142}
143
144int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
145 return fflush((FILE*)(vio->inst));
146}
147
148int     roar_vio_stdio_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
149
150 if ( vio == NULL || cmd == -1 )
151  return -1;
152
153 switch (cmd) {
154  case ROAR_VIO_CTL_GET_NAME:
155    if ( data == NULL )
156     return -1;
157
158    *(char**)data = "stdio";
159    return 0;
160   break;
161#ifdef ROAR_HAVE_FILENO
162  case ROAR_VIO_CTL_GET_FH:
163  case ROAR_VIO_CTL_GET_READ_FH:
164  case ROAR_VIO_CTL_GET_WRITE_FH:
165  case ROAR_VIO_CTL_GET_SELECT_FH:
166  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
167  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
168   *(int*)data = fileno((FILE*)(vio->inst));
169    return 0;
170   break;
171#endif
172 }
173
174 return -1;
175}
176
177int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
178 return fclose((FILE*)(vio->inst));
179}
180
181#endif
182
183//ll
Note: See TracBrowser for help on using the repository browser.