pulseaudio/polyp/strbuf.c
Lennart Poettering 0a2bbc528b * some commenting work
* add new field "read_only" to memory blocks
* add new API function pa_context_get_server()
* filter capture data through mcalign on client
* make module-tunnel use pa_socket_client_new_string() instead of using pa_resolve_server() directly.
* remove pa_resolve_server()
* remove debug.h and replace it by a macro definition on the gcc command line
* some strbuf cleanups
* small fixes in pa_stream for cleanup when server dies
* new CLI command "load-sample-dir-lazy"
* send FQDN as part of server info
* rework mcalign, this time with memory block merging
* fix iochannel cleanup when connection dies
* check getaddrinfo() results


git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@286 fefdeb5f-60dc-0310-8127-8f9354f1896f
2004-11-17 00:05:25 +00:00

177 lines
3.9 KiB
C

/* $Id$ */
/***
This file is part of polypaudio.
polypaudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
polypaudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with polypaudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <xmalloc.h>
#include "strbuf.h"
/* Some magic for zero-length arrays */
#ifdef __STDC_VERSION__
#if __STDC_VERSION__ >= 199901L
#ifndef STDC99
#define STDC99
#endif
#endif
#endif
/* A chunk of the linked list that makes up the string */
struct chunk {
struct chunk *next;
size_t length;
#ifdef STDC99
char text[];
#else
char text[0];
#endif
};
struct pa_strbuf {
size_t length;
struct chunk *head, *tail;
};
struct pa_strbuf *pa_strbuf_new(void) {
struct pa_strbuf *sb = pa_xmalloc(sizeof(struct pa_strbuf));
sb->length = 0;
sb->head = sb->tail = NULL;
return sb;
}
void pa_strbuf_free(struct pa_strbuf *sb) {
assert(sb);
while (sb->head) {
struct chunk *c = sb->head;
sb->head = sb->head->next;
pa_xfree(c);
}
pa_xfree(sb);
}
/* Make a C string from the string buffer. The caller has to free
* string with pa_xfree(). */
char *pa_strbuf_tostring(struct pa_strbuf *sb) {
char *t, *e;
struct chunk *c;
assert(sb);
t = pa_xmalloc(sb->length+1);
e = t;
for (c = sb->head; c; c = c->next) {
assert((size_t) (e-t) <= sb->length);
memcpy(e, c->text, c->length);
e += c->length;
}
/* Trailing NUL */
*e = 0;
return t;
}
/* Combination of pa_strbuf_free() and pa_strbuf_tostring() */
char *pa_strbuf_tostring_free(struct pa_strbuf *sb) {
char *t;
assert(sb);
t = pa_strbuf_tostring(sb);
pa_strbuf_free(sb);
return t;
}
/* Append a string to the string buffer */
void pa_strbuf_puts(struct pa_strbuf *sb, const char *t) {
assert(sb && t);
pa_strbuf_putsn(sb, t, strlen(t));
}
/* Append a new chunk to the linked list */
static void append(struct pa_strbuf *sb, struct chunk *c) {
assert(sb && c);
if (sb->tail) {
assert(sb->head);
sb->tail->next = c;
} else {
assert(!sb->head);
sb->head = c;
}
sb->tail = c;
sb->length += c->length;
c->next = NULL;
}
/* Append up to l bytes of a string to the string buffer */
void pa_strbuf_putsn(struct pa_strbuf *sb, const char *t, size_t l) {
struct chunk *c;
assert(sb && t);
if (!l)
return;
c = pa_xmalloc(sizeof(struct chunk)+l);
c->length = l;
memcpy(c->text, t, l);
append(sb, c);
}
/* Append a printf() style formatted string to the string buffer. */
/* The following is based on an example from the GNU libc documentation */
int pa_strbuf_printf(struct pa_strbuf *sb, const char *format, ...) {
int size = 100;
struct chunk *c = NULL;
assert(sb);
for(;;) {
va_list ap;
int r;
c = pa_xrealloc(c, sizeof(struct chunk)+size);
va_start(ap, format);
r = vsnprintf(c->text, size, format, ap);
va_end(ap);
if (r > -1 && r < size) {
c->length = r;
append(sb, c);
return r;
}
if (r > -1) /* glibc 2.1 */
size = r+1;
else /* glibc 2.0 */
size *= 2;
}
}