工具類庫系列(三)-IniReader
阿新 • • 發佈:2019-01-31
第三個工具類: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; } } } }