]>
code.delx.au - pulseaudio/blob - src/pulsecore/strbuf.c
4 This file is part of PulseAudio.
6 PulseAudio 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 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/types.h>
33 #include <pulse/xmalloc.h>
37 /* A chunk of the linked list that makes up the string */
43 #define CHUNK_TO_TEXT(c) ((char*) (c) + sizeof(struct chunk))
47 struct chunk
*head
, *tail
;
50 pa_strbuf
*pa_strbuf_new(void) {
51 pa_strbuf
*sb
= pa_xmalloc(sizeof(pa_strbuf
));
53 sb
->head
= sb
->tail
= NULL
;
57 void pa_strbuf_free(pa_strbuf
*sb
) {
60 struct chunk
*c
= sb
->head
;
61 sb
->head
= sb
->head
->next
;
68 /* Make a C string from the string buffer. The caller has to free
69 * string with pa_xfree(). */
70 char *pa_strbuf_tostring(pa_strbuf
*sb
) {
75 e
= t
= pa_xmalloc(sb
->length
+1);
77 for (c
= sb
->head
; c
; c
= c
->next
) {
78 assert((size_t) (e
-t
) <= sb
->length
);
79 memcpy(e
, CHUNK_TO_TEXT(c
), c
->length
);
86 assert(e
== t
+sb
->length
);
91 /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
92 char *pa_strbuf_tostring_free(pa_strbuf
*sb
) {
95 t
= pa_strbuf_tostring(sb
);
100 /* Append a string to the string buffer */
101 void pa_strbuf_puts(pa_strbuf
*sb
, const char *t
) {
103 pa_strbuf_putsn(sb
, t
, strlen(t
));
106 /* Append a new chunk to the linked list */
107 static void append(pa_strbuf
*sb
, struct chunk
*c
) {
119 sb
->length
+= c
->length
;
123 /* Append up to l bytes of a string to the string buffer */
124 void pa_strbuf_putsn(pa_strbuf
*sb
, const char *t
, size_t l
) {
131 c
= pa_xmalloc(sizeof(struct chunk
)+l
);
133 memcpy(CHUNK_TO_TEXT(c
), t
, l
);
138 /* Append a printf() style formatted string to the string buffer. */
139 /* The following is based on an example from the GNU libc documentation */
140 int pa_strbuf_printf(pa_strbuf
*sb
, const char *format
, ...) {
142 struct chunk
*c
= NULL
;
150 c
= pa_xrealloc(c
, sizeof(struct chunk
)+size
);
152 va_start(ap
, format
);
153 r
= vsnprintf(CHUNK_TO_TEXT(c
), size
, format
, ap
);
156 if (r
> -1 && r
< size
) {
162 if (r
> -1) /* glibc 2.1 */