📄 conffile.h
字号:
#ifndef CONFFILE#define CONFFILE#include <iostream>#include <string>#include <vector>#include <fstream>#include <sstream>using namespace std ;class ZConfSection{ public: vector<string> Lines; friend ostream& operator<<(ostream& ostr,ZConfSection& zc) {// ostr <<"["<<zc.name<<"]"<<endl; for (unsigned int i = 0; i<zc.Lines.size()-1; i++) ostr << zc.Lines[i] << endl; ostr <<zc.Lines[zc.Lines.size()-1]; return ostr; } friend istream& operator>>(istream& istr,ZConfSection& zc) { string line; getline(istr,line); zc.Lines.push_back(line); return istr; } void addLine(string s){Lines.push_back(s);} int find(string s) { string name;string value; for (unsigned int i = 1; i<Lines.size()-1; i++) { unsigned int b=Lines[i].find_first_of("=",1); if(b!=string::npos) { name=getName(i); value=getValue(i); if(s==name)return(i); } } return(0); } string getSectionName() { unsigned int b=Lines[0].find_first_of("["); unsigned int e=Lines[0].find_first_of("]"); if(b==string::npos || e==string::npos || e<b+1) return(""); return(Lines[0].substr(b+1,e-b-1)); } string getName(int i) { int b=Lines[i].find_first_of("=",1); return(Lines[i].substr(0,b)); } string getValue(int i) { int b=Lines[i].find_first_of("=",1); return(Lines[i].substr(b+1,Lines[i].size()-b-1)); } };class ZConfFile{ string FileName; vector<ZConfSection> Sections;public: ZConfFile(string FileName) { this->FileName=FileName; ifstream confFile(FileName.c_str()); if (confFile.fail()) { cout<<"Couldn't open file:"<<FileName<<endl; return; } string line; ZConfSection zchead; Sections.push_back(zchead); while(!confFile.eof()) { getline(confFile,line); unsigned int b=line.find_first_of("[",0); unsigned int e=line.find_first_of("]",0); if((b!=string::npos) && (e!=string::npos) && e>(b+1) ) { ZConfSection zc; Sections.push_back(zc); //line.substr(b+1,e-b-1)); } Sections[Sections.size()-1].addLine(line); // cout<<line; //confFile >> sc; } confFile.close(); } void write() { ofstream confFile(FileName.c_str()); if (confFile.fail()) { cout<<"Couldn't open file to Write:"<<FileName<<endl; return; } confFile << *this; } friend ostream& operator<<(ostream& ostr,ZConfFile& zc) { int size=zc.Sections.size(); if(size==0)return(ostr); for(int i=0;i<size-1;i++) { ostr<<zc.Sections[i]<<endl; } ostr<<zc.Sections[size-1]; return(ostr); } //void writeInt(string section,string line,int i); //void writeFloat(string section,string line,float f); //void writeString(string section,string line,string s); int readInt(string section,string line,int DefaultValue) { int s=findSection(section); if(s==0)return(DefaultValue);// cout<<"Section find success!" <<endl; int l=Sections[s].find(line); if(l==0)return(DefaultValue);// return(Sections[s].getValue(l).toInt()); stringstream stream; stream<<Sections[s].getValue(l); int i; stream>>i; return(i); } float readFloat(string section,string line,float DefaultValue) { int s=findSection(section); if(s==0)return(DefaultValue); int l=Sections[s].find(line); if(l==0)return(DefaultValue);// return(Sections[s].getValue(l).toInt()); stringstream stream; stream<<Sections[s].getValue(l); float f; stream>>f; return(f); } string readString(string section,string line,string DefaultValue) { int s=findSection(section); if(s==0)return(DefaultValue); int l=Sections[s].find(line); if(l==0)return(DefaultValue); return(Sections[s].getValue(l)); } //~ZConfFile(); int findSection(string s) {// cout<<"to be find section:"<<s<<endl;// cout<<"Sections="<<Sections.size()<<endl; for( unsigned int i=1;i<Sections.size();i++) { if(s==Sections[i].getSectionName()) {// cout<<"Section"<<i<<"="<<Sections[i].getSectionName()<<endl; return(i); } } return(0); } };#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -