diff options
Diffstat (limited to 'common/ini')
-rw-r--r-- | common/ini/ini_config.c | 22 | ||||
-rw-r--r-- | common/ini/ini_config_ut.c | 56 |
2 files changed, 45 insertions, 33 deletions
diff --git a/common/ini/ini_config.c b/common/ini/ini_config.c index 43f6d3b4..e4b1f365 100644 --- a/common/ini/ini_config.c +++ b/common/ini/ini_config.c @@ -73,6 +73,9 @@ #define RET_EOF 5 #define RET_ERROR 6 +#define INI_ERROR "errors" +#define INI_ERROR_NAME "errname" + /* Different error string functions can be passed as callbacks */ typedef const char * (*error_fn)(int error); @@ -175,7 +178,7 @@ static int ini_to_collection(const char *filename, TRACE_FLOW_STRING("ini_to_collection", "Entry"); /* Open file for reading */ - file = fopen(filename,"r"); + file = fopen(filename, "r"); if (file == NULL) { error = errno; TRACE_ERROR_NUMBER("Failed to open file - but this is OK", error); @@ -185,12 +188,20 @@ static int ini_to_collection(const char *filename, /* Open the collection of errors */ if (error_list != NULL) { *error_list = NULL; - error = col_create_collection(error_list, filename, COL_CLASS_INI_PERROR); + error = col_create_collection(error_list, INI_ERROR, COL_CLASS_INI_PERROR); if (error) { TRACE_ERROR_NUMBER("Failed to create error collection", error); fclose(file); return error; } + /* Add file name as the first item */ + error = col_add_str_property(*error_list, NULL, INI_ERROR_NAME, filename, 0); + if (error) { + TRACE_ERROR_NUMBER("Failed to and name to collection", error); + fclose(file); + col_destroy_collection(*error_list); + return error; + } created = 1; } @@ -891,9 +902,9 @@ static void print_error_list(FILE *file, /* Process collection header */ if (col_get_item_type(item) == COL_TYPE_COLLECTION) { col_get_collection_count(item, &count); - if (count > 1) - fprintf(file, error_header, col_get_item_property(item, NULL)); - else break; + if (count <= 2) break; + } else if (col_get_item_type(item) == COL_TYPE_STRING) { + fprintf(file, error_header, (char *)col_get_item_data(item)); } else { /* Put error into provided format */ @@ -1009,6 +1020,7 @@ void print_config_parsing_errors(FILE *file, if (error) { TRACE_ERROR_STRING("Error (extract):", FAILED_TO_PROCCESS); fprintf(file, "%s\n", FAILED_TO_PROCCESS); + col_unbind_iterator(iterator); return; } print_file_parsing_errors(file, file_errors); diff --git a/common/ini/ini_config_ut.c b/common/ini/ini_config_ut.c index bcb47e6d..3ce08aea 100644 --- a/common/ini/ini_config_ut.c +++ b/common/ini/ini_config_ut.c @@ -217,7 +217,7 @@ int real_test(const char *file) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; type = col_get_item_type(item); @@ -246,9 +246,9 @@ int get_test(void) { int error; - struct collection_item *ini_config = (struct collection_item *)(NULL); - struct collection_item *error_set = (struct collection_item *)(NULL); - struct collection_item *item = (struct collection_item *)(NULL); + struct collection_item *ini_config = NULL; + struct collection_item *error_set = NULL; + struct collection_item *item = NULL; int number; long number_long; double number_double; @@ -286,7 +286,7 @@ int get_test(void) printf("Negtive test - trying to get non existing key-value pair.\n"); /* Negative test */ - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor1", "description1", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -295,14 +295,14 @@ int get_test(void) } /* Item should not be found */ - if (item != (struct collection_item *)(NULL)) { + if (item != NULL) { printf("Expected NULL but got something else!\n"); free_ini_config(ini_config); return -1; } /* Another negative test but section exists this time */ - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor", "description1", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -311,7 +311,7 @@ int get_test(void) } /* Item should not be found */ - if(item != (struct collection_item *)(NULL)) { + if(item != NULL) { printf("Expected NULL but got something else!\n"); free_ini_config(ini_config); return -1; @@ -320,7 +320,7 @@ int get_test(void) printf("Trying to get an item.\n"); /* Positive test */ - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor", "description", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -329,7 +329,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected item but got something NULL!\n"); free_ini_config(ini_config); return -1; @@ -380,7 +380,7 @@ int get_test(void) /* Get a badly formated number */ printf("Convert item to number with strict conversion.\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor", "bad_number", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -389,7 +389,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected item but got something NULL!\n"); free_ini_config(ini_config); return -1; @@ -434,7 +434,7 @@ int get_test(void) printf("Fetch another item from section \"domains/LOCAL\" named \"enumerate\".\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/LOCAL","enumerate", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -443,7 +443,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -563,7 +563,7 @@ int get_test(void) /* Get real bool item and convert it */ printf("Get real bool item \"legacy\" and convert it.\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/LOCAL","legacy", ini_config, &item); if (error) { printf("Expected success but got error! %d\n",error); @@ -572,7 +572,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -597,7 +597,7 @@ int get_test(void) printf("Get binary item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/EXAMPLE.COM","binary_test", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -606,7 +606,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -632,7 +632,7 @@ int get_test(void) printf("Get string array item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains", "domainsorder", ini_config, &item); if(error) { printf("Expected success but got error! %d\n",error); @@ -641,7 +641,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -686,7 +686,7 @@ int get_test(void) printf("Get long array item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/EXAMPLE.COM", "long_array", ini_config, &item); if(error) { printf("Expected success but got error! %d\n", error); @@ -695,7 +695,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -719,7 +719,7 @@ int get_test(void) printf("Get double array item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/EXAMPLE.COM", "double_array", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -728,7 +728,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -800,14 +800,14 @@ int get_test(void) int main(int argc, char *argv[]) { - int error; - - char *srcdir; + int error = EOK; + char *srcdir = NULL; srcdir = getenv("srcdir"); if(srcdir) { - if(chdir(srcdir) == 0) { + if(chdir(srcdir) != 0) { error = errno; + printf("Failed to change directory, error %d\n", error); return error; } } |