ini_file_reader support flags

pull/37/head
yuqing 2017-03-02 09:54:35 +08:00
parent 2d60671511
commit 92f4763638
6 changed files with 83 additions and 47 deletions

View File

@ -1,5 +1,5 @@
Version 1.35 2017-03-01 Version 1.35 2017-03-02
* logger judge log_level in function log_it_ex and log_it_ex1 * logger judge log_level in function log_it_ex and log_it_ex1
* add php extension function: fastcommon_file_put_contents * add php extension function: fastcommon_file_put_contents
* add function fc_safe_read and fc_safe_write * add function fc_safe_read and fc_safe_write
@ -7,6 +7,7 @@ Version 1.35 2017-03-01
* add function fc_trim to avoid conflict with trim * add function fc_trim to avoid conflict with trim
* ini_file_reader support annotataion map set incrementally * ini_file_reader support annotataion map set incrementally
* init context support dynamic annotations and build-in annotations * init context support dynamic annotations and build-in annotations
* ini_file_reader disable shell execute in default
Version 1.34 2017-02-06 Version 1.34 2017-02-06
* ini_file_reader: LOCAL_IP support CIDR addresses * ini_file_reader: LOCAL_IP support CIDR addresses

View File

@ -190,21 +190,29 @@ static int iniAnnotationFuncShellExec(char *param, char **pOutValue, int max_val
return count; return count;
} }
static void iniSetBuiltinAnnotations(AnnotationMap *dest, int *dest_count) static void iniSetBuiltinAnnotations(IniContext *pContext,
AnnotationMap *dest, int *dest_count)
{ {
AnnotationMap builtins[_BUILTIN_ANNOTATION_COUNT]; AnnotationMap builtins[_BUILTIN_ANNOTATION_COUNT];
AnnotationMap *pAnnotation;
builtins[0].func_name = "LOCAL_IP_GET"; pAnnotation = builtins;
builtins[0].func_init = NULL; pAnnotation->func_name = "LOCAL_IP_GET";
builtins[0].func_destroy = NULL; pAnnotation->func_init = NULL;
builtins[0].func_get = iniAnnotationFuncLocalIpGet; pAnnotation->func_destroy = NULL;
pAnnotation->func_get = iniAnnotationFuncLocalIpGet;
pAnnotation++;
builtins[1].func_name = "SHELL_EXEC"; if ((pContext->flags & FAST_INI_FLAGS_SHELL_EXECUTE) != 0)
builtins[1].func_init = NULL; {
builtins[1].func_destroy = NULL; pAnnotation->func_name = "SHELL_EXEC";
builtins[1].func_get = iniAnnotationFuncShellExec; pAnnotation->func_init = NULL;
pAnnotation->func_destroy = NULL;
pAnnotation->func_get = iniAnnotationFuncShellExec;
pAnnotation++;
}
iniDoSetAnnotations(builtins, _BUILTIN_ANNOTATION_COUNT, dest, dest_count); iniDoSetAnnotations(builtins, pAnnotation - builtins, dest, dest_count);
} }
static int iniSetAnnotations(IniContext *pContext, const char annotation_type, static int iniSetAnnotations(IniContext *pContext, const char annotation_type,
@ -213,11 +221,11 @@ static int iniSetAnnotations(IniContext *pContext, const char annotation_type,
DynamicAnnotations *pDynamicAnnotations; DynamicAnnotations *pDynamicAnnotations;
pContext->annotation_type = annotation_type; pContext->annotation_type = annotation_type;
if (pContext->annotation_type == INI_ANNOTATION_DISABLE) if (pContext->annotation_type == FAST_INI_ANNOTATION_DISABLE)
{ {
return 0; return 0;
} }
if (pContext->annotation_type == INI_ANNOTATION_WITHOUT_BUILTIN && if (pContext->annotation_type == FAST_INI_ANNOTATION_WITHOUT_BUILTIN &&
annotations == NULL) annotations == NULL)
{ {
return 0; return 0;
@ -228,9 +236,9 @@ static int iniSetAnnotations(IniContext *pContext, const char annotation_type,
{ {
return ENOMEM; return ENOMEM;
} }
if (pContext->annotation_type == INI_ANNOTATION_WITH_BUILTIN) if (pContext->annotation_type == FAST_INI_ANNOTATION_WITH_BUILTIN)
{ {
iniSetBuiltinAnnotations(pDynamicAnnotations->annotations, iniSetBuiltinAnnotations(pContext, pDynamicAnnotations->annotations,
&pDynamicAnnotations->count); &pDynamicAnnotations->count);
} }
@ -307,7 +315,8 @@ 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) AnnotationMap *annotations, const int count,
const char flags)
{ {
int result; int result;
@ -320,6 +329,7 @@ static int iniInitContext(IniContext *pContext, const char annotation_type,
__LINE__, result, STRERROR(result)); __LINE__, result, STRERROR(result));
} }
pContext->flags = flags;
return iniSetAnnotations(pContext, annotation_type, annotations, count); return iniSetAnnotations(pContext, annotation_type, annotations, count);
} }
@ -351,11 +361,13 @@ static void iniSortItems(IniContext *pContext)
int iniLoadFromFile(const char *szFilename, IniContext *pContext) int iniLoadFromFile(const char *szFilename, IniContext *pContext)
{ {
return iniLoadFromFileEx(szFilename, pContext, return iniLoadFromFileEx(szFilename, pContext,
INI_ANNOTATION_WITHOUT_BUILTIN, NULL, 0); FAST_INI_ANNOTATION_WITH_BUILTIN,
NULL, 0, FAST_INI_FLAGS_NONE);
} }
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, AnnotationMap *annotations, const int count,
const char flags)
{ {
int result; int result;
int len; int len;
@ -363,7 +375,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
char full_filename[MAX_PATH_SIZE]; char full_filename[MAX_PATH_SIZE];
if ((result=iniInitContext(pContext, annotation_type, if ((result=iniInitContext(pContext, annotation_type,
annotations, count)) != 0) annotations, count, flags)) != 0)
{ {
return result; return result;
} }
@ -501,12 +513,13 @@ 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, AnnotationMap *annotations, const int count,
const char flags)
{ {
int result; int result;
if ((result=iniInitContext(pContext, annotation_type, if ((result=iniInitContext(pContext, annotation_type,
annotations, count)) != 0) annotations, count, flags)) != 0)
{ {
return result; return result;
} }
@ -527,7 +540,8 @@ int iniLoadFromBufferEx(char *content, IniContext *pContext,
int iniLoadFromBuffer(char *content, IniContext *pContext) int iniLoadFromBuffer(char *content, IniContext *pContext)
{ {
return iniLoadFromBufferEx(content, pContext, return iniLoadFromBufferEx(content, pContext,
INI_ANNOTATION_WITHOUT_BUILTIN, NULL, 0); FAST_INI_ANNOTATION_WITH_BUILTIN,
NULL, 0, FAST_INI_FLAGS_NONE);
} }
static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
@ -641,7 +655,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
strncasecmp(pLine+1, "@function", 9) == 0 && \ strncasecmp(pLine+1, "@function", 9) == 0 && \
(*(pLine+10) == ' ' || *(pLine+10) == '\t'))) (*(pLine+10) == ' ' || *(pLine+10) == '\t')))
{ {
if (pContext->annotation_type != INI_ANNOTATION_DISABLE) if (pContext->annotation_type != FAST_INI_ANNOTATION_DISABLE)
{ {
nNameLen = strlen(pLine + 11); nNameLen = strlen(pLine + 11);
if (nNameLen > FAST_INI_ITEM_NAME_LEN) if (nNameLen > FAST_INI_ITEM_NAME_LEN)
@ -1599,21 +1613,29 @@ static int iniDoProccessSet(char *pSet, char **ppSetEnd,
{ {
char *cmd; char *cmd;
cmd = value + 2; cmd = value + 2;
*(value + value_len - 1) = '\0'; //remove '}' *(value + value_len - 1) = '\0'; //remove ')'
if ((result=getExecResult(cmd, output, sizeof(output))) != 0) if ((pContext->flags & FAST_INI_FLAGS_SHELL_EXECUTE) != 0)
{
if ((result=getExecResult(cmd, output, sizeof(output))) != 0)
{
logWarning("file: "__FILE__", line: %d, "
"exec %s fail, errno: %d, error info: %s",
__LINE__, cmd, result, STRERROR(result));
return result;
}
if (*output == '\0')
{
logWarning("file: "__FILE__", line: %d, "
"empty reply when exec: %s", __LINE__, cmd);
}
value = fc_trim(output);
value_len = strlen(value);
}
else
{ {
logWarning("file: "__FILE__", line: %d, " logWarning("file: "__FILE__", line: %d, "
"exec %s fail, errno: %d, error info: %s", "shell execute disabled, cmd: %s", __LINE__, cmd);
__LINE__, cmd, result, STRERROR(result));
return result;
} }
if (*output == '\0')
{
logWarning("file: "__FILE__", line: %d, "
"empty reply when exec: %s", __LINE__, cmd);
}
value = fc_trim(output);
value_len = strlen(value);
} }
return hash_insert_ex(set->vars, key, strlen(key), return hash_insert_ex(set->vars, key, strlen(key),
@ -2369,7 +2391,7 @@ bool iniGetBoolValue(const char *szSectionName, const char *szItemName, \
} }
else else
{ {
return INI_STRING_IS_TRUE(pValue); return FAST_INI_STRING_IS_TRUE(pValue);
} }
} }

View File

@ -19,10 +19,13 @@
#define FAST_INI_ITEM_NAME_LEN 64 #define FAST_INI_ITEM_NAME_LEN 64
#define FAST_INI_ITEM_VALUE_LEN 256 #define FAST_INI_ITEM_VALUE_LEN 256
#define INI_ANNOTATION_WITHOUT_BUILTIN 0 #define FAST_INI_ANNOTATION_WITHOUT_BUILTIN 0
#define INI_ANNOTATION_DISABLE 1 #define FAST_INI_ANNOTATION_DISABLE 1
#define INI_ANNOTATION_WITH_BUILTIN 2 #define FAST_INI_ANNOTATION_WITH_BUILTIN 2
#define INI_ANNOTATION_NONE INI_ANNOTATION_DISABLE #define FAST_INI_ANNOTATION_NONE FAST_INI_ANNOTATION_DISABLE
#define FAST_INI_FLAGS_NONE 0
#define FAST_INI_FLAGS_SHELL_EXECUTE 1
typedef struct { typedef struct {
char *func_name; char *func_name;
@ -57,13 +60,14 @@ typedef struct
IniSection *current_section; //for load from ini file IniSection *current_section; //for load from ini file
char config_path[MAX_PATH_SIZE]; //save the config filepath char config_path[MAX_PATH_SIZE]; //save the config filepath
char annotation_type; char annotation_type;
char flags;
} IniContext; } IniContext;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define INI_STRING_IS_TRUE(pValue) \ #define FAST_INI_STRING_IS_TRUE(pValue) \
(strcasecmp(pValue, "true") == 0 || \ (strcasecmp(pValue, "true") == 0 || \
strcasecmp(pValue, "yes") == 0 || \ strcasecmp(pValue, "yes") == 0 || \
strcasecmp(pValue, "on") == 0 || \ strcasecmp(pValue, "on") == 0 || \
@ -87,10 +91,12 @@ int iniLoadFromFile(const char *szFilename, IniContext *pContext);
* annotation_type: the annotation type * annotation_type: the annotation type
* annotations: the annotations, can be NULL * annotations: the annotations, can be NULL
* count: the annotation count * count: the annotation count
* flags: the flags
* 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, AnnotationMap *annotations, const int count,
const char flags);
/** load ini items from string buffer /** load ini items from string buffer
* parameters: * parameters:
@ -107,10 +113,12 @@ int iniLoadFromBuffer(char *content, IniContext *pContext);
* annotation_type: the annotation type * annotation_type: the annotation type
* annotations: the annotations, can be NULL * annotations: the annotations, can be NULL
* count: the annotation count * count: the annotation count
* flags: the flags
* 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, AnnotationMap *annotations, const int count,
const char flags);
/** free ini context /** free ini context
* parameters: * parameters:

View File

@ -246,7 +246,8 @@ int get_base_path_from_conf_file(const char *filename, char *base_path,
int result; int result;
if ((result=iniLoadFromFileEx(filename, &iniContext, if ((result=iniLoadFromFileEx(filename, &iniContext,
INI_ANNOTATION_DISABLE, NULL, 0)) != 0) FAST_INI_ANNOTATION_DISABLE, NULL, 0,
FAST_INI_FLAGS_NONE)) != 0)
{ {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \
"load conf file \"%s\" fail, ret code: %d", \ "load conf file \"%s\" fail, ret code: %d", \

View File

@ -1395,7 +1395,8 @@ int load_log_level_ex(const char *conf_filename)
IniContext iniContext; IniContext iniContext;
if ((result=iniLoadFromFileEx(conf_filename, &iniContext, if ((result=iniLoadFromFileEx(conf_filename, &iniContext,
INI_ANNOTATION_DISABLE, NULL, 0)) != 0) FAST_INI_ANNOTATION_DISABLE, NULL, 0,
FAST_INI_FLAGS_NONE)) != 0)
{ {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \
"load conf file \"%s\" fail, ret code: %d", \ "load conf file \"%s\" fail, ret code: %d", \

View File

@ -54,8 +54,11 @@ int main(int argc, char *argv[])
annotations[0].func_destroy = NULL; annotations[0].func_destroy = NULL;
annotations[0].func_get = iniAnnotationFuncExpressCalc; annotations[0].func_get = iniAnnotationFuncExpressCalc;
if ((result=iniLoadFromFileEx(szFilename, &context, //printf("sizeof(IniContext): %d\n", (int)sizeof(IniContext));
INI_ANNOTATION_WITH_BUILTIN, annotations, 1)) != 0) result = iniLoadFromFileEx(szFilename, &context,
FAST_INI_ANNOTATION_WITH_BUILTIN, annotations, 1,
FAST_INI_FLAGS_SHELL_EXECUTE);
if (result != 0)
{ {
return result; return result;
} }