source: roaraudio/libroar/vio_stdio.c @ 5231:8b30ddb689b8

Last change on this file since 5231:8b30ddb689b8 was 5088:eb14fce06df5, checked in by phi, 13 years ago

relex type for fopencookie(), maybe helps on cygwin

File size: 5.1 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 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)
84int 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)
89ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
90#elif defined(ROAR_HAVE_FUNOPEN)
91int 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)
97ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
98#elif defined(ROAR_HAVE_FUNOPEN)
99int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
100#endif
101 ROAR_DBG("roar_vio_to_stdio_write(*) = ?");
102 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
103}
104
105#if defined(ROAR_HAVE_FOPENCOOKIE)
106int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
107#elif defined(ROAR_HAVE_FUNOPEN)
108fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
109 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
110}
111#endif
112#endif
113
114// VIOs:
115
116// stdio:
117#ifndef ROAR_WITHOUT_VIO_STDIO
118ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
119 return fread(buf, 1, count, (FILE*)(vio->inst));
120}
121
122ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
123 return fwrite(buf, 1, count, (FILE*)(vio->inst));
124}
125
126off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
127#if defined(ROAR_HAVE_FSEEK) && defined(ROAR_HAVE_FTELL)
128 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
129  return -1;
130
131 return ftell((FILE*)(vio->inst));
132#else
133 return (off_t)-1;
134#endif
135}
136
137int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
138 return fflush((FILE*)(vio->inst));
139}
140
141int     roar_vio_stdio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
142
143 if ( vio == NULL || cmd == -1 )
144  return -1;
145
146 switch (cmd) {
147  case ROAR_VIO_CTL_GET_NAME:
148    if ( data == NULL )
149     return -1;
150
151    *(char**)data = "stdio";
152    return 0;
153   break;
154#ifdef ROAR_HAVE_FILENO
155  case ROAR_VIO_CTL_GET_FH:
156  case ROAR_VIO_CTL_GET_READ_FH:
157  case ROAR_VIO_CTL_GET_WRITE_FH:
158  case ROAR_VIO_CTL_GET_SELECT_FH:
159  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
160  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
161   *(int*)data = fileno((FILE*)(vio->inst));
162    return 0;
163   break;
164#endif
165 }
166
167 return -1;
168}
169
170int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
171 return fclose((FILE*)(vio->inst));
172}
173
174#endif
175
176//ll
Note: See TracBrowser for help on using the repository browser.