ignore annotation in some cases

pull/5/head
liuwei 2015-09-11 18:22:49 +08:00
parent 29451eaf49
commit 5f52349bd6
4 changed files with 138 additions and 21 deletions

View File

@ -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,6 +511,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
strncasecmp(pLine+1, "@function", 9) == 0 && \
(*(pLine+10) == ' ' || *(pLine+10) == '\t')))
{
if (pContext->annotation != IGNORE_ANNOTATION) {
nNameLen = strlen(pLine + 11);
if (nNameLen > FAST_INI_ITEM_NAME_LEN)
{
@ -430,6 +532,7 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
__LINE__);
}
continue;
}
}
trim(pLine);

View File

@ -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

View File

@ -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", \

View File

@ -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", \