好友
阅读权限20
听众
最后登录1970-1-1
|
本帖最后由 JerusalemSky 于 2020-12-3 20:21 编辑
我们的电脑加密了,我直接贴代码出来了:
/*
* @Author: gongluck
* @Date: 2020-03-23 15:11:50
* @last Modified by: Zephaniah
* @Last Modified time: 2020-12-03 15:17:58
*/
// Profile read, dependent on boost
#pragma once
#include <iostream>
#include <map>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/foreach.hpp>
#include "ErrorCodeDef.h"
class config
{
public:
int open(const char* configfile)
{
if (configfile == nullptr)
{
return ERR_INVALID_DATA;
}
try
{
boost::property_tree::ini_parser::read_ini(configfile, lvptProperties_);
}
catch (std::exception& e)
{
//std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
return ERR_INVALID_DATA;
}
return RET_OK;
}
template <typename T>
int read(const char* session, const char* key, T& value, const char* configfile = nullptr)
{
if (configfile != nullptr && open(configfile) != 0)
{
return ERR_INVALID_DATA;
}
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 ERR_INVALID_DATA;
}
return RET_OK;
}
int read1Section(const char* section,
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 ERR_INVALID_DATA;
}
try
{
auto lvbtItems = lvptProperties_.get_child(section);
//const auto& i = lvbtItems.begin();
BOOST_FOREACH(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 ERR_INVALID_DATA;
}
return RET_OK;
}
int readall(const char* configfile)
{
std::vector<std::string> vSectionNameLists;
BOOST_FOREACH(auto & section, lvptProperties_)
{
std::string strSectionName = section.first;
vSectionNameLists.emplace_back(strSectionName);
}
std::vector<std::string>::iterator itSectionName = vSectionNameLists.begin();
for(; itSectionName != vSectionNameLists.end(); itSectionName++)
{
std::map<std::string, std::string> mapSection;
std::vector<std::pair<std::string, std::string>> results;
if (read1Section(itSectionName->c_str(), results, configfile) != 0)
{
/*std::cerr << __FILE__ << " : " << __LINE__ << " : "
<< " can not open " << configfile << std::endl;*/
return ERR_INVALID_DATA;
}
BOOST_FOREACH(auto& key , results)
{
mapSection.emplace(key);
}
m_map4AllItems.emplace(std::make_pair <std::string, std::map<std::string, std::string>>(*itSectionName, mapSection));
}
return RET_OK;
}
public:
std::map<std::string, std::map<std::string, std::string>> m_map4AllItems;
private:
boost::property_tree::ptree lvptProperties_;
};
dengmengzhao |
|