1 | //vio_stdio.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
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 | |
---|
38 | int 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 | |
---|
59 | FILE * 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 | if ( calls == NULL ) |
---|
65 | return NULL; |
---|
66 | |
---|
67 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
68 | memset(&foc_funcs, 0, sizeof(cookie_io_functions_t)); |
---|
69 | |
---|
70 | foc_funcs.close = roar_vio_to_stdio_close; |
---|
71 | foc_funcs.read = roar_vio_to_stdio_read; |
---|
72 | foc_funcs.write = roar_vio_to_stdio_write; |
---|
73 | |
---|
74 | return fopencookie((void*) calls, "rw", foc_funcs); |
---|
75 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
76 | return funopen((void*) calls, roar_vio_to_stdio_read, roar_vio_to_stdio_write, |
---|
77 | roar_vio_to_stdio_lseek, roar_vio_to_stdio_close); |
---|
78 | #else |
---|
79 | return NULL; |
---|
80 | #endif |
---|
81 | } |
---|
82 | |
---|
83 | #if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN) |
---|
84 | int roar_vio_to_stdio_close (void *__cookie) { |
---|
85 | return roar_vio_close((struct roar_vio_calls *) __cookie); |
---|
86 | } |
---|
87 | |
---|
88 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
89 | __ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) { |
---|
90 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
91 | int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) { |
---|
92 | #endif |
---|
93 | return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes); |
---|
94 | } |
---|
95 | |
---|
96 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
97 | __ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) { |
---|
98 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
99 | int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) { |
---|
100 | #endif |
---|
101 | return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n); |
---|
102 | } |
---|
103 | |
---|
104 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
105 | int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w); |
---|
106 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
107 | fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) { |
---|
108 | return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w); |
---|
109 | } |
---|
110 | #endif |
---|
111 | #endif |
---|
112 | |
---|
113 | // VIOs: |
---|
114 | |
---|
115 | // stdio: |
---|
116 | #ifndef ROAR_WITHOUT_VIO_STDIO |
---|
117 | ssize_t roar_vio_stdio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
118 | return fread(buf, 1, count, (FILE*)(vio->inst)); |
---|
119 | } |
---|
120 | |
---|
121 | ssize_t roar_vio_stdio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
122 | return fwrite(buf, 1, count, (FILE*)(vio->inst)); |
---|
123 | } |
---|
124 | |
---|
125 | off_t roar_vio_stdio_lseek (struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
126 | #if defined(ROAR_HAVE_FSEEK) && defined(ROAR_HAVE_FTELL) |
---|
127 | if ( fseek((FILE*)(vio->inst), offset, whence) == -1 ) |
---|
128 | return -1; |
---|
129 | |
---|
130 | return ftell((FILE*)(vio->inst)); |
---|
131 | #else |
---|
132 | return (off_t)-1; |
---|
133 | #endif |
---|
134 | } |
---|
135 | |
---|
136 | int roar_vio_stdio_sync (struct roar_vio_calls * vio) { |
---|
137 | return fflush((FILE*)(vio->inst)); |
---|
138 | } |
---|
139 | |
---|
140 | int roar_vio_stdio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
141 | |
---|
142 | if ( vio == NULL || cmd == -1 ) |
---|
143 | return -1; |
---|
144 | |
---|
145 | switch (cmd) { |
---|
146 | case ROAR_VIO_CTL_GET_NAME: |
---|
147 | if ( data == NULL ) |
---|
148 | return -1; |
---|
149 | |
---|
150 | *(char**)data = "stdio"; |
---|
151 | return 0; |
---|
152 | break; |
---|
153 | #ifdef ROAR_HAVE_FILENO |
---|
154 | case ROAR_VIO_CTL_GET_FH: |
---|
155 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
156 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
157 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
158 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
159 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
160 | *(int*)data = fileno((FILE*)(vio->inst)); |
---|
161 | return 0; |
---|
162 | break; |
---|
163 | #endif |
---|
164 | } |
---|
165 | |
---|
166 | return -1; |
---|
167 | } |
---|
168 | |
---|
169 | int roar_vio_stdio_close (struct roar_vio_calls * vio) { |
---|
170 | return fclose((FILE*)(vio->inst)); |
---|
171 | } |
---|
172 | |
---|
173 | #endif |
---|
174 | |
---|
175 | //ll |
---|