eliminate potential trouble

dev
liuwei 2015-08-19 10:37:31 +08:00
parent 8821594608
commit fdaa6afa59
2 changed files with 32 additions and 17 deletions

View File

@ -30,25 +30,27 @@ static int iniDoLoadFromFile(const char *szFilename, \
static int iniDoLoadItemsFromBuffer(char *content, \ static int iniDoLoadItemsFromBuffer(char *content, \
IniContext *pContext); IniContext *pContext);
void iniSetAnnotationCallBack(AnnotationMap *map, int count) int iniSetAnnotationCallBack(AnnotationMap *map, int count)
{ {
int bytes; int bytes;
AnnotationMap *p; AnnotationMap *p;
if (count <= 0) { if (count <= 0)
{
logWarning("file: "__FILE__", line: %d, " \ logWarning("file: "__FILE__", line: %d, " \
"iniSetAnnotationCallBack fail count(%d) is incorrectly.", \ "iniSetAnnotationCallBack fail count(%d) is incorrectly.", \
__LINE__, count); __LINE__, count);
return; return EINVAL;
} }
bytes = sizeof(AnnotationMap) * (count + 1); bytes = sizeof(AnnotationMap) * (count + 1);
g_annotataionMap = (AnnotationMap *) malloc(bytes); g_annotataionMap = (AnnotationMap *) malloc(bytes);
if (g_annotataionMap == NULL) { if (g_annotataionMap == NULL)
{
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \
"malloc (%d) fail, errno: %d, error info: %s", \ "malloc (%d) fail, errno: %d, error info: %s", \
__LINE__, bytes, errno, STRERROR(errno)); __LINE__, bytes, errno, STRERROR(errno));
return; return ENOMEM;
} }
memcpy(g_annotataionMap, map, sizeof(AnnotationMap) * count); memcpy(g_annotataionMap, map, sizeof(AnnotationMap) * count);
@ -58,6 +60,29 @@ void iniSetAnnotationCallBack(AnnotationMap *map, int count)
p->func_init = NULL; p->func_init = NULL;
p->func_destroy = NULL; p->func_destroy = NULL;
p->func_get = 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) 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) void iniFreeContext(IniContext *pContext)
{ {
AnnotationMap *pAnnoMap;
if (pContext == NULL) if (pContext == NULL)
{ {
return; return;
@ -660,15 +683,6 @@ void iniFreeContext(IniContext *pContext)
hash_walk(&pContext->sections, iniFreeHashData, NULL); hash_walk(&pContext->sections, iniFreeHashData, NULL);
hash_destroy(&pContext->sections); hash_destroy(&pContext->sections);
pAnnoMap = g_annotataionMap;
while (pAnnoMap->func_name)
{
if (pAnnoMap->func_destroy) {
pAnnoMap->func_destroy();
}
pAnnoMap++;
}
} }

View File

@ -51,7 +51,8 @@ typedef struct
extern "C" { extern "C" {
#endif #endif
void iniSetAnnotationCallBack(AnnotationMap *map, int count); int iniSetAnnotationCallBack(AnnotationMap *map, int count);
void iniDestroyAnnotationCallBack();
/** load ini items from file /** load ini items from file
* parameters: * parameters: