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