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++ 学习笔记的更多相关文章

  1. kvm虚拟化学习笔记(一)之kvm虚拟化环境安装

    平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...

  2. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  3. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  4. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  5. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  6. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  7. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  8. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  9. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

随机推荐

  1. Shiro - 自定义filterChainDefinitions和Realm

    在Spring Context中定义shiroFilter(org.apache.shiro.spring.web.ShiroFilterFactoryBean)时需要为其filterChainDef ...

  2. Windows Server: 将虚拟机迁移到 Azure (以阿里云为例)

    Azure 虚拟机能很容易地导出 vhd 并迁移到各种环境中,包含本地及云端环境,或者迁移至其他区域.这为开发.测试.扩展带来了极大的便利.本文以阿里云为例,阐述如何将Windows Server 的 ...

  3. 基于语法分析器GOLD Parser开发的数学表达式计算器

    最近发现一款文法分析神器,看完官网(http://goldparser.org/)的介绍后感觉很犀利的样子,于是就拿来测试了一番,写了一个数学表达式分析的小程序,支持的数学运算符如下所示:常规运算:+ ...

  4. 二:Vim常用命令

    一般模式下的命令: -- 插入命令 i 光标前插入 I 当前行开始 o 下一行 O 上一行插入新行 a 光标后插入 A 当前行末尾 -- 定位命令 :set nu 显示行号 :set nonu 取消行 ...

  5. xcode8 打开的 xib 踩坑

    之前开发都不敢工测试版的开发,一直用正式版的,xcode7.3.1的模糊匹配让我很蛋疼,自定义的类,类名不提示,每次都粘贴复制,8号苹果发布了 xcode8GM 版,迫不及待的从苹果开发者官网下了一个 ...

  6. 基数排序——Java实现

    一.基数排序思想 相比其它排序,主要是利用比较和交换,而基数排序则是利用分配和收集两种基本操作.基数 排序是一种按记录关键字的各位值逐步进行排序的方法.此种排序一般适用于记录的关键字为整数类型的情况. ...

  7. IIS6服务器的请求流程(图文&源码)

    1.IIS 7开发与管理完全参考手册  http://book.51cto.com/art/200908/146040.htm 2.Web服务IIS 6   https://technet.micro ...

  8. asp.net 、C#实现微信企业号OAuth2认证

    以微信企业号作为入口的应用,几乎都会遇到需要应用系统中个人信息和微信用户关联问题.从而进行其他业务处理.目前所做项目采取在企业号通讯录添加自定义字段存入应用系统用户信息表中唯一标识UserGuid进行 ...

  9. Android蓝牙自动配对Demo,亲测好使!!!

    蓝牙自动配对,即搜索到其它蓝牙设备之后直接进行配对,不需要弹出配对确认框或者密钥输入框. 转载请注明出处http://blog.csdn.net/qq_25827845/article/details ...

  10. Ubuntu 下 /etc/resolv.conf文件总是自动清除问题的解决方案

    最近学习Linux,在虚拟机中安装的是Ubuntu操作系统,用了几天发现Ubuntu无法上网,打开命令终端,输入命令: ping www.baidu.com -c2 结果显示名称无法识别,而直接与宿主 ...