ini_file_reader code refine
parent
86ed30d58a
commit
9cd25c3686
2
HISTORY
2
HISTORY
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
Version 1.40 2018-08-14
|
||||
Version 1.40 2018-08-16
|
||||
* add function conn_pool_parse_server_info and conn_pool_load_server_info
|
||||
|
||||
Version 1.39 2018-07-31
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@
|
|||
#define _MAX_DYNAMIC_CONTENTS 8
|
||||
#define _BUILTIN_ANNOTATION_COUNT 3
|
||||
|
||||
static AnnotationMap *g_annotation_map = NULL;
|
||||
static AnnotationEntry *g_annotations = NULL;
|
||||
static int g_annotation_count = 0;
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -72,7 +72,7 @@ typedef struct {
|
|||
typedef struct {
|
||||
int count;
|
||||
int alloc_count;
|
||||
AnnotationMap *annotations;
|
||||
AnnotationEntry *annotations;
|
||||
} DynamicAnnotations;
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -96,7 +96,7 @@ static int iniLoadItemsFromBuffer(char *content, \
|
|||
IniContext *pContext);
|
||||
static DynamicAnnotations *iniAllocAnnotations(IniContext *pContext,
|
||||
const int annotation_count);
|
||||
static AnnotationMap *iniGetAnnotations(IniContext *pContext);
|
||||
static AnnotationEntry *iniGetAnnotations(IniContext *pContext);
|
||||
static SetDirectiveVars *iniGetVars(IniContext *pContext);
|
||||
|
||||
#define STR_TRIM(pStr) \
|
||||
|
|
@ -105,13 +105,13 @@ static SetDirectiveVars *iniGetVars(IniContext *pContext);
|
|||
trim_left(pStr); \
|
||||
} while (0)
|
||||
|
||||
static void iniDoSetAnnotations(AnnotationMap *src, const int src_count,
|
||||
AnnotationMap *dest, int *dest_count)
|
||||
static void iniDoSetAnnotations(AnnotationEntry *src, const int src_count,
|
||||
AnnotationEntry *dest, int *dest_count)
|
||||
{
|
||||
AnnotationMap *pSrc;
|
||||
AnnotationMap *pSrcEnd;
|
||||
AnnotationMap *pDest;
|
||||
AnnotationMap *pDestEnd;
|
||||
AnnotationEntry *pSrc;
|
||||
AnnotationEntry *pSrcEnd;
|
||||
AnnotationEntry *pDest;
|
||||
AnnotationEntry *pDestEnd;
|
||||
|
||||
pSrcEnd = src + src_count;
|
||||
pDestEnd = dest + *dest_count;
|
||||
|
|
@ -375,10 +375,10 @@ static void iniAnnotationFuncFree(char **values, const int count)
|
|||
}
|
||||
|
||||
static void iniSetBuiltinAnnotations(IniContext *pContext,
|
||||
AnnotationMap *dest, int *dest_count)
|
||||
AnnotationEntry *dest, int *dest_count)
|
||||
{
|
||||
AnnotationMap builtins[_BUILTIN_ANNOTATION_COUNT];
|
||||
AnnotationMap *pAnnotation;
|
||||
AnnotationEntry builtins[_BUILTIN_ANNOTATION_COUNT];
|
||||
AnnotationEntry *pAnnotation;
|
||||
|
||||
pAnnotation = builtins;
|
||||
pAnnotation->func_name = "LOCAL_IP_GET";
|
||||
|
|
@ -409,7 +409,7 @@ static void iniSetBuiltinAnnotations(IniContext *pContext,
|
|||
}
|
||||
|
||||
static int iniSetAnnotations(IniContext *pContext, const char annotation_type,
|
||||
AnnotationMap *annotations, const int count)
|
||||
AnnotationEntry *annotations, const int count)
|
||||
{
|
||||
DynamicAnnotations *pDynamicAnnotations;
|
||||
|
||||
|
|
@ -443,32 +443,32 @@ static int iniSetAnnotations(IniContext *pContext, const char annotation_type,
|
|||
return 0;
|
||||
}
|
||||
|
||||
int iniSetAnnotationCallBack(AnnotationMap *map, int count)
|
||||
int iniSetAnnotationCallBack(AnnotationEntry *map, int count)
|
||||
{
|
||||
int bytes;
|
||||
AnnotationMap *pDest;
|
||||
AnnotationEntry *pDest;
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
logWarning("file: "__FILE__", line: %d, " \
|
||||
"iniSetAnnotationCallBack fail count(%d) is incorrectly.", \
|
||||
logWarning("file: "__FILE__", line: %d, "
|
||||
"iniSetAnnotationCallBack fail, count(%d) is invalid.",
|
||||
__LINE__, count);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
bytes = sizeof(AnnotationMap) * (g_annotation_count + count + 1);
|
||||
g_annotation_map = (AnnotationMap *)realloc(g_annotation_map, bytes);
|
||||
if (g_annotation_map == NULL)
|
||||
bytes = sizeof(AnnotationEntry) * (g_annotation_count + count + 1);
|
||||
g_annotations = (AnnotationEntry *)realloc(g_annotations, bytes);
|
||||
if (g_annotations == NULL)
|
||||
{
|
||||
logError("file: "__FILE__", line: %d, " \
|
||||
"realloc %d fail, errno: %d, error info: %s", \
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"realloc %d fail, errno: %d, error info: %s",
|
||||
__LINE__, bytes, errno, STRERROR(errno));
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
iniDoSetAnnotations(map, count, g_annotation_map, &g_annotation_count);
|
||||
iniDoSetAnnotations(map, count, g_annotations, &g_annotation_count);
|
||||
|
||||
pDest = g_annotation_map + g_annotation_count;
|
||||
pDest = g_annotations + g_annotation_count;
|
||||
pDest->func_name = NULL;
|
||||
pDest->func_init = NULL;
|
||||
pDest->func_destroy = NULL;
|
||||
|
|
@ -479,14 +479,14 @@ int iniSetAnnotationCallBack(AnnotationMap *map, int count)
|
|||
|
||||
void iniDestroyAnnotationCallBack()
|
||||
{
|
||||
AnnotationMap *pAnnoMap;
|
||||
AnnotationEntry *pAnnoMap;
|
||||
|
||||
if (g_annotation_map == NULL)
|
||||
if (g_annotations == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pAnnoMap = g_annotation_map;
|
||||
pAnnoMap = g_annotations;
|
||||
while (pAnnoMap->func_name)
|
||||
{
|
||||
if (pAnnoMap->func_destroy != NULL)
|
||||
|
|
@ -496,8 +496,8 @@ void iniDestroyAnnotationCallBack()
|
|||
pAnnoMap++;
|
||||
}
|
||||
|
||||
free(g_annotation_map);
|
||||
g_annotation_map = NULL;
|
||||
free(g_annotations);
|
||||
g_annotations = NULL;
|
||||
g_annotation_count = 0;
|
||||
|
||||
}
|
||||
|
|
@ -508,7 +508,7 @@ static int iniCompareByItemName(const void *p1, const void *p2)
|
|||
}
|
||||
|
||||
static int iniInitContext(IniContext *pContext, const char annotation_type,
|
||||
AnnotationMap *annotations, const int count,
|
||||
AnnotationEntry *annotations, const int count,
|
||||
const char flags)
|
||||
{
|
||||
int result;
|
||||
|
|
@ -559,7 +559,7 @@ int iniLoadFromFile(const char *szFilename, IniContext *pContext)
|
|||
}
|
||||
|
||||
int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||
const char annotation_type, AnnotationMap *annotations, const int count,
|
||||
const char annotation_type, AnnotationEntry *annotations, const int count,
|
||||
const char flags)
|
||||
{
|
||||
int result;
|
||||
|
|
@ -706,7 +706,7 @@ static int iniDoLoadFromFile(const char *szFilename, \
|
|||
}
|
||||
|
||||
int iniLoadFromBufferEx(char *content, IniContext *pContext,
|
||||
const char annotation_type, AnnotationMap *annotations, const int count,
|
||||
const char annotation_type, AnnotationEntry *annotations, const int count,
|
||||
const char flags)
|
||||
{
|
||||
int result;
|
||||
|
|
@ -985,14 +985,14 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
|
|||
|
||||
if (isAnnotation)
|
||||
{
|
||||
AnnotationMap *pAnnoMapBase;
|
||||
AnnotationMap *pAnnoMap = NULL;
|
||||
AnnotationEntry *pAnnoMapBase;
|
||||
AnnotationEntry *pAnnoMap = NULL;
|
||||
bool found;
|
||||
|
||||
isAnnotation = 0;
|
||||
if ((pAnnoMapBase=iniGetAnnotations(pContext)) == NULL)
|
||||
{
|
||||
pAnnoMapBase = g_annotation_map;
|
||||
pAnnoMapBase = g_annotations;
|
||||
}
|
||||
if (pAnnoMapBase == NULL)
|
||||
{
|
||||
|
|
@ -1034,9 +1034,9 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
|
|||
{
|
||||
break;
|
||||
}
|
||||
if (g_annotation_map != NULL && pAnnoMapBase != g_annotation_map)
|
||||
if (g_annotations != NULL && pAnnoMapBase != g_annotations)
|
||||
{
|
||||
pAnnoMapBase = g_annotation_map;
|
||||
pAnnoMapBase = g_annotations;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -1203,7 +1203,7 @@ static DynamicAnnotations *iniAllocDynamicAnnotation(IniContext *pContext)
|
|||
return &pair->dynamicAnnotations;
|
||||
}
|
||||
|
||||
static AnnotationMap *iniGetAnnotations(IniContext *pContext)
|
||||
static AnnotationEntry *iniGetAnnotations(IniContext *pContext)
|
||||
{
|
||||
static CDCPair *pair;
|
||||
|
||||
|
|
@ -1374,7 +1374,7 @@ static int iniCheckAllocAnnotations(DynamicAnnotations *pDynamicAnnotations,
|
|||
{
|
||||
int alloc_count;
|
||||
int bytes;
|
||||
AnnotationMap *annotations;
|
||||
AnnotationEntry *annotations;
|
||||
|
||||
if (pDynamicAnnotations->count + annotation_count <
|
||||
pDynamicAnnotations->alloc_count)
|
||||
|
|
@ -1394,8 +1394,8 @@ static int iniCheckAllocAnnotations(DynamicAnnotations *pDynamicAnnotations,
|
|||
{
|
||||
alloc_count *= 2;
|
||||
}
|
||||
bytes = sizeof(AnnotationMap) * alloc_count;
|
||||
annotations = (AnnotationMap *)malloc(bytes);
|
||||
bytes = sizeof(AnnotationEntry) * alloc_count;
|
||||
annotations = (AnnotationEntry *)malloc(bytes);
|
||||
if (annotations == NULL)
|
||||
{
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
|
|
@ -1406,7 +1406,7 @@ static int iniCheckAllocAnnotations(DynamicAnnotations *pDynamicAnnotations,
|
|||
if (pDynamicAnnotations->count > 0)
|
||||
{
|
||||
memcpy(annotations, pDynamicAnnotations->annotations,
|
||||
sizeof(AnnotationMap) * pDynamicAnnotations->count);
|
||||
sizeof(AnnotationEntry) * pDynamicAnnotations->count);
|
||||
free(pDynamicAnnotations->annotations);
|
||||
}
|
||||
pDynamicAnnotations->annotations = annotations;
|
||||
|
|
|
|||
|
|
@ -58,13 +58,13 @@ typedef struct ini_context
|
|||
char flags;
|
||||
} IniContext;
|
||||
|
||||
typedef struct ini_annotation_map {
|
||||
typedef struct ini_annotation_entry {
|
||||
char *func_name;
|
||||
int (*func_init) ();
|
||||
void (*func_destroy) ();
|
||||
int (*func_get) (IniContext *context, char *param, char **pOutValue, int max_values);
|
||||
void (*func_free) (char **values, const int count);
|
||||
} AnnotationMap;
|
||||
} AnnotationEntry;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
|
@ -76,7 +76,7 @@ extern "C" {
|
|||
strcasecmp(pValue, "on") == 0 || \
|
||||
strcmp(pValue, "1") == 0)
|
||||
|
||||
int iniSetAnnotationCallBack(AnnotationMap *map, int count);
|
||||
int iniSetAnnotationCallBack(AnnotationEntry *map, int count);
|
||||
void iniDestroyAnnotationCallBack();
|
||||
|
||||
/** load ini items from file
|
||||
|
|
@ -98,7 +98,7 @@ int iniLoadFromFile(const char *szFilename, IniContext *pContext);
|
|||
* return: error no, 0 for success, != 0 fail
|
||||
*/
|
||||
int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||
const char annotation_type, AnnotationMap *annotations, const int count,
|
||||
const char annotation_type, AnnotationEntry *annotations, const int count,
|
||||
const char flags);
|
||||
|
||||
/** load ini items from string buffer
|
||||
|
|
@ -120,7 +120,7 @@ int iniLoadFromBuffer(char *content, IniContext *pContext);
|
|||
* return: error no, 0 for success, != 0 fail
|
||||
*/
|
||||
int iniLoadFromBufferEx(char *content, IniContext *pContext,
|
||||
const char annotation_type, AnnotationMap *annotations, const int count,
|
||||
const char annotation_type, AnnotationEntry *annotations, const int count,
|
||||
const char flags);
|
||||
|
||||
/** free ini context
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# to support #@function CONFIG_GET
|
||||
#@add_annotation CONFIG_GET /usr/local/lib/libshmcache.so /usr/local/etc/libshmcache.conf
|
||||
|
||||
#@set author_name = yuqing
|
||||
#@set os_name = $(uname -a | awk '{print $1;}')
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@
|
|||
#include "fastcommon/shared_func.h"
|
||||
#include "fastcommon/ini_file_reader.h"
|
||||
|
||||
static int iniAnnotationFuncExpressCalc(char *param, char **pOutValue, int max_values)
|
||||
static int iniAnnotationFuncExpressCalc(IniContext *context, char *param,
|
||||
char **pOutValue, int max_values)
|
||||
{
|
||||
int count;
|
||||
int result;
|
||||
|
|
@ -41,7 +42,7 @@ int main(int argc, char *argv[])
|
|||
int result;
|
||||
IniContext context;
|
||||
const char *szFilename = "test.ini";
|
||||
AnnotationMap annotations[1];
|
||||
AnnotationEntry annotations[1];
|
||||
|
||||
if (argc > 1) {
|
||||
szFilename = argv[1];
|
||||
|
|
@ -53,6 +54,7 @@ int main(int argc, char *argv[])
|
|||
annotations[0].func_init = NULL;
|
||||
annotations[0].func_destroy = NULL;
|
||||
annotations[0].func_get = iniAnnotationFuncExpressCalc;
|
||||
annotations[0].func_free = NULL;
|
||||
|
||||
//printf("sizeof(IniContext): %d\n", (int)sizeof(IniContext));
|
||||
result = iniLoadFromFileEx(szFilename, &context,
|
||||
|
|
|
|||
Loading…
Reference in New Issue