]> code.delx.au - pulseaudio/blob - src/pulsecore/strbuf.c
a3ddc1144c3cae330722668ab68418cdd072d59c
[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 <assert.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34
35 #include <pulse/xmalloc.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) + 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 = pa_xmalloc(sizeof(pa_strbuf));
54 sb->length = 0;
55 sb->head = sb->tail = NULL;
56 return sb;
57 }
58
59 void pa_strbuf_free(pa_strbuf *sb) {
60 assert(sb);
61 while (sb->head) {
62 struct chunk *c = sb->head;
63 sb->head = sb->head->next;
64 pa_xfree(c);
65 }
66
67 pa_xfree(sb);
68 }
69
70 /* Make a C string from the string buffer. The caller has to free
71 * string with pa_xfree(). */
72 char *pa_strbuf_tostring(pa_strbuf *sb) {
73 char *t, *e;
74 struct chunk *c;
75 assert(sb);
76
77 e = t = pa_xmalloc(sb->length+1);
78
79 for (c = sb->head; c; c = c->next) {
80 assert((size_t) (e-t) <= sb->length);
81 memcpy(e, CHUNK_TO_TEXT(c), c->length);
82 e += c->length;
83 }
84
85 /* Trailing NUL */
86 *e = 0;
87
88 assert(e == t+sb->length);
89
90 return t;
91 }
92
93 /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
94 char *pa_strbuf_tostring_free(pa_strbuf *sb) {
95 char *t;
96 assert(sb);
97 t = pa_strbuf_tostring(sb);
98 pa_strbuf_free(sb);
99 return t;
100 }
101
102 /* Append a string to the string buffer */
103 void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
104 assert(sb && t);
105 pa_strbuf_putsn(sb, t, strlen(t));
106 }
107
108 /* Append a new chunk to the linked list */
109 static void append(pa_strbuf *sb, struct chunk *c) {
110 assert(sb && c);
111
112 if (sb->tail) {
113 assert(sb->head);
114 sb->tail->next = c;
115 } else {
116 assert(!sb->head);
117 sb->head = c;
118 }
119
120 sb->tail = c;
121 sb->length += c->length;
122 c->next = NULL;
123 }
124
125 /* Append up to l bytes of a string to the string buffer */
126 void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
127 struct chunk *c;
128 assert(sb && t);
129
130 if (!l)
131 return;
132
133 c = pa_xmalloc(sizeof(struct chunk)+l);
134 c->length = l;
135 memcpy(CHUNK_TO_TEXT(c), t, l);
136
137 append(sb, c);
138 }
139
140 /* Append a printf() style formatted string to the string buffer. */
141 /* The following is based on an example from the GNU libc documentation */
142 int pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
143 int size = 100;
144 struct chunk *c = NULL;
145
146 assert(sb);
147
148 for(;;) {
149 va_list ap;
150 int r;
151
152 c = pa_xrealloc(c, sizeof(struct chunk)+size);
153
154 va_start(ap, format);
155 r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);
156 va_end(ap);
157
158 if (r > -1 && r < size) {
159 c->length = r;
160 append(sb, c);
161 return r;
162 }
163
164 if (r > -1) /* glibc 2.1 */
165 size = r+1;
166 else /* glibc 2.0 */
167 size *= 2;
168 }
169 }