From a5937be7d057d3ee954ad33cd1d8a0488dbdc68e Mon Sep 17 00:00:00 2001
From: Zeying Xie <swpdtz@gmail.com>
Date: Mon, 27 May 2013 11:37:17 +0800
Subject: [PATCH] Config: some minor refactor that makes entry adding and
 section adding function more unified

Signed-off-by: Zeying Xie <swpdtz@gmail.com>
---
 src/mk_config.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/src/mk_config.c b/src/mk_config.c
index 24bf421..300f0f3 100644
--- a/src/mk_config.c
+++ b/src/mk_config.c
@@ -88,19 +88,10 @@ struct mk_config_section *mk_config_section_add(struct mk_config *conf,
 }
 
 /* Register a key/value entry in the last section available of the struct */
-static void mk_config_entry_add(struct mk_config *conf,
+static void mk_config_entry_add(struct mk_config_section *section,
                          const char *key, const char *val)
 {
-    struct mk_config_section *section;
     struct mk_config_entry *new;
-    struct mk_list *head = &conf->sections;
-
-    if (mk_list_is_empty(&conf->sections) == 0) {
-        mk_err("Error: there are not sections available!");
-    }
-
-    /* Last section */
-    section = mk_list_entry_last(head, struct mk_config_section, _head);
 
     /* Alloc new entry */
     new = mk_mem_malloc(sizeof(struct mk_config_entry));
@@ -223,8 +214,13 @@ struct mk_config *mk_config_create(const char *path)
         mk_string_trim(&key);
         mk_string_trim(&val);
 
+        if (mk_list_is_empty(&conf->sections) == 0) {
+            mk_err("Error: there are not sections available!");
+            return NULL;
+        }
+
         /* Register entry: key and val are copied as duplicated */
-        mk_config_entry_add(conf, key, val);
+        mk_config_entry_add(current, key, val);
 
         /* Free temporal key and val */
         mk_mem_free(key);
-- 
1.8.2.3

