settings: Extract section and key/value pair types and helper functions
authorTobias Brunner <tobias@strongswan.org>
Fri, 7 Mar 2014 16:13:31 +0000 (17:13 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 15 May 2014 09:28:06 +0000 (11:28 +0200)
This allows us to use them in the upcoming parser.

src/libstrongswan/Android.mk
src/libstrongswan/Makefile.am
src/libstrongswan/settings/settings_types.c [new file with mode: 0644]
src/libstrongswan/settings/settings_types.h [new file with mode: 0644]

index ccff30b..9dfd264 100644 (file)
@@ -32,9 +32,9 @@ networking/streams/stream_service.c networking/streams/stream_manager.c \
 pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c \
 processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \
 processing/watcher.c resolver/resolver_manager.c resolver/rr_set.c \
-selectors/traffic_selector.c settings/settings.c \
-threading/thread.c threading/thread_value.c \
-threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \
+selectors/traffic_selector.c settings/settings.c settings/settings_types.c \
+threading/thread.c threading/thread_value.c threading/mutex.c \
+threading/semaphore.c threading/rwlock.c threading/spinlock.c \
 utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \
 utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \
 utils/parser_helper.c utils/test.c utils/utils/strerror.c \
index e7f5ab0..de2c950 100644 (file)
@@ -30,13 +30,17 @@ networking/streams/stream_service.c networking/streams/stream_manager.c \
 pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c \
 processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \
 processing/watcher.c resolver/resolver_manager.c resolver/rr_set.c \
-selectors/traffic_selector.c settings/settings.c \
-threading/thread.c threading/thread_value.c \
-threading/mutex.c threading/semaphore.c threading/rwlock.c threading/spinlock.c \
+selectors/traffic_selector.c settings/settings.c settings/settings_types.c \
+threading/thread.c threading/thread_value.c threading/mutex.c \
+threading/semaphore.c threading/rwlock.c threading/spinlock.c \
 utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \
 utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \
 utils/parser_helper.c utils/test.c utils/utils/strerror.c
 
+# private header files
+noinst_HEADERS = \
+settings/settings_types.h
+
 if USE_DEV_HEADERS
 strongswan_includedir = ${dev_headers}
 nobase_strongswan_include_HEADERS = \
diff --git a/src/libstrongswan/settings/settings_types.c b/src/libstrongswan/settings/settings_types.c
new file mode 100644 (file)
index 0000000..015fd3f
--- /dev/null
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2010-2014 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program 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.
+ */
+
+#include "settings_types.h"
+
+/*
+ * Described in header
+ */
+kv_t *settings_kv_create(char *key, char *value)
+{
+       kv_t *this;
+
+       INIT(this,
+               .key = key,
+               .value = value,
+       );
+       return this;
+}
+
+/*
+ * Described in header
+ */
+void settings_kv_destroy(kv_t *this)
+{
+       free(this->key);
+       free(this->value);
+       free(this);
+}
+
+/*
+ * Described in header
+ */
+section_t *settings_section_create(char *name)
+{
+       section_t *this;
+
+       INIT(this,
+               .name = name,
+       );
+       return this;
+}
+
+/*
+ * Described in header
+ */
+void settings_section_destroy(section_t *this)
+{
+       array_destroy_function(this->sections, (void*)settings_section_destroy, NULL);
+       array_destroy_function(this->kv, (void*)settings_kv_destroy, NULL);
+       array_destroy(this->fallbacks);
+       free(this->name);
+       free(this);
+}
+
+/*
+ * Described in header
+ */
+void settings_section_extend(section_t *base, section_t *extension)
+{
+       enumerator_t *enumerator;
+       section_t *sec;
+       kv_t *kv;
+
+       enumerator = array_create_enumerator(extension->sections);
+       while (enumerator->enumerate(enumerator, (void**)&sec))
+       {
+               section_t *found;
+               if (array_bsearch(base->sections, sec->name, settings_section_find,
+                       &found) != -1)
+               {
+                       settings_section_extend(found, sec);
+               }
+               else
+               {
+                       array_remove_at(extension->sections, enumerator);
+                       array_insert_create(&base->sections, ARRAY_TAIL, sec);
+                       array_sort(base->sections, settings_section_sort, NULL);
+               }
+       }
+       enumerator->destroy(enumerator);
+
+       enumerator = array_create_enumerator(extension->kv);
+       while (enumerator->enumerate(enumerator, (void**)&kv))
+       {
+               kv_t *found;
+               if (array_bsearch(base->kv, kv->key, settings_kv_find, &found) != -1)
+               {
+                       free(found->value);
+                       found->value = kv->value;
+                       kv->value = NULL;
+               }
+               else
+               {
+                       array_remove_at(extension->kv, enumerator);
+                       array_insert_create(&base->kv, ARRAY_TAIL, kv);
+                       array_sort(base->kv, settings_kv_sort, NULL);
+               }
+       }
+       enumerator->destroy(enumerator);
+}
+
+/*
+ * Described in header
+ */
+int settings_section_find(const void *a, const void *b)
+{
+       const char *key = a;
+       const section_t *item = b;
+       return strcmp(key, item->name);
+}
+
+/*
+ * Described in header
+ */
+int settings_section_sort(const void *a, const void *b, void *user)
+{
+       const section_t *sa = a, *sb = b;
+       return strcmp(sa->name, sb->name);
+}
+
+/*
+ * Described in header
+ */
+int settings_kv_find(const void *a, const void *b)
+{
+       const char *key = a;
+       const kv_t *item = b;
+       return strcmp(key, item->key);
+}
+
+/*
+ * Described in header
+ */
+int settings_kv_sort(const void *a, const void *b, void *user)
+{
+       const kv_t *kva = a, *kvb = b;
+       return strcmp(kva->key, kvb->key);
+}
diff --git a/src/libstrongswan/settings/settings_types.h b/src/libstrongswan/settings/settings_types.h
new file mode 100644 (file)
index 0000000..79e8dbf
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2010-2014 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program 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.
+ */
+
+/**
+ * Internal data types and functions shared between the parser and t.
+ *
+ * @defgroup settings_types settings_types
+ * @{ @ingroup settings
+ */
+
+#ifndef SETTINGS_TYPES_H_
+#define SETTINGS_TYPES_H_
+
+typedef struct kv_t kv_t;
+typedef struct section_t section_t;
+
+#include "collections/array.h"
+
+/**
+ * Key/value pair.
+ */
+struct kv_t {
+
+       /**
+        * Key string, relative, not the full name.
+        */
+       char *key;
+
+       /**
+        * Value as string.
+        */
+       char *value;
+};
+
+/**
+ * Section containing subsections and key value pairs.
+ */
+struct section_t {
+
+       /**
+        * Name of the section.
+        */
+       char *name;
+
+       /**
+        * Fallback sections, as section_t.
+        */
+       array_t *fallbacks;
+
+       /**
+        * Subsections, as section_t.
+        */
+       array_t *sections;
+
+       /**
+        * Key value pairs, as kv_t.
+        */
+       array_t *kv;
+};
+
+/**
+ * Create a key/value pair.
+ *
+ * @param key          key (gets adopted)
+ * @param value                value (gets adopted)
+ * @return                     allocated key/value pair
+ */
+kv_t *settings_kv_create(char *key, char *value);
+
+/**
+ * Destroy a key/value pair.
+ *
+ * @param this         key/value pair to destroy
+ */
+void settings_kv_destroy(kv_t *this);
+
+/**
+ * Create a section with the given name.
+ *
+ * @param name         name (gets adopted)
+ * @return                     allocated section
+ */
+section_t *settings_section_create(char *name);
+
+/**
+ * Destroy a section.
+ *
+ * @param this         section to destroy
+ */
+void settings_section_destroy(section_t *this);
+
+/**
+ * Extend the first section with the values and sub-sections of the second
+ * section, from where they are consequently moved/removed (but there might
+ * still remain some leftovers).
+ *
+ * @param base         base section to extend
+ * @param extension    section whose data is extracted
+ */
+void settings_section_extend(section_t *base, section_t *extension);
+
+/**
+ * Callback to find a section by name
+ */
+int settings_section_find(const void *a, const void *b);
+
+/**
+ * Callback to sort sections by name
+ */
+int settings_section_sort(const void *a, const void *b, void *user);
+
+/**
+ * Callback to find a key/value pair by key
+ */
+int settings_kv_find(const void *a, const void *b);
+
+/**
+ * Callback to sort kv pairs by key
+ */
+int settings_kv_sort(const void *a, const void *b, void *user);
+
+#endif /** SETTINGS_TYPES_H_ @}*/