From 4e756ea255a77e3d421b9914240e4606f187f111 Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Tue, 25 Sep 2012 16:05:12 -0400
Subject: [PATCH 5/9] [INI] Add a search state to the config object

The search state allows one to search a configuration object
for the same key in the cases when a key permits more
than one value scattered across the config file.
The state needs to be stored, initialized and properly cleaned.
---
 ini/ini_config_priv.h |    7 +++++++
 ini/ini_configobj.c   |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/ini/ini_config_priv.h b/ini/ini_config_priv.h
index e714774..5d994f9 100644
--- a/ini/ini_config_priv.h
+++ b/ini/ini_config_priv.h
@@ -33,6 +33,13 @@ struct ini_cfgobj {
     struct collection_item *cfg;
     /* Boundary */
     uint32_t boundary;
+    /* Last search state */
+    char *section;
+    char *name;
+    int section_len;
+    int name_len;
+    struct collection_iterator *iterator;
+
     /*...         */
     /* Statistics? Timestamps? When created? Modified? - TBD */
     /*...         */
diff --git a/ini/ini_configobj.c b/ini/ini_configobj.c
index 538541f..193474b 100644
--- a/ini/ini_configobj.c
+++ b/ini/ini_configobj.c
@@ -56,11 +56,34 @@ void ini_cleanup_cb(const char *property,
     TRACE_FLOW_EXIT();
 }
 
+/* Clean the search state */
+void ini_config_clean_state(struct ini_cfgobj *ini_config)
+{
+    TRACE_FLOW_ENTRY();
+
+    if (ini_config) {
+        if (ini_config->iterator) col_unbind_iterator(ini_config->iterator);
+        ini_config->iterator = NULL;
+        free(ini_config->section);
+        ini_config->section = NULL;
+        free(ini_config->name);
+        ini_config->name = NULL;
+        ini_config->section_len = 0;
+        ini_config->name_len = 0;
+    }
+
+    TRACE_FLOW_EXIT();
+}
+
+
+
 /* Traverse the collection and clean the object */
 void ini_config_destroy(struct ini_cfgobj *ini_config)
 {
     TRACE_FLOW_ENTRY();
 
+    ini_config_clean_state(ini_config);
+
     if (ini_config) {
         if(ini_config->cfg) {
 
@@ -97,6 +120,11 @@ int ini_config_create(struct ini_cfgobj **ini_config)
 
     new_co->cfg = NULL;
     new_co->boundary = INI_WRAP_BOUNDARY;
+    new_co->section = NULL;
+    new_co->name = NULL;
+    new_co->section_len = 0;
+    new_co->name_len = 0;
+    new_co->iterator = NULL;
 
     /* Create a collection to hold configuration data */
     error = col_create_collection(&(new_co->cfg),
@@ -235,6 +263,11 @@ int ini_config_copy(struct ini_cfgobj *ini_config,
 
     new_co->cfg = NULL;
     new_co->boundary = ini_config->boundary;
+    new_co->section = NULL;
+    new_co->name = NULL;
+    new_co->section_len = 0;
+    new_co->name_len = 0;
+    new_co->iterator = NULL;
 
     error = col_copy_collection_with_cb(&(new_co->cfg),
                                         ini_config->cfg,
-- 
1.7.1

