summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitri Pal <dpal@redhat.com>2010-07-28 16:37:14 -0400
committerStephen Gallagher <sgallagh@redhat.com>2010-09-22 14:57:53 -0400
commit85c26abd9dabc0f192fe9bb695c3aac1493a9e34 (patch)
tree65e23761b5b8036f54fc6b959d27d25614f2df05
parentfed9fb7462a5ee8afcae8741cb66d8167f9372b1 (diff)
downloadding-libs-85c26abd9dabc0f192fe9bb695c3aac1493a9e34.tar.gz
ding-libs-85c26abd9dabc0f192fe9bb695c3aac1493a9e34.tar.xz
ding-libs-85c26abd9dabc0f192fe9bb695c3aac1493a9e34.zip
Fixes to the value object
* Removed the annoyong type casting in the serialization since there is a new function in the simplebuffer to addstring. * Changed the serialization code to not create simple buffer object but rather add to the passed in one. * Added serialization of the value that stores section * Started cleaning constants but then stopped since this effor should be done a bit later when the new high level ini config interface is better baked.
-rw-r--r--ini/ini_defines.h11
-rw-r--r--ini/ini_valueobj.c138
-rw-r--r--ini/ini_valueobj.h2
-rw-r--r--ini/ini_valueobj_ut.c64
4 files changed, 98 insertions, 117 deletions
diff --git a/ini/ini_defines.h b/ini/ini_defines.h
index 46ed00f..efa9c98 100644
--- a/ini/ini_defines.h
+++ b/ini/ini_defines.h
@@ -60,6 +60,10 @@
#define INI_ERROR "errors"
#define INI_ERROR_NAME "errname"
+#define INI_CONFIG_NAME "INI"
+
+#define INI_SPECIAL_KEY "="
+#define INI_SECTION_KEY "["
/* Internal sizes. MAX_KEY is defined in config.h */
#define MAX_VALUE PATH_MAX
@@ -68,6 +72,13 @@
/* Beffer length used for int to string conversions */
#define CONVERSION_BUFFER 80
+/* Size of the block for a value */
+#define INI_VALUE_BLOCK 100
+
+/* This constant belongs here. Move from ini_config - TBD */
+/* #define COL_CLASS_INI_BASE 20000 */
+
+
/* Different error string functions can be passed as callbacks */
typedef const char * (*error_fn)(int error);
diff --git a/ini/ini_valueobj.c b/ini/ini_valueobj.c
index e21e9e6..02b9732 100644
--- a/ini/ini_valueobj.c
+++ b/ini/ini_valueobj.c
@@ -27,6 +27,7 @@
#include "simplebuffer.h"
#include "ref_array.h"
#include "ini_comment.h"
+#include "ini_defines.h"
#include "trace.h"
struct value_obj {
@@ -40,9 +41,6 @@ struct value_obj {
struct ini_comment *ic;
};
-/* Size of the block for a value */
-#define INI_VALUE_BLOCK 100
-
/* The length of " =" which is 3 */
#define INI_FOLDING_OVERHEAD 3
@@ -51,6 +49,8 @@ struct value_obj {
/* Equal sign */
#define INI_EQUAL_SIGN " = "
+#define INI_OPEN_BR "["
+#define INI_CLOSE_BR "]"
/* Unfold the value represented by the array */
@@ -319,14 +319,12 @@ int value_create_from_refarray(struct ref_array *raw_lines,
if ((!raw_lines) || (!raw_lengths) || (!vo)) {
TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
return EINVAL;
-
}
new_vo = malloc(sizeof(struct value_obj));
if (!new_vo) {
TRACE_ERROR_NUMBER("No memory", ENOMEM);
return ENOMEM;
-
}
/* We are not using references here since
@@ -494,7 +492,6 @@ int value_create_new(const char *strvalue,
int error = EOK;
struct value_obj *new_vo = NULL;
struct simplebuffer *oneline = NULL;
- void *val;
TRACE_FLOW_ENTRY();
@@ -510,11 +507,9 @@ int value_create_new(const char *strvalue,
return error;
}
- /* Deal with the type casting */
- memcpy(&val, &strvalue, sizeof(char *));
/* Put value into the buffer */
- error = simplebuffer_add_raw(oneline,
- val,
+ error = simplebuffer_add_str(oneline,
+ strvalue,
length,
INI_VALUE_BLOCK);
if (error) {
@@ -658,7 +653,6 @@ int value_update(struct value_obj *vo,
{
int error = EOK;
struct simplebuffer *oneline = NULL;
- void *val;
if ((!value) || (!vo)) {
TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
@@ -672,11 +666,9 @@ int value_update(struct value_obj *vo,
return error;
}
- /* Deal with the type cast */
- memcpy(&val, &value, sizeof(char *));
/* Put value into the buffer */
- error = simplebuffer_add_raw(oneline,
- val,
+ error = simplebuffer_add_str(oneline,
+ value,
length,
INI_VALUE_BLOCK);
if (error) {
@@ -757,7 +749,7 @@ int value_put_comment(struct value_obj *vo, struct ini_comment *ic)
/* Serialize value */
int value_serialize(struct value_obj *vo,
const char *key,
- struct simplebuffer **sbobj)
+ struct simplebuffer *sbobj)
{
int error = EOK;
uint32_t num = 0;
@@ -765,10 +757,8 @@ int value_serialize(struct value_obj *vo,
uint32_t len = 0;
char *commentline = NULL;
char *ptr = NULL;
- struct simplebuffer *new_sbobj = NULL;
- void *val = NULL;
- const char *eq = INI_EQUAL_SIGN;
char *part = NULL;
+ int sec = 0;
TRACE_FLOW_ENTRY();
@@ -777,12 +767,6 @@ int value_serialize(struct value_obj *vo,
return EINVAL;
}
- error = simplebuffer_alloc(&new_sbobj);
- if (error) {
- TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
- return error;
- }
-
/* Put comment first */
if (vo->ic) {
@@ -790,7 +774,6 @@ int value_serialize(struct value_obj *vo,
error = ini_comment_get_numlines(vo->ic, &num);
if (error) {
TRACE_ERROR_NUMBER("Failed to get number of lines", errno);
- simplebuffer_free(new_sbobj);
return error;
}
@@ -802,55 +785,69 @@ int value_serialize(struct value_obj *vo,
error = ini_comment_get_line(vo->ic, i, &commentline, &len);
if (error) {
TRACE_ERROR_NUMBER("Failed to get number of lines", errno);
- simplebuffer_free(new_sbobj);
return error;
}
- error = simplebuffer_add_raw(new_sbobj,
+ error = simplebuffer_add_raw(sbobj,
commentline,
len,
INI_VALUE_BLOCK);
if (error) {
TRACE_ERROR_NUMBER("Failed to add comment", error);
- simplebuffer_free(new_sbobj);
return error;
}
- error = simplebuffer_add_cr(new_sbobj);
+ error = simplebuffer_add_cr(sbobj);
if (error) {
TRACE_ERROR_NUMBER("Failed to add CR", error);
- simplebuffer_free(new_sbobj);
return error;
}
}
}
- /* Deal with the type cast */
- memcpy(&val, &key, sizeof(char *));
-
- error = simplebuffer_add_raw(new_sbobj,
- val,
- vo->keylen,
- INI_VALUE_BLOCK);
- if (error) {
- TRACE_ERROR_NUMBER("Failed to add key", error);
- simplebuffer_free(new_sbobj);
- return error;
+ if (strncmp(key, INI_SPECIAL_KEY, sizeof(INI_SPECIAL_KEY)) == 0) {
+ /* Special key carries only a comment */
+ TRACE_FLOW_EXIT();
+ return EOK;
}
- /* Deal with the type cast */
- memcpy(&val, &eq, sizeof(char *));
+ /* Handle the case it is a section key */
+ if (strncmp(key,
+ INI_SECTION_KEY,
+ sizeof(INI_SECTION_KEY)) == 0) sec = 1;
- error = simplebuffer_add_raw(new_sbobj,
- val,
- sizeof(INI_EQUAL_SIGN) - 1,
- INI_VALUE_BLOCK);
- if (error) {
- TRACE_ERROR_NUMBER("Failed to add equal sign", error);
- simplebuffer_free(new_sbobj);
- return error;
+ if (sec) {
+ error = simplebuffer_add_str(sbobj,
+ INI_OPEN_BR,
+ sizeof(INI_OPEN_BR) - 1,
+ INI_VALUE_BLOCK);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to add opening section bracket", error);
+ return error;
+
+ }
}
+ else {
+ error = simplebuffer_add_str(sbobj,
+ key,
+ vo->keylen,
+ INI_VALUE_BLOCK);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to add key", error);
+ return error;
+ }
+
+ error = simplebuffer_add_str(sbobj,
+ INI_EQUAL_SIGN,
+ sizeof(INI_EQUAL_SIGN) - 1,
+ INI_VALUE_BLOCK);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to add equal sign", error);
+ return error;
+ }
+
+ }
if (vo->raw_lines) {
i = 0;
@@ -862,7 +859,6 @@ int value_serialize(struct value_obj *vo,
ref_array_get(vo->raw_lengths, i, (void *)&len);
if (error) {
TRACE_ERROR_NUMBER("Failed to add string", error);
- simplebuffer_free(new_sbobj);
return error;
}
@@ -871,31 +867,47 @@ int value_serialize(struct value_obj *vo,
TRACE_INFO_STRING("Value:", part);
TRACE_INFO_NUMBER("Lenght:", len);
- error = simplebuffer_add_raw(new_sbobj,
+ error = simplebuffer_add_raw(sbobj,
part,
len,
INI_VALUE_BLOCK);
if (error) {
TRACE_ERROR_NUMBER("Failed to add value", error);
- simplebuffer_free(new_sbobj);
return error;
}
- error = simplebuffer_add_cr(new_sbobj);
- if (error) {
- TRACE_ERROR_NUMBER("Failed to add CR", error);
- simplebuffer_free(new_sbobj);
- return error;
+ if (!sec) {
+ error = simplebuffer_add_cr(sbobj);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to add CR", error);
+ return error;
+ }
}
- i++;
+ i++;
}
else break;
}
}
- *sbobj = new_sbobj;
+ if (sec) {
+ error = simplebuffer_add_str(sbobj,
+ INI_CLOSE_BR,
+ sizeof(INI_CLOSE_BR) - 1,
+ INI_VALUE_BLOCK);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to add closing bracket", error);
+ return error;
+
+ }
+
+ error = simplebuffer_add_cr(sbobj);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to add CR", error);
+ return error;
+ }
+ }
- TRACE_INFO_STRING("Buffer:", (const char *)simplebuffer_get_buf(*sbobj));
+ TRACE_INFO_STRING("Buffer:", (const char *)simplebuffer_get_buf(sbobj));
TRACE_FLOW_EXIT();
return error;
}
diff --git a/ini/ini_valueobj.h b/ini/ini_valueobj.h
index d8e10de..2e13c0d 100644
--- a/ini/ini_valueobj.h
+++ b/ini/ini_valueobj.h
@@ -124,7 +124,7 @@ int value_put_comment(struct value_obj *vo,
/* Serialize value */
int value_serialize(struct value_obj *vo,
const char *key,
- struct simplebuffer **sbobj);
+ struct simplebuffer *sbobj);
#endif
diff --git a/ini/ini_valueobj_ut.c b/ini/ini_valueobj_ut.c
index 7736663..3def3f7 100644
--- a/ini/ini_valueobj_ut.c
+++ b/ini/ini_valueobj_ut.c
@@ -84,9 +84,14 @@ int save_value(FILE *ff, const char *key, struct value_obj *vo)
TRACE_FLOW_ENTRY();
+ error = simplebuffer_alloc(&sbobj);
+ if (error) {
+ TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
+ return error;
+ }
/* Serialize */
- error = value_serialize(vo, key, &sbobj);
+ error = value_serialize(vo, key, sbobj);
if (error) {
printf("Failed to serialize a value object %d.\n", error);
return error;
@@ -200,7 +205,7 @@ int other_create_test(FILE *ff, struct value_obj **vo)
}
/* Save value to the file */
- error = save_value(stdout, "bar", new_vo);
+ error = save_value(ff, "baz", new_vo);
if (error) {
printf("Failed to save value to file %d.\n", error);
value_destroy(new_vo);
@@ -342,6 +347,10 @@ int modify_test(FILE *ff, struct value_obj *vo)
strlen(strval),
INI_VALUE_CREATED,
10);
+ if (error) {
+ printf("Failed to update value %d.\n", error);
+ return error;
+ }
/* Save value to the file */
@@ -456,62 +465,11 @@ int vo_basic_test(void)
}
-int vo_file_test(void)
-{
- int error = EOK;
- FILE *ff = NULL;
- uint32_t line = 0;
- char *oneline = NULL;
- char buffer[BUFFER_SIZE + 1];
-
- TRACE_FLOW_ENTRY();
-
- errno = 0;
- ff = fopen("test.ini","r");
- if (ff == NULL) {
- error = errno;
- printf("Failed to open file. Error %d.\n", error);
- return error;
- }
-
- while (1) {
-
- oneline = NULL;
- errno = 0;
- line++;
-
- oneline = fgets(buffer, BUFFER_SIZE, ff);
- if (oneline == NULL) {
- error = errno;
- if (feof(ff)) break;
- printf("Failed to read file %d\n", error);
- fclose(ff);
- return error;
- }
-
- VOOUT(printf("Line %04d: %s", line, oneline));
-
- }
-
- fclose(ff);
-
- /* THIS TEST IS INCOMPLETE.
- * BUT I NEED TO ADD PARSING
- * TO COMPLETE IT.
- * Will be delivered later.
- */
-
- TRACE_FLOW_EXIT();
- return EOK;
-}
-
-
/* Main function of the unit test */
int main(int argc, char *argv[])
{
int error = 0;
test_fn tests[] = { vo_basic_test,
- vo_file_test,
NULL };
test_fn t;
int i = 0;