libconfig C++ 学习笔记
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
libconfig C++ 学习笔记的更多相关文章
- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装
平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- HTML学习笔记
HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...
随机推荐
- [转]NLog 自定义字段 写入 oracle
本文转自:http://www.cnblogs.com/skyapplezhao/p/5690695.html 1.通过Nuget安装NLog 下载,简单入门 请参照 我刚才转的几篇文章,下面我直接贴 ...
- JS 操作 HTML 和 AJAX 请求后台数据
为某个元素插入值,添加属性,添加子元素 <div class="col-sm-6"> <select class="form-control" ...
- request方法总结
1.获得指定的头 String header = response.getHeader("user-agent"); 2.获得所有头的名称 Enumeration<Stri ...
- Struts 2 框架相比Struts1的新特性
POJO形式和POJO动作 - 已经摆脱了Struts2的动作表单的Struts框架的一个组成部分.Struts2可以使用任何的POJO接收的形式输入.同样的,你现在可以看到任何POJO的Action ...
- 中南oj 1216: 异或最大值 数据结构
1216: 异或最大值 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 98 Solved: 29 [Submit][Status][Web Boar ...
- COGS2216 你猜是不是KMP
第一道自己写的FFT...... 不知为啥这题在网上找不到题解......真是麻烦,害得我推了半天...... 还是写个简要题解吧...... 首先把S和T拆成序列,a~z分别对应成1~26,?是0, ...
- cf1043D. Mysterious Crime(枚举)
题意 题目链接 给出\(m\)个长度为\(n\)的排列,问有多少连续公共子串 \(m \leqslant 10, n \leqslant 10^5\) Sol 非常naive的一道题然而交了3遍才过( ...
- Hadoop序列化程序报错
这个错误指向序列化类,注意实现的Read和Wirte顺序一定要一致,并且要使用基本类型,不要使用包装类. java.lang.Exception: java.io.EOFException at or ...
- 移动端meta标签的使用和设置
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale= ...
- Java xml 操作(Dom4J修改xml + xPath技术 + SAX解析 + XML约束)
1 XML基础 1)XML的作用 1.1 作为软件配置文件 1.2 作为小型的"数据库" 2)XML语法(由w3c组织规定的) 标签: 标签名不能以数字开头,中间不能有空格,区分大 ...