ini support retry gloabal section when item not exist

pull/37/head
YuQing 2020-09-13 13:20:41 +08:00
parent bc5efd235e
commit 23d8adf05f
2 changed files with 94 additions and 38 deletions

View File

@ -114,6 +114,10 @@ static SetDirectiveVars *iniGetVars(IniContext *pContext);
trim_left(pStr); \ trim_left(pStr); \
} while (0) } while (0)
#define RETRY_FETCH_GLOBAL(szSectionName, bRetryGlobal) \
((szSectionName != NULL && szSectionName != '\0') && bRetryGlobal)
static void iniDoSetAnnotations(AnnotationEntry *src, const int src_count, static void iniDoSetAnnotations(AnnotationEntry *src, const int src_count,
AnnotationEntry *dest, int *dest_count) AnnotationEntry *dest, int *dest_count)
{ {
@ -2798,8 +2802,8 @@ do { \
} while (0) } while (0)
char *iniGetStrValue(const char *szSectionName, const char *szItemName, \ char *iniGetStrValueEx(const char *szSectionName, const char *szItemName,
IniContext *pContext) IniContext *pContext, const bool bRetryGlobal)
{ {
IniItem targetItem; IniItem targetItem;
IniSection *pSection; IniSection *pSection;
@ -2807,33 +2811,50 @@ char *iniGetStrValue(const char *szSectionName, const char *szItemName, \
IniItem *pItem; IniItem *pItem;
IniItem *pItemEnd; IniItem *pItemEnd;
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; if (RETRY_FETCH_GLOBAL(szSectionName, bRetryGlobal))
{
szSectionName = NULL;
INI_FIND_ITEM(szSectionName, szItemName, pContext,
pSection, targetItem, pFound, NULL);
if (pFound == NULL)
{
return NULL;
}
}
else
{
return NULL;
}
} }
pItemEnd = pSection->items + pSection->count; pItemEnd = pSection->items + pSection->count;
for (pItem=pFound+1; pItem<pItemEnd; pItem++) for (pItem=pFound+1; pItem<pItemEnd; pItem++)
{ {
if (strcmp(pItem->name, szItemName) != 0) if (strcmp(pItem->name, szItemName) == 0)
{ {
break; pFound = pItem;
} }
else
pFound = pItem; {
} break;
}
}
return pFound->value; return pFound->value;
} }
int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \ int64_t iniGetInt64ValueEx(const char *szSectionName,
IniContext *pContext, const int64_t nDefaultValue) const char *szItemName, IniContext *pContext,
const int64_t nDefaultValue, const bool bRetryGlobal)
{ {
char *pValue; char *pValue;
pValue = iniGetStrValue(szSectionName, szItemName, pContext); pValue = iniGetStrValueEx(szSectionName, szItemName,
pContext, bRetryGlobal);
if (pValue == NULL) if (pValue == NULL)
{ {
return nDefaultValue; return nDefaultValue;
@ -2844,28 +2865,32 @@ int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \
} }
} }
int iniGetIntValue(const char *szSectionName, const char *szItemName, \ int iniGetIntValueEx(const char *szSectionName,
IniContext *pContext, const int nDefaultValue) const char *szItemName, IniContext *pContext,
const int nDefaultValue, const bool bRetryGlobal)
{ {
char *pValue; char *pValue;
pValue = iniGetStrValue(szSectionName, szItemName, pContext); pValue = iniGetStrValueEx(szSectionName, szItemName,
pContext, bRetryGlobal);
if (pValue == NULL) if (pValue == NULL)
{ {
return nDefaultValue; return nDefaultValue;
} }
else else
{ {
return atoi(pValue); return atoi(pValue);
} }
} }
double iniGetDoubleValue(const char *szSectionName, const char *szItemName, \ double iniGetDoubleValueEx(const char *szSectionName,
IniContext *pContext, const double dbDefaultValue) const char *szItemName, IniContext *pContext,
const double dbDefaultValue, const bool bRetryGlobal)
{ {
char *pValue; char *pValue;
pValue = iniGetStrValue(szSectionName, szItemName, pContext); pValue = iniGetStrValueEx(szSectionName, szItemName,
pContext, bRetryGlobal);
if (pValue == NULL) if (pValue == NULL)
{ {
return dbDefaultValue; return dbDefaultValue;
@ -2876,12 +2901,14 @@ double iniGetDoubleValue(const char *szSectionName, const char *szItemName, \
} }
} }
bool iniGetBoolValue(const char *szSectionName, const char *szItemName, \ bool iniGetBoolValueEx(const char *szSectionName,
IniContext *pContext, const bool bDefaultValue) const char *szItemName, IniContext *pContext,
const bool bDefaultValue, const bool bRetryGlobal)
{ {
char *pValue; char *pValue;
pValue = iniGetStrValue(szSectionName, szItemName, pContext); pValue = iniGetStrValueEx(szSectionName, szItemName,
pContext, bRetryGlobal);
if (pValue == NULL) if (pValue == NULL)
{ {
return bDefaultValue; return bDefaultValue;

View File

@ -107,6 +107,26 @@ extern "C" {
strcasecmp(pValue, "on") == 0 || \ strcasecmp(pValue, "on") == 0 || \
strcmp(pValue, "1") == 0) strcmp(pValue, "1") == 0)
#define iniGetStrValue(szSectionName, szItemName, pContext) \
iniGetStrValueEx(szSectionName, szItemName, pContext, false)
#define iniGetIntValue(szSectionName, szItemName, pContext, nDefaultValue) \
iniGetIntValueEx(szSectionName, szItemName, pContext, \
nDefaultValue, false)
#define iniGetInt64Value(szSectionName, szItemName, pContext, nDefaultValue) \
iniGetInt64ValueEx(szSectionName, szItemName, pContext, \
nDefaultValue, false)
#define iniGetDoubleValue(szSectionName, szItemName, pContext, dbDefaultValue) \
iniGetDoubleValueEx(szSectionName, szItemName, pContext, \
dbDefaultValue, false)
#define iniGetBoolValue(szSectionName, szItemName, pContext, bDefaultValue) \
iniGetBoolValueEx(szSectionName, szItemName, pContext, \
bDefaultValue, false)
int iniSetAnnotationCallBack(AnnotationEntry *annotations, int count); int iniSetAnnotationCallBack(AnnotationEntry *annotations, int count);
void iniDestroyAnnotationCallBack(); void iniDestroyAnnotationCallBack();
@ -170,10 +190,11 @@ void iniFreeContext(IniContext *pContext);
* global section * global section
* szItemName: the item name * szItemName: the item name
* pContext: the ini context * pContext: the ini context
* bRetryGlobal: if fetch from global section when the item not exist
* return: item value, return NULL when the item not exist * return: item value, return NULL when the item not exist
*/ */
char *iniGetStrValue(const char *szSectionName, const char *szItemName, \ char *iniGetStrValueEx(const char *szSectionName, const char *szItemName,
IniContext *pContext); IniContext *pContext, const bool bRetryGlobal);
/** get item string value /** get item string value
* parameters: * parameters:
@ -195,10 +216,12 @@ int iniGetValues(const char *szSectionName, const char *szItemName, \
* szItemName: the item name * szItemName: the item name
* pContext: the ini context * pContext: the ini context
* nDefaultValue: the default value * nDefaultValue: the default value
* bRetryGlobal: if fetch from global section when the item not exist
* return: item value, return nDefaultValue when the item not exist * return: item value, return nDefaultValue when the item not exist
*/ */
int iniGetIntValue(const char *szSectionName, const char *szItemName, \ int iniGetIntValueEx(const char *szSectionName,
IniContext *pContext, const int nDefaultValue); const char *szItemName, IniContext *pContext,
const int nDefaultValue, const bool bRetryGlobal);
/** get item string value array /** get item string value array
* parameters: * parameters:
@ -209,7 +232,7 @@ int iniGetIntValue(const char *szSectionName, const char *szItemName, \
* nTargetCount: store the item value count * nTargetCount: store the item value count
* return: item value array, return NULL when the item not exist * return: item value array, return NULL when the item not exist
*/ */
IniItem *iniGetValuesEx(const char *szSectionName, const char *szItemName, \ IniItem *iniGetValuesEx(const char *szSectionName, const char *szItemName,
IniContext *pContext, int *nTargetCount); IniContext *pContext, int *nTargetCount);
/** get item int64 value (64 bits) /** get item int64 value (64 bits)
@ -219,10 +242,12 @@ IniItem *iniGetValuesEx(const char *szSectionName, const char *szItemName, \
* szItemName: the item name * szItemName: the item name
* pContext: the ini context * pContext: the ini context
* nDefaultValue: the default value * nDefaultValue: the default value
* bRetryGlobal: if fetch from global section when the item not exist
* return: int64 value, return nDefaultValue when the item not exist * return: int64 value, return nDefaultValue when the item not exist
*/ */
int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \ int64_t iniGetInt64ValueEx(const char *szSectionName,
IniContext *pContext, const int64_t nDefaultValue); const char *szItemName, IniContext *pContext,
const int64_t nDefaultValue, const bool bRetryGlobal);
/** get item boolean value /** get item boolean value
* parameters: * parameters:
@ -231,10 +256,12 @@ int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \
* szItemName: the item name * szItemName: the item name
* pContext: the ini context * pContext: the ini context
* bDefaultValue: the default value * bDefaultValue: the default value
* bRetryGlobal: if fetch from global section when the item not exist
* return: item boolean value, return bDefaultValue when the item not exist * return: item boolean value, return bDefaultValue when the item not exist
*/ */
bool iniGetBoolValue(const char *szSectionName, const char *szItemName, \ bool iniGetBoolValueEx(const char *szSectionName,
IniContext *pContext, const bool bDefaultValue); const char *szItemName, IniContext *pContext,
const bool bDefaultValue, const bool bRetryGlobal);
/** get item double value /** get item double value
* parameters: * parameters:
@ -243,10 +270,12 @@ bool iniGetBoolValue(const char *szSectionName, const char *szItemName, \
* szItemName: the item name * szItemName: the item name
* pContext: the ini context * pContext: the ini context
* dbDefaultValue: the default value * dbDefaultValue: the default value
* bRetryGlobal: if fetch from global section when the item not exist
* return: item value, return dbDefaultValue when the item not exist * return: item value, return dbDefaultValue when the item not exist
*/ */
double iniGetDoubleValue(const char *szSectionName, const char *szItemName, \ double iniGetDoubleValueEx(const char *szSectionName,
IniContext *pContext, const double dbDefaultValue); const char *szItemName, IniContext *pContext,
const double dbDefaultValue, const bool bRetryGlobal);
/** print all items /** print all items
* parameters: * parameters: