From e73b37f7dafb07debc486203cf4ebf6393e416c3 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 14 Jan 2016 17:08:44 +0800 Subject: [PATCH] ini_file_reader add iniGetSectionNames and iniGetSectionItems --- HISTORY | 1 + src/ini_file_reader.c | 79 +++++++++++++++++++++++++++++++++++++++++++ src/ini_file_reader.h | 50 +++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) diff --git a/HISTORY b/HISTORY index 62d10b1..075ff5b 100644 --- a/HISTORY +++ b/HISTORY @@ -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 diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 0d776d8..35cb0a7 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -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; +} + diff --git a/src/ini_file_reader.h b/src/ini_file_reader.h index 9304d81..2a586ee 100644 --- a/src/ini_file_reader.h +++ b/src/ini_file_reader.h @@ -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