hyperrealm / libconfig

C/C++ library for processing configuration files
https://hyperrealm.github.io/libconfig/
GNU Lesser General Public License v2.1
1.1k stars 359 forks source link

How to enumerate keys for dict? #201

Closed nickhuangxinyu closed 2 years ago

nickhuangxinyu commented 2 years ago
dict = {
 key1="a";
 key2="b";
};

can i enumerate keys for dict (it's key1 and key2)?

i know this can be implemented by:

dict = (
{
 key="key1";
 value="a"
},
{
 key="key2";
value="b";
}
);

but this seems be wired, can you add the enumerate method in next version?

hyperrealm commented 2 years ago

You can enumerate groups the same way you enumerate lists and arrays, using config_setting_length() and config_setting_get_elem(). Or operator[] in C++.

nickhuangxinyu commented 2 years ago

can you provide a demo code? my config is:

dict = {
 key1="a";
 key2="b";
};

how to enumerate the dict without knowing key1 and key2? thanks very much!! @hyperrealm

hyperrealm commented 2 years ago

const Setting &dict = cfg.lookup("dict"); for (int i = 0; i < dict.getLength(); ++i) { const Setting &child = dict[i]; // child.getName() is the key }

examples/c++/example1.cpp has an example of exactly this