最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

實(shí)現(xiàn)了一個(gè)比較簡單的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,
如果原來沒有將會(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)簡易的.ini配置文件解析器的文章就介紹到這了,更多相關(guān)C++ ini配置文件解析器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++?雙向循環(huán)鏈表類模版實(shí)例詳解

    C++?雙向循環(huán)鏈表類模版實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了C++?雙向循環(huán)鏈表類模版實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C語言之通訊錄的模擬實(shí)現(xiàn)代碼

    C語言之通訊錄的模擬實(shí)現(xiàn)代碼

    這篇文章主要介紹了C語言之通訊錄的模擬實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 詳解C++ 拷貝構(gòu)造函數(shù)和賦值運(yùn)算符

    詳解C++ 拷貝構(gòu)造函數(shù)和賦值運(yùn)算符

    本文主要介紹了拷貝構(gòu)造函數(shù)和賦值運(yùn)算符的區(qū)別,以及在什么時(shí)候調(diào)用拷貝構(gòu)造函數(shù)、什么情況下調(diào)用賦值運(yùn)算符。最后,簡單的分析了下深拷貝和淺拷貝的問題。有需要的朋友可以看下
    2016-12-12
  • Qt利用ImageWatch實(shí)現(xiàn)圖片查看功能

    Qt利用ImageWatch實(shí)現(xiàn)圖片查看功能

    Visual Studio有專門針對(duì)OpenCV開發(fā)的插件,名叫ImageWatch,圖片放大之后可以查看RGB的像素值。本文將利用這一查件實(shí)現(xiàn)圖片查看功能,需要的可以參考一下
    2022-04-04
  • 聊聊C語言中sizeof運(yùn)算符的一個(gè)陷阱

    聊聊C語言中sizeof運(yùn)算符的一個(gè)陷阱

    在C語言中,sizeof()是一個(gè)判斷數(shù)據(jù)類型或者表達(dá)式長度的運(yùn)算符,下面這篇文章主要給大家介紹了關(guān)于C語言中sizeof運(yùn)算符的一個(gè)陷阱的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • C語言中關(guān)于sizeof 和 strlen的區(qū)別分析

    C語言中關(guān)于sizeof 和 strlen的區(qū)別分析

    本文通過示例簡單分析了4種情況下C語言中sizeof 和 strlen的區(qū)別,算是個(gè)人經(jīng)驗(yàn)的一個(gè)小小的總結(jié),如有遺漏還請(qǐng)大家告知。
    2015-02-02
  • C++關(guān)于引用作為函數(shù)的用法

    C++關(guān)于引用作為函數(shù)的用法

    今天小編就為大家分享一篇關(guān)于C++關(guān)于引用作為函數(shù)的用法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Windows窗口消息實(shí)例詳解

    Windows窗口消息實(shí)例詳解

    這篇文章主要介紹了Windows窗口消息,以實(shí)例形式詳細(xì)羅列了Windows窗口消息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • 使用Qt實(shí)現(xiàn)獲取本機(jī)IP和定位

    使用Qt實(shí)現(xiàn)獲取本機(jī)IP和定位

    這篇文章主要為大家詳細(xì)介紹了如何使用Qt實(shí)現(xiàn)獲取本機(jī)IP和定位,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • C語言實(shí)現(xiàn)簡單的定時(shí)器

    C語言實(shí)現(xiàn)簡單的定時(shí)器

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單的定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10

最新評(píng)論

阳高县| 堆龙德庆县| 北川| 安龙县| 东安县| 保德县| 卢湾区| 宁陵县| 安岳县| 翁源县| 宜州市| 理塘县| 获嘉县| 金门县| 信丰县| 长兴县| 浦城县| 咸宁市| 佳木斯市| 抚远县| 常熟市| 旬阳县| 长顺县| 南安市| 乌审旗| 专栏| 隆昌县| 通榆县| 淳化县| 盐边县| 长治市| 吉首市| 安新县| 衡山县| 西宁市| 文成县| 昌都县| 峡江县| 康定县| 朝阳区| 樟树市|