Use LGPL 2.1 on all files previously using LGPL 2
[pulseaudio-mirror.git] / src / pulsecore / strbuf.c
blob9f5a84b47a4c009ef947b582f66cc1b00ace6793
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.1 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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #include <pulse/xmalloc.h>
33 #include <pulsecore/macro.h>
35 #include "strbuf.h"
37 /* A chunk of the linked list that makes up the string */
38 struct chunk {
39 struct chunk *next;
40 size_t length;
43 #define CHUNK_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(struct chunk)))
45 struct pa_strbuf {
46 size_t length;
47 struct chunk *head, *tail;
50 pa_strbuf *pa_strbuf_new(void) {
51 pa_strbuf *sb;
53 sb = pa_xnew(pa_strbuf, 1);
54 sb->length = 0;
55 sb->head = sb->tail = NULL;
57 return sb;
60 void pa_strbuf_free(pa_strbuf *sb) {
61 pa_assert(sb);
63 while (sb->head) {
64 struct chunk *c = sb->head;
65 sb->head = sb->head->next;
66 pa_xfree(c);
69 pa_xfree(sb);
72 /* Make a C string from the string buffer. The caller has to free
73 * string with pa_xfree(). */
74 char *pa_strbuf_tostring(pa_strbuf *sb) {
75 char *t, *e;
76 struct chunk *c;
78 pa_assert(sb);
80 e = t = pa_xmalloc(sb->length+1);
82 for (c = sb->head; c; c = c->next) {
83 pa_assert((size_t) (e-t) <= sb->length);
84 memcpy(e, CHUNK_TO_TEXT(c), c->length);
85 e += c->length;
88 /* Trailing NUL */
89 *e = 0;
91 pa_assert(e == t+sb->length);
93 return t;
96 /* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
97 char *pa_strbuf_tostring_free(pa_strbuf *sb) {
98 char *t;
100 pa_assert(sb);
101 t = pa_strbuf_tostring(sb);
102 pa_strbuf_free(sb);
104 return t;
107 /* Append a string to the string buffer */
108 void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
110 pa_assert(sb);
111 pa_assert(t);
113 pa_strbuf_putsn(sb, t, strlen(t));
116 /* Append a new chunk to the linked list */
117 static void append(pa_strbuf *sb, struct chunk *c) {
118 pa_assert(sb);
119 pa_assert(c);
121 if (sb->tail) {
122 pa_assert(sb->head);
123 sb->tail->next = c;
124 } else {
125 pa_assert(!sb->head);
126 sb->head = c;
129 sb->tail = c;
130 sb->length += c->length;
131 c->next = NULL;
134 /* Append up to l bytes of a string to the string buffer */
135 void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
136 struct chunk *c;
138 pa_assert(sb);
139 pa_assert(t);
141 if (!l)
142 return;
144 c = pa_xmalloc(PA_ALIGN(sizeof(struct chunk)) + l);
145 c->length = l;
146 memcpy(CHUNK_TO_TEXT(c), t, l);
148 append(sb, c);
151 /* Append a printf() style formatted string to the string buffer. */
152 /* The following is based on an example from the GNU libc documentation */
153 size_t pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
154 size_t size = 100;
155 struct chunk *c = NULL;
157 pa_assert(sb);
158 pa_assert(format);
160 for(;;) {
161 va_list ap;
162 int r;
164 c = pa_xrealloc(c, PA_ALIGN(sizeof(struct chunk)) + size);
166 va_start(ap, format);
167 r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);
168 CHUNK_TO_TEXT(c)[size-1] = 0;
169 va_end(ap);
171 if (r > -1 && (size_t) r < size) {
172 c->length = (size_t) r;
173 append(sb, c);
174 return (size_t) r;
177 if (r > -1) /* glibc 2.1 */
178 size = (size_t) r+1;
179 else /* glibc 2.0 */
180 size *= 2;
184 pa_bool_t pa_strbuf_isempty(pa_strbuf *sb) {
185 pa_assert(sb);
187 return sb->length <= 0;