菜鸟笔记
提升您的技术认知

libconfig++示例详解

libconfig++(http://www.hyperrealm.com/libconfig/)是一个用于处理结构化配置文件的简单库。libconfig++的配置的文件格式非常简洁,可读性也非常的好,而且是type-aware,普通的配置文件读取后存取的类型为字符串,libconfig++可以“识别”整形的配置。libconfig++的配置形式也非常的灵活。此外,libconfig++还可以修改配置文件。

安装

安装包:libconfig-1.5.tar.gz
安装过程:解压,配置,编译,安装
//解压安装包
tar -zxvf libconfig-1.5.tar.gz 
//安装前的引导配置,默认安装到/usr/local,可以通过./configure --prefix=PREFIX进行修改,也可以修改脚本参数ac_default_prefix的值
cd libconfig-1.5; ./configure 
//编译源码
make
//安装前的检查
make check
//安装
make install

使用说明
所需头文件libconfig.h++,编译时需要链接动态库libconfig++

class LIBCONFIGXX_API Config
{
public:

    Config();  //默认构造函数
    virtual ~Config(); //析构函数
    ...
    void read(FILE *stream); //以文件流的方式读取配置
    void write(FILE *stream) const;//以文件流的方式修改配置

    void readFile(const char *filename); //读取指定文件名的配置
    void writeFile(const char *filename);//修改指定文件名的配置

    Setting & lookup(const char *path) const; //
    inline Setting & lookup(const std::string &path) const
    { return(lookup(path.c_str())); }

    bool exists(const char *path) const; //判断是否存在某项配置名
    inline bool exists(const std::string &path) const
    { return(exists(path.c_str())); }

    //查找指定节点的值
    bool lookupValue(const char *path, bool &value) const;
    bool lookupValue(const char *path, int &value) const;
    bool lookupValue(const char *path, unsigned int &value) const;
    ...

    Setting & getRoot() const;//获取配置的根节点
    ...
private:

    ...
    config_t *_config;
    Setting::Format _defaultFormat;
    //禁止拷贝和赋值
    Config(const Config& other); // not supported
    Config& operator=(const Config& other); // not supported
};
//获取/修改配置
class LIBCONFIGXX_API Setting
{
friend class Config;
...

public:

    virtual ~Setting();

    inline Type getType() const { return(_type); }

    Setting & lookup(const char *path) const;
    inline Setting & lookup(const std::string &path) const
    { return(lookup(path.c_str())); }

    Setting & operator[](const char *name) const;
    Setting & operator[](int index) const;

    //从readFile的结果中查找path的值,并将其赋给指定的类型vlaue
    bool lookupValue(const char *name, bool &value) const;
    bool lookupValue(const char *name, int &value) const;
    bool lookupValue(const char *name, unsigned int &value) const;
    bool lookupValue(const char *name, long long &value) const;
    bool lookupValue(const char *name, unsigned long long &value) const;
    ...

    //用于添加指定类型的节点
     Setting & add(const char *name, Type type);

    inline Setting & add(const std::string &name, Type type)
    { return(add(name.c_str(), type)); }

    Setting & add(Type type);

    bool exists(const char *name) const;

    //判断某项配置是否存在
    inline bool exists(const std::string &name) const
    { return(exists(name.c_str())); }

    ...
private:

    config_setting_t *_setting;
    ...
    Setting(const Setting& other); // not supported
    Setting& operator=(const Setting& other); // not supported
};
//其他相关
迭代器
class SettingIterator;
class SettingConstIterator;
各种异常捕获类
class SettingTypeException;
class SettingNotFoundException;
class SettingNameException;
class FileIOException;
class ParseException;

示例代码
下面给出示例代码,代码中先对配置文件进行读取,最后进行修改,涉及常用的各种类型。

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "libconfig.h++"

using namespace std;

int main()
{
    libconfig::Config mConfig;
    string strConfPath = "./example.cfg";
    //解读配置文件
    try
    {
        mConfig.readFile(strConfPath.c_str());
    }
    catch(libconfig::FileIOException &e)
    {
        cout<<"read file [ "<<strConfPath<< " ] FileIOException"<<endl;
        return -1;
    }
    catch(libconfig::ParseException &e)
    {
        cout<<"read file [ "<<strConfPath<< " ],ParaseException: "<<e.getError()<<",line:"<<e.getLine()<<endl;
        return -1;
    }
    catch(...)
    {
        cout<<"read file ["<<strConfPath<< " ] unknown exception"<<endl;
        return -1;
    }

    //int 
    try
    {
        int num = mConfig.lookup("NUMBER");
        cout<<"Number:"<<num<<endl;
    }
    catch(...)
    {
        cout<<"Get Number from "<<strConfPath<<" failed ..."<<endl;
    }
    //string 
    try
    {
        string str = mConfig.lookup("STRING");
        //or
        mConfig.lookupValue("STRING",str);
        cout<<"String:"<<str<<endl;
    }
    catch(...)
    {
        cout<<"Get String from "<<strConfPath<<" failed ..."<<endl;
    }
    //Array 
    try
    {
        const libconfig::Setting &root = mConfig.getRoot();
        const libconfig::Setting &arr = root["ARRAY"];
        int cnt = arr.getLength();
        vector<string> vs;
        for(int i = 0 ; i < cnt ;++i)
        {
            string tmp = arr[i];
            vs.push_back(tmp);
            cout<<"Array["<<i<<"]:"<<tmp<<endl;
        }
    }
    catch(...)
    {
        cout<<"Get Array from "<<strConfPath<<" failed ..."<<endl;
    }

    //Map 
    try
    {
        const libconfig::Setting &root = mConfig.getRoot();
        const libconfig::Setting &maps = root["MAP"];
        int cnt = maps.getLength();
        int key;
        string value;
        map<int,string> mi;
        for(int i = 0 ; i < cnt ;++i)
        {
            const libconfig::Setting &tmp = maps[i];
            tmp.lookupValue("key",key);
            tmp.lookupValue("value",value);
            mi.insert(make_pair(key,value));
            cout<<"Map["<<i<<"]:"<<key<<"->"<<value<<endl;
        }
    }
    catch(...)
    {
        cout<<"Get Map from "<<strConfPath<<" failed ..."<<endl;
    }


    libconfig::Setting &root = mConfig.getRoot();
    //增加一行整数配置
    root.add("aaa", libconfig::Setting::TypeInt)=95;
    //增加一行字符串配置
    root.add("bbb", libconfig::Setting::TypeString)="add string";
    //增加数组
    libconfig::Setting &array = root.add("array", libconfig::Setting::TypeArray);
    for(int i = 0; i < 10; ++i)
        array.add(libconfig::Setting::TypeInt) = 10 * i;

    // Add some settings to the configuration.
    libconfig::Setting &address = root.add("address", libconfig::Setting::TypeGroup);
    address.add("city", libconfig::Setting::TypeString) = "Nanjing";
    address.add("province", libconfig::Setting::TypeString) = "JS";

    // Write out the new configuration.
    try
    {
        mConfig.writeFile(strConfPath.c_str());
        cout << "New configuration successfully written to: " << strConfPath << endl;
    }
    catch( libconfig::FileIOException &fioex)
    {
        cerr << "I/O error while writing file: " << strConfPath << endl;
        return(-1);
    }
    return 0;
}

运行结果

[root@localhost linconfig]# g++ -g main.cpp -lconfig++ -o run
[root@localhost linconfig]# ./run
Number:100
String:hello world
Array[0]:apple
Array[1]:banana
Array[2]:orange
Map[0]:100->hundred
Map[1]:1000->thousand
New configuration successfully written to: ./example.cfg
新增的配置会被写到example.cfg中

本文对libconfig++做了简单的介绍,对于其中重要的几个类进行了概要的说明,然而给出了相关使用示例,示例涉及常用的操作,可以被应用到工程中。