From 85354b6ef61019f52882b0e35badaf7c70166eed Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 2 Dec 2020 18:10:45 +0800 Subject: [PATCH] add functions: iniGetByteValueEx and iniGetIntCorrectValueEx --- src/fast_timer.c | 6 +-- src/ini_file_reader.c | 100 ++++++++++++++++++++++++++++++++++++++++++ src/ini_file_reader.h | 97 +++++++++++++++++++++++++++++++++++++++- src/locked_timer.c | 6 +-- 4 files changed, 197 insertions(+), 12 deletions(-) diff --git a/src/fast_timer.c b/src/fast_timer.c index bd1f74b..73bfb5b 100644 --- a/src/fast_timer.c +++ b/src/fast_timer.c @@ -85,7 +85,7 @@ void fast_timer_add_ex(FastTimer *timer, FastTimerEntry *entry, new_expires = expires; new_set_expires = set_expires; } else { - new_expires = timer->current_time; + new_expires = timer->current_time + 1; //plus 1 for rare case new_set_expires = true; } slot = TIMER_GET_SLOT_POINTER(timer, new_expires); @@ -97,10 +97,6 @@ int fast_timer_modify(FastTimer *timer, FastTimerEntry *entry, { int result; - if (new_expires <= timer->current_time) { - return ETIMEDOUT; - } - if (new_expires > entry->expires) { entry->rehash = TIMER_GET_SLOT_INDEX(timer, new_expires) != TIMER_GET_SLOT_INDEX(timer, entry->expires); diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 5e71eeb..a4afb84 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -2854,6 +2854,44 @@ char *iniGetStrValueEx(const char *szSectionName, const char *szItemName, return pFound->value; } +#define INI_FILL_SECTION_PROMPT(prompt, size, section_name) \ + do { \ + if (section_name != NULL && *(section_name) != '\0') { \ + snprintf(prompt, size, "section: %s, ", section_name); \ + } else { \ + *prompt = '\0'; \ + } \ + } while (0) + +int64_t iniCheckAndCorrectIntValue(IniFullContext *pIniContext, + const char *szItemName, const int64_t nValue, + const int64_t nMinValue, const int64_t nMaxValue) +{ + char section_prompt[128]; + if (nValue < nMinValue) { + INI_FILL_SECTION_PROMPT(section_prompt, sizeof(section_prompt), + pIniContext->section_name); + logWarning("file: "__FILE__", line: %d, " + "config file: %s, %sitem name: %s, item value: %"PRId64 + " < min value: %"PRId64", set to min value: %"PRId64, + __LINE__, pIniContext->filename, section_prompt, szItemName, + nValue, nMinValue, nMinValue); + + return nMinValue; + } else if (nValue > nMaxValue) { + INI_FILL_SECTION_PROMPT(section_prompt, sizeof(section_prompt), + pIniContext->section_name); + logWarning("file: "__FILE__", line: %d, " + "config file: %s, %sitem name: %s, item value: %"PRId64 + " > max value: %"PRId64", set to max value: %"PRId64, + __LINE__, pIniContext->filename, section_prompt, szItemName, + nValue, nMaxValue, nMaxValue); + return nMaxValue; + } + + return nValue; +} + int64_t iniGetInt64ValueEx(const char *szSectionName, const char *szItemName, IniContext *pContext, const int64_t nDefaultValue, const bool bRetryGlobal) @@ -2872,6 +2910,56 @@ int64_t iniGetInt64ValueEx(const char *szSectionName, } } +int64_t iniGetInt64CorrectValueEx(IniFullContext *pIniContext, + const char *szItemName, const int64_t nDefaultValue, + const int64_t nMinValue, const int64_t nMaxValue, + const bool bRetryGlobal) +{ + int64_t value; + + value = iniGetInt64ValueEx(pIniContext->section_name, szItemName, + pIniContext->context, nDefaultValue, bRetryGlobal); + return iniCheckAndCorrectIntValue(pIniContext, szItemName, + value, nMinValue, nMaxValue); +} + +int64_t iniGetByteValueEx(const char *szSectionName, + const char *szItemName, IniContext *pContext, + const int64_t nDefaultValue, const int nDefaultUnitBytes, + const bool bRetryGlobal) +{ + char *pValue; + int64_t nValue; + + pValue = iniGetStrValueEx(szSectionName, + szItemName, pContext, bRetryGlobal); + if (pValue == NULL) + { + return nDefaultValue; + } + + if (parse_bytes(pValue, nDefaultUnitBytes, &nValue) != 0) + { + return nDefaultValue; + } + + return nValue; +} + +int64_t iniGetByteCorrectValueEx(IniFullContext *pIniContext, + const char *szItemName, const int64_t nDefaultValue, + const int nDefaultUnitBytes, const int64_t nMinValue, + const int64_t nMaxValue, const bool bRetryGlobal) +{ + int64_t nValue; + + nValue = iniGetByteValueEx(pIniContext->section_name, szItemName, + pIniContext->context, nDefaultValue, nDefaultUnitBytes, + bRetryGlobal); + return iniCheckAndCorrectIntValue(pIniContext, szItemName, + nValue, nMinValue, nMaxValue); +} + int iniGetIntValueEx(const char *szSectionName, const char *szItemName, IniContext *pContext, const int nDefaultValue, const bool bRetryGlobal) @@ -2890,6 +2978,18 @@ int iniGetIntValueEx(const char *szSectionName, } } +int iniGetIntCorrectValueEx(IniFullContext *pIniContext, + const char *szItemName, const int nDefaultValue, + const int nMinValue, const int nMaxValue, const bool bRetryGlobal) +{ + int value; + + value = iniGetIntValueEx(pIniContext->section_name, szItemName, + pIniContext->context, nDefaultValue, bRetryGlobal); + return iniCheckAndCorrectIntValue(pIniContext, szItemName, + value, nMinValue, nMaxValue); +} + double iniGetDoubleValueEx(const char *szSectionName, const char *szItemName, IniContext *pContext, const double dbDefaultValue, const bool bRetryGlobal) diff --git a/src/ini_file_reader.h b/src/ini_file_reader.h index ab75081..25cf112 100644 --- a/src/ini_file_reader.h +++ b/src/ini_file_reader.h @@ -136,6 +136,25 @@ extern "C" { #define iniGetPercentValue(ini_ctx, item_name, item_value, default_value) \ iniGetPercentValueEx(ini_ctx, item_name, item_value, default_value, false) +#define iniGetByteValue(szSectionName, szItemName, pContext, nDefaultValue) \ + iniGetByteValueEx(szSectionName, szItemName, pContext, \ + nDefaultValue, 1, false) + +#define iniGetIntCorrectValue(ini_ctx, item_name, \ + default_value, min_value, max_value) \ + iniGetIntCorrectValueEx(ini_ctx, item_name, \ + default_value, min_value, max_value, false) + +#define iniGetInt64CorrectValue(ini_ctx, item_name, \ + default_value, min_value, max_value) \ + iniGetInt64CorrectValueEx(ini_ctx, item_name, \ + default_value, min_value, max_value, false) + +#define iniGetByteCorrectValue(ini_ctx, item_name, \ + default_value, min_value, max_value) \ + iniGetByteCorrectValueEx(ini_ctx, item_name, \ + default_value, 1, min_value, max_value, false) + int iniSetAnnotationCallBack(AnnotationEntry *annotations, int count); void iniDestroyAnnotationCallBack(); @@ -232,6 +251,33 @@ int iniGetIntValueEx(const char *szSectionName, const char *szItemName, IniContext *pContext, const int nDefaultValue, const bool bRetryGlobal); +/** check and correct item value + * parameters: + * pIniContext: the full ini context + * szItemName: the item name + * nValue: the item value + * nMinValue: the min value to check (including) + * nMaxValue: the max value to check (including) + * return: corrected value +*/ +int64_t iniCheckAndCorrectIntValue(IniFullContext *pIniContext, + const char *szItemName, const int64_t nValue, + const int64_t nMinValue, const int64_t nMaxValue); + +/** get item correct value (32 bits integer) + * parameters: + * pIniContext: the full ini context + * szItemName: the item name + * nDefaultValue: the default value + * nMinValue: the min value to check (including) + * nMaxValue: the max value to check (including) + * bRetryGlobal: if fetch from global section when the item not exist + * return: item value, return nDefaultValue when the item not exist +*/ +int iniGetIntCorrectValueEx(IniFullContext *pIniContext, + const char *szItemName, const int nDefaultValue, + const int nMinValue, const int nMaxValue, const bool bRetryGlobal); + /** get item string value array * parameters: * szSectionName: the section name, NULL or empty string for @@ -244,9 +290,9 @@ int iniGetIntValueEx(const char *szSectionName, IniItem *iniGetValuesEx(const char *szSectionName, const char *szItemName, IniContext *pContext, int *nTargetCount); -/** get item int64 value (64 bits) +/** get item value (64 bits integer) * parameters: - * szSectionName: the section name, NULL or empty string for + * szSectionName: the section name, NULL or empty string for * global section * szItemName: the item name * pContext: the ini context @@ -258,6 +304,53 @@ int64_t iniGetInt64ValueEx(const char *szSectionName, const char *szItemName, IniContext *pContext, const int64_t nDefaultValue, const bool bRetryGlobal); +/** get item correct value (64 bits integer) + * parameters: + * pIniContext: the full ini context + * szItemName: the item name + * nDefaultValue: the default value + * nMinValue: the min value to check (including) + * nMaxValue: the max value to check (including) + * bRetryGlobal: if fetch from global section when the item not exist + * return: int64 value, return nDefaultValue when the item not exist +*/ +int64_t iniGetInt64CorrectValueEx(IniFullContext *pIniContext, + const char *szItemName, const int64_t nDefaultValue, + const int64_t nMinValue, const int64_t nMaxValue, + const bool bRetryGlobal); + +/** get item byte value (64 bits integer) + * parameters: + * szSectionName: the section name, NULL or empty string for + * global section + * szItemName: the item name + * pContext: the ini context + * nDefaultValue: the default value + * nDefaultUnitBytes: the default byte unit, such as 1 for byte, 1024 for KB + * bRetryGlobal: if fetch from global section when the item not exist + * return: int64 value, return nDefaultValue when the item not exist +*/ +int64_t iniGetByteValueEx(const char *szSectionName, + const char *szItemName, IniContext *pContext, + const int64_t nDefaultValue, const int nDefaultUnitBytes, + const bool bRetryGlobal); + +/** get item correct byte value (64 bits integer) + * parameters: + * pIniContext: the full ini context + * szItemName: the item name + * nDefaultValue: the default value + * nDefaultUnitBytes: the default byte unit, such as 1 for byte, 1024 for KB + * nMinValue: the min value to check (including) + * nMaxValue: the max value to check (including) + * bRetryGlobal: if fetch from global section when the item not exist + * return: int64 value, return nDefaultValue when the item not exist +*/ +int64_t iniGetByteCorrectValueEx(IniFullContext *pIniContext, + const char *szItemName, const int64_t nDefaultValue, + const int nDefaultUnitBytes, const int64_t nMinValue, + const int64_t nMaxValue, const bool bRetryGlobal); + /** get item boolean value * parameters: * szSectionName: the section name, NULL or empty string for diff --git a/src/locked_timer.c b/src/locked_timer.c index 909c667..1f71d55 100644 --- a/src/locked_timer.c +++ b/src/locked_timer.c @@ -227,7 +227,7 @@ void locked_timer_add_ex(LockedTimer *timer, LockedTimerEntry *entry, new_expires = expires; new_flags = flags; } else { - new_expires = timer->current_time; + new_expires = timer->current_time + 1; //plus 1 for rare case new_flags = flags | FAST_TIMER_FLAGS_SET_EXPIRES; } slot = TIMER_GET_SLOT_POINTER(timer, new_expires); @@ -240,10 +240,6 @@ int locked_timer_modify(LockedTimer *timer, LockedTimerEntry *entry, int result; int slot_index; - if (new_expires <= timer->current_time) { - return ETIMEDOUT; - } - if (new_expires > entry->expires) { if ((result=check_entry_status(timer, entry, &slot_index)) != 0) { return result;