首页 技术 正文
技术 2022年11月21日
0 收藏 716 点赞 3,833 浏览 4714 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893