libConfuse 使用说明

网友投稿 297 2022-12-02

libConfuse 使用说明

【前言】   libconfuse 是一个用 C 实现配置文件解析器库,授权的 ISC 许可的条件下,它支持段(列表)和值(字符串,整数,浮点数,布尔值或其他部分),以及一些其他功能(如单/双引号字符串,环境变量扩展,功能嵌套include语句)。它可以添加配置文件的能力,使用简单的 API 使程序读取配置文件非常容易。

​​ 作者:Martin Hedenfalk libConfuse下载:​​​ ​​ 手册:​​​通过一个实例来介绍 libConfuse

考虑下面这个例子:

#include int main(void){ printf("Hello, World!\n"); return 0;}

这个例子足够简单,但现在我们想扩展这个程序以便于它可以问候其他人。可能我们只想问候邻居,而不是全世界呢。我们可以使用 libConfuse 让用户决定将要问候谁。

#include #include int main(void){ cfg_opt_t opts[] = { CFG_STR("target", "World", CFGF_NONE), CFG_END() }; cfg_t *cfg; cfg = cfg_init(opts, CFGF_NONE); if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR) return 1; printf("Hello, %s!\n", cfg_getstr(cfg, "target")); cfg_free(cfg); return 0;}

All programs using libConfuse must first include the confuse.h header file. This is done on line 2.

On line 6 - 10, the options that should be recognized are defined in an array of cfg_opt_t structs. This is passed to the cfg_init function on line 13. The resulting cfg_t context is used by cfg_parse(), which reads the configuration file “hello.conf”. When reading the configuration file, only options defined in the array of options passed to cfg_init() are recognized.

The friendly greeting is now replaced with a parameter read from the configuration file. The value of the target option is retrieved with cfg_getstr(cfg, “target”).

Lets take a look at the configuration file hello.conf:

# this is the configuration file for the hello programtarget = "Neighbour"

Here, the target option is set to the string value “Neighbour”. What if the configuration file was empty or didn’t exist? Then the default value for the target option would be used. When we initialized our options, the second parameter to the CFG_STR() macro specified the default value. Thus, if no target option was specified in the configuration file, the hello program would have printed the standard greeting “Hello, World”.

1.1. 环境变量

What else can we do in the configuration file? We can set the value to an environment variable:

target = ${USER}

This results in the hello program greeting the user who runs it. On some systems, the USER variable might not be available, so we want to specify a default value in those cases:

target = ${USER:-User}

Now, if the USER environment variable is unset, the string “User” will be used instead.

2. 其他选项类型

Of course, not only strings can be specified in the configuration file. libConfuse can parse strings, integers, booleans and floating point values. These are the fundamental values, and they are all also available as lists. We’ll talk more about lists in the next chapter.

The macros used to initialize a string, integer, boolean and a float is, respectively, CFG_STR(), CFG_INT(), CFG_BOOL(), CFG_FLOAT() and CFG_PTR(). All macros take three parameters: the name of the option, a default value and flags. To retrieve the values, use cfg_getstr(), cfg_getint(), cfg_getbool(), cfg_getfloat() or cfg_getptr(), respectively.

Let’s introduce an integer option that tells us how many times to print the greeting:

#include #include int main(void){ cfg_opt_t opts[] = { CFG_STR("target", "World", CFGF_NONE), CFG_INT("repeat", 1, CFGF_NONE), CFG_END() }; cfg_t *cfg; int repeat; cfg = cfg_init(opts, CFGF_NONE); if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR) return 1; repeat = cfg_getint(cfg, "repeat"); while(repeat--) printf("Hello, %s!\n", cfg_getstr(cfg, "target")); cfg_free(cfg); return 0;}

Here we have used the CFG_INT() macro to initialize an integer option named “repeat”. The default value is 1 as in the standard greeting. The value is retrieved with cfg_getint().

But, wait a moment, what if the user specified a negative value for “repeat”? Or a too large positive value? libConfuse can handle that with a so-called validating callback. We’ll come back to this problem later on, but we will first take a look at lists.

3. 介绍 lists

4. 使用 sections

5. 从内部缓冲区中解析

6. 使回调函数生效

6.1. 安装回调函数

7. Value parsing callback

8. 函数

8.1. 预定义函数

9. 保存配置文件

9.1. Altering the printing of certain options

(未完待续)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:oRTP库——入门
下一篇:springboot 接口版本区分方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~