source: roaraudio/libroar/vio.c @ 1171:02f540634383

Last change on this file since 1171:02f540634383 was 1171:02f540634383, checked in by phi, 15 years ago

use fsync() if there is no fdatasync()

File size: 6.0 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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
55 return 0;
56}
57
58int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
59 if ( vio == NULL )
60  return -1;
61
62 vio->inst = inst;
63
64 return 0;
65}
66
67int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
68 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
69}
70
71int roar_vio_get_fh   (struct roar_vio_calls * vio) {
72 if ( vio == NULL )
73  return -1;
74
75 return ((int)(ROAR_INSTINT)vio->inst) - 1;
76}
77
78
79ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
80 if ( vio == NULL )
81  return -1;
82
83 if ( vio->read == NULL )
84  return -1;
85
86 return vio->read(vio, buf, count);
87}
88
89ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
90 if ( vio == NULL )
91  return -1;
92
93 if ( vio->write == NULL )
94  return -1;
95
96 return vio->write(vio, buf, count);
97}
98
99off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
100 if ( vio == NULL )
101  return -1;
102
103 if ( vio->lseek == NULL )
104  return -1;
105
106 return vio->lseek(vio, offset, whence);
107}
108
109int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
110 if ( vio == NULL )
111  return -1;
112
113 if ( vio->nonblock == NULL )
114  return -1;
115
116 return vio->nonblock(vio, state);
117}
118
119int     roar_vio_sync    (struct roar_vio_calls * vio) {
120 if ( vio == NULL )
121  return -1;
122
123 if ( vio->sync == NULL )
124  return -1;
125
126 return vio->sync(vio);
127}
128
129int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
130 if ( vio == NULL )
131  return -1;
132
133 if ( vio->ctl == NULL )
134  return -1;
135
136 return vio->ctl(vio, cmd, data);
137}
138
139// VIOs:
140
141// basic
142ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
143 return read(roar_vio_get_fh(vio), buf, count);
144}
145
146ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
147 return write(roar_vio_get_fh(vio), buf, count);
148}
149
150off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
151 return lseek(roar_vio_get_fh(vio), offset, whence);
152}
153
154int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
155 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
156  return -1;
157
158 if ( state == ROAR_SOCKET_NONBLOCK )
159  return 0;
160
161 roar_vio_sync(vio);
162
163 return 0;
164}
165
166int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
167 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
168}
169
170// null
171ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
172 if ( vio == NULL || buf == NULL )
173  return -1;
174
175 return 0;
176}
177
178// pass
179ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
180 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
181}
182
183ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
184 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
185}
186
187off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
188 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
189}
190
191
192// re
193ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
194  size_t len =  0;
195 ssize_t r   = -1;
196
197 if ( vio == NULL )
198  return -1;
199
200 if ( vio->inst == NULL )
201  return -1;
202
203 errno = 0;
204
205 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
206  len   += r;
207  buf   += r;
208  count -= r;
209  if ( count == 0 )
210   break;
211 }
212
213 if ( len == 0 && r == -1 )
214  return -1;
215
216 return len;
217}
218
219ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
220  size_t len =  0;
221 ssize_t r   = -1;
222
223 if ( vio == NULL )
224  return -1;
225
226 if ( vio->inst == NULL )
227  return -1;
228
229 errno = 0;
230
231 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
232  len   += r;
233  buf   += r;
234  count -= r;
235  if ( count == 0 )
236   break;
237 }
238
239 if ( len == 0 && r == -1 )
240  return -1;
241
242 return len;
243}
244
245// we should do a some more intelgent thing here.
246off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
247 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
248}
249
250
251//ll
Note: See TracBrowser for help on using the repository browser.