]> code.delx.au - pulseaudio/blob - src/pulsecore/strbuf.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / strbuf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/macro.h>
36
37 #include "strbuf.h"
38
39 /* A chunk of the linked list that makes up the string */
40 struct chunk {
41 struct chunk *next;
42 size_t length;
43 };
44
45 #define CHUNK_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(struct chunk)))
46
47 struct pa_strbuf {
48 size_t length;
49 struct chunk *head, *tail;
50 };
51
52 pa_strbuf *pa_strbuf_new(void) {
53 pa_strbuf *sb;
54
55 sb = pa_xnew(pa_strbuf, 1);
56 sb->length = 0;
57 sb->head = sb->tail = NULL;
58
59 return sb;
60 }
61
62 void pa_strbuf_free(pa_strbuf *sb) {
63 pa_assert(sb);
64
65 while (sb->head) {
66 struct chunk *c = sb->head;
67 sb->head = sb->head->next;
68 pa_xfree(c);
69 }
70
71 pa_xfree(sb);
72 }
73
74 /* Make a C string from the string buffer. The caller has to free
75 * string with pa_xfree(). */
76 char *pa_strbuf_tostring(pa_strbuf *sb) {
77 char *t, *e;
78 struct chunk *c;
79
80 pa_assert(sb);
81
82 e = t = pa_xnew(char, sb->length+1);
83
84 for (c = sb->head; c; c = c->next) {
85 pa_assert((size_t) (e-t) <= sb->length);
86 memcpy(e, CHUNK_TO_TEXT(c), c->length);
87 e += c->length;
88 }
89
90 /* Trailing NUL */
91 *e = 0;
92
93 pa_assert(e == t+sb->length);
94
95 return t;
96 }
97
98 /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
99 char *pa_strbuf_tostring_free(pa_strbuf *sb) {
100 char *t;
101
102 pa_assert(sb);
103 t = pa_strbuf_tostring(sb);
104 pa_strbuf_free(sb);
105
106 return t;
107 }
108
109 /* Append a string to the string buffer */
110 void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
111
112 pa_assert(sb);
113 pa_assert(t);
114
115 pa_strbuf_putsn(sb, t, strlen(t));
116 }
117
118 /* Append a new chunk to the linked list */
119 static void append(pa_strbuf *sb, struct chunk *c) {
120 pa_assert(sb);
121 pa_assert(c);
122
123 if (sb->tail) {
124 pa_assert(sb->head);
125 sb->tail->next = c;
126 } else {
127 pa_assert(!sb->head);
128 sb->head = c;
129 }
130
131 sb->tail = c;
132 sb->length += c->length;
133 c->next = NULL;
134 }
135
136 /* Append up to l bytes of a string to the string buffer */
137 void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
138 struct chunk *c;
139
140 pa_assert(sb);
141 pa_assert(t);
142
143 if (!l)
144 return;
145
146 c = pa_xmalloc(PA_ALIGN(sizeof(struct chunk)) + l);
147 c->length = l;
148 memcpy(CHUNK_TO_TEXT(c), t, l);
149
150 append(sb, c);
151 }
152
153 /* Append a printf() style formatted string to the string buffer. */
154 /* The following is based on an example from the GNU libc documentation */
155 int pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
156 int size = 100;
157 struct chunk *c = NULL;
158
159 pa_assert(sb);
160 pa_assert(format);
161
162 for(;;) {
163 va_list ap;
164 int r;
165
166 c = pa_xrealloc(c, PA_ALIGN(sizeof(struct chunk)) + size);
167
168 va_start(ap, format);
169 r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);
170 CHUNK_TO_TEXT(c)[size-1] = 0;
171 va_end(ap);
172
173 if (r > -1 && r < size) {
174 c->length = r;
175 append(sb, c);
176 return r;
177 }
178
179 if (r > -1) /* glibc 2.1 */
180 size = r+1;
181 else /* glibc 2.0 */
182 size *= 2;
183 }
184 }