zoukankan      html  css  js  c++  java
  • libconfig 读取配置文件

    libconfig库的官方网站:http://www.hyperrealm.com/libconfig/

    libconfig是什么?

    • libconfig是一个结构化的配置文件库,通常定义为配置文件,例如test.cfg 。它比xml可读性更好,而且更简洁;而且不像xml,它是 type-aware类型自我感知的,因此不需要做string parsing (分词?);而ini太弱。

    libconfig格式:这些配置可以从文件中读取或写入文件,也可以在内存中操作。一个配置由一组setting构成,setting由名字(name)关联,并有相应的值(value)

    • 1、常见的数据类型:

      • 整数(int):可以用10进制和16进制表示。0x打头的数字libconfig会自动解析为16进制的数字。
      • 64位整数(int64_t):在数字的后面加上L即可。
      • 浮点数(float):
      • 布尔数(bool):true或者false。不区分大小写。
      • 字符串(string):这个字符串非常强大。
    • 2、复杂类型

      • 数组结构。一组标量值的序列,所有的标量值必须为同一类型。
      • 群组结构。多个setting的集合
      • 列表结构。一组值(value)的序列,各个值(value)可以分别为不同的类型,其他的列表也可以包含其中。
    • 注意:
      a、支持转义字符’, ‘f’, ‘ ’, ‘ ’,‘x’ and ‘ ’。
      b、相邻的字符串libconfig会自动拼接。这样太常的内容,我们可以多行来写,可读性非常好。比如:
      example = “hello world”; 等价于
      example = “hello”
      ” world”;

    【注意】我们可以使用’=’,也可以使用’:’来作为赋值号。既然是C/C++程序员,还是使用’=’号看得舒服一些。和C/C++的注释一样,/**/就是跨行的注释。 //就是单行注释。当然还支持脚本语言的注释符号#,#也是单行注释。但是特殊的是,如果注释符在双引号中使用,那就不再是注释符了,libconfig会解析为正常的明文。

    通过群组结构和列表结构,方便灵活的进行各种变态的配置读取。除了读取配置,libconfig还可以生成配置文件:必要的时候,我们可以把内存里面的一些值,通过libconfig生成一份标准的配置文件。

    C++版本的api说明

    Method on Setting: bool lookupValue (const char *name, bool &value)
    Method on Setting: bool lookupValue (const std::string &name, bool &value)
    Method on Setting: bool lookupValue (const char *name, int &value)
    Method on Setting: bool lookupValue (const std::string &name, int &value)
    Method on Setting: bool lookupValue (const char *name, unsigned int &value)
    Method on Setting: bool lookupValue (const std::string &name, unsigned int &value)
    Method on Setting: bool lookupValue (const char *name, long long &value)
    Method on Setting: bool lookupValue (const std::string &name, long long &value)
    Method on Setting: bool lookupValue (const char *name, unsigned long long &value)
    Method on Setting: bool lookupValue (const std::string &name, unsigned long long &value)
    Method on Setting: bool lookupValue (const char *name, float &value)
    Method on Setting: bool lookupValue (const std::string &name, float &value)
    Method on Setting: bool lookupValue (const char *name, double &value)
    Method on Setting: bool lookupValue (const std::string &name, double &value)
    Method on Setting: bool lookupValue (const char *name, const char *&value)
    Method on Setting: bool lookupValue (const std::string &name, const char *&value)
    Method on Setting: bool lookupValue (const char *name, std::string &value)
    Method on Setting: bool lookupValue (const std::string &name, std::string &value)

    安装

    • sudo apt-get install libconfig8 libconfig8-dev
    • 或者直接下载源码:./configure; make ; make install

    资料

  • 相关阅读:
    93.修改私有属性的值
    92.私有属性和私有方法
    91.多层继承
    python基础入门之四 —— 列表
    python基础入门之三 —— 字符串
    python基础入门之二 —— 条件、循环语句
    如何将本地图片变成图片链接
    python基础入门之一 —— 变量与运算符
    Elasticserach 配置文件详解
    elasticserch:性能优化策略
  • 原文地址:https://www.cnblogs.com/xiaohaigegede/p/14779910.html
Copyright © 2011-2022 走看看