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 = ; },
{ title = "Snow Crash";
author = "Neal Stephenson";
price = 9.99;
qty = ; }
); movies = ( { title = "Brazil";
media = "DVD";
price = 19.99;
qty = ; },
{ title = "The City of Lost Children";
media = "DVD";
price = 18.99;
qty = ; },
{ title = "Memento";
media = "Blu-Ray";
price = 24.99;
qty = ;
},
{ title = "Howard the Duck"; }
);
}; // Store hours:
hours =
{
mon = { open = ; close = ; };
tue = { open = ; close = ; };
wed = { open = ; close = ; };
thu = { open = ; close = ; };
fri = { open = ; close = ; };
sat = { open = ; close = ; };
sun = { open = ; close = ; };
};

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() << left << "TITLE" << " "
<< setw() << left << "AUTHOR" << " "
<< setw() << left << "PRICE" << " "
<< "QTY"
<< endl; for(int i = ; 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() << left << title << " "
<< setw() << left << author << " "
<< '$' << setw() << 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() << left << "TITLE" << " "
<< setw() << left << "MEDIA" << " "
<< setw() << left << "PRICE" << " "
<< "QTY"
<< endl; for(int i = ; 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() << left << title << " "
<< setw() << left << media << " "
<< '$' << setw() << right << price << " "
<< qty
<< endl;
}
cout << endl;
}
catch(const SettingNotFoundException &nfex)
{
// Ignore.
}
return ;
}

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

最新文章

  1. [译]JavaScript规范-葵花宝典
  2. SQL各种连接查询详解(左连接、右连接..)
  3. YUM安装提示PYCURL ERROR 6 - "Couldn&#39;t错误的解决办法
  4. HTML5塔防游戏——《三国塔防》 - Yorhom&#39;s Game Box
  5. Android Timer的使用
  6. TensorFlow安装-ubuntu
  7. 如何调节Eclipse下console输出字体的大小??
  8. Python的__main__.py用法
  9. I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor
  10. Kruskal模板
  11. javaSE-多线程
  12. kafka消息的分发与消费
  13. 详解webpack中的hash、chunkhash、contenthash区别
  14. 利用HBuilder开发基于MUI的H5+ app中使用百度地图定位功能
  15. 关于linux上cron服务的python封装工具
  16. zeroMQ 学习
  17. 【技巧总结】Penetration Test Engineer[1]-Basic
  18. JS中给函数参数添加默认值
  19. 链表求和12 &#183; Add Two Numbers
  20. 在windows平台上构建自己的PHP(php5.3+)

热门文章

  1. [H5表单]html5自带表单验证体验优化及提示气泡修改
  2. 数据结构(四)--- 红黑树(RedBlock-Tree)
  3. ajax传json
  4. 三、Bean的初始化
  5. thinkphp下mysql用用户名或者手机号登录
  6. 不同浏览器下word-wrap,word-break,white-space强制换行和不换行总结
  7. csharp:using Newtonsoft.Json.Net2.0 in .net 2.0 webform
  8. 在 CentOS6 上安装 GraphicsMagick-1.3.30
  9. 【Android】10.0 UI开发——如何编写程序界面、常见控件的使用
  10. npm属性笔记