]> code.delx.au - pulseaudio/blob - src/pulsecore/strbuf.c
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / strbuf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include "strbuf.h"
36
37 /* A chunk of the linked list that makes up the string */
38 struct chunk {
39 struct chunk *next;
40 size_t length;
41 };
42
43 #define CHUNK_TO_TEXT(c) ((char*) (c) + sizeof(struct chunk))
44
45 struct pa_strbuf {
46 size_t length;
47 struct chunk *head, *tail;
48 };
49
50 pa_strbuf *pa_strbuf_new(void) {
51 pa_strbuf *sb = pa_xmalloc(sizeof(pa_strbuf));
52 sb->length = 0;
53 sb->head = sb->tail = NULL;
54 return sb;
55 }
56
57 void pa_strbuf_free(pa_strbuf *sb) {
58 assert(sb);
59 while (sb->head) {
60 struct chunk *c = sb->head;
61 sb->head = sb->head->next;
62 pa_xfree(c);
63 }
64
65 pa_xfree(sb);
66 }
67
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) {
71 char *t, *e;
72 struct chunk *c;
73 assert(sb);
74
75 e = t = pa_xmalloc(sb->length+1);
76
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);
80 e += c->length;
81 }
82
83 /* Trailing NUL */
84 *e = 0;
85
86 assert(e == t+sb->length);
87
88 return t;
89 }
90
91 /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
92 char *pa_strbuf_tostring_free(pa_strbuf *sb) {
93 char *t;
94 assert(sb);
95 t = pa_strbuf_tostring(sb);
96 pa_strbuf_free(sb);
97 return t;
98 }
99
100 /* Append a string to the string buffer */
101 void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
102 assert(sb && t);
103 pa_strbuf_putsn(sb, t, strlen(t));
104 }
105
106 /* Append a new chunk to the linked list */
107 static void append(pa_strbuf *sb, struct chunk *c) {
108 assert(sb && c);
109
110 if (sb->tail) {
111 assert(sb->head);
112 sb->tail->next = c;
113 } else {
114 assert(!sb->head);
115 sb->head = c;
116 }
117
118 sb->tail = c;
119 sb->length += c->length;
120 c->next = NULL;
121 }
122
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) {
125 struct chunk *c;
126 assert(sb && t);
127
128 if (!l)
129 return;
130
131 c = pa_xmalloc(sizeof(struct chunk)+l);
132 c->length = l;
133 memcpy(CHUNK_TO_TEXT(c), t, l);
134
135 append(sb, c);
136 }
137
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, ...) {
141 int size = 100;
142 struct chunk *c = NULL;
143
144 assert(sb);
145
146 for(;;) {
147 va_list ap;
148 int r;
149
150 c = pa_xrealloc(c, sizeof(struct chunk)+size);
151
152 va_start(ap, format);
153 r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);
154 va_end(ap);
155
156 if (r > -1 && r < size) {
157 c->length = r;
158 append(sb, c);
159 return r;
160 }
161
162 if (r > -1) /* glibc 2.1 */
163 size = r+1;
164 else /* glibc 2.0 */
165 size *= 2;
166 }
167 }