利用C++實(shí)現(xiàn)簡(jiǎn)易的.ini配置文件解析器
前言
實(shí)現(xiàn)了一個(gè)比較簡(jiǎn)單的ini文件解析器,下面介紹一下怎么用的
在最開(kāi)始實(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)沒(méi)有將會(huì)添加 4.IniValue getIniConfig(const std::string& sectionName,const std::string& keyName,const std::string& defValue) const; 獲取指定位置的config,如果沒(méi)有,將返回默認(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++ 拷貝構(gòu)造函數(shù)和賦值運(yùn)算符
本文主要介紹了拷貝構(gòu)造函數(shù)和賦值運(yùn)算符的區(qū)別,以及在什么時(shí)候調(diào)用拷貝構(gòu)造函數(shù)、什么情況下調(diào)用賦值運(yùn)算符。最后,簡(jiǎn)單的分析了下深拷貝和淺拷貝的問(wèn)題。有需要的朋友可以看下2016-12-12
Qt利用ImageWatch實(shí)現(xiàn)圖片查看功能
Visual Studio有專門針對(duì)OpenCV開(kāi)發(fā)的插件,名叫ImageWatch,圖片放大之后可以查看RGB的像素值。本文將利用這一查件實(shí)現(xiàn)圖片查看功能,需要的可以參考一下2022-04-04
聊聊C語(yǔ)言中sizeof運(yùn)算符的一個(gè)陷阱
在C語(yǔ)言中,sizeof()是一個(gè)判斷數(shù)據(jù)類型或者表達(dá)式長(zhǎng)度的運(yùn)算符,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中sizeof運(yùn)算符的一個(gè)陷阱的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
C語(yǔ)言中關(guān)于sizeof 和 strlen的區(qū)別分析
本文通過(guò)示例簡(jiǎn)單分析了4種情況下C語(yǔ)言中sizeof 和 strlen的區(qū)別,算是個(gè)人經(jīng)驗(yàn)的一個(gè)小小的總結(jié),如有遺漏還請(qǐng)大家告知。2015-02-02
使用Qt實(shí)現(xiàn)獲取本機(jī)IP和定位
這篇文章主要為大家詳細(xì)介紹了如何使用Qt實(shí)現(xiàn)獲取本機(jī)IP和定位,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的定時(shí)器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10

