1. C++API 頭文件 #include <libconfig.h++> ,命名空間:using namespace libconfig;
2.多線程使用問題:
(1)libconfig是完全可重入的,庫中函數不使用全局變量和不保持成功調用間的狀態。所以兩個獨立配置文件在兩個不同線程間並發操作可以是安全的;
(2)libconfig是線程不安全,libconfig不考慮系統線程模型。所以一個配置文件的實例被多個線程獲取,必須使用同步機制(例如讀寫鎖或互斥鎖)來保護。
(3)libconfig不是異步安全,不能在信號句柄中調用
(4)libconfig不能保證取消安全。因為它不知道主機系統的線程模型,庫不包含任何線程取消點。在大多數情況下這將不是多線程程序的問題。但是,請注意一些庫中的例程(即那些從/到文件讀取/寫入配置的程序)流)使用可能會阻止的C庫例程執行I/O;是否這些C庫例程是取消安全的,取決於主機系統。
3.C++API
C++API使用Config類和Setting類,因為不提供公有的copy constructor or assignment operator,所以在函數參數中傳遞時使用引用或指針方式
4.使用方法(使用官方example):
配置文件:
// An example configuration file that stores information about a store. // Basic store information: name = "Books, Movies & More"; // Store inventory: inventory = { books = ( { title = "Treasure Island"; author = "Robert Louis Stevenson"; price = 29.99; qty = 5; }, { title = "Snow Crash"; author = "Neal Stephenson"; price = 9.99; qty = 8; } ); movies = ( { title = "Brazil"; media = "DVD"; price = 19.99; qty = 11; }, { title = "The City of Lost Children"; media = "DVD"; price = 18.99; qty = 5; }, { title = "Memento"; media = "Blu-Ray"; price = 24.99; qty = 20; }, { title = "Howard the Duck"; } ); }; // Store hours: hours = { mon = { open = 9; close = 18; }; tue = { open = 9; close = 18; }; wed = { open = 9; close = 18; }; thu = { open = 9; close = 18; }; fri = { open = 9; close = 20; }; sat = { open = 9; close = 20; }; sun = { open = 11; close = 16; }; };
C++讀取配置程序:
#include <cstdlib> #include <libconfig.h++> #include <iostream> #include <iomanip> using namespace std; using namespace libconfig; int main(){ Config cfg; //1.聲明 Config對象 cfg.readFile("example.cfg"); //讀取配置文件 // Get the store name. try { string name = cfg.lookup("name"); cout << "Store name: " << name << endl << endl; } catch(const SettingNotFoundException &nfex) //配置沒找到會有SettingNotFoundException 異常 { cerr << "No 'name' setting in configuration file." << endl; } // Get the store name. try { string name = cfg.lookup("name"); cout << "Store name: " << name << endl << endl; } catch(const SettingNotFoundException &nfex) { cerr << "No 'name' setting in configuration file." << endl; } const Setting& root = cfg.getRoot(); // Output a list of all books in the inventory. try { const Setting &books = root["inventory"]["books"]; int count = books.getLength(); cout << setw(30) << left << "TITLE" << " " << setw(30) << left << "AUTHOR" << " " << setw(6) << left << "PRICE" << " " << "QTY" << endl; for(int i = 0; i < count; ++i) { const Setting &book = books[i]; // Only output the record if all of the expected fields are present. string title, author; double price; int qty; if(!(book.lookupValue("title", title) && book.lookupValue("author", author) && book.lookupValue("price", price) && book.lookupValue("qty", qty))) continue; cout << setw(30) << left << title << " " << setw(30) << left << author << " " << '$' << setw(6) << right << price << " " << qty << endl; } cout << endl; } catch(const SettingNotFoundException &nfex) { // Ignore. } // Output a list of all books in the inventory. try { const Setting &movies = root["inventory"]["movies"]; int count = movies.getLength(); cout << setw(30) << left << "TITLE" << " " << setw(10) << left << "MEDIA" << " " << setw(6) << left << "PRICE" << " " << "QTY" << endl; for(int i = 0; i < count; ++i) { const Setting &movie = movies[i]; // Only output the record if all of the expected fields are present. string title, media; double price; int qty; if(!(movie.lookupValue("title", title) && movie.lookupValue("media", media) && movie.lookupValue("price", price) && movie.lookupValue("qty", qty))) continue; cout << setw(30) << left << title << " " << setw(10) << left << media << " " << '$' << setw(6) << right << price << " " << qty << endl; } cout << endl; } catch(const SettingNotFoundException &nfex) { // Ignore. } return 0; }
5.常用Config類方法:
(1)Method on Config: Setting & getRoot () const
This method returns the root setting for the configuration, which is a group.
(2)
Method on Config: Setting & lookup (const std::string &path) constMethod on Config: Setting & lookup (const char * path) const
These methods locate the setting specified by the path path. If the requested setting is not found, a SettingNotFoundException
is thrown.
(3)
Method on Config: bool lookupValue (const char *path, bool &value) constMethod on Config: bool lookupValue (const std::string &path, bool &value) constMethod on Config: bool lookupValue (const char *path, int &value) constMethod on Config: bool lookupValue (const std::string &path, int &value) constMethod on Config: bool lookupValue (const char *path, unsigned int &value) constMethod on Config: bool lookupValue (const std::string &path, unsigned int &value) constMethod on Config: bool lookupValue (const char *path, long long &value) constMethod on Config: bool lookupValue (const std::string &path, long long &value) constMethod on Config: bool lookupValue (const char *path, float &value) constMethod on Config: bool lookupValue (const std::string &path, float &value) constMethod on Config: bool lookupValue (const char *path, double &value) constMethod on Config: bool lookupValue (const std::string &path, double &value) constMethod on Config: bool lookupValue (const char *path, const char *&value) constMethod on Config: bool lookupValue (const std::string &path, const char *&value) constMethod on Config: bool lookupValue (const char *path, std::string &value) constMethod on Config: bool lookupValue (const std::string &path, std::string &value) const