libconfig c语言实例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43608153/article/details/90946327

在工作项目中,使用到了配置文件导入导出等功能(否则程序就不是可配置的),所以就需要合理选择配置文件得读写操作,libconfig就是这么样一个东西。

Libconfig是一个结构化的配置文件库,它可以定义一些配置文件,例如test.cfg . 它比xml可读性更好,而且更简洁。libconfig主要是通过路径读取对应的 .cfg文件来获取其中的内容,不必要去解析字符串等内容,直接通过函数获取数据,非常方便。

官方的例子虽然简单,但是有点长,通过下面这个简单的例子,向大家接好一下其中几个主要的函数的用法
首先定义测试的配置文件 test.cfg, 其中需要用到的配置数据如下:

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

以下是代码部分:

#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;
}

因为使用到了libconfig库,所以在编译的时候要链接库文件
gcc test.c -o test -lconfig

运行结果如下:
在这里插入图片描述

这个简单的例子,通过一些列的库函数将配置文件中的一些列值取出。因为时间有限,没有花费太多的时间去搞,有兴趣的朋友可以自己试一试。

猜你喜欢

转载自blog.csdn.net/weixin_43608153/article/details/90946327