From ad51130d2dd534b701a8da56c453f4e613b0cda8 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 17 Aug 2015 16:05:32 +0800 Subject: [PATCH 01/10] add annotation mechanism for ini parse --- src/ini_file_reader.c | 281 +++++++++++++++++++++++++++++++++++------- src/ini_file_reader.h | 9 ++ 2 files changed, 246 insertions(+), 44 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index a55e0f2..9df28b3 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -22,11 +22,44 @@ #define _LINE_BUFFER_SIZE 512 #define _INIT_ALLOC_ITEM_COUNT 32 +static AnnotationMap *g_annotataionMap; + +static int remallocSection(IniSection *pSection, IniItem **pItem); static int iniDoLoadFromFile(const char *szFilename, \ IniContext *pContext); static int iniDoLoadItemsFromBuffer(char *content, \ IniContext *pContext); +void iniSetAnnotationCallBack(AnnotationMap *map, int count) +{ + int bytes; + AnnotationMap *p; + + if (count <= 0) { + logWarning("file: "__FILE__", line: %d, " \ + "iniSetAnnotationCallBack fail count(%d) is incorrectly.", \ + __LINE__, count); + return; + } + + bytes = sizeof(AnnotationMap) * (count + 1); + g_annotataionMap = (AnnotationMap *) malloc(bytes); + if (g_annotataionMap == NULL) { + logError("file: "__FILE__", line: %d, " \ + "malloc fail, errno: %d, error info: %s", \ + __LINE__, errno, STRERROR(errno)); + return; + } + + memcpy(g_annotataionMap, map, bytes); + + p = g_annotataionMap + count; + p->func_name = NULL; + p->func_init = NULL; + p->func_destroy = NULL; + p->func_get = NULL; +} + static int iniCompareByItemName(const void *p1, const void *p2) { return strcmp(((IniItem *)p1)->name, ((IniItem *)p2)->name); @@ -241,19 +274,30 @@ int iniLoadFromBuffer(char *content, IniContext *pContext) static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) { + AnnotationMap *pAnnoMap; IniSection *pSection; IniItem *pItem; char *pLine; char *pLastEnd; char *pEqualChar; + char *pFunc_name; + char *pItemName; + char *pAnnoItemLine; char *pIncludeFilename; + char *pItemValue[100]; char full_filename[MAX_PATH_SIZE]; + int i; int nLineLen; int nNameLen; + int nItemCnt; int nValueLen; int result; + int isAnnotation; result = 0; + nItemCnt = -1; + pAnnoItemLine = NULL; + isAnnotation = 0; pLastEnd = content - 1; pSection = pContext->current_section; pItem = pSection->items + pSection->count; @@ -328,6 +372,136 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) free(pIncludeFilename); continue; } + else if (isAnnotation || (*pLine == '#' && \ + strncasecmp(pLine+1, "@function", 9) == 0 && \ + (*(pLine+10) == ' ' || *(pLine+10) == '\t'))) + { + if (isAnnotation == 0) { + pFunc_name = strdup(pLine + 11); + if (pFunc_name == NULL) { + logError("file: "__FILE__", line: %d, " \ + "strdup %d bytes fail", __LINE__, \ + (int)strlen(pLine + 11) + 1); + result = errno != 0 ? errno : ENOMEM; + break; + } + + trim(pFunc_name); + isAnnotation = 1; + pAnnoItemLine = pLastEnd + 1; + continue; + } + + isAnnotation = 0; + + if (pLine != pAnnoItemLine) { + logError("file: "__FILE__", line: %d, " \ + "the @function and annotation item " \ + "must be next to each other", __LINE__); + result = EINVAL; + free(pFunc_name); + break; + } + + trim(pLine); + if (*pLine == '#' || *pLine == '\0') { + logError("file: "__FILE__", line: %d, " \ + "the @function and annotation item " \ + "must be next to each other", __LINE__); + result = EINVAL; + free(pFunc_name); + break; + } + + pEqualChar = strchr(pLine, '='); + if (pEqualChar == NULL) { + logError("file: "__FILE__", line: %d, " \ + "the @function and annotation item " \ + "must be next to each other", __LINE__); + result = EINVAL; + free(pFunc_name); + break; + } + + nNameLen = pEqualChar - pLine; + nValueLen = strlen(pLine) - (nNameLen + 1); + if (nNameLen > FAST_INI_ITEM_NAME_LEN) { + nNameLen = FAST_INI_ITEM_NAME_LEN; + } + + if (pSection->count >= pSection->alloc_count) { + result = remallocSection(pSection, &pItem); + if (result) { + free(pFunc_name); + break; + } + } + + memcpy(pItem->name, pLine, nNameLen); + memcpy(pItem->value, pEqualChar + 1, nValueLen); + + trim(pItem->name); + trim(pItem->value); + + if (g_annotataionMap == NULL) { + logWarning("file: "__FILE__", line: %d, " \ + "not set annotataionMap and (%s) will use " \ + "the item value (%s)", __LINE__, pItem->name, + pItem->value); + pSection->count++; + pItem++; + free(pFunc_name); + continue; + } + + pAnnoMap = g_annotataionMap; + while (pAnnoMap->func_name) { + if (strlen(pAnnoMap->func_name) == strlen(pFunc_name) + && strcasecmp(pFunc_name, pAnnoMap->func_name) == 0) + { + pAnnoMap->func_init(); + nItemCnt = pAnnoMap->func_get(pItem->value, pItemValue, 100); + break; + } + pAnnoMap++; + } + + if (nItemCnt == -1) { + logWarning("file: "__FILE__", line: %d, " \ + "not found corresponding annotation func (%s)" \ + " and (%s) will use the item value (%s).", __LINE__, + pItem->name, pFunc_name, pItem->value); + pSection->count++; + pItem++; + free(pFunc_name); + continue; + } else if (nItemCnt == 0) { + logWarning("file: "__FILE__", line: %d, " \ + "annotation func(%s) execute failed and" + "(%s) will use the item value (%s)", __LINE__, + pItem->name, pFunc_name, pItem->value); + pSection->count++; + pItem++; + free(pFunc_name); + continue; + } + + pItemName = pItem->name; + for (i = 0; i < nItemCnt; i++) { + memcpy(pItem->name, pItemName, strlen(pItemName) + 1); + memcpy(pItem->value, pItemValue[i], strlen(pItemValue[i]) + 1); + pSection->count++; + pItem++; + if (pSection->count >= pSection->alloc_count) { + result = remallocSection(pSection, &pItem); + if (result) { + break; + } + } + } + free(pFunc_name); + continue; + } trim(pLine); if (*pLine == '#' || *pLine == '\0') @@ -368,7 +542,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) __LINE__, \ (int)sizeof(IniSection), \ result, STRERROR(result)); - + break; } @@ -395,66 +569,38 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) pItem = pSection->items + pSection->count; continue; } - + pEqualChar = strchr(pLine, '='); if (pEqualChar == NULL) { continue; } - + nNameLen = pEqualChar - pLine; nValueLen = strlen(pLine) - (nNameLen + 1); if (nNameLen > FAST_INI_ITEM_NAME_LEN) { nNameLen = FAST_INI_ITEM_NAME_LEN; } - + if (nValueLen > FAST_INI_ITEM_VALUE_LEN) { nValueLen = FAST_INI_ITEM_VALUE_LEN; } - - if (pSection->count >= pSection->alloc_count) - { - int bytes; - IniItem *pNew; - if (pSection->alloc_count == 0) - { - pSection->alloc_count = _INIT_ALLOC_ITEM_COUNT; - } - else - { - pSection->alloc_count *= 2; - } - bytes = sizeof(IniItem) * pSection->alloc_count; - pNew = (IniItem *)malloc(bytes); - if (pNew == NULL) - { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail", __LINE__, bytes); - result = errno != 0 ? errno : ENOMEM; - break; - } - if (pSection->count > 0) - { - memcpy(pNew, pSection->items, - sizeof(IniItem) * pSection->count); - free(pSection->items); + if (pSection->count >= pSection->alloc_count) { + result = remallocSection(pSection, &pItem); + if (result) { + break; } - - pSection->items = pNew; - pItem = pSection->items + pSection->count; - memset(pItem, 0, sizeof(IniItem) * \ - (pSection->alloc_count - pSection->count)); } memcpy(pItem->name, pLine, nNameLen); memcpy(pItem->value, pEqualChar + 1, nValueLen); - + trim(pItem->name); trim(pItem->value); - + pSection->count++; pItem++; } @@ -462,6 +608,44 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) return result; } +static int remallocSection(IniSection *pSection, IniItem **pItem) +{ + int bytes, result; + IniItem *pNew; + + if (pSection->alloc_count == 0) + { + pSection->alloc_count = _INIT_ALLOC_ITEM_COUNT; + } + else + { + pSection->alloc_count *= 2; + } + bytes = sizeof(IniItem) * pSection->alloc_count; + pNew = (IniItem *)malloc(bytes); + if (pNew == NULL) + { + logError("file: "__FILE__", line: %d, " \ + "malloc %d bytes fail", __LINE__, bytes); + result = errno != 0 ? errno : ENOMEM; + return result; + } + + if (pSection->count > 0) + { + memcpy(pNew, pSection->items, + sizeof(IniItem) * pSection->count); + free(pSection->items); + } + + pSection->items = pNew; + *pItem = pSection->items + pSection->count; + memset(*pItem, 0, sizeof(IniItem) * \ + (pSection->alloc_count - pSection->count)); + + return 0; +} + static int iniFreeHashData(const int index, const HashData *data, void *args) { IniSection *pSection; @@ -485,6 +669,8 @@ static int iniFreeHashData(const int index, const HashData *data, void *args) void iniFreeContext(IniContext *pContext) { + AnnotationMap *pAnnoMap; + if (pContext == NULL) { return; @@ -498,6 +684,13 @@ void iniFreeContext(IniContext *pContext) hash_walk(&pContext->sections, iniFreeHashData, NULL); hash_destroy(&pContext->sections); + + pAnnoMap = g_annotataionMap; + while (pAnnoMap->func_name) + { + pAnnoMap->func_destroy(); + pAnnoMap++; + } } @@ -551,7 +744,7 @@ int64_t iniGetInt64Value(const char *szSectionName, const char *szItemName, \ IniContext *pContext, const int64_t nDefaultValue) { char *pValue; - + pValue = iniGetStrValue(szSectionName, szItemName, pContext); if (pValue == NULL) { @@ -567,7 +760,7 @@ int iniGetIntValue(const char *szSectionName, const char *szItemName, \ IniContext *pContext, const int nDefaultValue) { char *pValue; - + pValue = iniGetStrValue(szSectionName, szItemName, pContext); if (pValue == NULL) { @@ -583,7 +776,7 @@ double iniGetDoubleValue(const char *szSectionName, const char *szItemName, \ IniContext *pContext, const double dbDefaultValue) { char *pValue; - + pValue = iniGetStrValue(szSectionName, szItemName, pContext); if (pValue == NULL) { @@ -599,7 +792,7 @@ bool iniGetBoolValue(const char *szSectionName, const char *szItemName, \ IniContext *pContext, const bool bDefaultValue) { char *pValue; - + pValue = iniGetStrValue(szSectionName, szItemName, pContext); if (pValue == NULL) { @@ -628,7 +821,7 @@ int iniGetValues(const char *szSectionName, const char *szItemName, \ { return 0; } - + INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ targetItem, pFound, 0) if (pFound == NULL) @@ -677,7 +870,7 @@ IniItem *iniGetValuesEx(const char *szSectionName, const char *szItemName, \ IniItem *pItem; IniItem *pItemEnd; IniItem *pItemStart; - + *nTargetCount = 0; INI_FIND_ITEM(szSectionName, szItemName, pContext, pSection, \ targetItem, pFound, NULL) diff --git a/src/ini_file_reader.h b/src/ini_file_reader.h index b3ba6b7..8ed4bbf 100644 --- a/src/ini_file_reader.h +++ b/src/ini_file_reader.h @@ -19,6 +19,13 @@ #define FAST_INI_ITEM_NAME_LEN 64 #define FAST_INI_ITEM_VALUE_LEN 256 +typedef struct { + char *func_name; + int (*func_init) (); + void (*func_destroy) (); + int (*func_get) (char *key, char **pOutValue, int max_values); +} AnnotationMap; + typedef struct { char name[FAST_INI_ITEM_NAME_LEN + 1]; @@ -44,6 +51,8 @@ typedef struct extern "C" { #endif +void iniSetAnnotationCallBack(AnnotationMap *map, int count); + /** load ini items from file * parameters: * szFilename: the filename, can be an URL From a173000298cfde46f710a3de82d620103ac78a47 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 17 Aug 2015 19:32:33 +0800 Subject: [PATCH 02/10] optimize --- src/ini_file_reader.c | 232 +++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 128 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 9df28b3..bd1a5aa 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -285,6 +285,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) char *pAnnoItemLine; char *pIncludeFilename; char *pItemValue[100]; + char full_funcName[128]; char full_filename[MAX_PATH_SIZE]; int i; int nLineLen; @@ -301,6 +302,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) pLastEnd = content - 1; pSection = pContext->current_section; pItem = pSection->items + pSection->count; + pFunc_name = full_funcName; while (pLastEnd != NULL) { @@ -372,137 +374,27 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) free(pIncludeFilename); continue; } - else if (isAnnotation || (*pLine == '#' && \ + else if ((*pLine == '#' && \ strncasecmp(pLine+1, "@function", 9) == 0 && \ (*(pLine+10) == ' ' || *(pLine+10) == '\t'))) { - if (isAnnotation == 0) { - pFunc_name = strdup(pLine + 11); - if (pFunc_name == NULL) { - logError("file: "__FILE__", line: %d, " \ - "strdup %d bytes fail", __LINE__, \ - (int)strlen(pLine + 11) + 1); - result = errno != 0 ? errno : ENOMEM; - break; - } - - trim(pFunc_name); - isAnnotation = 1; - pAnnoItemLine = pLastEnd + 1; - continue; - } - - isAnnotation = 0; - - if (pLine != pAnnoItemLine) { - logError("file: "__FILE__", line: %d, " \ - "the @function and annotation item " \ - "must be next to each other", __LINE__); - result = EINVAL; - free(pFunc_name); - break; - } - - trim(pLine); - if (*pLine == '#' || *pLine == '\0') { - logError("file: "__FILE__", line: %d, " \ - "the @function and annotation item " \ - "must be next to each other", __LINE__); - result = EINVAL; - free(pFunc_name); - break; - } - - pEqualChar = strchr(pLine, '='); - if (pEqualChar == NULL) { - logError("file: "__FILE__", line: %d, " \ - "the @function and annotation item " \ - "must be next to each other", __LINE__); - result = EINVAL; - free(pFunc_name); - break; - } - - nNameLen = pEqualChar - pLine; - nValueLen = strlen(pLine) - (nNameLen + 1); - if (nNameLen > FAST_INI_ITEM_NAME_LEN) { - nNameLen = FAST_INI_ITEM_NAME_LEN; - } - - if (pSection->count >= pSection->alloc_count) { - result = remallocSection(pSection, &pItem); - if (result) { - free(pFunc_name); - break; - } - } - - memcpy(pItem->name, pLine, nNameLen); - memcpy(pItem->value, pEqualChar + 1, nValueLen); - - trim(pItem->name); - trim(pItem->value); - - if (g_annotataionMap == NULL) { - logWarning("file: "__FILE__", line: %d, " \ - "not set annotataionMap and (%s) will use " \ - "the item value (%s)", __LINE__, pItem->name, - pItem->value); - pSection->count++; - pItem++; - free(pFunc_name); - continue; - } - - pAnnoMap = g_annotataionMap; - while (pAnnoMap->func_name) { - if (strlen(pAnnoMap->func_name) == strlen(pFunc_name) - && strcasecmp(pFunc_name, pAnnoMap->func_name) == 0) - { - pAnnoMap->func_init(); - nItemCnt = pAnnoMap->func_get(pItem->value, pItemValue, 100); - break; - } - pAnnoMap++; - } - - if (nItemCnt == -1) { - logWarning("file: "__FILE__", line: %d, " \ - "not found corresponding annotation func (%s)" \ - " and (%s) will use the item value (%s).", __LINE__, - pItem->name, pFunc_name, pItem->value); - pSection->count++; - pItem++; - free(pFunc_name); - continue; - } else if (nItemCnt == 0) { - logWarning("file: "__FILE__", line: %d, " \ - "annotation func(%s) execute failed and" - "(%s) will use the item value (%s)", __LINE__, - pItem->name, pFunc_name, pItem->value); - pSection->count++; - pItem++; - free(pFunc_name); - continue; - } - - pItemName = pItem->name; - for (i = 0; i < nItemCnt; i++) { - memcpy(pItem->name, pItemName, strlen(pItemName) + 1); - memcpy(pItem->value, pItemValue[i], strlen(pItemValue[i]) + 1); - pSection->count++; - pItem++; - if (pSection->count >= pSection->alloc_count) { - result = remallocSection(pSection, &pItem); - if (result) { - break; - } - } - } - free(pFunc_name); + nNameLen = strlen(pLine + 11) + 1; + nNameLen = nNameLen > 128 ? 128 : nNameLen; + memcpy(pFunc_name, pLine + 11, nNameLen); + trim(pFunc_name); + isAnnotation = 1; + pAnnoItemLine = pLastEnd + 1; continue; } + if (isAnnotation && pLine != pAnnoItemLine) + { + logWarning("file: "__FILE__", line: %d, " \ + "the @function and annotation item " \ + "must be next to each other", __LINE__); + isAnnotation = 0; + } + trim(pLine); if (*pLine == '#' || *pLine == '\0') { @@ -588,9 +480,11 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) nValueLen = FAST_INI_ITEM_VALUE_LEN; } - if (pSection->count >= pSection->alloc_count) { + if (pSection->count >= pSection->alloc_count) + { result = remallocSection(pSection, &pItem); - if (result) { + if (result) + { break; } } @@ -601,6 +495,86 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) trim(pItem->name); trim(pItem->value); + if (isAnnotation) + { + isAnnotation = 0; + + if (g_annotataionMap == NULL) + { + logWarning("file: "__FILE__", line: %d, " \ + "not set annotataionMap and (%s) will use " \ + "the item value (%s)", __LINE__, pItem->name, + pItem->value); + pSection->count++; + pItem++; + continue; + } + + pAnnoMap = g_annotataionMap; + while (pAnnoMap->func_name) + { + if (strlen(pAnnoMap->func_name) == strlen(pFunc_name) + && strcasecmp(pFunc_name, pAnnoMap->func_name) == 0) + { + if (pAnnoMap->func_init) + { + pAnnoMap->func_init(); + } + + if (pAnnoMap->func_get) + { + nItemCnt = pAnnoMap->func_get(pItem->value, pItemValue, 100); + } + break; + } + pAnnoMap++; + } + + if (nItemCnt == -1) + { + logWarning("file: "__FILE__", line: %d, " \ + "not found corresponding annotation func (%s)" \ + " and (%s) will use the item value (%s).", __LINE__, + pItem->name, pFunc_name, pItem->value); + pSection->count++; + pItem++; + continue; + } + else if (nItemCnt == 0) + { + logWarning("file: "__FILE__", line: %d, " \ + "annotation func(%s) execute failed and" + "(%s) will use the item value (%s)", __LINE__, + pItem->name, pFunc_name, pItem->value); + pSection->count++; + pItem++; + continue; + } + + pItemName = pItem->name; + nNameLen = strlen(pItemName) + 1; + for (i = 0; i < nItemCnt; i++) + { + nValueLen = strlen(pItemValue[i]) + 1; + if (nValueLen > FAST_INI_ITEM_VALUE_LEN) + { + nValueLen = FAST_INI_ITEM_VALUE_LEN; + } + memcpy(pItem->name, pItemName, nNameLen); + memcpy(pItem->value, pItemValue[i], nValueLen); + pSection->count++; + pItem++; + if (pSection->count >= pSection->alloc_count) + { + result = remallocSection(pSection, &pItem); + if (result) + { + break; + } + } + } + continue; + } pSection->count++; pItem++; } @@ -688,7 +662,9 @@ void iniFreeContext(IniContext *pContext) pAnnoMap = g_annotataionMap; while (pAnnoMap->func_name) { - pAnnoMap->func_destroy(); + if (pAnnoMap->func_destroy) { + pAnnoMap->func_destroy(); + } pAnnoMap++; } } From 9208cfde96f71f96d0708f8b858d9aacfc8f720d Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 18 Aug 2015 09:46:25 +0800 Subject: [PATCH 03/10] eliminate potential trouble --- src/ini_file_reader.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index bd1a5aa..96862fc 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -285,7 +285,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) char *pAnnoItemLine; char *pIncludeFilename; char *pItemValue[100]; - char full_funcName[128]; + char full_funcName[FAST_INI_ITEM_VALUE_LEN]; char full_filename[MAX_PATH_SIZE]; int i; int nLineLen; @@ -296,7 +296,6 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) int isAnnotation; result = 0; - nItemCnt = -1; pAnnoItemLine = NULL; isAnnotation = 0; pLastEnd = content - 1; @@ -379,7 +378,8 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) (*(pLine+10) == ' ' || *(pLine+10) == '\t'))) { nNameLen = strlen(pLine + 11) + 1; - nNameLen = nNameLen > 128 ? 128 : nNameLen; + nNameLen = nNameLen > FAST_INI_ITEM_VALUE_LEN ? + FAST_INI_ITEM_VALUE_LEN : nNameLen; memcpy(pFunc_name, pLine + 11, nNameLen); trim(pFunc_name); isAnnotation = 1; @@ -510,11 +510,11 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) continue; } + nItemCnt = -1; pAnnoMap = g_annotataionMap; while (pAnnoMap->func_name) { - if (strlen(pAnnoMap->func_name) == strlen(pFunc_name) - && strcasecmp(pFunc_name, pAnnoMap->func_name) == 0) + if (strcmp(pFunc_name, pAnnoMap->func_name) == 0) { if (pAnnoMap->func_init) { From ecfafbcf38173aeb64a9e171a30722838af5787a Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 18 Aug 2015 16:43:42 +0800 Subject: [PATCH 04/10] optimizing --- src/ini_file_reader.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 96862fc..5e743e0 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -280,12 +280,11 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) char *pLine; char *pLastEnd; char *pEqualChar; - char *pFunc_name; char *pItemName; char *pAnnoItemLine; char *pIncludeFilename; char *pItemValue[100]; - char full_funcName[FAST_INI_ITEM_VALUE_LEN]; + char pFunc_name[FAST_INI_ITEM_NAME_LEN + 1]; char full_filename[MAX_PATH_SIZE]; int i; int nLineLen; @@ -301,7 +300,6 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) pLastEnd = content - 1; pSection = pContext->current_section; pItem = pSection->items + pSection->count; - pFunc_name = full_funcName; while (pLastEnd != NULL) { @@ -377,17 +375,17 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) strncasecmp(pLine+1, "@function", 9) == 0 && \ (*(pLine+10) == ' ' || *(pLine+10) == '\t'))) { - nNameLen = strlen(pLine + 11) + 1; - nNameLen = nNameLen > FAST_INI_ITEM_VALUE_LEN ? - FAST_INI_ITEM_VALUE_LEN : nNameLen; - memcpy(pFunc_name, pLine + 11, nNameLen); + nNameLen = strlen(pLine + 11); + nNameLen = nNameLen > FAST_INI_ITEM_NAME_LEN ? + FAST_INI_ITEM_NAME_LEN : nNameLen; + memcpy(pFunc_name, pLine + 11, nNameLen + 1); trim(pFunc_name); isAnnotation = 1; pAnnoItemLine = pLastEnd + 1; continue; } - if (isAnnotation && pLine != pAnnoItemLine) + if (isAnnotation && pLine != pAnnoItemLine) { logWarning("file: "__FILE__", line: %d, " \ "the @function and annotation item " \ From a9fc4a5a2af1116f1d8561f158ed85a26c296c34 Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 18 Aug 2015 17:21:50 +0800 Subject: [PATCH 05/10] optimizing --- src/ini_file_reader.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 5e743e0..684c451 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -284,7 +284,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) char *pAnnoItemLine; char *pIncludeFilename; char *pItemValue[100]; - char pFunc_name[FAST_INI_ITEM_NAME_LEN + 1]; + char pFuncName[FAST_INI_ITEM_NAME_LEN + 1]; char full_filename[MAX_PATH_SIZE]; int i; int nLineLen; @@ -376,10 +376,12 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) (*(pLine+10) == ' ' || *(pLine+10) == '\t'))) { nNameLen = strlen(pLine + 11); - nNameLen = nNameLen > FAST_INI_ITEM_NAME_LEN ? - FAST_INI_ITEM_NAME_LEN : nNameLen; - memcpy(pFunc_name, pLine + 11, nNameLen + 1); - trim(pFunc_name); + if (nNameLen > FAST_INI_ITEM_NAME_LEN) { + nNameLen = FAST_INI_ITEM_NAME_LEN; + } + memcpy(pFuncName, pLine + 11, nNameLen); + pFuncName[nNameLen] = '\0'; + trim(pFuncName); isAnnotation = 1; pAnnoItemLine = pLastEnd + 1; continue; @@ -512,7 +514,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) pAnnoMap = g_annotataionMap; while (pAnnoMap->func_name) { - if (strcmp(pFunc_name, pAnnoMap->func_name) == 0) + if (strcmp(pFuncName, pAnnoMap->func_name) == 0) { if (pAnnoMap->func_init) { @@ -533,7 +535,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) logWarning("file: "__FILE__", line: %d, " \ "not found corresponding annotation func (%s)" \ " and (%s) will use the item value (%s).", __LINE__, - pItem->name, pFunc_name, pItem->value); + pItem->name, pFuncName, pItem->value); pSection->count++; pItem++; continue; @@ -543,7 +545,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) logWarning("file: "__FILE__", line: %d, " \ "annotation func(%s) execute failed and" "(%s) will use the item value (%s)", __LINE__, - pItem->name, pFunc_name, pItem->value); + pItem->name, pFuncName, pItem->value); pSection->count++; pItem++; continue; From 333f9b56e469f0c252adbcc11237c9bbccf3826d Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 18 Aug 2015 17:52:57 +0800 Subject: [PATCH 06/10] eliminate potential trouble --- src/ini_file_reader.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 684c451..23134af 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -283,7 +283,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) char *pItemName; char *pAnnoItemLine; char *pIncludeFilename; - char *pItemValue[100]; + char *pItemValues[100]; char pFuncName[FAST_INI_ITEM_NAME_LEN + 1]; char full_filename[MAX_PATH_SIZE]; int i; @@ -376,7 +376,8 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) (*(pLine+10) == ' ' || *(pLine+10) == '\t'))) { nNameLen = strlen(pLine + 11); - if (nNameLen > FAST_INI_ITEM_NAME_LEN) { + if (nNameLen > FAST_INI_ITEM_NAME_LEN) + { nNameLen = FAST_INI_ITEM_NAME_LEN; } memcpy(pFuncName, pLine + 11, nNameLen); @@ -514,7 +515,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) pAnnoMap = g_annotataionMap; while (pAnnoMap->func_name) { - if (strcmp(pFuncName, pAnnoMap->func_name) == 0) + if (strcasecmp(pFuncName, pAnnoMap->func_name) == 0) { if (pAnnoMap->func_init) { @@ -523,7 +524,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) if (pAnnoMap->func_get) { - nItemCnt = pAnnoMap->func_get(pItem->value, pItemValue, 100); + nItemCnt = pAnnoMap->func_get(pItem->value, pItemValues, 100); } break; } @@ -555,13 +556,14 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) nNameLen = strlen(pItemName) + 1; for (i = 0; i < nItemCnt; i++) { - nValueLen = strlen(pItemValue[i]) + 1; + nValueLen = strlen(pItemValues[i]); if (nValueLen > FAST_INI_ITEM_VALUE_LEN) { nValueLen = FAST_INI_ITEM_VALUE_LEN; } memcpy(pItem->name, pItemName, nNameLen); - memcpy(pItem->value, pItemValue[i], nValueLen); + memcpy(pItem->value, pItemValues[i], nValueLen); + pItem->value[nValueLen] = '\0'; pSection->count++; pItem++; if (pSection->count >= pSection->alloc_count) From 8821594608a78a192d09d84a5f2232c8c6ece9b1 Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 18 Aug 2015 18:36:07 +0800 Subject: [PATCH 07/10] eliminate potential trouble --- src/ini_file_reader.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 23134af..937e29f 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -46,12 +46,12 @@ void iniSetAnnotationCallBack(AnnotationMap *map, int count) g_annotataionMap = (AnnotationMap *) malloc(bytes); if (g_annotataionMap == NULL) { logError("file: "__FILE__", line: %d, " \ - "malloc fail, errno: %d, error info: %s", \ - __LINE__, errno, STRERROR(errno)); + "malloc (%d) fail, errno: %d, error info: %s", \ + __LINE__, bytes, errno, STRERROR(errno)); return; } - memcpy(g_annotataionMap, map, bytes); + memcpy(g_annotataionMap, map, sizeof(AnnotationMap) * count); p = g_annotataionMap + count; p->func_name = NULL; From fdaa6afa5973ced6ec217b09f9514ec38f90fbc5 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 19 Aug 2015 10:37:31 +0800 Subject: [PATCH 08/10] eliminate potential trouble --- src/ini_file_reader.c | 46 ++++++++++++++++++++++++++++--------------- src/ini_file_reader.h | 3 ++- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 937e29f..90871c4 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -30,25 +30,27 @@ static int iniDoLoadFromFile(const char *szFilename, \ static int iniDoLoadItemsFromBuffer(char *content, \ IniContext *pContext); -void iniSetAnnotationCallBack(AnnotationMap *map, int count) +int iniSetAnnotationCallBack(AnnotationMap *map, int count) { int bytes; AnnotationMap *p; - if (count <= 0) { + if (count <= 0) + { logWarning("file: "__FILE__", line: %d, " \ "iniSetAnnotationCallBack fail count(%d) is incorrectly.", \ __LINE__, count); - return; + return EINVAL; } bytes = sizeof(AnnotationMap) * (count + 1); g_annotataionMap = (AnnotationMap *) malloc(bytes); - if (g_annotataionMap == NULL) { + if (g_annotataionMap == NULL) + { logError("file: "__FILE__", line: %d, " \ "malloc (%d) fail, errno: %d, error info: %s", \ __LINE__, bytes, errno, STRERROR(errno)); - return; + return ENOMEM; } memcpy(g_annotataionMap, map, sizeof(AnnotationMap) * count); @@ -58,6 +60,29 @@ void iniSetAnnotationCallBack(AnnotationMap *map, int count) p->func_init = NULL; p->func_destroy = NULL; p->func_get = NULL; + + return 0; +} + +void iniDestroyAnnotationCallBack() +{ + AnnotationMap *pAnnoMap; + + pAnnoMap = g_annotataionMap; + + if (pAnnoMap == NULL) + { + return; + } + + while (pAnnoMap->func_name) + { + if (pAnnoMap->func_destroy) + { + pAnnoMap->func_destroy(); + } + pAnnoMap++; + } } static int iniCompareByItemName(const void *p1, const void *p2) @@ -645,8 +670,6 @@ static int iniFreeHashData(const int index, const HashData *data, void *args) void iniFreeContext(IniContext *pContext) { - AnnotationMap *pAnnoMap; - if (pContext == NULL) { return; @@ -660,15 +683,6 @@ void iniFreeContext(IniContext *pContext) hash_walk(&pContext->sections, iniFreeHashData, NULL); hash_destroy(&pContext->sections); - - pAnnoMap = g_annotataionMap; - while (pAnnoMap->func_name) - { - if (pAnnoMap->func_destroy) { - pAnnoMap->func_destroy(); - } - pAnnoMap++; - } } diff --git a/src/ini_file_reader.h b/src/ini_file_reader.h index 8ed4bbf..35d05c3 100644 --- a/src/ini_file_reader.h +++ b/src/ini_file_reader.h @@ -51,7 +51,8 @@ typedef struct extern "C" { #endif -void iniSetAnnotationCallBack(AnnotationMap *map, int count); +int iniSetAnnotationCallBack(AnnotationMap *map, int count); +void iniDestroyAnnotationCallBack(); /** load ini items from file * parameters: From d3e8cc1833e2fe923e35d33c4d878102ba71b87b Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 19 Aug 2015 15:52:10 +0800 Subject: [PATCH 09/10] eliminate potential trouble --- src/ini_file_reader.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 90871c4..f363d1d 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -22,7 +22,7 @@ #define _LINE_BUFFER_SIZE 512 #define _INIT_ALLOC_ITEM_COUNT 32 -static AnnotationMap *g_annotataionMap; +static AnnotationMap *g_annotataionMap = NULL; static int remallocSection(IniSection *pSection, IniItem **pItem); static int iniDoLoadFromFile(const char *szFilename, \ @@ -83,6 +83,7 @@ void iniDestroyAnnotationCallBack() } pAnnoMap++; } + g_annotataionMap = NULL; } static int iniCompareByItemName(const void *p1, const void *p2) From d1026167d4c9f1de9456ebbd6fb350774cca6ca2 Mon Sep 17 00:00:00 2001 From: liuwei Date: Thu, 20 Aug 2015 15:46:59 +0800 Subject: [PATCH 10/10] eliminate potential trouble --- src/ini_file_reader.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index f363d1d..9eaeeea 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -336,6 +336,14 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) *pLastEnd = '\0'; } + if (isAnnotation && pLine != pAnnoItemLine) + { + logWarning("file: "__FILE__", line: %d, " \ + "the @function and annotation item " \ + "must be next to each other", __LINE__); + isAnnotation = 0; + } + if (*pLine == '#' && \ strncasecmp(pLine+1, "include", 7) == 0 && \ (*(pLine+8) == ' ' || *(pLine+8) == '\t')) @@ -414,14 +422,6 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) continue; } - if (isAnnotation && pLine != pAnnoItemLine) - { - logWarning("file: "__FILE__", line: %d, " \ - "the @function and annotation item " \ - "must be next to each other", __LINE__); - isAnnotation = 0; - } - trim(pLine); if (*pLine == '#' || *pLine == '\0') { @@ -607,6 +607,13 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) pItem++; } + if (!result && isAnnotation) + { + logWarning("file: "__FILE__", line: %d, " \ + "the @function and annotation item " \ + "must be next to each other", __LINE__); + } + return result; }