libconfig c language examples

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43608153/article/details/90946327

In the work item, use the configuration file import and export functions (otherwise the program is not configurable), so we need to have a reasonable choice of configuration file read and write operations, libconfig is such a kind of thing.

Libconfig is a structured profile library, it can define some configuration files, such as test.cfg. It is more readable than xml, and more concise. libconfig mainly by reading .cfg file path corresponding to the acquired contents, the contents necessary to parse the string and the like, the data acquired by the function directly, very convenient.

Examples official is simple, but a little long, by following this simple example, we take a good look at the usage among several major functions
first define the profile test.cfg test, where the need to use the configuration data is as follows:

		timeFormatComb =
		{
			type = "combo";
			label = "时间格式";
			x = 355;
			y = 170;
			w = 120;
			h = 32;
			member = ["12小时","24小时"];
			comboH = 200;
		};

The following is the code section:

#include "stdio.h"
#include "libconfig.h"

#define STR_LEN    (128)
#define GUI_MAIN_CFG_PATH "/mnt/hgfs/share/projects/test.cfg"

int main(int argc, const char *argv[])
{
	int ret = 0;
	int result = 0;
	int i= 0;
	int cfgx = 0;
	int cfgy = 0;
	int cfgw = 0;
	int cfgh = 0;
	int comboH = 0;
	int memberCount = 0;
    const char *pLabel = NULL;
	config_t *cfgHandle; /* 配置文件操作句柄 */
    config_setting_t *pCfgSetting = NULL;
    config_setting_t *pMemberDetail = NULL;
	char path[128] = {0};

	pLabel = (char *)malloc(STR_LEN);
	if(pLabel == NULL)
	{
		printf("malloc() error! pLabel = %p\n", pLabel);
		goto EXIT;
	}
    /* 读取配置文件,获取文件句柄 */
	result = config_read_file(cfgHandle, GUI_MAIN_CFG_PATH);
	if(ret != 0)
	{
		printf("config_read_file() error! result = %d\n", result);
	}

	snprintf(path, sizeof(path), "%s", "winSysSymTimeCfgSet.timeCfgPanel.timeFormatComb");

	/* 通过文件句柄查找数据 */
	pCfgSetting = config_lookup(cfgHandle, path);
	if(pCfgSetting == NULL)
	{
		printf("config_lookup() error! path = %s\n", path);
		goto EXIT;
	}
	printf("pCfgSetting = %p", pCfgSetting);

    /* 查找字符串 */
	result = config_setting_lookup_string(pCfgSetting, "label", &pLabel);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_string() error.\n");
		goto EXIT;
	}
	printf("pLabel = %s", pLabel);

    /* 查找int数值 */
	result = config_setting_lookup_int(pCfgSetting, "x", &cfgx);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	
	result = config_setting_lookup_int(pCfgSetting, "y", &cfgy);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	
	result = config_setting_lookup_int(pCfgSetting, "w", &cfgw);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	
	result = config_setting_lookup_int(pCfgSetting, "h", &cfgh);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	printf("x = %d; y = %d; w = %d; h = %d\n", cfgx, cfgy, cfgw, cfgh);

	result = config_setting_lookup_int(pCfgSetting, "comboH", &comboH);
	if(result != CONFIG_TRUE)
	{
		guiLogErr("config_setting_lookup_int() error!\n");
		goto EXIT;
	}
	printf("x = %d; y = %d; w = %d; h = %d\n", cfgx, cfgy, cfgw, cfgh);

    /* 查找数组 */
	pMemberDetail = config_setting_get_member(pCfgSetting, "member");
	if(pMemberDetail == NULL)
	{
		guiLogErr("config_setting_get_member() error!\n");
		goto EXIT;
	}
	
    /* 获取数组长度 */
    memberCount = config_setting_length(pMemberDetail);
	if(memberCount <= 0)
	{
		guiLogErr("config_setting_length() error!\n");
		goto EXIT;
	}
	printf("memberCount = %d\n", memberCount);

	for(i = 0; i < memberCount; i++)
	{
		pLabel = config_setting_get_string_elem(pMemberDetail, i);
	    if(pLabel == NULL) 
	    {
	        guiLogErr("config_setting_get_string_elem = %s\n", pLabel);
	        goto EXIT;
	    }
		printf("pLabel = %s", pLabel);
	}
	config_destroy(&cfgHandle);

EXIT:

	return ret;
}

Because the libconfig to use the library, so at compile time to link library files
gcc test.c -o test -lconfig

Results are as follows:
Here Insert Picture Description

This simple example, some column values ​​is extracted through the configuration file a series of library functions. Because of time constraints, do not spend too much time to engage, interested friends can try it yourself.

Guess you like

Origin blog.csdn.net/weixin_43608153/article/details/90946327