libconfig++(http://www.hyperrealm.com/libconfig/)是一個(gè)用于處理結(jié)構(gòu)化配置文件的簡單庫。libconfig++的配置的文件格式非常簡潔,可讀性也非常的好,而且是type-aware,普通的配置文件讀取后存取的類型為字符串,libconfig++可以“識別”整形的配置。libconfig++的配置形式也非常的靈活。此外,libconfig++還可以修改配置文件。
安裝
安裝包:libconfig-1.5.tar.gz安裝過程:解壓,配置,編譯,安裝//解壓安裝包tar -zxvf libconfig-1.5.tar.gz //安裝前的引導(dǎo)配置,默認(rèn)安裝到/usr/local,可以通過./configure --PRefix=PREFIX進(jìn)行修改,也可以修改腳本參數(shù)ac_default_prefix的值cd libconfig-1.5; ./configure //編譯源碼make//安裝前的檢查make check//安裝make install使用說明 所需頭文件libconfig.h++,編譯時(shí)需要鏈接動(dòng)態(tài)庫libconfig++
class LIBCONFIGXX_API Config{public: Config(); //默認(rèn)構(gòu)造函數(shù) virtual ~Config(); //析構(gòu)函數(shù) ... 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; //判斷是否存在某項(xiàng)配置名 inline bool exists(const std::string &path) const { return(exists(path.c_str())); } //查找指定節(jié)點(diǎn)的值 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;//獲取配置的根節(jié)點(diǎn) ...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的結(jié)果中查找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; ... //用于添加指定類型的節(jié)點(diǎn) 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; //判斷某項(xiàng)配置是否存在 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};//其他相關(guān)迭代器class SettingIterator;class SettingConstIterator;各種異常捕獲類class SettingTypeException;class SettingNotFoundException;class SettingNameException;class FileIOException;class ParseException;示例代碼 下面給出示例代碼,代碼中先對配置文件進(jìn)行讀取,最后進(jìn)行修改,涉及常用的各種類型。
#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"); 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(); //增加一行整數(shù)配置 root.add("aaa", libconfig::Setting::TypeInt)=95; //增加一行字符串配置 root.add("bbb", libconfig::Setting::TypeString)="add string"; //增加數(shù)組 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;}運(yùn)行結(jié)果
[root@localhost linconfig]# g++ -g main.cpp -lconfig++ -o run[root@localhost linconfig]# ./runNumber:100String:hello worldArray[0]:appleArray[1]:bananaArray[2]:orangeMap[0]:100->hundredMap[1]:1000->thousandNew configuration successfully written to: ./example.cfg新增的配置會被寫到example.cfg中本文對libconfig++做了簡單的介紹,對于其中重要的幾個(gè)類進(jìn)行了概要的說明,然而給出了相關(guān)使用示例,示例涉及常用的操作,可以被應(yīng)用到工程中。
新聞熱點(diǎn)
疑難解答