From 87995c650ddab5ce9d236f7f274767e5c95b3f41 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 19 Jan 2017 18:19:45 +0800 Subject: [PATCH] ini_file_reader: return the last when get single value --- HISTORY | 2 ++ src/ini_file_reader.c | 70 ++++++++++++++++++++++--------------- src/tests/test_ini_parser.c | 4 +++ 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/HISTORY b/HISTORY index 1f87b35..3619413 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,8 @@ Version 1.34 2017-01-19 * ini_file_reader: LOCAL_IP support CIDR addresses + * ini_file_reader: return the last when get single value, + such as iniGetStrValue and iniGetIntValue Version 1.33 2017-01-04 * add function hash_get_prime_capacity diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index b922301..155c6c9 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -1591,29 +1591,31 @@ void iniFreeContext(IniContext *pContext) #define INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ - targetItem, pItem, return_val) \ - if (szSectionName == NULL || *szSectionName == '\0') \ - { \ - pSection = &pContext->global; \ - } \ - else \ - { \ - pSection = (IniSection *)hash_find(&pContext->sections, \ - szSectionName, strlen(szSectionName)); \ - if (pSection == NULL) \ - { \ - return return_val; \ - } \ - } \ - \ - if (pSection->count <= 0) \ - { \ - return return_val; \ - } \ - \ - snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \ - pItem = (IniItem *)bsearch(&targetItem, pSection->items, \ - pSection->count, sizeof(IniItem), iniCompareByItemName); + targetItem, pItem, return_val) \ +do { \ + if (szSectionName == NULL || *szSectionName == '\0') \ + { \ + pSection = &pContext->global; \ + } \ + else \ + { \ + pSection = (IniSection *)hash_find(&pContext->sections, \ + szSectionName, strlen(szSectionName)); \ + if (pSection == NULL) \ + { \ + return return_val; \ + } \ + } \ + \ + if (pSection->count <= 0) \ + { \ + return return_val; \ + } \ + \ + snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \ + pItem = (IniItem *)bsearch(&targetItem, pSection->items, \ + pSection->count, sizeof(IniItem), iniCompareByItemName); \ +} while (0) char *iniGetStrValue(const char *szSectionName, const char *szItemName, \ @@ -1621,19 +1623,29 @@ char *iniGetStrValue(const char *szSectionName, const char *szItemName, \ { IniItem targetItem; IniSection *pSection; + IniItem *pFound; IniItem *pItem; + IniItem *pItemEnd; INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ - targetItem, pItem, NULL) - - if (pItem == NULL) + targetItem, pFound, NULL); + if (pFound == NULL) { return NULL; } - else + + pItemEnd = pSection->items + pSection->count; + for (pItem=pFound+1; pItemvalue; + if (strcmp(pItem->name, szItemName) != 0) + { + break; + } + + pFound = pItem; } + + return pFound->value; } int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \ @@ -1746,7 +1758,7 @@ IniItem *iniGetValuesEx(const char *szSectionName, const char *szItemName, \ *nTargetCount = 0; INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ - targetItem, pFound, NULL) + targetItem, pFound, NULL); if (pFound == NULL) { return NULL; diff --git a/src/tests/test_ini_parser.c b/src/tests/test_ini_parser.c index e5cf587..1044bd2 100644 --- a/src/tests/test_ini_parser.c +++ b/src/tests/test_ini_parser.c @@ -15,6 +15,10 @@ int main(int argc, char *argv[]) int result; IniContext context; const char *szFilename = "/home/yuqing/watchd-config/order.conf"; + + if (argc > 1) { + szFilename = argv[1]; + } log_init(); g_log_context.log_level = LOG_DEBUG;