From 5f52349bd66efdf90099c456ca87e4a7f32725ac Mon Sep 17 00:00:00 2001 From: liuwei Date: Fri, 11 Sep 2015 18:22:49 +0800 Subject: [PATCH] ignore annotation in some cases --- src/ini_file_reader.c | 141 ++++++++++++++++++++++++++++++++++++------ src/ini_file_reader.h | 13 ++++ src/process_ctrl.c | 3 +- src/shared_func.c | 2 +- 4 files changed, 138 insertions(+), 21 deletions(-) diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index b3cb2cb..1798d4c 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -230,6 +230,107 @@ int iniLoadFromFile(const char *szFilename, IniContext *pContext) return result; } +int iniLoadFromFileEx(const char *szFilename, IniContext *pContext, + char annotation) +{ + int result; + int len; + char *pLast; + char full_filename[MAX_PATH_SIZE]; + + if ((result=iniInitContext(pContext)) != 0) + { + return result; + } + + pContext->annotation = annotation; + + if (strncasecmp(szFilename, "http://", 7) == 0) + { + *pContext->config_path = '\0'; + snprintf(full_filename, sizeof(full_filename),"%s",szFilename); + } + else + { + if (*szFilename == '/') + { + pLast = strrchr(szFilename, '/'); + len = pLast - szFilename; + if (len >= sizeof(pContext->config_path)) + { + logError("file: "__FILE__", line: %d, "\ + "the path of the config file: %s is " \ + "too long!", __LINE__, szFilename); + return ENOSPC; + } + + memcpy(pContext->config_path, szFilename, len); + *(pContext->config_path + len) = '\0'; + snprintf(full_filename, sizeof(full_filename), \ + "%s", szFilename); + } + else + { + memset(pContext->config_path, 0, \ + sizeof(pContext->config_path)); + if (getcwd(pContext->config_path, sizeof( \ + pContext->config_path)) == NULL) + { + logError("file: "__FILE__", line: %d, " \ + "getcwd fail, errno: %d, " \ + "error info: %s", \ + __LINE__, errno, STRERROR(errno)); + return errno != 0 ? errno : EPERM; + } + + len = strlen(pContext->config_path); + if (len > 0 && pContext->config_path[len - 1] == '/') + { + len--; + *(pContext->config_path + len) = '\0'; + } + + snprintf(full_filename, sizeof(full_filename), \ + "%s/%s", pContext->config_path, szFilename); + + pLast = strrchr(szFilename, '/'); + if (pLast != NULL) + { + int tail_len; + + tail_len = pLast - szFilename; + if (len + 1 + tail_len >= sizeof( \ + pContext->config_path)) + { + logError("file: "__FILE__", line: %d, "\ + "the path of the config " \ + "file: %s is too long!", \ + __LINE__, szFilename); + return ENOSPC; + } + + *(pContext->config_path + len++) = '/'; + memcpy(pContext->config_path + len, \ + szFilename, tail_len); + len += tail_len; + *(pContext->config_path + len) = '\0'; + } + } + } + + result = iniDoLoadFromFile(full_filename, pContext); + if (result == 0) + { + iniSortItems(pContext); + } + else + { + iniFreeContext(pContext); + } + + return result; +} + static int iniDoLoadFromFile(const char *szFilename, \ IniContext *pContext) { @@ -410,26 +511,28 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) strncasecmp(pLine+1, "@function", 9) == 0 && \ (*(pLine+10) == ' ' || *(pLine+10) == '\t'))) { - nNameLen = strlen(pLine + 11); - if (nNameLen > FAST_INI_ITEM_NAME_LEN) - { - nNameLen = FAST_INI_ITEM_NAME_LEN; + if (pContext->annotation != IGNORE_ANNOTATION) { + nNameLen = strlen(pLine + 11); + if (nNameLen > FAST_INI_ITEM_NAME_LEN) + { + nNameLen = FAST_INI_ITEM_NAME_LEN; + } + memcpy(pFuncName, pLine + 11, nNameLen); + pFuncName[nNameLen] = '\0'; + trim(pFuncName); + if ((int)strlen(pFuncName) > 0) + { + isAnnotation = 1; + pAnnoItemLine = pLastEnd + 1; + } + else + { + logWarning("file: "__FILE__", line: %d, " \ + "the function name of annotation line is empty", \ + __LINE__); + } + continue; } - memcpy(pFuncName, pLine + 11, nNameLen); - pFuncName[nNameLen] = '\0'; - trim(pFuncName); - if ((int)strlen(pFuncName) > 0) - { - isAnnotation = 1; - pAnnoItemLine = pLastEnd + 1; - } - else - { - logWarning("file: "__FILE__", line: %d, " \ - "the function name of annotation line is empty", \ - __LINE__); - } - continue; } trim(pLine); diff --git a/src/ini_file_reader.h b/src/ini_file_reader.h index 35d05c3..388328c 100644 --- a/src/ini_file_reader.h +++ b/src/ini_file_reader.h @@ -19,6 +19,8 @@ #define FAST_INI_ITEM_NAME_LEN 64 #define FAST_INI_ITEM_VALUE_LEN 256 +#define IGNORE_ANNOTATION 1 + typedef struct { char *func_name; int (*func_init) (); @@ -45,6 +47,7 @@ typedef struct HashArray sections; //key is session name, and value is IniSection IniSection *current_section; //for load from ini file char config_path[MAX_PATH_SIZE]; //save the config filepath + char annotation; } IniContext; #ifdef __cplusplus @@ -62,6 +65,16 @@ void iniDestroyAnnotationCallBack(); */ int iniLoadFromFile(const char *szFilename, IniContext *pContext); +/** load ini items from file + * parameters: + * szFilename: the filename, can be an URL + * pContext: the ini context + * annotation: whether ignore annotation + * return: error no, 0 for success, != 0 fail +*/ +int iniLoadFromFileEx(const char *szFilename, IniContext *pContext, + char annotation); + /** load ini items from string buffer * parameters: * content: the string buffer to parse diff --git a/src/process_ctrl.c b/src/process_ctrl.c index cb69e13..48f3496 100644 --- a/src/process_ctrl.c +++ b/src/process_ctrl.c @@ -211,7 +211,8 @@ int get_base_path_from_conf_file(const char *filename, char *base_path, int result; memset(&iniContext, 0, sizeof(IniContext)); - if ((result=iniLoadFromFile(filename, &iniContext)) != 0) + + if ((result=iniLoadFromFileEx(filename, &iniContext, IGNORE_ANNOTATION)) != 0) { logError("file: "__FILE__", line: %d, " \ "load conf file \"%s\" fail, ret code: %d", \ diff --git a/src/shared_func.c b/src/shared_func.c index a38225e..8330e34 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1322,7 +1322,7 @@ int load_log_level_ex(const char *conf_filename) int result; IniContext iniContext; - if ((result=iniLoadFromFile(conf_filename, &iniContext)) != 0) + if ((result=iniLoadFromFileEx(conf_filename, &iniContext, IGNORE_ANNOTATION)) != 0) { logError("file: "__FILE__", line: %d, " \ "load conf file \"%s\" fail, ret code: %d", \