欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用C++實(shí)現(xiàn)簡(jiǎn)易的.ini配置文件解析器

 更新時(shí)間:2023年03月09日 08:52:00   作者:咩~~  
這篇文章主要為大家詳細(xì)介紹了如何基于C++編寫一個(gè)簡(jiǎn)易的.ini配置文件解析器,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下

前言

實(shí)現(xiàn)了一個(gè)比較簡(jiǎn)單的ini文件解析器,下面介紹一下怎么用的

在最開始實(shí)例化一個(gè)IniHelper 可以使用默認(rèn)的config.ini文件路徑,也可以自己傳入一個(gè)文件路徑(讀取指定位置的config.ini)

1.saveIniConfig();
將內(nèi)存中的配置寫入config.ini
2.LogIniConfig()
打印config.ini
3.setIniConfig(const std::string& sectionName,const std::string& keyName,const std::string& value);設(shè)置sectionName中keyName為value,
如果原來(lái)沒有將會(huì)添加
4.IniValue getIniConfig(const std::string& sectionName,const std::string& keyName,const std::string& defValue) const; 
獲取指定位置的config,如果沒有,將返回默認(rèn)值
5.bool removeIniConfig(const std::string& sectionName,const std::string& keyName); 
刪除指定位置的config
里面使用到的IniValue重載了類型轉(zhuǎn)換運(yùn)算符,可以直接轉(zhuǎn)換為std::string

編譯使用的是VScode + CMake

具體實(shí)現(xiàn)如下

代碼

Util.h

#pragma once
#define NAME_SPACE_START(name) namespace name {
#define NAME_SPACE_END() }

#include <string>
#include <map>
#ifndef _UTIL_
#define _UTIL_

NAME_SPACE_START(INI)

#define REMOVE_SPACE(str) \
    for(auto it=str.begin();it!=str.end();){    \
        if(*it==' ') it=str.erase(it);      \
        else break;                         \
    }

#define INI_FILE_PATH "config.ini"

class IniValue{
public:
    friend class IniHelper;
    IniValue();
    IniValue(const bool val);
    IniValue(const int val);
    IniValue(const char* val);
    IniValue(const std::string val);
    IniValue& operator=(const bool& val);
    IniValue& operator=(const int& val);
    IniValue& operator=(const char* val);
    IniValue& operator=(const std::string& val);
    operator bool() const;
    operator int() const;
    operator std::string() const;
private:
    std::string m_value;
};

class IniHelper{
private:
    std::map<std::string, std::map<std::string, IniValue>> m_map;
    std::string m_filePath{INI_FILE_PATH};
public:
    IniHelper();
    IniHelper(const std::string& filePath);
    void saveIniConfig();
    void LogIniConfig() const;
    void setIniConfig(const std::string& sectionName,const std::string& keyName,const std::string& value);
    IniValue getIniConfig(const std::string& sectionName,const std::string& keyName,const std::string& defValue) const;
    bool removeIniConfig(const std::string& sectionName,const std::string& keyName);
protected:
    void loadIniFile();
    void trim(std::string& str);
};

NAME_SPACE_END()

#endif //!_UTIL_

Util.cpp

#include "Util.h"
#include <algorithm>
#include <atomic>
#include <cstring>
#include <cwchar>
#include <exception>
#include <fstream>
#include <ios>
#include <iostream>
#include <iterator>
#include <locale>
#include <map>
#include <ostream>
#include <sstream>
#include <stdlib.h>
#include <streambuf>
#include <string>


NAME_SPACE_START(INI)
IniValue::IniValue()
{

}

IniValue::IniValue(const bool val){
    m_value=val?"true":"false";
}

IniValue::IniValue(const int val){
    m_value=std::to_string(val);
}

IniValue::IniValue(const char* val){
    m_value=val;
}

IniValue::IniValue(const std::string val){
    m_value=val;
}

IniValue& IniValue::operator=(const bool& val){
    IniValue temp(val);
    m_value=temp.m_value;
    return *this;
}

IniValue& IniValue::operator=(const int& val){
    IniValue temp(val);
    m_value=temp.m_value;
    return *this;
}
IniValue& IniValue::operator=(const char* val){
    m_value=val;
    return *this;
}
IniValue& IniValue::operator=(const std::string& val){
    m_value=val;
    return *this;
}

IniValue::operator bool() const{
    return m_value=="true"?true:false;
}

IniValue::operator int() const{
    std::stringstream ss(m_value);
    int res=0;
    ss>>res;
    return res;
}

IniValue::operator std::string() const{
    return m_value;
}

IniHelper::IniHelper(){
    loadIniFile();
}
IniHelper::IniHelper(const std::string& filePath){
    m_filePath=filePath;
    loadIniFile();
}

void IniHelper::saveIniConfig(){
    std::fstream file;
    file.open(m_filePath,std::ios_base::out);
    if(file.fail()) return;
    for(auto it=m_map.begin();it!=m_map.end();it++){
        std::string sectionName="["+it->first+"]\n";
        file<<sectionName;
        auto keySection = it->second;
        for(auto key=keySection.begin();key!=keySection.end();key++){
            std::string keyValue=key->first;
            keyValue.append("=").append(key->second.m_value).append("\n");
            file<<keyValue;
        }
    }
    file.close();
}

void IniHelper::LogIniConfig() const{
    for(auto it=m_map.begin();it!=m_map.end();it++){
        std::string sectionName="["+it->first+"]\n";
        std::cout<<sectionName;
        auto keySection = it->second;
        for(auto key=keySection.begin();key!=keySection.end();key++){
            std::string keyValue=key->first;
            keyValue.append("=").append(key->second.m_value).append("\n");
            std::cout<<keyValue;
        }
    }
}

void IniHelper::setIniConfig(const std::string& sectionName,const std::string& keyName,const std::string& value){
    if(m_map.find(sectionName)==m_map.end()){
        std::map<std::string, IniValue> temp;
        temp[keyName]=value;
        m_map[sectionName]=temp;
    }
    else{
        m_map[sectionName][keyName]=value;
    }
}

IniValue IniHelper::getIniConfig(const std::string& sectionName,const std::string& keyName,const std::string& defValue) const{
    if(m_map.find(sectionName)==m_map.end()) return defValue;
    std::map<std::string, IniValue> mapping=m_map.at(sectionName);
    if(mapping.find(keyName)==mapping.end()) return defValue;
    return mapping[keyName];
}

bool IniHelper::removeIniConfig(const std::string& sectionName,const std::string& keyName){
    try {
        if(m_map.find(sectionName)==m_map.end()) return true;
        auto pos = m_map.at(sectionName).find(keyName);
        if(pos==m_map.at(sectionName).end()) return true;
        m_map.at(sectionName).erase(pos);
        return true;
    } catch (std::exception ex) {
        //std::cout<<ex.what()<<std::endl;
        return false;
    }
}

void IniHelper::loadIniFile(){
    std::fstream file;
    file.open(m_filePath,std::ios_base::in);
    if(file.fail()) return;
    std::string sectionName="",t="";
    while(std::getline(file,t)){
        trim(t);        //去除前后空格
        if(t=="\n"||t=="") continue;
        else if(t[0]=='['){
            sectionName = t.substr(1,t.size()-2);
            std::map<std::string, IniValue> p;
            m_map[sectionName]=p;
        }
        else{
            if(sectionName=="") continue;
            int left=0,right=0,equalPos=0;
            equalPos=t.find("=");
            if(equalPos==std::string::npos) continue;
            std::string keyName="",keyValue="";
            keyName=t.substr(0,equalPos);
            keyValue=t.substr(equalPos+1,t.size()-1-equalPos);
            trim(keyName);
            trim(keyValue);
            m_map[sectionName][keyName]=keyValue;
        }
    }
    file.close();
}

void IniHelper::trim(std::string& str){
    REMOVE_SPACE(str);
    std::reverse(str.begin(), str.end());
    REMOVE_SPACE(str);
    std::reverse(str.begin(), str.end());
}

NAME_SPACE_END()

main.cpp

#include <iostream>
#include <string>
#include "Util.h"
using namespace std;
using namespace INI;

int main(){
    IniHelper file("../config.ini");
    //file.removeIniConfig("localhost", "name");
    file.setIniConfig("localhost", "name", "cxn");
    file.LogIniConfig();
    file.saveIniConfig();
    return 0;
}

運(yùn)行截圖

到此這篇關(guān)于利用C++實(shí)現(xiàn)簡(jiǎn)易的.ini配置文件解析器的文章就介紹到這了,更多相關(guān)C++ ini配置文件解析器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C語(yǔ)言實(shí)現(xiàn)井字棋游戲(人機(jī)對(duì)弈)

    C語(yǔ)言實(shí)現(xiàn)井字棋游戲(人機(jī)對(duì)弈)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)井字棋人機(jī)對(duì)弈游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C++?超詳細(xì)分析數(shù)據(jù)結(jié)構(gòu)中的時(shí)間復(fù)雜度

    C++?超詳細(xì)分析數(shù)據(jù)結(jié)構(gòu)中的時(shí)間復(fù)雜度

    時(shí)間復(fù)雜度一般指時(shí)間復(fù)雜性。?在計(jì)算機(jī)科學(xué)中,時(shí)間復(fù)雜性,又稱時(shí)間復(fù)雜度,算法的時(shí)間復(fù)雜度是一個(gè)函數(shù),它定性描述該算法的運(yùn)行時(shí)間
    2022-03-03
  • 一文詳解Qt的QObject類

    一文詳解Qt的QObject類

    Qt的QObject類是Qt框架中的基類,它是所有Qt對(duì)象的父類,本文主要介紹了Qt的QObject類,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • C++標(biāo)準(zhǔn)模板庫(kù)STL的介紹

    C++標(biāo)準(zhǔn)模板庫(kù)STL的介紹

    今天小編就為大家分享一篇關(guān)于C++標(biāo)準(zhǔn)模板庫(kù)STL的介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • C++ Invalidaterect()函數(shù)作用案例詳解

    C++ Invalidaterect()函數(shù)作用案例詳解

    這篇文章主要介紹了C++ Invalidaterect()函數(shù)作用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C++?OpenGL實(shí)現(xiàn)旋轉(zhuǎn)立方體的繪制

    C++?OpenGL實(shí)現(xiàn)旋轉(zhuǎn)立方體的繪制

    這篇文章主要主要為大家詳細(xì)介紹了如何利用C++和OpenGL實(shí)現(xiàn)旋轉(zhuǎn)立方體的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下
    2022-07-07
  • 用C語(yǔ)言實(shí)現(xiàn)掃雷游戲

    用C語(yǔ)言實(shí)現(xiàn)掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了用C語(yǔ)言實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C++實(shí)現(xiàn)簡(jiǎn)單單向鏈表

    C++實(shí)現(xiàn)簡(jiǎn)單單向鏈表

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單單向鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Opencv實(shí)現(xiàn)讀取攝像頭和視頻數(shù)據(jù)

    Opencv實(shí)現(xiàn)讀取攝像頭和視頻數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Opencv實(shí)現(xiàn)讀取攝像頭和視頻數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評(píng)論