summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 363e5bd)
raw | patch | inline | side by side (parent: 363e5bd)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 Mar 2013 05:54:20 +0000 (22:54 -0700) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 Mar 2013 05:54:20 +0000 (22:54 -0700) |
Whenever writing to the buffer, it will ensure that enough space is allocated
to store all of the string.
to store all of the string.
src/Makefile.am | patch | blob | history | |
src/include/utils/strbuf.h | [new file with mode: 0644] | patch | blob |
src/utils/strbuf.c | [new file with mode: 0644] | patch | blob |
diff --git a/src/Makefile.am b/src/Makefile.am
index b754beb962b4a50a333c01dafc4f1483afc0c129..9876de64a7dc1ab52a84460f6c7ca3eac146b8f0 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
include/utils/data.h \
utils/error.c include/utils/error.h \
utils/llist.c include/utils/llist.h \
+ utils/strbuf.c include/utils/strbuf.h \
utils/time.c include/utils/time.h \
utils/unixsock.c include/utils/unixsock.h
libsysdb_la_CFLAGS = $(AM_CFLAGS)
diff --git a/src/include/utils/strbuf.h b/src/include/utils/strbuf.h
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * SysDB - src/include/utils/strbuf.h
+ * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * SysDB string buffer:
+ * This is an implementation of an automatically growing string. Whenever
+ * writing to the buffer, it will be ensured that enough space is allocated to
+ * store all of the string.
+ */
+
+#ifndef SDB_UTILS_STRBUF_H
+#define SDB_UTILS_STRBUF_H 1
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct sdb_strbuf sdb_strbuf_t;
+
+/*
+ * sdb_strbuf_create, sdb_strbuf_destroy:
+ * Allocate / deallocate string buffer objects. The initial size of a newly
+ * created string buffer is determined by the 'size' argument of the create
+ * function.
+ *
+ * sdb_strbuf_create returns:
+ * - the new string buffer object on success
+ * - NULL else
+ */
+sdb_strbuf_t *
+sdb_strbuf_create(size_t size);
+
+void
+sdb_strbuf_destroy(sdb_strbuf_t *strbuf);
+
+/*
+ * sdb_strbuf_vappend, sdb_strbuf_append:
+ * Append to an existing string buffer. The new text will be added at the end
+ * of the current content of the buffer.
+ *
+ * The 'fmt' and all following arguments are identical to those passed to the
+ * sprintf / vsprintf functions.
+ *
+ * Returns:
+ * - the number of bytes written
+ * - a negative value on error
+ */
+ssize_t
+sdb_strbuf_vappend(sdb_strbuf_t *strbuf, const char *fmt, va_list ap);
+ssize_t
+sdb_strbuf_append(sdb_strbuf_t *strbuf, const char *fmt, ...);
+
+/*
+ * sdb_strbuf_vsprintf, sdb_strbuf_sprintf:
+ * Write to an existing string buffer, overwriting any previous content.
+ *
+ * The 'fmt' and all following arguments are identical to those passed to the
+ * sprintf / vsprintf functions.
+ *
+ * Returns:
+ * - the number of bytes written
+ * - a negative value on error
+ */
+ssize_t
+sdb_strbuf_vsprintf(sdb_strbuf_t *strbuf, const char *fmt, va_list ap);
+ssize_t
+sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...);
+
+/*
+ * sdb_strbuf_string:
+ * Returns the content of the string buffer. The caller may not modify the
+ * string.
+ */
+const char *
+sdb_strbuf_string(sdb_strbuf_t *strbuf);
+
+/*
+ * sdb_strbuf_len:
+ * Returns the length of the string buffer's content.
+ */
+size_t
+sdb_strbuf_len(sdb_strbuf_t *strbuf);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* ! SDB_UTILS_STRBUF_H */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/src/utils/strbuf.c b/src/utils/strbuf.c
--- /dev/null
+++ b/src/utils/strbuf.c
@@ -0,0 +1,192 @@
+/*
+ * SysDB - src/utils/strbuf.c
+ * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "utils/strbuf.h"
+
+#include <assert.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+/*
+ * private data structures
+ */
+
+struct sdb_strbuf {
+ char *string;
+ size_t size;
+ size_t pos;
+};
+
+/*
+ * private helper functions
+ */
+
+static int
+strbuf_resize(sdb_strbuf_t *strbuf)
+{
+ char *tmp;
+
+ assert(strbuf->size);
+
+ tmp = realloc(strbuf->string, 2 * strbuf->size);
+ if (! tmp)
+ return -1;
+
+ strbuf->string = tmp;
+ strbuf->size *= 2;
+ return 0;
+} /* strbuf_resize */
+
+/*
+ * public API
+ */
+
+sdb_strbuf_t *
+sdb_strbuf_create(size_t size)
+{
+ sdb_strbuf_t *strbuf;
+
+ if (! size)
+ return NULL;
+
+ strbuf = calloc(1, sizeof(*strbuf));
+ if (! strbuf)
+ return NULL;
+
+ strbuf->string = malloc(size);
+ if (! strbuf->string) {
+ free(strbuf);
+ return NULL;
+ }
+
+ strbuf->string[0] = '\0';
+ strbuf->size = size;
+ strbuf->pos = 0;
+
+ return strbuf;
+} /* sdb_strbuf_create */
+
+void
+sdb_strbuf_destroy(sdb_strbuf_t *strbuf)
+{
+ if (! strbuf)
+ return;
+
+ free(strbuf->string);
+ free(strbuf);
+} /* sdb_strbuf_destroy */
+
+ssize_t
+sdb_strbuf_vappend(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
+{
+ int status;
+
+ if ((! strbuf) || (! fmt))
+ return -1;
+
+ assert(strbuf->string[strbuf->pos] == '\0');
+
+ if (strbuf->pos >= strbuf->size)
+ if (strbuf_resize(strbuf))
+ return -1;
+
+ status = vsnprintf(strbuf->string + strbuf->pos,
+ strbuf->size - strbuf->pos, fmt, ap);
+
+ if (status < 0)
+ return status;
+
+ if ((size_t)status >= strbuf->size - strbuf->pos) {
+ strbuf_resize(strbuf);
+
+ /* reset string and try again */
+ strbuf->string[strbuf->pos] = '\0';
+ return sdb_strbuf_vappend(strbuf, fmt, ap);
+ }
+
+ strbuf->pos += (size_t)status;
+ return (ssize_t)status;
+} /* sdb_strbuf_vappend */
+
+ssize_t
+sdb_strbuf_append(sdb_strbuf_t *strbuf, const char *fmt, ...)
+{
+ va_list ap;
+ ssize_t status;
+
+ va_start(ap, fmt);
+ status = sdb_strbuf_vappend(strbuf, fmt, ap);
+ va_end(ap);
+
+ return status;
+} /* sdb_strbuf_append */
+
+ssize_t
+sdb_strbuf_vsprintf(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
+{
+ if (! strbuf)
+ return -1;
+
+ strbuf->string[0] = '\0';
+ strbuf->pos = 0;
+
+ return sdb_strbuf_vappend(strbuf, fmt, ap);
+} /* sdb_strbuf_vsprintf */
+
+ssize_t
+sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...)
+{
+ va_list ap;
+ ssize_t status;
+
+ va_start(ap, fmt);
+ status = sdb_strbuf_vsprintf(strbuf, fmt, ap);
+ va_end(ap);
+
+ return status;
+} /* sdb_strbuf_sprintf */
+
+const char *
+sdb_strbuf_string(sdb_strbuf_t *strbuf)
+{
+ if (! strbuf)
+ return NULL;
+ return strbuf->string;
+} /* sdb_strbuf_string */
+
+size_t
+sdb_strbuf_len(sdb_strbuf_t *strbuf)
+{
+ if (! strbuf)
+ return 0;
+ return strbuf->pos;
+} /* sdb_strbuf_string */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+