summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d67b2c8)
raw | patch | inline | side by side (parent: d67b2c8)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 9 Sep 2016 02:29:27 +0000 (22:29 -0400) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 9 Sep 2016 02:29:27 +0000 (22:29 -0400) |
src/Makefile.am | patch | blob | history | |
src/core/timeseries.c | patch | blob | history | |
src/include/utils/strings.h | [new file with mode: 0644] | patch | blob |
src/utils/strings.c | [new file with mode: 0644] | patch | blob |
diff --git a/src/Makefile.am b/src/Makefile.am
index 9edaf4b1ec8702c5bc9d978bdcd32e231cce4653..0a6c8b33ec13038582e99124c1b15d7bef95d7f1 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
include/utils/proto.h \
include/utils/ssl.h \
include/utils/strbuf.h \
+ include/utils/strings.h \
include/utils/unixsock.h
pkgclientincludedir = $(pkgincludedir)/client
utils/error.c include/utils/error.h \
utils/proto.c include/utils/proto.h \
utils/ssl.c include/utils/ssl.h \
- utils/strbuf.c include/utils/strbuf.h
+ utils/strbuf.c include/utils/strbuf.h \
+ utils/strings.c include/utils/strings.h
libsysdbclient_la_CFLAGS = $(AM_CFLAGS) @OPENSSL_CFLAGS@
libsysdbclient_la_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL)
libsysdbclient_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0 \
utils/proto.c include/utils/proto.h \
utils/ssl.c include/utils/ssl.h \
utils/strbuf.c include/utils/strbuf.h \
+ utils/strings.c include/utils/strings.h \
utils/unixsock.c include/utils/unixsock.h
libsysdb_la_CFLAGS = $(AM_CFLAGS) @OPENSSL_CFLAGS@
libsysdb_la_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL)
diff --git a/src/core/timeseries.c b/src/core/timeseries.c
index 01854aa5779778f42e4bbdaf45bbad536a4e40d9..9574ee8a75fd13b188b091af15ad2e52654c5803 100644 (file)
--- a/src/core/timeseries.c
+++ b/src/core/timeseries.c
#include "sysdb.h"
#include "core/timeseries.h"
+#include "utils/strings.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
-static int
-copy_strings(char ***out, size_t *out_len,
- const char * const *in, size_t in_len)
-{
- size_t i;
-
- *out = calloc(in_len, sizeof(**out));
- if (! *out)
- return -1;
-
- *out_len = in_len;
- for (i = 0; i < in_len; ++i) {
- (*out)[i] = strdup(in[i]);
- if (! (*out)[i])
- return -1;
- }
- return 0;
-} /* copy_strings */
-
-static void
-free_strings(char ***strings, size_t *strings_len)
-{
- size_t i;
-
- if (*strings) {
- for (i = 0; i < *strings_len; ++i) {
- if ((*strings)[i])
- free((*strings)[i]);
- (*strings)[i] = NULL;
- }
- free(*strings);
- }
-
- *strings = NULL;
- *strings_len = 0;
-} /* free_strings */
-
/*
* public API
*/
if (! ts_info)
return NULL;
- if (copy_strings(&ts_info->data_names, &ts_info->data_names_len,
+ if (stringv_copy(&ts_info->data_names, &ts_info->data_names_len,
data_names, data_names_len)) {
sdb_timeseries_info_destroy(ts_info);
return NULL;
if (! ts_info)
return;
- free_strings(&ts_info->data_names, &ts_info->data_names_len);
+ stringv_free(&ts_info->data_names, &ts_info->data_names_len);
free(ts_info);
} /* sdb_timeseries_info_destroy */
if (! ts)
return NULL;
- if (copy_strings(&ts->data_names, &ts->data_names_len,
+ if (stringv_copy(&ts->data_names, &ts->data_names_len,
data_names, data_names_len)) {
sdb_timeseries_destroy(ts);
return NULL;
ts->data = NULL;
ts->data_len = 0;
- free_strings(&ts->data_names, &ts->data_names_len);
+ stringv_free(&ts->data_names, &ts->data_names_len);
free(ts);
} /* sdb_timeseries_destroy */
diff --git a/src/include/utils/strings.h b/src/include/utils/strings.h
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * SysDB - src/include/utils/strings.h
+ * Copyright (C) 2016 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.
+ */
+
+#ifndef SDB_UTILS_STRINGS_H
+#define SDB_UTILS_STRINGS_H 1
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * stringv_copy:
+ * Copy a string vector from 'src' to 'dst'. If non-NULL, 'dst' will be
+ * reallocated to fit the required size.
+ *
+ * Returns:
+ * - 0 on success
+ * - a negative value else
+ */
+int
+stringv_copy(char ***dst, size_t *dst_len,
+ const char * const *src, size_t src_len);
+
+/*
+ * stringv_free:
+ * Free the memory used by 's' and all of it's elements.
+ */
+void
+stringv_free(char ***s, size_t *s_len);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* ! SDB_UTILS_STRINGS_H */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/src/utils/strings.c b/src/utils/strings.c
--- /dev/null
+++ b/src/utils/strings.c
@@ -0,0 +1,88 @@
+/*
+ * SysDB - src/utils/strings.c
+ * Copyright (C) 2016 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.
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "utils/strings.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * public API
+ */
+
+int
+stringv_copy(char ***dst, size_t *dst_len,
+ const char * const *src, size_t src_len)
+{
+ char **tmp;
+ size_t i;
+
+ if (*dst) {
+ tmp = realloc(*dst, src_len * sizeof(*tmp));
+ if (tmp)
+ memset(tmp, 0, src_len * sizeof(*tmp));
+ }
+ else
+ tmp = calloc(src_len, sizeof(*tmp));
+
+ if (! tmp)
+ return -1;
+
+ *dst = tmp;
+ *dst_len = src_len;
+ for (i = 0; i < src_len; ++i) {
+ (*dst)[i] = strdup(src[i]);
+ if (! (*dst)[i])
+ return -1;
+ }
+ return 0;
+} /* stringv_copy */
+
+void
+stringv_free(char ***s, size_t *s_len)
+{
+ size_t i;
+
+ if (*s) {
+ for (i = 0; i < *s_len; ++i) {
+ if ((*s)[i])
+ free((*s)[i]);
+ (*s)[i] = NULL;
+ }
+ free(*s);
+ }
+
+ *s = NULL;
+ *s_len = 0;
+} /* stringv_free */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+