summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitri Pal <dpal@redhat.com>2010-07-28 16:44:09 -0400
committerStephen Gallagher <sgallagh@redhat.com>2010-09-22 14:57:53 -0400
commit49ea94d1755e9003cbc2cba99c218cdd0a391813 (patch)
tree460d3d868c4f3d0a39f104ba7f947831e52ec52a
parent85c26abd9dabc0f192fe9bb695c3aac1493a9e34 (diff)
downloadding-libs-49ea94d1755e9003cbc2cba99c218cdd0a391813.tar.gz
ding-libs-49ea94d1755e9003cbc2cba99c218cdd0a391813.tar.xz
ding-libs-49ea94d1755e9003cbc2cba99c218cdd0a391813.zip
The beginning of the new INI interface
* ini_config_priv.h - private header * ini_configobj.h - future public header for the new interface * ini_configobj.c - just constructor/decructor for now * ini_serialize.c - code to serialize the configuration into a buffer so it can be written somewhere.
-rw-r--r--ini/ini_config_priv.h45
-rw-r--r--ini/ini_configobj.c114
-rw-r--r--ini/ini_configobj.h45
-rw-r--r--ini/ini_serialize.c88
4 files changed, 292 insertions, 0 deletions
diff --git a/ini/ini_config_priv.h b/ini/ini_config_priv.h
new file mode 100644
index 0000000..9dff6cd
--- /dev/null
+++ b/ini/ini_config_priv.h
@@ -0,0 +1,45 @@
+/*
+ INI LIBRARY
+
+ Header for the internal structures used by INI interface.
+
+ Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
+
+ INI Library 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 3 of the License, or
+ (at your option) any later version.
+
+ INI Library 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 Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with INI Library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef INI_CONFIG_PRIV_H
+#define INI_CONFIG_PRIV_H
+
+#include "collection.h"
+
+/* Configuration object */
+struct configobj {
+ /* For now just a collection */
+ struct collection_item *cfg;
+ /* Boundary ? */
+ /*... */
+ /* Statistics? Timestamps? When created? Modified? - TBD */
+ /*... */
+};
+
+/* Internal cleanup callback */
+void ini_cleanup_cb(const char *property,
+ int property_len,
+ int type,
+ void *data,
+ int length,
+ void *custom_data);
+
+#endif
diff --git a/ini/ini_configobj.c b/ini/ini_configobj.c
new file mode 100644
index 0000000..e75f093
--- /dev/null
+++ b/ini/ini_configobj.c
@@ -0,0 +1,114 @@
+/*
+ INI LIBRARY
+
+ Module represents interface to the main INI object.
+
+ Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
+
+ INI Library 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 3 of the License, or
+ (at your option) any later version.
+
+ INI Library 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 Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with INI Library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include "config.h"
+#include "trace.h"
+#include "collection.h"
+#include "ini_config_priv.h"
+#include "ini_defines.h"
+#include "ini_valueobj.h"
+
+/* This constant belongs to ini_defines.h. Move from ini_config - TBD */
+#define COL_CLASS_INI_BASE 20000
+#define COL_CLASS_INI_CONFIG COL_CLASS_INI_BASE + 0
+
+/* Callback */
+void ini_cleanup_cb(const char *property,
+ int property_len,
+ int type,
+ void *data,
+ int length,
+ void *custom_data)
+{
+ struct value_obj *vo = NULL;
+
+ TRACE_FLOW_ENTRY();
+
+ /* Banary items are the values */
+ if(type == COL_TYPE_BINARY) {
+ vo = *((struct value_obj **)(data));
+ value_destroy(vo);
+ }
+
+ TRACE_FLOW_EXIT();
+}
+
+/* Traverse the collection and clean the object */
+void ini_config_destroy(struct configobj *ini_config)
+{
+ TRACE_FLOW_ENTRY();
+
+ if (ini_config) {
+ if(ini_config->cfg) {
+
+ col_destroy_collection_with_cb(ini_config->cfg,
+ ini_cleanup_cb,
+ NULL);
+ }
+ free(ini_config);
+ }
+
+ TRACE_FLOW_EXIT();
+}
+
+/* Create a config object */
+int ini_config_create(struct configobj **ini_config)
+{
+ int error = EOK;
+ struct configobj *new_co = NULL;
+
+ TRACE_FLOW_ENTRY();
+
+ if (!ini_config) {
+ TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
+ return EINVAL;
+ }
+
+ errno = 0;
+ new_co = malloc(sizeof(struct configobj));
+ if (!new_co) {
+ error = errno;
+ TRACE_ERROR_NUMBER("Failed to allocate memory", ENOMEM);
+ return ENOMEM;
+ }
+
+ new_co->cfg = NULL;
+
+ /* Create a collection to hold configuration data */
+ error = col_create_collection(&(new_co->cfg),
+ INI_CONFIG_NAME,
+ COL_CLASS_INI_CONFIG);
+ if (error != EOK) {
+ TRACE_ERROR_NUMBER("Failed to create collection.", error);
+ ini_config_destroy(new_co);
+ return error;
+ }
+
+ *ini_config = new_co;
+
+ TRACE_FLOW_EXIT();
+ return error;
+}
diff --git a/ini/ini_configobj.h b/ini/ini_configobj.h
new file mode 100644
index 0000000..21f9754
--- /dev/null
+++ b/ini/ini_configobj.h
@@ -0,0 +1,45 @@
+/*
+ INI LIBRARY
+
+ Header file for the ini collection object.
+
+ Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
+
+ INI Library 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 3 of the License, or
+ (at your option) any later version.
+
+ INI Library 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 Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with INI Library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef INI_CONFIGOBJ_H
+#define INI_CONFIGOBJ_H
+
+#include <stdio.h>
+#include "simplebuffer.h"
+
+/********************************************************************/
+/* THIS IS A BEGINNING OF THE THE NEW CONFIG OBJECT INTERFACE - TBD */
+/********************************************************************/
+
+struct configobj;
+
+/* Create a configuration object */
+int ini_config_create(struct configobj **ini_config);
+
+/* Destroy a configuration object */
+void ini_config_destroy(struct configobj *ini_config);
+
+/* Serialize configuration object into provided buffer */
+int ini_serialize_config(struct configobj *ini_config,
+ struct simplebuffer *sbobj);
+
+
+#endif
diff --git a/ini/ini_serialize.c b/ini/ini_serialize.c
new file mode 100644
index 0000000..542da98
--- /dev/null
+++ b/ini/ini_serialize.c
@@ -0,0 +1,88 @@
+/*
+ INI LIBRARY
+
+ Module contains functions to serialize configuration object.
+
+ Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
+
+ INI Library 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 3 of the License, or
+ (at your option) any later version.
+
+ INI Library 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 Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with INI Library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include "config.h"
+#include "simplebuffer.h"
+#include "collection.h"
+#include "ini_valueobj.h"
+#include "ini_defines.h"
+#include "ini_config_priv.h"
+#include "trace.h"
+
+/* Callback */
+static int ini_serialize_cb(const char *property,
+ int property_len,
+ int type,
+ void *data,
+ int length,
+ void *custom_data,
+ int *stop)
+{
+ int error = EOK;
+ struct simplebuffer *sbobj;
+ struct value_obj *vo;
+
+ TRACE_FLOW_ENTRY();
+
+ /* Banary items are the values */
+ if(type == COL_TYPE_BINARY) {
+ sbobj = (struct simplebuffer *)custom_data;
+ vo = *((struct value_obj **)(data));
+ error = value_serialize(vo, property, sbobj);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to serizlize value", error);
+ *stop = 1;
+ }
+ }
+
+ TRACE_FLOW_EXIT();
+ return error;
+}
+
+/* Traverse the collection and build the serialization object */
+int ini_serialize_config(struct configobj *ini_config,
+ struct simplebuffer *sbobj)
+{
+ int error = EOK;
+ TRACE_FLOW_ENTRY();
+
+ if (!ini_config) {
+ TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
+ return EINVAL;
+ }
+
+ if (ini_config->cfg) {
+ error = col_traverse_collection(ini_config->cfg,
+ COL_TRAVERSE_DEFAULT,
+ ini_serialize_cb,
+ (void *)sbobj);
+ }
+
+ TRACE_INFO_NUMBER("Serialization returned:", error);
+
+ TRACE_FLOW_EXIT();
+ return error;
+}