From 82df7442ec1502af6b1562ffbb05d2f68d516f5a Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Fri, 21 Sep 2012 19:33:55 -0400
Subject: [PATCH 3/9] [INI] Adding couple functions to value object

The patch adds function to get the length of
the concatenated value. It also adds a convenince
print function that is convenint in debugging
the values inside configuration object.
Patch also add checkes for input values
that can be passed by pointer.
---
 ini/ini_valueobj.c    |   72 +++++++++++++++++++++++++++++++++++++++++++++++++
 ini/ini_valueobj.h    |    6 ++++
 ini/ini_valueobj_ut.c |    9 +++++-
 3 files changed, 86 insertions(+), 1 deletions(-)

diff --git a/ini/ini_valueobj.c b/ini/ini_valueobj.c
index 8c378ac..7d90ab0 100644
--- a/ini/ini_valueobj.c
+++ b/ini/ini_valueobj.c
@@ -695,12 +695,42 @@ int value_get_concatenated(struct value_obj *vo,
         return EINVAL;
     }
 
+    if (!fullstr)
+    {
+        TRACE_ERROR_NUMBER("Invalid output value", EINVAL);
+        return EINVAL;
+    }
+
     *fullstr = (const char *)simplebuffer_get_buf(vo->unfolded);
 
     TRACE_FLOW_EXIT();
     return EOK;
 }
 
+/* Get length of the concatenated value */
+int value_get_concatenated_len(struct value_obj *vo,
+                               uint32_t *len)
+{
+    TRACE_FLOW_ENTRY();
+
+    if (!vo) {
+        TRACE_ERROR_NUMBER("Invalid object", EINVAL);
+        return EINVAL;
+    }
+
+    if (!len)
+    {
+        TRACE_ERROR_NUMBER("Invalid output value", EINVAL);
+        return EINVAL;
+    }
+
+    *len = simplebuffer_get_len(vo->unfolded);
+
+    TRACE_FLOW_EXIT();
+    return EOK;
+}
+
+
 /* Get value's origin */
 int value_get_origin(struct value_obj *vo, uint32_t *origin)
 {
@@ -711,6 +741,12 @@ int value_get_origin(struct value_obj *vo, uint32_t *origin)
         return EINVAL;
     }
 
+    if (!origin)
+    {
+        TRACE_ERROR_NUMBER("Invalid output value", EINVAL);
+        return EINVAL;
+    }
+
     *origin = vo->origin;
 
     TRACE_FLOW_EXIT();
@@ -727,6 +763,12 @@ int value_get_line(struct value_obj *vo, uint32_t *line)
         return EINVAL;
     }
 
+    if (!line)
+    {
+        TRACE_ERROR_NUMBER("Invalid output value", EINVAL);
+        return EINVAL;
+    }
+
     *line = vo->line;
 
     TRACE_FLOW_EXIT();
@@ -1077,3 +1119,33 @@ extern void ref_array_debug(struct ref_array *ra, int num);
     TRACE_FLOW_EXIT();
     return error;
 }
+
+/* Print value */
+void value_print(const char *key, struct value_obj *vo)
+{
+
+    int error = EOK;
+    struct simplebuffer *sbobj = NULL;
+
+    TRACE_FLOW_ENTRY();
+
+    error = simplebuffer_alloc(&sbobj);
+    if (error) {
+        printf("Failed to allocate dynamic string %d.\n", error);
+        return;
+    }
+
+    /* Serialize */
+    error = value_serialize(vo, key, sbobj);
+    if (error) {
+        printf("Failed to serialize a value object %d.\n", error);
+        simplebuffer_free(sbobj);
+        return;
+    }
+
+    printf("%s", simplebuffer_get_buf(sbobj));
+    simplebuffer_free(sbobj);
+
+    TRACE_FLOW_EXIT();
+}
+
diff --git a/ini/ini_valueobj.h b/ini/ini_valueobj.h
index 3267bcc..cbecc75 100644
--- a/ini/ini_valueobj.h
+++ b/ini/ini_valueobj.h
@@ -98,6 +98,10 @@ void value_destroy(struct value_obj *vo);
 int value_get_concatenated(struct value_obj *vo,
                            const char **fullstr);
 
+/* Get length of the concatenated value */
+int value_get_concatenated_len(struct value_obj *vo,
+                               uint32_t *len);
+
 /* Get value's origin */
 int value_get_origin(struct value_obj *vo,
                      uint32_t *origin);
@@ -133,5 +137,7 @@ int value_serialize(struct value_obj *vo,
                     const char *key,
                     struct simplebuffer *sbobj);
 
+/* Print value */
+void value_print(const char *key, struct value_obj *vo);
 
 #endif
diff --git a/ini/ini_valueobj_ut.c b/ini/ini_valueobj_ut.c
index 0e7959f..e4fc6b4 100644
--- a/ini/ini_valueobj_ut.c
+++ b/ini/ini_valueobj_ut.c
@@ -141,6 +141,8 @@ int other_create_test(FILE *ff, struct value_obj **vo)
     int i;
     uint32_t origin = 0;
     uint32_t line = 0;
+    uint32_t len = 0;
+    uint32_t expected_len = 0;
 
 
     TRACE_FLOW_ENTRY();
@@ -230,7 +232,12 @@ int other_create_test(FILE *ff, struct value_obj **vo)
         return error;
     }
 
-    if (strncmp(fullstr, expected, strlen(expected) + 1) != 0) {
+    /* Get length of the concatenated value */
+    value_get_concatenated_len(new_vo, &len);
+    expected_len = strlen(expected);
+
+    if ((len != expected_len) ||
+        (strncmp(fullstr, expected, expected_len + 1) != 0)) {
         printf("The expected value is different.\n%s\n", fullstr);
         value_destroy(new_vo);
         return EINVAL;
-- 
1.7.1

