diff options
-rw-r--r-- | common/ini/ini_config.c | 251 | ||||
-rw-r--r-- | common/ini/ini_config.h | 53 | ||||
-rw-r--r-- | common/ini/ini_config_ut.c | 345 |
3 files changed, 460 insertions, 189 deletions
diff --git a/common/ini/ini_config.c b/common/ini/ini_config.c index 7effc0426..0652bd1cf 100644 --- a/common/ini/ini_config.c +++ b/common/ini/ini_config.c @@ -46,12 +46,23 @@ #define FILE_ERROR_SET "ini_file_error_set" /* Text error strings used when errors are printed out */ -#define WARNING_TXT _("Warning") -#define ERROR_TXT _("Error") -#define WRONG_COLLECTION _("Passed in list is not a list of parse errors.\n") -#define FAILED_TO_PROCCESS _("Internal Error. Failed to process error list.\n") -#define ERROR_HEADER _("Parsing errors and warnings in file: %s\n") -#define LINE_FORMAT _("%s (%d) on line %d: %s\n") +#define WARNING_TXT _("Warning") +#define ERROR_TXT _("Error") +/* For parse errors */ +#define WRONG_COLLECTION _("Passed in list is not a list of parse errors.\n") +#define FAILED_TO_PROCCESS _("Internal Error. Failed to process error list.\n") +#define ERROR_HEADER _("Parsing errors and warnings in file: %s\n") +/* For grammar errors */ +#define WRONG_GRAMMAR _("Passed in list is not a list of grammar errors.\n") +#define FAILED_TO_PROC_G _("Internal Error. Failed to process list of grammar errors.\n") +#define ERROR_HEADER_G _("Logical errors and warnings in file: %s\n") +/* For validation errors */ +#define WRONG_VALIDATION _("Passed in list is not a list of validation errors.\n") +#define FAILED_TO_PROC_V _("Internal Error. Failed to process list of validation errors.\n") +#define ERROR_HEADER_V _("Validation errors and warnings in file: %s\n") + +#define LINE_FORMAT _("%s (%d) on line %d: %s\n") + /* Codes that parsing function can return */ #define RET_PAIR 0 @@ -62,11 +73,13 @@ #define RET_EOF 5 #define RET_ERROR 6 +/* Different error string functions can be passed as callbacks */ +typedef const char * (*error_fn)(int error); /* Function to return parsing error */ inline const char *parsing_error_str(int parsing_error) { - const char *placeholder= _("Unknown error."); + const char *placeholder= _("Unknown pasing error."); const char *str_error[] = { _("Data is too long."), _("No closing bracket."), _("Section name is missing."), @@ -83,6 +96,47 @@ inline const char *parsing_error_str(int parsing_error) return str_error[parsing_error-1]; } +/* Function to return grammar error */ +inline const char *grammar_error_str(int grammar_error) +{ + const char *placeholder= _("Unknown grammar error."); + const char *str_error[] = { _(""), + _(""), + _(""), + _(""), + _(""), + _(""), + _("") + }; + + /* Check the range */ + if ((grammar_error < 1) || (grammar_error > ERR_MAXGRAMMAR)) + return placeholder; + else + return str_error[grammar_error-1]; +} + +/* Function to return validation error */ +inline const char *validation_error_str(int validation_error) +{ + const char *placeholder= _("Unknown validation error."); + const char *str_error[] = { _(""), + _(""), + _(""), + _(""), + _(""), + _(""), + _("") + }; + + /* Check the range */ + if ((validation_error < 1) || (validation_error > ERR_MAXVALID)) + return placeholder; + else + return str_error[validation_error-1]; +} + + /* Internal function to read line from INI file */ int read_line(FILE *file, char *buf, @@ -100,7 +154,7 @@ static int add_or_update(struct collection_item *current_section, int type) { int found = COL_NOMATCH; - int error; + int error; TRACE_FLOW_STRING("add_or_update", "Entry"); @@ -129,7 +183,8 @@ static int add_or_update(struct collection_item *current_section, static int ini_to_collection(const char *filename, struct collection_item *ini_config, int error_level, - struct collection_item **error_list) + struct collection_item **error_list, + struct collection_item **lines) { FILE *file; int error; @@ -178,6 +233,21 @@ static int ini_to_collection(const char *filename, switch (status) { case RET_PAIR: + /* Add line to the collection of lines */ + if (lines) { + error = add_int_property(*lines, NULL, key, line); + if (error) { + TRACE_ERROR_NUMBER("Failed to add line to line collection", error); + fclose(file); + destroy_collection(current_section); + if (created) { + destroy_collection(*error_list); + *error_list = NULL; + } + return error; + } + } + /* Do we have a section at the top of the file ? */ if (section_count == 0) { /* Check if collection already exists */ @@ -221,6 +291,21 @@ static int ini_to_collection(const char *filename, break; case RET_SECTION: + /* Add line to the collection of lines */ + if (lines) { + error = add_int_property(*lines, NULL, key, line); + if (error) { + TRACE_ERROR_NUMBER("Failed to add line to line collection", error); + fclose(file); + destroy_collection(current_section); + if (created) { + destroy_collection(*error_list); + *error_list = NULL; + } + return error; + } + } + /* Read a new section */ destroy_collection(current_section); current_section = NULL; @@ -324,14 +409,38 @@ static int ini_to_collection(const char *filename, /*********************************************************************/ /* Read configuration information from a file */ -int config_from_file(const char *application, +inline int config_from_file(const char *application, const char *config_file, struct collection_item **ini_config, int error_level, struct collection_item **error_list) { int error; + + TRACE_FLOW_STRING("config_from_file", "Entry"); + error = config_from_file_with_lines(application, + config_file, + ini_config, + error_level, + error_list, + NULL); + TRACE_FLOW_NUMBER("config_from_file. Returns", error); + return error; +} + +/* Function to read the ini file and have a collection + * of which item appers on which line + */ +int config_from_file_with_lines(const char *application, + const char *config_file, + struct collection_item **ini_config, + int error_level, + struct collection_item **error_list, + struct collection_item **lines) +{ + int error; int created = 0; + int created_lines = 0; TRACE_FLOW_STRING("config_from_file", "Entry"); @@ -358,14 +467,47 @@ int config_from_file(const char *application, return EINVAL; } + + /* Create collection if needed */ + if (lines) { + + /* Make sure that the lines collection is empty */ + if (*lines) { + TRACE_ERROR_NUMBER("Collection of lines is not empty", EINVAL); + if (created) { + destroy_collection(*ini_config); + *ini_config = NULL; + } + return EINVAL; + } + + error = create_collection(lines, + application, + COL_CLASS_INI_LINES); + if (error != EOK) { + TRACE_ERROR_NUMBER("Failed to create collection", error); + if (created) { + destroy_collection(*ini_config); + *ini_config = NULL; + } + return error; + } + created_lines = 1; + } + /* Do the actual work */ error = ini_to_collection(config_file, *ini_config, - error_level, error_list); + error_level, error_list, lines); /* In case of error when we created collection - delete it */ if (error && created) { destroy_collection(*ini_config); *ini_config = NULL; } + /* Also create collection of lines if we created it */ + if (error && created_lines) { + destroy_collection(*lines); + *lines = NULL; + } TRACE_FLOW_NUMBER("config_from_file. Returns", error); return error; @@ -439,7 +581,7 @@ int config_for_app(const char *application, if (config_file != NULL) { TRACE_INFO_STRING("Reading master file:", config_file); error = ini_to_collection(config_file, *ini_config, - error_level, pass_common); + error_level, pass_common, NULL); /* ENOENT and EOK are Ok */ if (error && (error != ENOENT)) { TRACE_ERROR_NUMBER("Failed to read master file", error); @@ -490,8 +632,8 @@ int config_for_app(const char *application, TRACE_INFO_STRING("Opening file:", file_name); /* Read master file */ - error = ini_to_collection(file_name, *ini_config, - error_level, pass_specific); + error = ini_to_collection(file_name, *ini_config, + error_level, pass_specific, NULL); free(file_name); /* ENOENT and EOK are Ok */ if (error && (error != ENOENT)) { @@ -688,9 +830,16 @@ int read_line(FILE *file, } -/* Print errors and warnings that were detected while parsing one file */ -void print_file_parsing_errors(FILE *file, - struct collection_item *error_list) + +/* Internal function that prints errors */ +static void print_error_list(FILE *file, + struct collection_item *error_list, + int cclass, + char *wrong_col_error, + char *failed_to_process, + char *error_header, + char *line_format, + error_fn error_function) { struct collection_iterator *iterator; int error; @@ -698,7 +847,7 @@ void print_file_parsing_errors(FILE *file, struct parse_error *pe; unsigned int count; - TRACE_FLOW_STRING("print_file_parsing_errors", "Entry"); + TRACE_FLOW_STRING("print_error_list", "Entry"); /* If we have something to print print it */ if (error_list == NULL) { @@ -707,17 +856,17 @@ void print_file_parsing_errors(FILE *file, } /* Make sure we go the right collection */ - if (!is_of_class(error_list, COL_CLASS_INI_PERROR)) { - TRACE_ERROR_STRING("Wrong collection class:", WRONG_COLLECTION); - fprintf(file,"%s\n", WRONG_COLLECTION); + if (!is_of_class(error_list, cclass)) { + TRACE_ERROR_STRING("Wrong collection class:", wrong_col_error); + fprintf(file,"%s\n", wrong_col_error); return; } /* Bind iterator */ error = bind_iterator(&iterator, error_list, COL_TRAVERSE_DEFAULT); if (error) { - TRACE_ERROR_STRING("Error (bind):", FAILED_TO_PROCCESS); - fprintf(file, "%s\n", FAILED_TO_PROCCESS); + TRACE_ERROR_STRING("Error (bind):", failed_to_process); + fprintf(file, "%s\n", failed_to_process); return; } @@ -725,8 +874,8 @@ void print_file_parsing_errors(FILE *file, /* Loop through a collection */ error = iterate_collection(iterator, &item); if (error) { - TRACE_ERROR_STRING("Error (iterate):", FAILED_TO_PROCCESS); - fprintf(file, "%s\n", FAILED_TO_PROCCESS); + TRACE_ERROR_STRING("Error (iterate):", failed_to_process); + fprintf(file, "%s\n", failed_to_process); unbind_iterator(iterator); return; } @@ -738,17 +887,17 @@ void print_file_parsing_errors(FILE *file, if (get_item_type(item) == COL_TYPE_COLLECTION) { get_collection_count(item, &count); if (count > 1) - fprintf(file, ERROR_HEADER, get_item_property(item, NULL)); + fprintf(file, error_header, get_item_property(item, NULL)); else break; } else { /* Put error into provided format */ pe = (struct parse_error *)(get_item_data(item)); - fprintf(file, LINE_FORMAT, + fprintf(file, line_format, get_item_property(item, NULL), /* Error or warning */ pe->error, /* Error */ - pe->line, /* Line */ - parsing_error_str(pe->error)); /* Error str */ + pe->line, /* Line */ + error_function(pe->error)); /* Error str */ } } @@ -756,9 +905,51 @@ void print_file_parsing_errors(FILE *file, /* Do not forget to unbind iterator - otherwise there will be a leak */ unbind_iterator(iterator); - TRACE_FLOW_STRING("print_file_parsing_errors", "Exit"); + TRACE_FLOW_STRING("print_error_list", "Exit"); } +/* Print errors and warnings that were detected while parsing one file */ +void inline print_file_parsing_errors(FILE *file, + struct collection_item *error_list) +{ + print_error_list(file, + error_list, + COL_CLASS_INI_PERROR, + WRONG_COLLECTION, + FAILED_TO_PROCCESS, + ERROR_HEADER, + LINE_FORMAT, + parsing_error_str); +} + + +/* Print errors and warnings that were detected while processing grammar */ +void inline print_grammar_errors(FILE *file, + struct collection_item *error_list) +{ + print_error_list(file, + error_list, + COL_CLASS_INI_GERROR, + WRONG_GRAMMAR, + FAILED_TO_PROC_G, + ERROR_HEADER_G, + LINE_FORMAT, + grammar_error_str); +} + +/* Print errors and warnings that were detected while validating INI file. */ +void inline print_validation_errors(FILE *file, + struct collection_item *error_list) +{ + print_error_list(file, + error_list, + COL_CLASS_INI_VERROR, + WRONG_VALIDATION, + FAILED_TO_PROC_V, + ERROR_HEADER_V, + LINE_FORMAT, + validation_error_str); +} /* Print errors and warnings that were detected while parsing * the whole configuration */ diff --git a/common/ini/ini_config.h b/common/ini/ini_config.h index 95cd52416..fdf354382 100644 --- a/common/ini/ini_config.h +++ b/common/ini/ini_config.h @@ -35,6 +35,9 @@ #define COL_CLASS_INI_SECTION COL_CLASS_INI_BASE + 1 /* A one level collection of key value pairs where values are always stings */ #define COL_CLASS_INI_PERROR COL_CLASS_INI_BASE + 2 /* A one level collection of parse errors - store parse_error structs */ #define COL_CLASS_INI_PESET COL_CLASS_INI_BASE + 3 /* A one level collection of parse error collections */ +#define COL_CLASS_INI_GERROR COL_CLASS_INI_BASE + 4 /* A one level collection of grammar errors - store parse_error structs */ +#define COL_CLASS_INI_VERROR COL_CLASS_INI_BASE + 5 /* A one level collection of validation errors - store parse_error structs */ +#define COL_CLASS_INI_LINES COL_CLASS_INI_BASE + 6 /* A one level collection of lines in INI file */ /* Error levels */ @@ -54,6 +57,15 @@ #define ERR_MAXPARSE ERR_LONGKEY +/* Grammar errors and warnings */ +/* Placeholder for now... */ +#define ERR_MAXGRAMMAR 0 + +/* Validation errors and warnings */ +/* Placeholder for now... */ +#define ERR_MAXVALID 0 + + /* Internal sizes */ /* FIXME - make them configurable via config.h */ @@ -69,6 +81,19 @@ struct parse_error { /* Function to return parsing error */ const char *parsing_error_str(int parsing_error); +/* Function to return grammar error in template. + * This error is returned when the template + * is translated into the grammar object. + */ +const char *grammar_error_str(int parsing_error); + +/* Function to return validation error. + * This is the error that it is returned when + * the INI file is validated against the + * grammar object. + */ +const char *validation_error_str(int parsing_error); + /* Read configuration information from a file */ int config_from_file(const char *application, /* Name of the application - will be used as name of the collection */ const char *config_file, /* Name of the config file - if NULL the collection will be empty */ @@ -78,6 +103,17 @@ int config_from_file(const char *application, /* Name of the appli struct collection_item **error_list); /* List of errors for a file */ +/* Read configuration information from a file with extra collection of line numbers */ +int config_from_file_with_lines( + const char *application, /* Name of the application - will be used as name of the collection */ + const char *config_file, /* Name of the config file - if NULL the collection will be empty */ + struct collection_item **ini_config, /* If *ini_config is NULL a new ini object will be allocated, */ + /* otherwise the one that is pointed to will be updated. */ + int error_level, /* Error level - break for errors, warnings or best effort (don't break) */ + struct collection_item **error_list, /* List of errors for a file */ + struct collection_item **lines); /* Collection of pairs where key is the key and value is line number */ + + /* Read default config file and then overwrite it with a specific one from the directory */ int config_for_app(const char *application, /* Name of the application that will be used to get config for */ const char *config_file, /* Name of the configuration file with default settings for all apps */ @@ -91,6 +127,19 @@ int config_for_app(const char *application, /* Name of the applica void print_file_parsing_errors(FILE *file, /* File to send errors to */ struct collection_item *error_list); /* List of parsing errors */ + +/* Print errors and warnings that were detected while + * checking grammar of the template. + */ +void print_grammar_errors(FILE *file, /* File to send errors to */ + struct collection_item *error_list); /* List of grammar errors */ + +/* Print errors and warnings that were detected while + * checking INI file against grammar object. + */ +void print_validation_errors(FILE *file, /* File to send errors to */ + struct collection_item *error_list); /* List of validation errors */ + /* Print errors and warnings that were detected parsing configuration as a whole */ /* Use this function to print results of the config_for_app() call */ void print_config_parsing_errors(FILE *file, /* File to send errors to */ @@ -118,7 +167,7 @@ int get_config_item(const char *section, /* Section. If NULL struct collection_item *ini_config, /* Collection to search */ struct collection_item **item); /* Item returned. Will be NULL is not found. */ -/* Convertion functions for the configuration item. +/* Conversion functions for the configuration item. * Sets error to EINVAL if the item is bad. * Sets error to EIO if the conversion failed. * These functions do not allocate memory. @@ -143,7 +192,7 @@ const char *get_const_string_config_value(struct collection_item *item, int *err /* A get_bin_value and get_xxx_array functions allocate memory. * It is the responsibility of the caller to free it after use. - * free_xxx conviniece wrappers are provided for this purpose. + * free_xxx convenience wrappers are provided for this purpose. * Functions will return NULL if conversion failed. */ /* A special hex format is assumed. diff --git a/common/ini/ini_config_ut.c b/common/ini/ini_config_ut.c index 5441e02c0..9aefbe301 100644 --- a/common/ini/ini_config_ut.c +++ b/common/ini/ini_config_ut.c @@ -31,11 +31,12 @@ int basic_test() { int error; - struct collection_item *ini_config = (struct collection_item *)(NULL); - struct collection_item *error_set = (struct collection_item *)(NULL); + struct collection_item *ini_config = NULL; + struct collection_item *error_set = NULL; - error = config_for_app("test", "./ini/ini.conf", "./ini/ini.d", &ini_config,INI_STOP_ON_NONE,&error_set); - if(error) { + error = config_for_app("test", "./ini/ini.conf", "./ini/ini.d", + &ini_config, INI_STOP_ON_NONE, &error_set); + if (error) { printf("Attempt to read configuration returned error: %d\n",error); return error; } @@ -44,10 +45,10 @@ int basic_test() print_collection(ini_config); print_collection(error_set); - printf("\n\n----------------------\n"); + printf("\n\n----------------------\n"); /* Output parsing errors (if any) */ - print_config_parsing_errors(stdout,error_set); - printf("----------------------\n\n\n"); + print_config_parsing_errors(stdout, error_set); + printf("----------------------\n\n\n"); destroy_collection(ini_config); @@ -58,33 +59,62 @@ int basic_test() int single_file() { int error; - struct collection_item *ini_config = (struct collection_item *)(NULL); - struct collection_item *error_set = (struct collection_item *)(NULL); - - error = config_from_file("test", "./ini/not_exist_ini.conf", &ini_config,INI_STOP_ON_NONE,&error_set); - if(error) { - printf("Attempt to read configuration returned error: %d. EXPECTED.\n\n",error); + struct collection_item *ini_config = NULL; + struct collection_item *error_set = NULL; + struct collection_item *lines = NULL; + + error = config_from_file("test", "./ini/not_exist_ini.conf", + &ini_config, INI_STOP_ON_NONE, &error_set); + if (error) { + printf("Attempt to read configuration returned error: %d. EXPECTED.\n\n", error); if(error != ENOENT) return error; } - error = config_from_file("test", "./ini/ini.conf", &ini_config,INI_STOP_ON_NONE,&error_set); - if(error) { + error = config_from_file("test", "./ini/ini.conf", &ini_config, INI_STOP_ON_NONE, &error_set); + if (error) { printf("Attempt to read configuration returned error: %d\n",error); return error; } - debug_collection(ini_config,COL_TRAVERSE_DEFAULT); + debug_collection(ini_config, COL_TRAVERSE_DEFAULT); print_collection(ini_config); print_collection(error_set); - printf("\n\n----------------------\n"); + printf("\n\n----------------------\n"); + /* Output parsing errors (if any) */ + print_file_parsing_errors(stdout, error_set); + printf("----------------------\n\n\n"); + + + destroy_collection(ini_config); + destroy_collection(error_set); + + ini_config = NULL; + error_set = NULL; + + printf("TEST WITH LINES\n"); + + error = config_from_file_with_lines("test", "./ini/ini.conf", + &ini_config, INI_STOP_ON_NONE, + &error_set, &lines); + if (error) { + printf("Attempt to read configuration returned error: %d\n",error); + return error; + } + + debug_collection(ini_config, COL_TRAVERSE_DEFAULT); + debug_collection(lines, COL_TRAVERSE_DEFAULT); + + printf("\n\n----------------------\n"); /* Output parsing errors (if any) */ - print_file_parsing_errors(stdout,error_set); - printf("----------------------\n\n\n"); + print_file_parsing_errors(stdout, error_set); + printf("----------------------\n\n\n"); destroy_collection(ini_config); destroy_collection(error_set); + destroy_collection(lines); + return 0; } @@ -92,40 +122,40 @@ int negative_test() { int error; unsigned int count; - struct collection_item *ini_config = (struct collection_item *)(NULL); + struct collection_item *ini_config = NULL; /* App name is null - expect failure */ - error = config_for_app(NULL, NULL, NULL, NULL,INI_STOP_ON_NONE,NULL); - if(!error) { + error = config_for_app(NULL, NULL, NULL, NULL, INI_STOP_ON_NONE, NULL); + if (!error) { printf("Expected error: %d got success\n",EINVAL); return -1; } /* Config collection storage is NULL - expect failure */ - error = config_for_app("real", NULL, NULL, NULL,INI_STOP_ON_NONE,NULL); - if(!error) { + error = config_for_app("real", NULL, NULL, NULL, INI_STOP_ON_NONE, NULL); + if (!error) { printf("Expected error: %d got success\n",EINVAL); return -1; } /* Config collection storage is NULL - expect failure */ - error = config_for_app("real", "real.conf", NULL, NULL,INI_STOP_ON_NONE,NULL); - if(!error) { + error = config_for_app("real", "real.conf", NULL, NULL, INI_STOP_ON_NONE, NULL); + if (!error) { printf("Expected error: %d got success\n",EINVAL); return -1; } /* Expect success but empty config */ - error = config_for_app("real", "real.conf", NULL, &ini_config,INI_STOP_ON_NONE,NULL); - if(error) { + error = config_for_app("real", "real.conf", NULL, &ini_config, INI_STOP_ON_NONE, NULL); + if (error) { printf("Expected success got error: %d\n",error); return error; } count = 0; - (void)get_collection_count(ini_config,&count); - if(count > 1) { - printf("Expected empty collection but got contents with %d elements\n",count); + (void)get_collection_count(ini_config, &count); + if (count > 1) { + printf("Expected empty collection but got contents with %d elements\n", count); print_collection(ini_config); return -1; } @@ -138,70 +168,72 @@ int negative_test() int real_test(const char *file) { int error; - struct collection_item *ini_config = (struct collection_item *)(NULL); - struct collection_item *error_set = (struct collection_item *)(NULL); - struct collection_iterator *iterator = (struct collection_iterator *)(NULL); - struct collection_item *item = (struct collection_item *)(NULL); + struct collection_item *ini_config = NULL; + struct collection_item *error_set = NULL; + struct collection_iterator *iterator = NULL; + struct collection_item *item = NULL; int type; - printf("\n\n===== REAL TEST START ======\n"); - printf("Reading collection\n"); - error = config_for_app("real", file, "./ini/ini.d", &ini_config,INI_STOP_ON_NONE,&error_set); - if(error) { - printf("Attempt to read configuration returned error: %d\n",error); + printf("\n\n===== REAL TEST START ======\n"); + printf("Reading collection\n"); + error = config_for_app("real", file, "./ini/ini.d", + &ini_config, INI_STOP_ON_NONE, &error_set); + if (error) { + printf("Attempt to read configuration returned error: %d\n", error); return error; } - printf("Debugging the config collection:\n"); - debug_collection(ini_config,COL_TRAVERSE_DEFAULT); - printf("Debugging the error collection:\n"); - debug_collection(error_set,COL_TRAVERSE_DEFAULT); + printf("Debugging the config collection:\n"); + debug_collection(ini_config, COL_TRAVERSE_DEFAULT); + printf("Debugging the error collection:\n"); + debug_collection(error_set, COL_TRAVERSE_DEFAULT); - printf("About to print parsing errors:\n"); - printf("\n\n----------------------\n"); + printf("About to print parsing errors:\n"); + printf("\n\n----------------------\n"); /* Output parsing errors (if any) */ - print_config_parsing_errors(stdout,error_set); - printf("----------------------\n\n\n"); + print_config_parsing_errors(stdout, error_set); + printf("----------------------\n\n\n"); - printf("About to bind iterator to print the config file contents.\n"); + printf("About to bind iterator to print the config file contents.\n"); /* Bind iterator */ - error = bind_iterator(&iterator,ini_config,COL_TRAVERSE_DEFAULT|COL_TRAVERSE_END); - if(error) { + error = bind_iterator(&iterator, ini_config, + COL_TRAVERSE_DEFAULT|COL_TRAVERSE_END); + if (error) { printf("Failed to bind iterator: %d\n",error); destroy_collection(ini_config); destroy_collection(error_set); return error; } - printf("About to start iteration loop.\n"); + printf("About to start iteration loop.\n"); do { /* Loop through a collection */ error = iterate_collection(iterator, &item); - if(error) { - printf("Error iterating collection: %d",error); + if (error) { + printf("Error iterating collection: %d", error); unbind_iterator(iterator); return error; } /* Are we done ? */ - if(item == (struct collection_item *)(NULL)) break; + if (item == (struct collection_item *)(NULL)) break; type = get_item_type(item); /* Start of the collection */ - if(type == COL_TYPE_COLLECTION) - printf("Contents of the configuration for application %s\n",get_item_property(item,NULL)); + if (type == COL_TYPE_COLLECTION) + printf("Contents of the configuration for application %s\n", get_item_property(item, NULL)); /* End of section */ - else if(type == COL_TYPE_END) printf("\n"); + else if (type == COL_TYPE_END) printf("\n"); /* Section header ? */ - else if(type == COL_TYPE_COLLECTIONREF) printf("[%s]\n",get_item_property(item,NULL)); + else if (type == COL_TYPE_COLLECTIONREF) printf("[%s]\n", get_item_property(item, NULL)); /* Anything else - we know they are all strings*/ - else printf("%s = %s\n",get_item_property(item,NULL), (char *)get_item_data(item)); + else printf("%s = %s\n", get_item_property(item, NULL), (char *)get_item_data(item)); } while(1); /* Do not forget to unbind iterator - otherwise there will be a leak */ - printf("About to clean up.\n"); + printf("About to clean up.\n"); unbind_iterator(iterator); destroy_collection(ini_config); @@ -228,40 +260,41 @@ int get_test() void *binary; int length; int i; - char **strarray; + char **strarray; char **strptr; int size; long *array; double *darray; char **prop_array; - printf("\n\n===== GET TEST START ======\n"); - printf("Reading collection\n"); - error = config_for_app("real", NULL, "./ini/ini.d", &ini_config,INI_STOP_ON_NONE,&error_set); - if(error) { - printf("Attempt to read configuration returned error: %d\n",error); + printf("\n\n===== GET TEST START ======\n"); + printf("Reading collection\n"); + error = config_for_app("real", NULL, "./ini/ini.d", + &ini_config, INI_STOP_ON_NONE, &error_set); + if (error) { + printf("Attempt to read configuration returned error: %d\n", error); return error; } - printf("Debugging the config collection:\n"); - debug_collection(ini_config,COL_TRAVERSE_DEFAULT); - printf("Debugging the error collection:\n"); - debug_collection(error_set,COL_TRAVERSE_DEFAULT); + printf("Debugging the config collection:\n"); + debug_collection(ini_config, COL_TRAVERSE_DEFAULT); + printf("Debugging the error collection:\n"); + debug_collection(error_set, COL_TRAVERSE_DEFAULT); destroy_collection(error_set); printf("Negtive test - trying to get non existing key-value pair.\n"); /* Negative test */ item = (struct collection_item *)(NULL); - error = get_config_item("monitor1","description1", ini_config, &item); - if(error) { - printf("Expected success but got error! %d\n",error); + error = get_config_item("monitor1", "description1", ini_config, &item); + if (error) { + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } /* Item should not be found */ - if(item != (struct collection_item *)(NULL)) { + if (item != (struct collection_item *)(NULL)) { printf("Expected NULL but got something else!\n"); destroy_collection(ini_config); return -1; @@ -269,9 +302,9 @@ int get_test() /* Another negative test but section exists this time */ item = (struct collection_item *)(NULL); - error = get_config_item("monitor","description1", ini_config, &item); - if(error) { - printf("Expected success but got error! %d\n",error); + error = get_config_item("monitor", "description1", ini_config, &item); + if (error) { + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } @@ -287,15 +320,15 @@ int get_test() /* Positive test */ item = (struct collection_item *)(NULL); - error = get_config_item("monitor","description", ini_config, &item); - if(error) { - printf("Expected success but got error! %d\n",error); + error = get_config_item("monitor", "description", ini_config, &item); + if (error) { + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected item but got something NULL!\n"); destroy_collection(ini_config); return -1; @@ -308,7 +341,7 @@ int get_test() /* Get a string without duplicication */ /* Negative test */ cstrn = get_const_string_config_value(NULL, NULL); - if(cstrn != NULL) { + if (cstrn != NULL) { printf("Expected error got success.\n"); destroy_collection(ini_config); return -1; @@ -319,13 +352,13 @@ int get_test() /* Now get string from the right item */ error = 0; cstr = get_const_string_config_value(item, &error); - if(error) { - printf("Expected success got error %d.\n",error); + if (error) { + printf("Expected success got error %d.\n", error); destroy_collection(ini_config); return error; } - printf("Value: [%s]\n",cstr); + printf("Value: [%s]\n", cstr); /* Same thing but create a dup */ @@ -333,13 +366,13 @@ int get_test() error = 0; str = get_string_config_value(item, &error); - if(error) { - printf("Expected success got error %d.\n",error); + if (error) { + printf("Expected success got error %d.\n", error); destroy_collection(ini_config); return error; } - printf("Value: [%s]\n",str); + printf("Value: [%s]\n", str); free(str); @@ -347,15 +380,15 @@ int get_test() printf("Convert item to number with strict conversion.\n"); item = (struct collection_item *)(NULL); - error = get_config_item("monitor","bad_number", ini_config, &item); - if(error) { - printf("Expected success but got error! %d\n",error); + error = get_config_item("monitor", "bad_number", ini_config, &item); + if (error) { + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected item but got something NULL!\n"); destroy_collection(ini_config); return -1; @@ -367,7 +400,7 @@ int get_test() /* Now try to get value in different ways */ error = 0; number = get_int_config_value(item, 1, 10, &error); - if(error) { + if (error) { /* We expected error in this case */ printf("Expected error.\n"); if(number != 10) { @@ -382,36 +415,34 @@ int get_test() error = 0; number = 1; number = get_int_config_value(item, 0, 10, &error); - if(error) { + if (error) { /* We expected error in this case */ printf("Did not expect error.\n"); destroy_collection(ini_config); return error; } - if(number != 5) { + if (number != 5) { /* We expected error in this case */ printf("We expected that the conversion will return 5.\n"); destroy_collection(ini_config); return -1; } - - /* Get real integer */ printf("Fetch another item from section \"domains/LOCAL\" named \"enumerate\".\n"); item = (struct collection_item *)(NULL); error = get_config_item("domains/LOCAL","enumerate", ini_config, &item); - if(error) { - printf("Expected success but got error! %d\n",error); + if (error) { + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected success but got NULL.\n"); destroy_collection(ini_config); return -1; @@ -422,14 +453,14 @@ int get_test() /* Take number out of it */ error = 0; number = get_int_config_value(item, 1, 100, &error); - if(error) { - printf("Did not expect error. Got %d\n",error); + if (error) { + printf("Did not expect error. Got %d\n", error); destroy_collection(ini_config); return error; } /* It is 3 in the file */ - if(number != 3) { + if (number != 3) { printf("We expected that the conversion will return 3.\n"); destroy_collection(ini_config); return -1; @@ -442,14 +473,14 @@ int get_test() /* Take number out of it */ error = 0; number_long = get_long_config_value(item, 1, 100, &error); - if(error) { - printf("Did not expect error. Got %d\n",error); + if (error) { + printf("Did not expect error. Got %d\n", error); destroy_collection(ini_config); return error; } /* It is 3 in the file */ - if(number_long != 3) { + if (number_long != 3) { printf("We expected that the conversion will return 3.\n"); destroy_collection(ini_config); return -1; @@ -462,8 +493,8 @@ int get_test() /* Take number out of it */ error = 0; number_unsigned = get_unsigned_config_value(item, 1, 100, &error); - if(error) { - printf("Did not expect error. Got %d\n",error); + if (error) { + printf("Did not expect error. Got %d\n", error); destroy_collection(ini_config); return error; } @@ -482,14 +513,14 @@ int get_test() /* Take number out of it */ error = 0; number_ulong = get_ulong_config_value(item, 1, 100, &error); - if(error) { - printf("Did not expect error. Got %d\n",error); + if (error) { + printf("Did not expect error. Got %d\n", error); destroy_collection(ini_config); return error; } /* It is 3 in the file */ - if(number_ulong != 3) { + if (number_ulong != 3) { printf("We expected that the conversion will return 3.\n"); destroy_collection(ini_config); return -1; @@ -502,14 +533,14 @@ int get_test() /* Take number out of it */ error = 0; number_double = get_double_config_value(item, 1, 100., &error); - if(error) { - printf("Did not expect error. Got %d\n",error); + if (error) { + printf("Did not expect error. Got %d\n", error); destroy_collection(ini_config); return error; } /* It is 3 in the file */ - if(number_double != 3.) { + if (number_double != 3.) { printf("We expected that the conversion will return 3.\n"); destroy_collection(ini_config); return -1; @@ -522,7 +553,7 @@ int get_test() /* Take number out of it */ error = 0; logical = get_bool_config_value(item, 1, &error); - if(!error) { + if (!error) { printf("Expect error. Got success.\n"); destroy_collection(ini_config); return -1; @@ -533,14 +564,14 @@ int get_test() item = (struct collection_item *)(NULL); error = get_config_item("domains/LOCAL","legacy", ini_config, &item); - if(error) { + if (error) { printf("Expected success but got error! %d\n",error); destroy_collection(ini_config); return error; } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected success but got NULL.\n"); destroy_collection(ini_config); return -1; @@ -550,13 +581,13 @@ int get_test() error = 0; logical = get_bool_config_value(item, 1, &error); - if(error) { - printf("Expect success got error %d.\n",error); + if (error) { + printf("Expect success got error %d.\n", error); destroy_collection(ini_config); return error; } - if(logical) { + if (logical) { printf("Expected false but got true - bad.\n"); return -1; } @@ -567,14 +598,14 @@ int get_test() item = (struct collection_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); + if (error) { + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected success but got NULL.\n"); destroy_collection(ini_config); return -1; @@ -584,14 +615,14 @@ int get_test() error = 0; binary = get_bin_config_value(item, &length, &error); - if(error) { - printf("Expect success got error %d.\n",error); + if (error) { + printf("Expect success got error %d.\n", error); destroy_collection(ini_config); return error; } printf("Binary value (expect 123) = "); - for(i=0;i<length;i++) { + for (i=0;i<length;i++) { printf("%d",*((unsigned char*)(binary)+i)); } printf("\n"); @@ -601,7 +632,7 @@ int get_test() printf("Get string array item\n"); item = (struct collection_item *)(NULL); - error = get_config_item("domains","domainsorder", ini_config, &item); + error = get_config_item("domains", "domainsorder", ini_config, &item); if(error) { printf("Expected success but got error! %d\n",error); destroy_collection(ini_config); @@ -609,7 +640,7 @@ int get_test() } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected success but got NULL.\n"); destroy_collection(ini_config); return -1; @@ -621,15 +652,15 @@ int get_test() error = 0; strarray = get_string_config_array(item, ",", NULL, &error); - if(error) { - printf("Expect success got error %d.\n",error); + if (error) { + printf("Expect success got error %d.\n", error); destroy_collection(ini_config); return error; } /* Can be used with this cycle */ strptr = strarray; - while(*strptr != NULL) { + while (*strptr != NULL) { printf("[%s]\n",*strptr); strptr++; } @@ -641,29 +672,29 @@ int get_test() error = 0; size = 0; strarray = get_string_config_array(item, ",", &size, &error); - if(error) { - printf("Expect success got error %d.\n",error); + if (error) { + printf("Expect success got error %d.\n", error); destroy_collection(ini_config); return error; } /* Can be used with this cycle */ - for(i=0;i<size;i++) printf("[%s]\n",*(strarray + i)); + for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)); free_string_config_array(strarray); printf("Get long array item\n"); item = (struct collection_item *)(NULL); - error = get_config_item("domains/EXAMPLE.COM","long_array", ini_config, &item); + error = get_config_item("domains/EXAMPLE.COM", "long_array", ini_config, &item); if(error) { - printf("Expected success but got error! %d\n",error); + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected success but got NULL.\n"); destroy_collection(ini_config); return -1; @@ -675,28 +706,28 @@ int get_test() size = 0; /* Here size is not optional!!! */ array = get_long_config_array(item, &size, &error); if(error) { - printf("Expect success got error %d.\n",error); + printf("Expect success got error %d.\n", error); destroy_collection(ini_config); return error; } /* Can be used with this cycle */ - for(i=0;i<size;i++) printf("%ld\n",*(array + i)); + for (i=0;i<size;i++) printf("%ld\n", *(array + i)); free_long_config_array(array); printf("Get double array item\n"); item = (struct collection_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); + error = get_config_item("domains/EXAMPLE.COM", "double_array", ini_config, &item); + if (error) { + printf("Expected success but got error! %d\n", error); destroy_collection(ini_config); return error; } /* Item should be found */ - if(item == (struct collection_item *)(NULL)) { + if (item == (struct collection_item *)(NULL)) { printf("Expected success but got NULL.\n"); destroy_collection(ini_config); return -1; @@ -707,21 +738,21 @@ int get_test() error = 0; size = 0; /* Here size is not optional!!! */ darray = get_double_config_array(item, &size, &error); - if(error) { - printf("Expect success got error %d.\n",error); + if (error) { + printf("Expect success got error %d.\n", error); destroy_collection(ini_config); return error; } /* Can be used with this cycle */ - for(i=0;i<size;i++) printf("%.4f\n",darray[i]); + for (i=0;i<size;i++) printf("%.4f\n", darray[i]); free_double_config_array(darray); printf("\n\nSection list - no size\n"); /* Do not care about the error or size */ - prop_array = get_section_list(ini_config,NULL,NULL); + prop_array = get_section_list(ini_config, NULL, NULL); if (prop_array == NULL) { printf("Expect success got error.\n"); destroy_collection(ini_config); @@ -730,8 +761,8 @@ int get_test() i = 0; while (prop_array[i]) { - printf("Section: [%s]\n", prop_array[i]); - i++; + printf("Section: [%s]\n", prop_array[i]); + i++; } free_section_list(prop_array); @@ -770,13 +801,13 @@ int main() { int error; - if((error=basic_test()) || - (error=single_file()) || - (error=real_test(NULL)) || - /* This should result in merged configuration */ - (error=real_test("./ini/ini.conf")) || - (error= get_test())) { - printf("Test failed! Error %d.\n",error); + if ((error = basic_test()) || + (error = single_file()) || + (error = real_test(NULL)) || + /* This should result in merged configuration */ + (error = real_test("./ini/ini.conf")) || + (error = get_test())) { + printf("Test failed! Error %d.\n", error); return -1; } printf("Success!\n"); |