ini_file_reader: return the last when get single value

pull/12/head
yuqing 2017-01-19 18:19:45 +08:00
parent dc702cddc0
commit 87995c650d
3 changed files with 47 additions and 29 deletions

View File

@ -1,6 +1,8 @@
Version 1.34 2017-01-19 Version 1.34 2017-01-19
* ini_file_reader: LOCAL_IP support CIDR addresses * 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 Version 1.33 2017-01-04
* add function hash_get_prime_capacity * add function hash_get_prime_capacity

View File

@ -1591,29 +1591,31 @@ void iniFreeContext(IniContext *pContext)
#define INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ #define INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \
targetItem, pItem, return_val) \ targetItem, pItem, return_val) \
if (szSectionName == NULL || *szSectionName == '\0') \ do { \
{ \ if (szSectionName == NULL || *szSectionName == '\0') \
pSection = &pContext->global; \ { \
} \ pSection = &pContext->global; \
else \ } \
{ \ else \
pSection = (IniSection *)hash_find(&pContext->sections, \ { \
szSectionName, strlen(szSectionName)); \ pSection = (IniSection *)hash_find(&pContext->sections, \
if (pSection == NULL) \ szSectionName, strlen(szSectionName)); \
{ \ if (pSection == NULL) \
return return_val; \ { \
} \ return return_val; \
} \ } \
\ } \
if (pSection->count <= 0) \ \
{ \ if (pSection->count <= 0) \
return return_val; \ { \
} \ return return_val; \
\ } \
snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \ \
pItem = (IniItem *)bsearch(&targetItem, pSection->items, \ snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \
pSection->count, sizeof(IniItem), iniCompareByItemName); pItem = (IniItem *)bsearch(&targetItem, pSection->items, \
pSection->count, sizeof(IniItem), iniCompareByItemName); \
} while (0)
char *iniGetStrValue(const char *szSectionName, const char *szItemName, \ char *iniGetStrValue(const char *szSectionName, const char *szItemName, \
@ -1621,19 +1623,29 @@ char *iniGetStrValue(const char *szSectionName, const char *szItemName, \
{ {
IniItem targetItem; IniItem targetItem;
IniSection *pSection; IniSection *pSection;
IniItem *pFound;
IniItem *pItem; IniItem *pItem;
IniItem *pItemEnd;
INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \
targetItem, pItem, NULL) targetItem, pFound, NULL);
if (pFound == NULL)
if (pItem == NULL)
{ {
return NULL; return NULL;
} }
else
pItemEnd = pSection->items + pSection->count;
for (pItem=pFound+1; pItem<pItemEnd; pItem++)
{ {
return pItem->value; if (strcmp(pItem->name, szItemName) != 0)
{
break;
}
pFound = pItem;
} }
return pFound->value;
} }
int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \ int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \
@ -1746,7 +1758,7 @@ IniItem *iniGetValuesEx(const char *szSectionName, const char *szItemName, \
*nTargetCount = 0; *nTargetCount = 0;
INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \
targetItem, pFound, NULL) targetItem, pFound, NULL);
if (pFound == NULL) if (pFound == NULL)
{ {
return NULL; return NULL;

View File

@ -15,6 +15,10 @@ int main(int argc, char *argv[])
int result; int result;
IniContext context; IniContext context;
const char *szFilename = "/home/yuqing/watchd-config/order.conf"; const char *szFilename = "/home/yuqing/watchd-config/order.conf";
if (argc > 1) {
szFilename = argv[1];
}
log_init(); log_init();
g_log_context.log_level = LOG_DEBUG; g_log_context.log_level = LOG_DEBUG;