Code

strings utilities: Add helper functions for common string operations.
authorSebastian Harl <sh@tokkee.org>
Fri, 9 Sep 2016 02:29:27 +0000 (22:29 -0400)
committerSebastian Harl <sh@tokkee.org>
Fri, 9 Sep 2016 02:29:27 +0000 (22:29 -0400)
src/Makefile.am
src/core/timeseries.c
src/include/utils/strings.h [new file with mode: 0644]
src/utils/strings.c [new file with mode: 0644]

index 9edaf4b..0a6c8b3 100644 (file)
@@ -45,6 +45,7 @@ pkgutilsinclude_HEADERS = \
                include/utils/proto.h \
                include/utils/ssl.h \
                include/utils/strbuf.h \
+               include/utils/strings.h \
                include/utils/unixsock.h
 
 pkgclientincludedir = $(pkgincludedir)/client
@@ -62,7 +63,8 @@ libsysdbclient_la_SOURCES = \
                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 \
@@ -105,6 +107,7 @@ libsysdb_la_SOURCES = \
                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)
index 01854aa..9574ee8 100644 (file)
 
 #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
  */
@@ -86,7 +50,7 @@ sdb_timeseries_info_create(size_t data_names_len, const char * const *data_names
        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;
@@ -100,7 +64,7 @@ sdb_timeseries_info_destroy(sdb_timeseries_info_t *ts_info)
        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 */
 
@@ -115,7 +79,7 @@ sdb_timeseries_create(size_t data_names_len, const char * const *data_names,
        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;
@@ -156,7 +120,7 @@ sdb_timeseries_destroy(sdb_timeseries_t *ts)
        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
new file mode 100644 (file)
index 0000000..61298ee
--- /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
new file mode 100644 (file)
index 0000000..c07cfa1
--- /dev/null
@@ -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 : */
+