ini文件解析器

iniFile.h

#pragma omnce
#include <iostream>
#include <sstream>
#include <map>
#include <fstream>
#include <ostream>
namespace yazi
{
    namespace ini
    {
        class Vaule
        {
        public:
            Vaule();
            Vaule(bool val);
            Vaule(int val);
            Vaule(double val);
            Vaule(const char* val);
            Vaule(const std::string val);
            Vaule& operator=(bool val);
            Vaule& operator=(int val);
            Vaule& operator=(double val);
            Vaule& operator=(const char* val);
            Vaule& operator=(const std::string& val);
            operator bool();
            operator int();
            operator double();
            operator std::string();
        private:
            std::string _val;
        };

        typedef std::map<std::string, Vaule> Section;
        class IniFile
        {
        public:
            IniFile();
            bool load(const std::string& filename);
            Vaule& get(const std::string& section, const std::string& key);
            void set(const std::string& section, const std::string& key, const Vaule& vaule);
            bool has(const std::string& section, const std::string& key);
            bool has(const std::string& section);
            void remove(const std::string& section, const std::string& key);
            void remove(const std::string& section);
            void clear();
            std::string str();
            void show();
            bool save(const std::string filename);
            Section& operator[](const std::string& section)
            {
                return _sections[section];
            }
        private:
            std::string trim(std::string& s);
        private:
            std::string _fileName;
            std::map<std::string, Section> _sections;
        };
    }
}

iniFile.cc

#include "iniFile.h"
using namespace yazi::ini;
Vaule::Vaule()
{
}
Vaule::Vaule(bool val)
{
    *this = val ? "true" : "false";
}
Vaule::Vaule(int val)
{
    *this = val;
}
Vaule::Vaule(double val)
{
    *this = val;
}
Vaule::Vaule(const char* val)
{
    *this = val;
}
Vaule::Vaule(const std::string val)
{
    *this = val;
}
Vaule& Vaule::operator=(bool val)
{
    *this = val ? "true" : "false";
    return *this;
}
Vaule& Vaule::operator=(int val)
{
    std::stringstream ss;
    ss << val;
    _val = ss.str();
    return *this;
}
Vaule& Vaule::operator=(double val)
{
    std::stringstream ss;
    ss << val;
    _val = ss.str();
    return *this;
}
Vaule& Vaule::operator=(const char* val)
{
    _val = val;
    return *this;
}
Vaule& Vaule::operator=(const std::string& val)
{
    _val = val;
    return *this;
}

Vaule::operator bool()
{
    return _val == "true";
}
Vaule::operator int()
{
    return std::atoi(_val.c_str());
}
Vaule::operator double()
{
    return std::atof(_val.c_str());
}
Vaule::operator std::string()
{
    return _val;
}

IniFile::IniFile()
{
}
bool IniFile::load(const std::string& filename)
{
    _fileName = filename;
    std::ifstream in(filename);
    if (in.fail())
    {
        return false;
    }
    std::string line;
    std::string section;
    while (std::getline(in, line))
    {
        line = trim(line);
        if (line == " ")
        {
            continue;
        }
        if (line[0] == '[')
        {
            int pos = line.find_first_of(']');
            section = line.substr(1, pos - 1);
            section = trim(section);
            _sections[section] = Section();
        }
        else
        {
            int pos = line.find_first_of('=');
            std::string key = line.substr(0, pos);
            key = trim(key);
            std::string vaule = line.substr(pos + 1, line.length() - pos);
            vaule = trim(vaule);
            _sections[section][key] = vaule;
        }
    }
    in.close();
    return true;
}
std::string IniFile::trim(std::string& s)
{
    if (s.empty())
    {
        return s;
    }   
    s.erase(0, s.find_first_not_of(" \n\r"));
    s.erase(s.find_last_not_of(" \n\r") + 1);
    return s;
}
Vaule& IniFile::get(const std::string& section, const std::string& key)
{
    return _sections[section][key];
}
void IniFile::set(const std::string& section, const std::string& key, const Vaule& vaule)
{
    _sections[section][key] = vaule;
}
bool IniFile::has(const std::string& section, const std::string& key)
{
    std::map<std::string, Section>::const_iterator it = _sections.find(section);
    if (it != _sections.end())
    {
        return (it->second.find(key) != it->second.end());
    }
    return false;
}
bool IniFile::has(const std::string& section)
{
    return _sections.find(section) != _sections.end();
}
void IniFile::remove(const std::string& section, const std::string& key)
{
    std::map<std::string, Section>::iterator it = _sections.find(section);
    if (it != _sections.end())
    {
        it->second.erase(key);
    }
}
void IniFile::remove(const std::string& section)
{
    _sections.erase(section);
}
void IniFile::clear()
{
    _sections.clear();
}
void IniFile::show()
{
    std::cout << str();
}
std::string IniFile::str()
{
    std::stringstream ss;
    for (auto& e : _sections)
    {
        ss << "[" << e.first << "]" << std::endl;
        for (auto& x : (e.second))
        {
            ss << x.first << " = " << std::string(x.second) << std::endl;
        }
       ss << std::endl;
    }
    return ss.str();
}
bool IniFile::save(const std::string filename)
{
    std::ofstream out(filename);
    if (out.fail())
    {
        return false;
    }
    out << str();
    out.close();
    return true;
}

main.cc

#include "iniFile.h"
using namespace yazi::ini;

int main()
{
    IniFile ini;
    ini.load("./test.ini");
    std::cout << ini.str();
    // const std::string& ip = ini.get("server","ip");
    // int port = ini.get("server","port");
    // bool falg = ini.get("server","falg");
    const std::string& ip = ini["server"]["ip"];
    int port = ini["server"]["port"];
    ini.set("server", "timeout", 1000);
    ini.set("server", "falg", true);
    ini.set("client", "ip", "127.0.0.1");
    std::cout << ip << " " << port << std::endl;
    ini.save("./test.ini");
    return 0;
}

test.ini

[client]
ip = 127.0.0.1

[server]
falg = true
ip = 192.168.0.1
port = 8088
timeout = 1000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值