]>
code.delx.au - pulseaudio/blob - polyp/strbuf.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 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 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
26 #include <sys/types.h>
43 struct chunk
*head
, *tail
;
46 struct pa_strbuf
*pa_strbuf_new(void) {
47 struct pa_strbuf
*sb
= malloc(sizeof(struct pa_strbuf
));
50 sb
->head
= sb
->tail
= NULL
;
54 void pa_strbuf_free(struct pa_strbuf
*sb
) {
57 struct chunk
*c
= sb
->head
;
58 sb
->head
= sb
->head
->next
;
65 char *pa_strbuf_tostring(struct pa_strbuf
*sb
) {
70 t
= malloc(sb
->length
+1);
74 for (c
= sb
->head
; c
; c
= c
->next
) {
75 memcpy(e
, c
->text
, c
->length
);
84 char *pa_strbuf_tostring_free(struct pa_strbuf
*sb
) {
87 t
= pa_strbuf_tostring(sb
);
92 void pa_strbuf_puts(struct pa_strbuf
*sb
, const char *t
) {
94 pa_strbuf_putsn(sb
, t
, strlen(t
));
97 void pa_strbuf_putsn(struct pa_strbuf
*sb
, const char *t
, size_t l
) {
104 c
= malloc(sizeof(struct chunk
)+l
);
109 memcpy(c
->text
, t
, l
);
123 /* The following is based on an example from the GNU libc documentation */
125 int pa_strbuf_printf(struct pa_strbuf
*sb
, const char *format
, ...) {
127 struct chunk
*c
= NULL
;
134 c
= realloc(c
, sizeof(struct chunk
)+size
);
137 va_start(ap
, format
);
138 r
= vsnprintf(c
->text
, size
, format
, ap
);
141 if (r
> -1 && r
< size
) {
159 if (r
> -1) /* glibc 2.1 */