LibConfig - 如何获取容器列表

时间:2016-01-05 12:24:18

标签: c libconfig

我有以下配置,我知道如何使用config_lookup()遍历每个部分,但我不知道如何找到每个部分的名称!

downloads = {

    john = {

        configid = 1;
        status = 1;
        configname = "John's File Server";
        configtype = 0;
        ipaddress = "192.168.1.100";
        username = "test";
        password = "test";
    };

    jill = {

        configid = 3;
        status = 1;
        configname = "Jill's file server";
        configtype = 0;
        ipaddress = "10.10.20.50";
        username = "test";
        password = "test";
    };
};

我想知道如何获得部分名称,即。,jack& jill with libconfig。我知道如何获得jack和jill中的值,因为我事先知道了param的名字,但是jack和jill是用户配置的。缩短的代码就像这样 -

config_t cfg;
config_setting_t *setting;
int i = 0, count = 0;

if(!config_read_file(&cfg, filename))
    return false;

if((setting = config_lookup(&cfg, "downloads")) != NULL)
{
    count = config_setting_length(setting);

    for(i = 0; i < count; ++i)
    {
        // do stuff
    }
}

config_destroy(&cfg);

知道怎么做吗?提前致谢

1 个答案:

答案 0 :(得分:0)

好的,因为Ian没有发布答案,我是。归功于伊恩。

config_t cfg;
config_setting_t *setting;
int i = 0, count = 0;

if(!config_read_file(&cfg, filename))
    return false;

if((setting = config_lookup(&cfg, "downloads")) != NULL)
{
    count = config_setting_length(setting);

    for(i = 0; i < count; ++i)
    {
        config_setting_t *nodes = config_setting_get_elem(setting, i);
        const char *section_name = = config_setting_name(nodes); // Section name, no need to free, the library manages that;
    }
}

config_destroy(&cfg);