ini_file_reader add iniGetSectionNames and iniGetSectionItems

pull/5/head
yuqing 2016-01-14 17:08:44 +08:00
parent bb57593f14
commit e73b37f7da
3 changed files with 130 additions and 0 deletions

View File

@ -7,6 +7,7 @@ Version 1.24 2016-01-14
* add system_info.h and system_info.c
* add function get_mounted_filesystems
* add function get_processes for Linux
* ini_file_reader add iniGetSectionNames and iniGetSectionItems
Version 1.23 2015-11-16
* sched_thread.c: task can execute in a new thread

View File

@ -966,3 +966,82 @@ void iniPrintItems(IniContext *pContext)
hash_walk(&pContext->sections, iniPrintHashData, NULL);
}
struct section_walk_arg {
IniSectionInfo *sections;
int count;
int size;
};
static int iniSectionWalkCallback(const int index, const HashData *data,
void *args)
{
struct section_walk_arg *walk_arg;
IniSection *pSection;
char *section_name;
int section_len;
pSection = (IniSection *)data->value;
if (pSection == NULL)
{
return 0;
}
walk_arg = (struct section_walk_arg *)args;
if (walk_arg->count >= walk_arg->size)
{
return ENOSPC;
}
section_len = data->key_len;
if (section_len > FAST_INI_ITEM_NAME_LEN)
{
section_len = FAST_INI_ITEM_NAME_LEN;
}
section_name = walk_arg->sections[walk_arg->count].section_name;
memcpy(section_name, data->key, section_len);
*(section_name + section_len) = '\0';
walk_arg->sections[walk_arg->count].pSection = pSection;
walk_arg->count++;
return 0;
}
int iniGetSectionNames(IniContext *pContext, IniSectionInfo *sections,
const int max_size, int *nCount)
{
struct section_walk_arg walk_arg;
int result;
walk_arg.sections = sections;
walk_arg.count = 0;
walk_arg.size = max_size;
result = hash_walk(&pContext->sections, iniSectionWalkCallback, &walk_arg);
*nCount = walk_arg.count;
return result;
}
IniItem *iniGetSectionItems(const char *szSectionName, IniContext *pContext,
int *nCount)
{
IniSection *pSection;
if (szSectionName == NULL || *szSectionName == '\0')
{
pSection = &pContext->global;
}
else
{
pSection = (IniSection *)hash_find(&pContext->sections,
szSectionName, strlen(szSectionName));
if (pSection == NULL)
{
*nCount = 0;
return NULL;
}
}
*nCount = pSection->count;
return pSection->items;
}

View File

@ -39,6 +39,12 @@ typedef struct
int alloc_count;
} IniSection;
typedef struct
{
char section_name[FAST_INI_ITEM_NAME_LEN + 1];
IniSection *pSection;
} IniSectionInfo;
typedef struct
{
IniSection global;
@ -179,6 +185,50 @@ double iniGetDoubleValue(const char *szSectionName, const char *szItemName, \
*/
void iniPrintItems(IniContext *pContext);
/** return the path of config filename
* parameters:
* pContext: the ini context
* return: the config path
*/
static inline const char *iniGetConfigPath(IniContext *pContext)
{
return pContext->config_path;
}
/** return the items of global section
* parameters:
* pContext: the ini context
* sections: the section array
* max_size: the max size of sections
* nCount: return the section count
* return: errno, 0 for success, != 0 for fail
*/
int iniGetSectionNames(IniContext *pContext, IniSectionInfo *sections,
const int max_size, int *nCount);
/** return the items of global section
* parameters:
* pContext: the ini context
* nCount: return the item count
* return: the global items
*/
static inline IniItem *iniGetGlobalItems(IniContext *pContext, int *nCount)
{
*nCount = pContext->global.count;
return pContext->global.items;
}
/** return the section items
* parameters:
* szSectionName: the section name, NULL or empty string for
* global section
* pContext: the ini context
* nCount: return the item count
* return: the section items, NULL for not exist
*/
IniItem *iniGetSectionItems(const char *szSectionName, IniContext *pContext,
int *nCount);
#ifdef __cplusplus
}
#endif