代码地址
https://github.com/gongluck/Code-snippet/tree/master/cpp/config
需求
开发中,读取配置文件信息必不可少。Windows平台有现成的API可用,也很方便。但是一旦项目迁移到Linux平台下,原先在Windows平台下的代码就全部作废。所以,实现一套跨平台的配置文件读取功能代码可以节省不少的劳动力。
实现
依赖于boost的ini_parser,可以实现跨平台读取ini格式的配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// config.h
/*
* @Author: gongluck
* @Date: 2020-03-23 15:11:50
* @Last Modified by: gongluck
* @Last Modified time: 2020-03-23 15:17:58
*/
// Profile read, dependent on boost
#pragma once
#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>
namespace gconf
{
class config
{
public :
int open( const char *configfile);
template < typename T>
int read( const char *session, const char *key, T &value, const char *configfile = nullptr)
{
if (configfile != nullptr && open(configfile) != 0)
{
return -1;
}
try
{
auto lvbtItems = lvptProperties_.get_child(session);
value = lvbtItems.get<T>(key);
}
catch (std::exception &e)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
return -1;
}
return 0;
}
int readall( const char *session,
std::vector<std::pair<std::string, std::string>> &results,
const char *configfile = nullptr);
private :
boost::property_tree::ptree lvptProperties_;
};
} // namespace gconf
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// config.cpp
/*
* @Author: gongluck
* @Date: 2020-03-23 15:13:13
* @Last Modified by: gongluck
* @Last Modified time: 2020-03-23 15:17:56
*/
#include "config.h"
#include <boost/property_tree/ini_parser.hpp>
namespace gconf
{
int config::open( const char *configfile)
{
if (configfile == nullptr)
{
return -1;
}
try
{
boost::property_tree::ini_parser::read_ini(configfile, lvptProperties_);
}
catch (std::exception &e)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
return -1;
}
return 0;
}
int config::readall( const char *session,
std::vector<std::pair<std::string, std::string>> &results,
const char *configfile /*= nullptr*/ )
{
if (configfile != nullptr && open(configfile) != 0)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : "
<< " can not open " << configfile << std::endl;
return -1;
}
try
{
auto lvbtItems = lvptProperties_.get_child(session);
for ( const auto &i : lvbtItems)
{
results.push_back(std::make_pair(i.first.data(), i.second.data()));
}
}
catch (std::exception &e)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
return -1;
}
return 0;
}
} // namespace gconf
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// testcode
#include <iostream>
#include "../config/config.h"
#define CHECKRET(ret)\
if (ret != 0)\
{\
std::cin.get();\
return ret;\
}
int main()
{
gconf::config conf;
auto ret = conf.open( "./config.ini" );
CHECKRET(ret);
int file = 0;
ret = conf.read< int >( "log" , "file" , file);
CHECKRET(ret);
std::vector<std::pair<std::string, std::string>>kvs;
ret = conf.readall( "log" , kvs);
CHECKRET(ret);
return 0;
}
|
以上就是C++读取配置文件的示例代码的详细内容,更多关于C++读取配置文件的资料请关注服务器之家其它相关文章!
原文链接:https://cloud.tencent.com/developer/article/1604692