ini_file_reader code refine

pull/37/head
yuqing 2017-02-28 15:56:15 +08:00
parent 5fb8531218
commit c78d3d7a2a
1 changed files with 28 additions and 11 deletions

View File

@ -376,7 +376,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
char *pLine; char *pLine;
char *pLastEnd; char *pLastEnd;
char *pEqualChar; char *pEqualChar;
char *pItemName; char pItemName[FAST_INI_ITEM_NAME_LEN + 1];
char *pAnnoItemLine; char *pAnnoItemLine;
char *pIncludeFilename; char *pIncludeFilename;
char *pItemValues[100]; char *pItemValues[100];
@ -581,18 +581,28 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
nValueLen = strlen(pLine) - (nNameLen + 1); nValueLen = strlen(pLine) - (nNameLen + 1);
if (nNameLen > FAST_INI_ITEM_NAME_LEN) if (nNameLen > FAST_INI_ITEM_NAME_LEN)
{ {
logWarning("file: "__FILE__", line: %d, "
"name length: %d exceeds %d, "
"truncate it to \"%.*s\"", __LINE__,
nNameLen, FAST_INI_ITEM_NAME_LEN,
FAST_INI_ITEM_NAME_LEN, pLine);
nNameLen = FAST_INI_ITEM_NAME_LEN; nNameLen = FAST_INI_ITEM_NAME_LEN;
} }
if (nValueLen > FAST_INI_ITEM_VALUE_LEN) if (nValueLen > FAST_INI_ITEM_VALUE_LEN)
{ {
logWarning("file: "__FILE__", line: %d, "
"value length: %d exceeds %d, "
"truncate it to \"%.*s\"", __LINE__,
nValueLen, FAST_INI_ITEM_VALUE_LEN,
FAST_INI_ITEM_VALUE_LEN, pEqualChar + 1);
nValueLen = FAST_INI_ITEM_VALUE_LEN; nValueLen = FAST_INI_ITEM_VALUE_LEN;
} }
if (pSection->count >= pSection->alloc_count) if (pSection->count >= pSection->alloc_count)
{ {
result = remallocSection(pSection, &pItem); result = remallocSection(pSection, &pItem);
if (result) if (result != 0)
{ {
break; break;
} }
@ -607,7 +617,6 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
if (isAnnotation) if (isAnnotation)
{ {
isAnnotation = 0; isAnnotation = 0;
if (g_annotataionMap == NULL) if (g_annotataionMap == NULL)
{ {
logWarning("file: "__FILE__", line: %d, " \ logWarning("file: "__FILE__", line: %d, " \
@ -660,16 +669,21 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
continue; continue;
} }
pItemName = pItem->name; strcpy(pItemName, pItem->name);
nNameLen = strlen(pItemName); nNameLen = strlen(pItemName);
for (i = 0; i < nItemCnt; i++) for (i = 0; i < nItemCnt; i++)
{ {
nValueLen = strlen(pItemValues[i]); nValueLen = strlen(pItemValues[i]);
if (nValueLen > FAST_INI_ITEM_VALUE_LEN) if (nValueLen > FAST_INI_ITEM_VALUE_LEN)
{ {
logWarning("file: "__FILE__", line: %d, "
"value length: %d exceeds %d, "
"truncate it to \"%.*s\"", __LINE__,
nValueLen, FAST_INI_ITEM_VALUE_LEN,
FAST_INI_ITEM_VALUE_LEN, pItemValues[i]);
nValueLen = FAST_INI_ITEM_VALUE_LEN; nValueLen = FAST_INI_ITEM_VALUE_LEN;
} }
memcpy(pItem->name, pItemName, nNameLen); strcpy(pItem->name, pItemName);
memcpy(pItem->value, pItemValues[i], nValueLen); memcpy(pItem->value, pItemValues[i], nValueLen);
pItem->value[nValueLen] = '\0'; pItem->value[nValueLen] = '\0';
pSection->count++; pSection->count++;
@ -677,7 +691,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
if (pSection->count >= pSection->alloc_count) if (pSection->count >= pSection->alloc_count)
{ {
result = remallocSection(pSection, &pItem); result = remallocSection(pSection, &pItem);
if (result) if (result != 0)
{ {
break; break;
} }
@ -1861,18 +1875,20 @@ static int iniLoadItemsFromBuffer(char *content, IniContext *pContext)
static int remallocSection(IniSection *pSection, IniItem **pItem) static int remallocSection(IniSection *pSection, IniItem **pItem)
{ {
int bytes, result; int bytes;
int result;
int alloc_count;
IniItem *pNew; IniItem *pNew;
if (pSection->alloc_count == 0) if (pSection->alloc_count == 0)
{ {
pSection->alloc_count = _INIT_ALLOC_ITEM_COUNT; alloc_count = _INIT_ALLOC_ITEM_COUNT;
} }
else else
{ {
pSection->alloc_count *= 2; alloc_count = pSection->alloc_count * 2;
} }
bytes = sizeof(IniItem) * pSection->alloc_count; bytes = sizeof(IniItem) * alloc_count;
pNew = (IniItem *)malloc(bytes); pNew = (IniItem *)malloc(bytes);
if (pNew == NULL) if (pNew == NULL)
{ {
@ -1889,9 +1905,10 @@ static int remallocSection(IniSection *pSection, IniItem **pItem)
free(pSection->items); free(pSection->items);
} }
pSection->alloc_count = alloc_count;
pSection->items = pNew; pSection->items = pNew;
*pItem = pSection->items + pSection->count; *pItem = pSection->items + pSection->count;
memset(*pItem, 0, sizeof(IniItem) * \ memset(*pItem, 0, sizeof(IniItem) *
(pSection->alloc_count - pSection->count)); (pSection->alloc_count - pSection->count));
return 0; return 0;