[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Confuse-devel] Need help with libconfuse
From: |
Martin Hedenfalk |
Subject: |
Re: [Confuse-devel] Need help with libconfuse |
Date: |
Fri, 17 Oct 2003 14:09:34 +0200 |
On Fri, 2003-10-17 at 13:13, Rajeev Sampige wrote:
> Hi,
Hello!
> I'm developing a server which is supposed to open a default config file
> and get the information about some components present on the system.
>
> Sample config file looks like,
>
> # sessions
>
> polling_interval = 5
>
> retry_count=4
>
> ABC_0
> IPAddress=10.0.10.1
> UserID=admin
> Passwd=admin
>
> DEF_0
> IPAddress=10.0.10.2
> UserID=user1
> Passwd=user2
>
> The plan is to try connect to ABC_0, DEF_0 ..etc using IP address and
> other details. we could have many sets similar to ABC_0, but we will
> have only one entry of retry_count and polling_interval.
> Is it possible for me to use libconfuse APIs for this purpose ?
Yes, you can create a section called e.g. "server", and your config file
would then look something like this:
polling_interval = 5
retry_count = 4
server ABC_0 {
IPAddress=10.0.10.1
UserID=admin
Passwd=admin
}
server DEF_0 {
IPAddress=10.0.10.2
UserID=user1
Passwd=user2
}
> Could you suggest me a skeletal code in c ?
/* options for the "server" section */
cfg_opt_t server_opts[] = {
CFG_STR("IPAddress", 0, CFGF_NONE),
CFG_STR("UserID", 0, CFGF_NONE),
CFG_STR("Passwd", 0, CFGF_NONE),
}
/* global options */
cfg_opt_t opts[] = {
CFG_INT("polling_interval", 5, CFGF_NONE),
CFG_INT("retry_count", 4, CFGF_NONE),
CFG_SEC("server", server_opts, CFGF_MULTI | CFGF_TITLE),
CFG_END()
}
main()
{
int i;
/* initialize and parse config file */
cfg_t *cfg = cfg_init(opts, CFGF_NOCASE);
cfg_parse(cfg, "filename.conf");
/* do something with each configured server */
for(i = 0; i < cfg_size(cfg, "server"); i++
{
cfg_sec_t *sec = cfg_getnsec(cfg, "server", i);
do_something(cfg_getstr(sec, "IPAddress"),
cfg_getstr(sec, "UserID"),
cfg_getstr(sec, "Passwd"));
}
cfg_free(cfg);
}
> Thanks,
> Rajeev
>
HTH / Martin