diff options
-rw-r--r-- | ini/ini_configobj.h | 10 | ||||
-rw-r--r-- | ini/ini_get_valueobj.c | 10 | ||||
-rw-r--r-- | ini/ini_parse_ut.c | 32 |
3 files changed, 44 insertions, 8 deletions
diff --git a/ini/ini_configobj.h b/ini/ini_configobj.h index 5b4fc81..a186ac0 100644 --- a/ini/ini_configobj.h +++ b/ini/ini_configobj.h @@ -345,10 +345,12 @@ * * @{ */ -/** @brief Get the first value (default). */ -#define INI_GET_FIRST_VALUE 0 -/** @brief Look for the next value in the section */ -#define INI_GET_NEXT_VALUE 1 +/** Enumeration of parsing errors. */ +enum INI_GET { + INI_GET_FIRST_VALUE, /**< Get the first value (default). */ + INI_GET_NEXT_VALUE, /**< Look for the next value in the section */ + INI_GET_LAST_VALUE /**< Look for the last value in the section */ +}; /** * @} diff --git a/ini/ini_get_valueobj.c b/ini/ini_get_valueobj.c index 8a99642..a1fa19b 100644 --- a/ini/ini_get_valueobj.c +++ b/ini/ini_get_valueobj.c @@ -100,6 +100,7 @@ int ini_get_config_valueobj(const char *section, int error = EOK; struct collection_item *section_handle = NULL; struct collection_item *item = NULL; + struct collection_item *last_found = NULL; const char *to_find; char default_section[] = INI_DEFAULT_SECTION; uint64_t hash = 0; @@ -121,7 +122,7 @@ int ini_get_config_valueobj(const char *section, } if ((mode < INI_GET_FIRST_VALUE) || - (mode > INI_GET_NEXT_VALUE)) { + (mode > INI_GET_LAST_VALUE)) { TRACE_ERROR_NUMBER("Invalid argument mode:", mode); return EINVAL; } @@ -213,6 +214,11 @@ int ini_get_config_valueobj(const char *section, /* Are we done ? */ if (item == NULL) { + /* We looped to the end and found last value */ + if ((mode == INI_GET_LAST_VALUE) && (last_found)) { + item = last_found; + break; + } /* There is nothing left to look for */ ini_config_clean_state(ini_config); TRACE_FLOW_EXIT(); @@ -223,6 +229,8 @@ int ini_get_config_valueobj(const char *section, (strncasecmp(col_get_item_property(item, &len), name, name_len) == 0) && (len == name_len)) { TRACE_INFO_STRING("Item is found", name); + last_found = item; + if (mode == INI_GET_LAST_VALUE) continue; break; } } diff --git a/ini/ini_parse_ut.c b/ini/ini_parse_ut.c index 521c2f7..e743eae 100644 --- a/ini/ini_parse_ut.c +++ b/ini/ini_parse_ut.c @@ -2307,6 +2307,31 @@ int get_test(void) /***************************************/ + INIOUT(printf("\nGet last value\n")); + + vo = NULL; + error = ini_get_config_valueobj("domains/EXAMPLE.COM", + "server", + ini_config, + INI_GET_LAST_VALUE, + &vo); + if(error) { + printf("Expected success but got error! %d\n", error); + ini_config_destroy(ini_config); + return error; + } + + /* Value should be found */ + if (vo == NULL) { + printf("Expected success but got NULL.\n"); + ini_config_destroy(ini_config); + return -1; + } + + INIOUT(value_print("server", vo)); + + /***************************************/ + INIOUT(printf("\nGet sequence of the multi-value keys\n")); vo = NULL; @@ -2330,6 +2355,7 @@ int get_test(void) INIOUT(value_print("server", vo)); + do { vo = NULL; @@ -2388,7 +2414,7 @@ int get_test(void) "server", ini_config, INI_GET_NEXT_VALUE, - &vo); + &vo); if(error) { printf("Expected success but got error! %d\n", error); ini_config_destroy(ini_config); @@ -2406,7 +2432,7 @@ int get_test(void) "empty_value", ini_config, INI_GET_NEXT_VALUE, - &vo); + &vo); if(error) { printf("Expected success but got error! %d\n", error); ini_config_destroy(ini_config); @@ -2424,7 +2450,7 @@ int get_test(void) /***************************************/ - INIOUT(printf("\nGet multi-value keys with key interrupt\n")); + INIOUT(printf("\nGet multi-value keys with section interrupt\n")); i = 0; |