]>
code.delx.au - pulseaudio/blob - src/polypcore/ioline.c
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <polyp/error.h>
33 #include <polyp/xmalloc.h>
35 #include <polypcore/log.h>
39 #define BUFFER_LIMIT (64*1024)
40 #define READ_SIZE (1024)
44 pa_defer_event
*defer_event
;
45 pa_mainloop_api
*mainloop
;
50 size_t wbuf_length
, wbuf_index
, wbuf_valid_length
;
53 size_t rbuf_length
, rbuf_index
, rbuf_valid_length
;
55 void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
);
61 static void io_callback(pa_iochannel
*io
, void *userdata
);
62 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
64 pa_ioline
* pa_ioline_new(pa_iochannel
*io
) {
68 l
= pa_xmalloc(sizeof(pa_ioline
));
73 l
->wbuf_length
= l
->wbuf_index
= l
->wbuf_valid_length
= 0;
76 l
->rbuf_length
= l
->rbuf_index
= l
->rbuf_valid_length
= 0;
82 l
->mainloop
= pa_iochannel_get_mainloop_api(io
);
84 l
->defer_event
= l
->mainloop
->defer_new(l
->mainloop
, defer_callback
, l
);
85 l
->mainloop
->defer_enable(l
->defer_event
, 0);
89 pa_iochannel_set_callback(io
, io_callback
, l
);
94 static void ioline_free(pa_ioline
*l
) {
98 pa_iochannel_free(l
->io
);
101 l
->mainloop
->defer_free(l
->defer_event
);
108 void pa_ioline_unref(pa_ioline
*l
) {
109 assert(l
&& l
->ref
>= 1);
115 pa_ioline
* pa_ioline_ref(pa_ioline
*l
) {
116 assert(l
&& l
->ref
>= 1);
122 void pa_ioline_close(pa_ioline
*l
) {
123 assert(l
&& l
->ref
>= 1);
127 pa_iochannel_free(l
->io
);
131 if (l
->defer_event
) {
132 l
->mainloop
->defer_free(l
->defer_event
);
133 l
->defer_event
= NULL
;
137 void pa_ioline_puts(pa_ioline
*l
, const char *c
) {
139 assert(l
&& c
&& l
->ref
>= 1 && !l
->dead
);
144 if (len
> BUFFER_LIMIT
- l
->wbuf_valid_length
)
145 len
= BUFFER_LIMIT
- l
->wbuf_valid_length
;
148 assert(l
->wbuf_length
>= l
->wbuf_valid_length
);
150 /* In case the allocated buffer is too small, enlarge it. */
151 if (l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
152 size_t n
= l
->wbuf_valid_length
+len
;
153 char *new = pa_xmalloc(n
);
155 memcpy(new, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
161 } else if (l
->wbuf_index
+ l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
163 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
164 memmove(l
->wbuf
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
168 assert(l
->wbuf_index
+ l
->wbuf_valid_length
+ len
<= l
->wbuf_length
);
170 /* Append the new string */
171 memcpy(l
->wbuf
+ l
->wbuf_index
+ l
->wbuf_valid_length
, c
, len
);
172 l
->wbuf_valid_length
+= len
;
174 l
->mainloop
->defer_enable(l
->defer_event
, 1);
180 void pa_ioline_set_callback(pa_ioline
*l
, void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
), void *userdata
) {
181 assert(l
&& l
->ref
>= 1);
182 l
->callback
= callback
;
183 l
->userdata
= userdata
;
186 static void failure(pa_ioline
*l
) {
187 assert(l
&& l
->ref
>= 1 && !l
->dead
);
192 l
->callback(l
, NULL
, l
->userdata
);
197 static void scan_for_lines(pa_ioline
*l
, size_t skip
) {
198 assert(l
&& l
->ref
>= 1 && skip
< l
->rbuf_valid_length
);
200 while (!l
->dead
&& l
->rbuf_valid_length
> skip
) {
204 if (!(e
= memchr(l
->rbuf
+ l
->rbuf_index
+ skip
, '\n', l
->rbuf_valid_length
- skip
)))
209 p
= l
->rbuf
+ l
->rbuf_index
;
212 l
->rbuf_index
+= m
+1;
213 l
->rbuf_valid_length
-= m
+1;
215 /* A shortcut for the next time */
216 if (l
->rbuf_valid_length
== 0)
220 l
->callback(l
, p
, l
->userdata
);
225 /* If the buffer became too large and still no newline was found, drop it. */
226 if (l
->rbuf_valid_length
>= BUFFER_LIMIT
)
227 l
->rbuf_index
= l
->rbuf_valid_length
= 0;
230 static int do_write(pa_ioline
*l
);
232 static int do_read(pa_ioline
*l
) {
233 assert(l
&& l
->ref
>= 1);
235 while (!l
->dead
&& pa_iochannel_is_readable(l
->io
)) {
239 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
241 /* Check if we have to enlarge the read buffer */
242 if (len
< READ_SIZE
) {
243 size_t n
= l
->rbuf_valid_length
+READ_SIZE
;
245 if (n
>= BUFFER_LIMIT
)
248 if (l
->rbuf_length
>= n
) {
249 /* The current buffer is large enough, let's just move the data to the front */
250 if (l
->rbuf_valid_length
)
251 memmove(l
->rbuf
, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
253 /* Enlarge the buffer */
254 char *new = pa_xmalloc(n
);
255 if (l
->rbuf_valid_length
)
256 memcpy(new, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
265 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
267 assert(len
>= READ_SIZE
);
270 r
= pa_iochannel_read(l
->io
, l
->rbuf
+l
->rbuf_index
+l
->rbuf_valid_length
, len
);
272 /* Got an EOF, so fake an exit command. */
274 snprintf (l
->rbuf
, l
->rbuf_length
, "exit\n");
276 pa_ioline_puts(l
, "\nExiting.\n");
279 pa_log(__FILE__
": read(): %s", pa_cstrerror(errno
));
284 l
->rbuf_valid_length
+= r
;
286 /* Look if a line has been terminated in the newly read data */
287 scan_for_lines(l
, l
->rbuf_valid_length
- r
);
293 /* Try to flush the buffer */
294 static int do_write(pa_ioline
*l
) {
296 assert(l
&& l
->ref
>= 1);
298 while (!l
->dead
&& pa_iochannel_is_writable(l
->io
) && l
->wbuf_valid_length
) {
300 if ((r
= pa_iochannel_write(l
->io
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
)) < 0) {
301 pa_log(__FILE__
": write(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
307 l
->wbuf_valid_length
-= r
;
309 /* A shortcut for the next time */
310 if (l
->wbuf_valid_length
== 0)
317 /* Try to flush read/write data */
318 static void do_work(pa_ioline
*l
) {
319 assert(l
&& l
->ref
>= 1);
323 l
->mainloop
->defer_enable(l
->defer_event
, 0);
331 if (l
->defer_close
&& !l
->wbuf_valid_length
)
337 static void io_callback(pa_iochannel
*io
, void *userdata
) {
338 pa_ioline
*l
= userdata
;
339 assert(io
&& l
&& l
->ref
>= 1);
344 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
345 pa_ioline
*l
= userdata
;
346 assert(l
&& l
->ref
>= 1 && l
->mainloop
== m
&& l
->defer_event
== e
);
351 void pa_ioline_defer_close(pa_ioline
*l
) {
356 if (!l
->wbuf_valid_length
)
357 l
->mainloop
->defer_enable(l
->defer_event
, 1);
360 void pa_ioline_printf(pa_ioline
*s
, const char *format
, ...) {
365 va_start(ap
, format
);
366 t
= pa_vsprintf_malloc(format
, ap
);
369 pa_ioline_puts(s
, t
);