第三个工具类:IniReader
就是读ini配置文件的一个工具类,很简单,就是封装了一下boost库的ptree
IniReader.h
#ifndef __IniReader_h__
#define __IniReader_h__
#include <string>
#include <boost/property_tree/ptree.hpp>
namespace common{
namespace tool{
class IniReader
{
public:
IniReader();
~IniReader();
bool InitIni(const std::string& path);
bool GetValue(const std::string& root, const std::string& key, std::string& value);
bool GetValue(const std::string& root, const std::string& key, unsigned int& value);
private:
bool m_IsInit;
boost::property_tree::ptree m_Pt;
};
}
}
#endif
IniReader.cpp
#include "IniReader.h"
#include <boost/property_tree/ini_parser.hpp>
namespace common{
namespace tool{
IniReader::IniReader()
{
m_IsInit = false;
}
IniReader::~IniReader()
{
}
bool IniReader::InitIni(const std::string& path)
{
try
{
boost::property_tree::ini_parser::read_ini(path, m_Pt);
m_IsInit = true;
}
catch (...)
{
m_IsInit = false;
}
return m_IsInit;
}
bool IniReader::GetValue(const std::string& root, const std::string& key, std::string& value)
{
if (m_IsInit)
{
try
{
std::string strTemp(root + "." + key);
value = m_Pt.get<std::string>(strTemp);
return true;
}
catch (...)
{
return false;
}
}
else
{
return false;
}
}
bool IniReader::GetValue(const std::string& root, const std::string& key, unsigned int& value)
{
if (m_IsInit)
{
try
{
std::string strTemp(root + "." + key);
value = m_Pt.get<unsigned int>(strTemp);
return true;
}
catch (...)
{
return false;
}
}
else
{
return false;
}
}
}
}