From c5e10054e0f1ce6c5a62cd66f7b202229c943042 Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Wed, 28 Jul 2010 16:44:09 -0400
Subject: [PATCH] [INI] 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.
---
 common/ini/{ini_parse.h => ini_config_priv.h} |   37 +++++---
 common/ini/ini_configobj.c                    |  114 +++++++++++++++++++++++++
 common/ini/{ini_parse.h => ini_configobj.h}   |   31 +++++---
 common/ini/ini_serialize.c                    |   88 +++++++++++++++++++
 4 files changed, 245 insertions(+), 25 deletions(-)
 copy common/ini/{ini_parse.h => ini_config_priv.h} (54%)
 create mode 100644 common/ini/ini_configobj.c
 copy common/ini/{ini_parse.h => ini_configobj.h} (52%)
 create mode 100644 common/ini/ini_serialize.c

diff --git a/common/ini/ini_parse.h b/common/ini/ini_config_priv.h
similarity index 54%
copy from common/ini/ini_parse.h
copy to common/ini/ini_config_priv.h
index 56a0db92edd235f9961210f869e74cce58daa7b5..9dff6cda6a56997f2f78a5648ab17357b9330a7d 100644
--- a/common/ini/ini_parse.h
+++ b/common/ini/ini_config_priv.h
@@ -1,7 +1,7 @@
 /*
     INI LIBRARY
 
-    Header file for the internal parsing functions.
+    Header for the internal structures used by INI interface.
 
     Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
 
@@ -19,18 +19,27 @@
     along with INI Library.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#ifndef INI_PARSE_H
-#define INI_PARSE_H
-
-#include <stdio.h>
-
-/* Internal function to read line from INI file */
-int read_line(FILE *file,
-              char *buf,
-              int read_size,
-              char **key,
-              char **value,
-              int *length,
-              int *ext_error);
+#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/common/ini/ini_configobj.c b/common/ini/ini_configobj.c
new file mode 100644
index 0000000000000000000000000000000000000000..e75f093fb3a55436f92f328f46eb063dae4e777c
--- /dev/null
+++ b/common/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/common/ini/ini_parse.h b/common/ini/ini_configobj.h
similarity index 52%
copy from common/ini/ini_parse.h
copy to common/ini/ini_configobj.h
index 56a0db92edd235f9961210f869e74cce58daa7b5..21f9754a0279d2db3b101eb6c581826d0b0f0d29 100644
--- a/common/ini/ini_parse.h
+++ b/common/ini/ini_configobj.h
@@ -1,7 +1,7 @@
 /*
     INI LIBRARY
 
-    Header file for the internal parsing functions.
+    Header file for the ini collection object.
 
     Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
 
@@ -19,18 +19,27 @@
     along with INI Library.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#ifndef INI_PARSE_H
-#define INI_PARSE_H
+#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);
 
-/* Internal function to read line from INI file */
-int read_line(FILE *file,
-              char *buf,
-              int read_size,
-              char **key,
-              char **value,
-              int *length,
-              int *ext_error);
 
 #endif
diff --git a/common/ini/ini_serialize.c b/common/ini/ini_serialize.c
new file mode 100644
index 0000000000000000000000000000000000000000..542da98c1f98dbcf3e795baeda04a228eb13f5d2
--- /dev/null
+++ b/common/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;
+}
-- 
1.5.5.6

