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

【C++】手搓读写ini文件源码

【C++】手搓读写ini文件源码

  • 思路
  • 需求:
  • ini.h
  • ini.cpp
  • config.conf
  • mian.cpp

思路

ini文件是一种系统配置文件,它有特定的格式组成。通常做法,我们读取ini文件并按照ini格式进行解析即可。在c++语言中,提供了模板类的功能,所以我们可以提供一个更通用的模板类来解析ini文件。c++中和键值对最贴切的就是STL中的map了。所以我使用map作为properties的实际内存存储,同时为了方便使用,另外多一个set类型的字段记录所有的key。大致流程为:

1、逐行扫描文件内容;
2、过滤注释(#后面的为注释);
3、根据等号切割key和value;
4、保存section,key和value到文件中;

需求:

1、当key没有值时:可以设定个默认值
2、读取文件时只有KEY没哟默认值会报错,添加一个默认值给该KEY
3、修改KEY的值时并保存到文件中,形成固定格式

ini.h

/********************************************************************************* @file           : ini.h* @author         : CircleDBA* @mail           : weiyuanquan@kingbase.com.cn* @blog           : circle-dba.blog.csdn.net* @date           : 24-5-8*******************************************************************************/#ifndef KINGBASEMANAGERTOOLS_INI_H
#define KINGBASEMANAGERTOOLS_INI_H#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>#include <set>
#include <filesystem>#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/filesystem.hpp>using namespace std;namespace Circle {class ini {protected:string config_path;set<string>* keys = nullptr;map<string, string>* props = nullptr;void trim(string& s);vector<string> split(const string& str, char pattern);private:public:ini();virtual ~ini();void file(boost::filesystem::path path);bool is_exists();bool load(std::string defaultValue);bool load(){return load("None");};set<string>* getKeys() const;map<std::string, string> *const getProps() const;string getValue(const string& key,const string& defaultValue);string setValue(const string& key,const string& Value);bool save();};} // Circle#endif //KINGBASEMANAGERTOOLS_INI_H

ini.cpp

/********************************************************************************* @file           : ini.cpp* @author         : CircleDBA* @mail           : weiyuanquan@kingbase.com.cn* @blog           : circle-dba.blog.csdn.net* @date           : 24-5-8*******************************************************************************/#include "ini.h"namespace fs = boost::filesystem;namespace Circle {Circle::ini::ini() {this->props = new map<string, string>;this->keys = new set<string>();}Circle::ini::~ini() {delete props;delete keys;}void Circle::ini::file(boost::filesystem::path path){this->config_path = path.string();}bool Circle::ini::is_exists(){return fs::exists(this->config_path);}void Circle::ini::trim(string& s){if (!s.empty()){s.erase(0, s.find_first_not_of(" "));s.erase(s.find_last_not_of(" ") + 1);}}vector<string> Circle::ini::split(const string& str, char pattern){vector<string> res;stringstream input(str);string temp;while (getline(input, temp, pattern)){res.push_back(temp);}return res;}bool Circle::ini::load(std::string defaultValue = "None"){std::ifstream file(this->config_path);std::string line, key, value, section;while (getline(file, line)) {trim(line);//去空行if (line.empty() || line == "\r" || line[0] == '#'){continue;}int s_startpos, s_endpos;if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1){section = line.substr(s_startpos + 1, s_endpos - 1);continue;}//处理等号后为空的配置vector<string> res = split(line, '=');if (res.size() < 2){res[1] = defaultValue;}int t = res[1].find("#");if (t != string::npos) {res[1].erase(t);}for_each(res.begin(), res.end(), [=](string& s)mutable {trim(s);});props->insert(make_pair(section+"."+res[0],res[1]));keys->insert(section);}file.close();return true;}set<string>* Circle::ini::getKeys() const {return keys;}map<std::string, string> *const Circle::ini::getProps() const {return this->props;}string Circle::ini::getValue(const string& key,const string& defaultValue) {if (props->find(key) == props->end()){return defaultValue;}string value = this->props->at(key);return value;}string Circle::ini::setValue(const string& key,const string& Value) {if (props->find(key) == props->end()){this->props->insert(make_pair(key, Value));}else{props->at(key) = Value;}return this->props->at(key);}bool Circle::ini::save(){std::ofstream outFile(this->config_path);set<string>* keysMap = getKeys();for (std::set<string>::const_iterator it = keysMap->begin(); it != keysMap->end(); ++it) {outFile << "[" << *it << "]" << std::endl;for (const auto &pair: *props) {vector<string> res = split(pair.first,'.');if(res[0] == *it){outFile << res[1] << " = " << pair.second << std::endl;}};}return true;}
} // Circle

config.conf

[group1]
IP = 192.168.30.1
name = group1
port = 7000
[group2]
IP = 192.168.1.101
name = group2
port = 7002

mian.cpp

/********************************************************************************* @file           : Application.h* @author         : CircleDBA* @mail           : weiyuanquan@kingbase.com.cn* @blog           : circle-dba.blog.csdn.net* @date           : 24-5-6*******************************************************************************/#ifndef KINGBASEMANAGERTOOLS_APPLICATION_H
#define KINGBASEMANAGERTOOLS_APPLICATION_H
#include <iostream>
#include <boost/filesystem.hpp>
#include "src/path/path.h"
#include "src/config/ini.h"namespace Circle {class Application {protected:private:public:boost::filesystem::path RootPath,ConfigPath,DefaultConfigPath;Circle::path* Path;Application() {RootPath = Path->ApplictionPath();ConfigPath = RootPath / "config";DefaultConfigPath = RootPath  / "include" / "Application" / "config";boost::filesystem::path config = DefaultConfigPath / "config.conf";std::cout << "--------------------------------> start" << std::endl;Circle::ini ini;ini.file(config);if(ini.is_exists()){ini.load();std::cout << ini.getValue("group1.IP","192.168.30.1") << std::endl;std::cout << ini.setValue("group1.IP","192.168.30.1") << std::endl;ini.save();std::cout << "-------------------------------->for start" << std::endl;map<string, string>* dataMap = ini.getProps();for (const auto &pair : *dataMap) {std::cout << pair.first << "=>" << pair.second <<std::endl;};}std::cout << "--------------------------------> end" << std::endl;}};} // Application#endif //KINGBASEMANAGERTOOLS_APPLICATION_H

相关文章:

【C++】手搓读写ini文件源码

【C】手搓读写ini文件源码 思路需求&#xff1a;ini.hini.cppconfig.confmian.cpp 思路 ini文件是一种系统配置文件&#xff0c;它有特定的格式组成。通常做法&#xff0c;我们读取ini文件并按照ini格式进行解析即可。在c语言中&#xff0c;提供了模板类的功能&#xff0c;所以…...

undolog

undolog回滚段 undolog执行的时间&#xff1a;在执行器操作bufferpool之前。 undolog页...

项目文档分享

Hello , 我是小恒。提前祝福妈妈母亲节快乐 。 本文写一篇初成的项目文档 &#xff08;不是README.md哈&#xff09;&#xff0c;仅供参考 项目名称 脚本存储网页 项目简介 本项目旨在创建一个网页&#xff0c;用于存储和展示各种命令&#xff0c;用户可以通过粘贴复制命令到…...

【深耕 Python】Quantum Computing 量子计算机(5)量子物理概念(二)

写在前面 往期量子计算机博客&#xff1a; 【深耕 Python】Quantum Computing 量子计算机&#xff08;1&#xff09;图像绘制基础 【深耕 Python】Quantum Computing 量子计算机&#xff08;2&#xff09;绘制电子运动平面波 【深耕 Python】Quantum Computing 量子计算机&…...

手写Spring5【笔记】

Spring5【笔记】 前言前言推荐Spring5【笔记】1介绍2手写 最后 前言 这是陈旧已久的草稿2022-12-01 23:32:59 这个是刷B站的时候&#xff0c;看到一个手写Spring的课程。 最后我自己好像运行不了&#xff0c;就没写。 现在2024-5-12 22:22:46&#xff0c;发布到[笔记]专栏中…...

2024中国(重庆)机器人展览会8月举办

2024中国(重庆)机器人展览会8月举办 邀请函 主办单位&#xff1a; 中国航空学会 重庆市南岸区人民政府 招商执行单位&#xff1a; 重庆港华展览有限公司 2024中国重庆机器人展会将汇聚机器人全产业链知名企业&#xff0c;世界科技领先的生产制造企业与来自多个国家和地区…...

Apache 开源项目文档中心 (英文 + 中文)

进度&#xff1a;持续更新中。。。 Apache Ambari 2.7.5 Apache Ambari Installation 2.7.5.0 (latest)Apache Ambari Installation 2.7.5.0 中文版 (latest) Apache DolphinScheduler Apache DolphinScheduler 1.2.0 中文版Apache DolphinScheduler 1.2.1 中文版...

蓝桥杯 算法提高 ADV-1164 和谐宿舍 python AC

贪心&#xff0c;二分 同类型题&#xff1a;蓝桥杯 算法提高 ADV-1175 打包 def judge(x):wood 0max_val 0ans_len 0for i in ll:if i > x:return Falseelif max(max_val, i) * (ans_len 1) < x:max_val max(max_val, i)ans_len 1else:wood 1max_val ians_len …...

Dragonfly 拓扑的路由算法

Dragonfly 拓扑的路由算法 1. Dragonfly 上的路由 (1)最小路由(2)非最小路由 2. 评估3. 存在问题 (1)吞吐量限制(2)较高的中间延迟 references Dragonfly 拓扑的路由算法 John Kim, William J. Dally 等人在 2008 年的 ISCA 中提出技术驱动、高度可扩展的 Dragonfly 拓扑。而…...

android基础-服务

同样使用intent来传递服务 oncreate是服务第一次启动调用&#xff0c;onStartCommand是服务每次启动的时候调用&#xff0c;也就是说服务只要启动后就不会调用oncreate方法了。可以在myservice中的任何位置调用stopself方法让服务停止下来。 服务生命周期 前台服务类似于通知会…...

mysql 事物

MySQL中的事务&#xff08;Transaction&#xff09;是一个确保数据完整性和一致性的重要概念。它将一组SQL操作捆绑在一起&#xff0c;当作一个单一的工作单元来执行。事务具备以下四个关键特性&#xff0c;即ACID特性&#xff1a; 原子性&#xff08;Atomicity&#xff09;&am…...

Unity Shader中获取像素点深度信息

1.顶点着色器中对深度进行计算 v2f vert(appdata v) {v2f o;o.pos UnityObjectToClipPos(v.vertex);o.uv TRANSFORM_TEX(v.uv, _MainTex);o.depth (o.pos.z / o.pos.w 1.0) * 0.5; // Normalize depth to [0, 1]return o; }但是达不到预期&#xff0c;最后返回的值一直大于…...

ROS——Action学习

文章目录 ROS Action概念自定义Action类型参考ROS Action概念 ROS Service会阻塞程序流,程序无法进行其它的工作,有时我们需要同时进行多个任务。 ROS Action可以满足要求,ROS Action提供程序的非阻塞执行。 Action是ROS Node的通信方式之一 Action server 向ROS系统广…...

基于C语言中的类型转换,C++标准创造出了更加可视化的类型转换

目录 前言 一、 C语言中的类型转换 二、为什么C需要四种类型转换 三、C中新增的四种强制类型转换操作符以及它们的应用场景 1.static_cast 2.reinterpret_cast 3.const_cast 4.dynamic_cast 前言 在C语言中&#xff0c;如果赋值运算符左右两侧的类型不同&#xff0c;或者…...

如何创建族表

https://jingyan.baidu.com/article/c275f6bafa5714a23c756768.html...

【UnityRPG游戏制作】Unity_RPG项目_PureMVC框架应用

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;就业…...

并行计算的一些知识点分享--并行系统,并行程序, 并发,并行,分布式

并行计算 核是个啥&#xff1f; 在并行计算中&#xff0c;“核”通常指的是处理器的核心&#xff08;CPU核心&#xff09;。每个核心都是一个独立的处理单元&#xff0c;能够执行计算任务。多核处理器指的是拥有多个这样核心的单一物理处理器&#xff0c;这样的设计可以允许多…...

设计模式:访问者模式

访问者模式&#xff08;Visitor Pattern&#xff09;是行为设计模式的一种&#xff0c;它使你能够在不修改对象结构的情况下&#xff0c;给对象结构中的每个元素添加新的功能。访问者模式将数据结构和作用于结构上的操作解耦&#xff0c;使得操作集合可相对自由地演化。 核心概…...

vivado Virtex-7 配置存储器器件

Virtex-7 配置存储器器件 下表所示闪存器件支持通过 Vivado 软件对 Virtex -7 器件执行擦除、空白检查、编程和验证等配置操作。 本附录中的表格所列赛灵思系列非易失性存储器将不断保持更新 &#xff0c; 并支持通过 Vivado 软件对其中所列非易失性存储器 进行擦除、…...

检测服务器环境,实现快速部署。适用于CRMEB_PRO/多店

运行效果如图&#xff1a; 最近被好多人问&#xff0c;本来运行的好好的&#xff0c;突然swoole就启动不了了。 本工具为爱发电&#xff0c;如果工具正好解决了您的需求。我会很开心 代码如下&#xff1a; """本脚本为爱发电by:网前雨刮器 """…...

生成xcframework

打包 XCFramework 的方法 XCFramework 是苹果推出的一种多平台二进制分发格式&#xff0c;可以包含多个架构和平台的代码。打包 XCFramework 通常用于分发库或框架。 使用 Xcode 命令行工具打包 通过 xcodebuild 命令可以打包 XCFramework。确保项目已经配置好需要支持的平台…...

rknn优化教程(二)

文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK&#xff0c;开始写第二篇的内容了。这篇博客主要能写一下&#xff1a; 如何给一些三方库按照xmake方式进行封装&#xff0c;供调用如何按…...

【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)

可以使用Sqliteviz这个网站免费编写sql语句&#xff0c;它能够让用户直接在浏览器内练习SQL的语法&#xff0c;不需要安装任何软件。 链接如下&#xff1a; sqliteviz 注意&#xff1a; 在转写SQL语法时&#xff0c;关键字之间有一个特定的顺序&#xff0c;这个顺序会影响到…...

Cinnamon修改面板小工具图标

Cinnamon开始菜单-CSDN博客 设置模块都是做好的&#xff0c;比GNOME简单得多&#xff01; 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...

uniapp微信小程序视频实时流+pc端预览方案

方案类型技术实现是否免费优点缺点适用场景延迟范围开发复杂度​WebSocket图片帧​定时拍照Base64传输✅ 完全免费无需服务器 纯前端实现高延迟高流量 帧率极低个人demo测试 超低频监控500ms-2s⭐⭐​RTMP推流​TRTC/即构SDK推流❌ 付费方案 &#xff08;部分有免费额度&#x…...

IT供电系统绝缘监测及故障定位解决方案

随着新能源的快速发展&#xff0c;光伏电站、储能系统及充电设备已广泛应用于现代能源网络。在光伏领域&#xff0c;IT供电系统凭借其持续供电性好、安全性高等优势成为光伏首选&#xff0c;但在长期运行中&#xff0c;例如老化、潮湿、隐裂、机械损伤等问题会影响光伏板绝缘层…...

HDFS分布式存储 zookeeper

hadoop介绍 狭义上hadoop是指apache的一款开源软件 用java语言实现开源框架&#xff0c;允许使用简单的变成模型跨计算机对大型集群进行分布式处理&#xff08;1.海量的数据存储 2.海量数据的计算&#xff09;Hadoop核心组件 hdfs&#xff08;分布式文件存储系统&#xff09;&a…...

纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join

纯 Java 项目&#xff08;非 SpringBoot&#xff09;集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...

论文阅读笔记——Muffin: Testing Deep Learning Libraries via Neural Architecture Fuzzing

Muffin 论文 现有方法 CRADLE 和 LEMON&#xff0c;依赖模型推理阶段输出进行差分测试&#xff0c;但在训练阶段是不可行的&#xff0c;因为训练阶段直到最后才有固定输出&#xff0c;中间过程是不断变化的。API 库覆盖低&#xff0c;因为各个 API 都是在各种具体场景下使用。…...

k8s从入门到放弃之HPA控制器

k8s从入门到放弃之HPA控制器 Kubernetes中的Horizontal Pod Autoscaler (HPA)控制器是一种用于自动扩展部署、副本集或复制控制器中Pod数量的机制。它可以根据观察到的CPU利用率&#xff08;或其他自定义指标&#xff09;来调整这些对象的规模&#xff0c;从而帮助应用程序在负…...