当前位置: 首页 > news >正文

CLion实现ini 解析器设计与实现

INI格式:

[server]
ip = 127.0.0.1
port = 80[profile]
name = jack
gender = male
age = 30

IniFile.h

#pragma once
#include <string>
#include <map>
class Value {
public:Value();explicit Value(bool value);explicit Value(int value);explicit Value(double value);explicit Value(const char * value);explicit Value(const std::string & value);Value& operator = (bool value);Value& operator = (int value);Value& operator = (double value);Value& operator = (const char * value);Value& operator = (const std::string & value);explicit operator bool();explicit  operator int();explicit operator double();explicit operator std::string();
private:std::string m_value;
};typedef std::map<std::string, Value> Section;
class IniFile {
public:IniFile();bool load(const std::string & filename);Value & get(const std::string & section, const std::string & key);void set(const std::string & section, const std::string & key,const Value & value);bool has(const std::string & section,const std::string & key);bool has(const std::string & secton);void remove(const std::string & section,const std::string & key);void remove(const std::string & section);void clear();bool save(const std::string& filename);Section & operator [] (const std::string & section) {return m_sections[section];}
private:std::string trim(std::string & str);
private:std::string m_filename;std::map<std::string,Section> m_sections;
};

IniFile.cpp

#include "IniFile.h"
#include <fstream>
#include <iostream>
Value::Value() {m_value = "";
}
Value::Value(bool value) {*this = value;
}
Value::Value(int value) {*this = value;
}
Value::Value(double value) {*this = value;
}
Value::Value(const char * value) {*this = value;
}
Value::Value(const std::string & value) {*this = value;
}Value& Value::operator=(bool value) {m_value = value ? "true" : "false";return *this;
}Value& Value::operator=(int value) {m_value = std::to_string(value);return *this;
}Value& Value::operator=(double value) {m_value = std::to_string(value);return *this;
}Value& Value::operator=(const char* value) {m_value = value;return *this;
}Value& Value::operator=(const std::string& value) {m_value = value;return *this;
}Value::operator bool() {return m_value == "true";
}
Value::operator int() {return std::stoi(m_value);
}
Value::operator double() {return std::stod(m_value);
}
Value::operator std::string() {return m_value;
}IniFile::IniFile() = default;bool IniFile::load(const std::string & filename) {m_filename = filename;std::ifstream fin( filename);if(fin.fail()) {std::cout << "Failed to open file: " <<std::endl;return false;}std::string line;std::string section;while(std::getline(fin, line)) {line = trim(line);if(line.empty()) {continue;}int pos = line.find_first_of(']');if(pos != std::string::npos) {section = line.substr(1, pos - 1);section = trim(section);m_sections[section] = Section();}else {int pos = line.find_first_of('=');if(pos != std::string::npos) {std::string key = line.substr(0, pos);key = trim(key);std::string value = line.substr(pos + 1);value = trim(value);m_sections[section][key] = value;}}}fin.close();return true;
}std::string IniFile::trim(std::string &str) {if(str.empty()) {return str;}str.erase(0, str.find_first_not_of(" \n\r"));str.erase(str.find_last_not_of(" \n\r") + 1);return str;
}Value & IniFile::get(const std::string & section, const std::string & key) {return m_sections[section][key];
}void IniFile::set(const std::string & section, const std::string & key,const Value & value) {m_sections[section][key] = value;
}bool IniFile::has(const std::string & section,const std::string & key) {std::map<std::string,Section>::const_iterator it = m_sections.find(section);if(it != m_sections.end()) {return it->second.find(key) != it->second.end();}return false;
}// 在 IniFile 类中添加保存方法
bool IniFile::save(const std::string& filename) {std::ofstream fout(filename );if (fout.fail()) {return false;}for ( auto& section_pair : m_sections) {fout << "[" << section_pair.first << "]\n";for ( auto& key_value : section_pair.second) {fout << key_value.first << "=" << static_cast<std::string>(key_value.second) << "\n";}fout << "\n";}fout.close();return true;
}bool IniFile::has(const std::string & section) {return m_sections.find(section) != m_sections.end();
}void IniFile::remove(const std::string & section,const std::string & key) {std::map<std::string,Section>::iterator it = m_sections.find(section);if(it != m_sections.end()) {it->second.erase(key);}
}
void IniFile::remove(const std::string & section) {m_sections.erase(section);
}
void IniFile::clear() {m_sections.clear();
}

main.cpp

#include <iostream>
#include "IniFile.h"int main() {IniFile ini;ini.load("test.ini");const std::string & ip = static_cast<std::string>(ini.get("server", "ip"));int port = static_cast< int>(ini.get("server", "port"));std::cout<<"ip:"<<ip<<" port:"<<port<<std::endl;const std::string ipp = static_cast<std::string>(ini["server"]["ip"]);std::cout<<"ipp:"<<ipp<<std::endl;//ini.set("server", "ip", "192.168.1.1");//std::cout<<"ip:"<<ip<<" port:"<<port<<std::endl;ini.set("server","timeout",Value(1000));std::cout<<"timeout:"<<static_cast<int>(ini.get("server","timeout"))<<std::endl;bool b = ini.has("server","timeout");std::cout<<"has timeout:"<<b<<std::endl;b = ini.has("server");std::cout<<"has server:"<<b<<std::endl;ini.remove("server","timeout");ini.save("test.ini");return 0;
}

http://www.dtcms.com/a/445837.html

相关文章:

  • python全栈(基础篇)——day04:后端内容(字符编码+list与tuple+条件判断+实战演示+每日一题)
  • 广州网站建设功能洛阳霞光企业网站建设公司
  • list 与 forward_list:一场 STL 中的“链表哲学”之争
  • Vue 学习与实践大纲(后端视角)
  • 2025时序数据库选型,从架构基因到AI赋能来解析
  • 三合一网站平台做网站 如何注册公司
  • 中山今科网站建设德州百度推广公司
  • Rust 与 传统语言:现代系统编程的深度对比
  • STM32--智能小车
  • Rust 登堂 之 Cell 和 RefCell(十二)
  • 分布式追踪系统实战:OpenTelemetry集成Istio实现全链路故障定位
  • 不同光谱的工业相机有哪些?能做什么?
  • 计算机网络——数据链路层笔记整理
  • CSS高效开发三大方向
  • 网站开发中的开版什么意思宁波汽车网站建设
  • dot1q termination vid vlan-id 概念及题目
  • 在越南做一个网站怎么做百度ai开放平台
  • IEEE Transactions 风格补充材料(Word)快速排版教程
  • php 8.4.11 更新日志
  • 二分查找_优选算法(C++)二分查找算法
  • 安卓设备分区作用详解-测试机红米K40
  • 网站开发进度计划是什么长沙游戏推广
  • AI与敏捷开发管理系列4:双向赋能——AI技术如何优化敏捷实践
  • opencv cv2.MorphologyEx
  • 【多线程】读写锁(Read-Write Lock)是什么?
  • 电子商务网站建设步骤百度文库网站ip地址 a记录
  • 常规可见光相机在工业视觉检测中的应用
  • 佛山企业网站建设平台如何把官网
  • Vue3双向数据绑定v-model
  • Vue keep-alive