网站建设工作量统计表,网站怎么做微信支付功能,台州建设工程网站,架设网站是自己架设服务器还是租服务器【C】手搓读写ini文件源码 思路需求#xff1a;ini.hini.cppconfig.confmian.cpp 思路
ini文件是一种系统配置文件#xff0c;它有特定的格式组成。通常做法#xff0c;我们读取ini文件并按照ini格式进行解析即可。在c语言中#xff0c;提供了模板类的功能#xff0c;所以… 【C】手搓读写ini文件源码 思路需求ini.hini.cppconfig.confmian.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 : weiyuanquankingbase.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.hppusing namespace std;namespace Circle {class ini {protected:string config_path;setstring* keys nullptr;mapstring, string* props nullptr;void trim(string s);vectorstring 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);};setstring* getKeys() const;mapstd::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_Hini.cpp
/********************************************************************************* file : ini.cpp* author : CircleDBA* mail : weiyuanquankingbase.com.cn* blog : circle-dba.blog.csdn.net* date : 24-5-8*******************************************************************************/#include ini.hnamespace fs boost::filesystem;namespace Circle {Circle::ini::ini() {this-props new mapstring, string;this-keys new setstring();}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);}}vectorstring Circle::ini::split(const string str, char pattern){vectorstring 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;}//处理等号后为空的配置vectorstring 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;}setstring* Circle::ini::getKeys() const {return keys;}mapstd::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);setstring* keysMap getKeys();for (std::setstring::const_iterator it keysMap-begin(); it ! keysMap-end(); it) {outFile [ *it ] std::endl;for (const auto pair: *props) {vectorstring res split(pair.first,.);if(res[0] *it){outFile res[1] pair.second std::endl;}};}return true;}
} // Circleconfig.conf
[group1]
IP 192.168.30.1
name group1
port 7000
[group2]
IP 192.168.1.101
name group2
port 7002mian.cpp
/********************************************************************************* file : Application.h* author : CircleDBA* mail : weiyuanquankingbase.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.hnamespace 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;mapstring, 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
文章转载自: http://www.morning.drndl.cn.gov.cn.drndl.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.bytgy.com.gov.cn.bytgy.com http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.cokcb.cn.gov.cn.cokcb.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.ftldl.cn.gov.cn.ftldl.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.sknbb.cn.gov.cn.sknbb.cn http://www.morning.kwnnx.cn.gov.cn.kwnnx.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.tgcw.cn.gov.cn.tgcw.cn http://www.morning.rnqyy.cn.gov.cn.rnqyy.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.tdgwg.cn.gov.cn.tdgwg.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn http://www.morning.bfmrq.cn.gov.cn.bfmrq.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn