source: roaraudio/libroar/vio_stack.c @ 5229:d7e314825b8a

Last change on this file since 5229:d7e314825b8a was 4975:1b8be0a0ba5f, checked in by phi, 13 years ago

removed more usages of system's malloc()/free() calles.

File size: 5.3 KB
Line 
1//vio_stack.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_stack    (struct roar_vio_calls * calls) {
39 return roar_vio_open_stack2(calls, free);
40}
41
42int     roar_vio_open_stack2   (struct roar_vio_calls * calls, void (*func)(void*)) {
43 struct roar_vio_stack * self;
44
45 if ( calls == NULL ) {
46  roar_err_set(ROAR_ERROR_FAULT);
47  return -1;
48 }
49
50 if ( func == NULL )
51  func = roar_mm_free_retvoid;
52
53 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_stack))) == NULL )
54  return -1;
55
56 memset(self,  0, sizeof(struct roar_vio_stack));
57 memset(calls, 0, sizeof(struct roar_vio_calls));
58
59 self->free      = func;
60
61 calls->inst     = self;
62 calls->close    = roar_vio_stack_close;
63 calls->ctl      = roar_vio_stack_ctl;
64 calls->read     = roar_vio_stack_read;
65 calls->write    = roar_vio_stack_write;
66 calls->lseek    = roar_vio_stack_lseek;
67 calls->nonblock = roar_vio_stack_nonblock;
68 calls->sync     = roar_vio_stack_sync;
69
70 return 0;
71}
72
73int     roar_vio_stack_add     (struct roar_vio_calls * calls, struct roar_vio_calls * vio) {
74 struct roar_vio_stack * self;
75
76 if ( calls == NULL || vio == NULL ) {
77  roar_err_set(ROAR_ERROR_FAULT);
78  return -1;
79 }
80
81 if ( (self = calls->inst) == NULL ) {
82  roar_err_set(ROAR_ERROR_FAULT);
83  return -1;
84 }
85
86 if ( self->next == ROAR_VIO_STACK_MAX )
87  return -1;
88
89 self->cur = self->calls[self->next++] = vio;
90
91 return 0;
92}
93
94int     roar_vio_stack_close   (struct roar_vio_calls * vio) {
95 struct roar_vio_stack * self;
96 int i;
97
98 if ( vio == NULL ) {
99  roar_err_set(ROAR_ERROR_FAULT);
100  return -1;
101 }
102
103 if ( (self = vio->inst) == NULL )
104  return -1;
105
106 if ( self->cur != NULL ) {
107  if ( roar_vio_close(self->cur) == -1 )
108   return -1;
109
110  for (i = 0; i < self->next; i++)
111   self->free(self->calls[i]);
112 }
113
114 roar_mm_free(self);
115
116 return 0;
117}
118
119int     roar_vio_stack_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
120 if (vio == NULL || cmd == -1)
121  return -1;
122
123 switch (cmd) {
124  case ROAR_VIO_CTL_GET_NAME:
125    if ( data == NULL )
126     return -1;
127
128    *(char**)data = "stack";
129    return 0;
130   break;
131  case ROAR_VIO_CTL_GET_FH:
132  case ROAR_VIO_CTL_GET_READ_FH:
133  case ROAR_VIO_CTL_GET_WRITE_FH:
134  case ROAR_VIO_CTL_SELECT:
135    return roar_vio_ctl(((struct roar_vio_stack*)(vio->inst))->cur, cmd, data);
136   break;
137  case ROAR_VIO_CTL_GET_NEXT:
138    *(struct roar_vio_calls **)data = ((struct roar_vio_stack*)(vio->inst))->cur;
139    return 0;
140   break;
141 }
142
143 return roar_vio_ctl(((struct roar_vio_stack*)(vio->inst))->cur, cmd, data);
144}
145
146
147ssize_t roar_vio_stack_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
148 if ( vio == NULL )
149  return -1;
150
151 if ( vio->inst == NULL )
152  return -1;
153
154 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
155  return -1;
156
157 return roar_vio_read(((struct roar_vio_stack*)(vio->inst))->cur, buf, count);
158}
159
160ssize_t roar_vio_stack_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
161 if ( vio == NULL )
162  return -1;
163
164 if ( vio->inst == NULL )
165  return -1;
166
167 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
168  return -1;
169
170 return roar_vio_write(((struct roar_vio_stack*)(vio->inst))->cur, buf, count);
171}
172
173off_t   roar_vio_stack_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
174 if ( vio == NULL )
175  return -1;
176
177 if ( vio->inst == NULL )
178  return -1;
179
180 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
181  return -1;
182
183 return roar_vio_lseek(((struct roar_vio_stack*)(vio->inst))->cur, offset, whence);
184}
185
186int     roar_vio_stack_nonblock(struct roar_vio_calls * vio, int state) {
187 if ( vio == NULL )
188  return -1;
189
190 if ( vio->inst == NULL )
191  return -1;
192
193 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
194  return -1;
195
196 return roar_vio_nonblock(((struct roar_vio_stack*)(vio->inst))->cur, state);
197}
198
199int     roar_vio_stack_sync    (struct roar_vio_calls * vio) {
200 if ( vio == NULL )
201  return -1;
202
203 if ( vio->inst == NULL )
204  return -1;
205
206 if ( ((struct roar_vio_stack*)(vio->inst))->cur == NULL )
207  return -1;
208
209 return roar_vio_sync(((struct roar_vio_stack*)(vio->inst))->cur);
210}
211
212//ll
Note: See TracBrowser for help on using the repository browser.