记录一个纯粹的c++写的ini文件的读写类,网上开源的源码,我也乐意完善之后再分享出来
网上关于纯粹的config.ini文件的读写的源码很多,我也是借鉴别人的,加了点注解,和使用介绍,删除了一些不需要的组件,简化代码
IniFileSTL.h
#ifndef _FISH_INI_FILE_ #define _FISH_INI_FILE_ #include <string> #include <vector> using namespace std; class IniFileSTL { public: explicit IniFileSTL(const string &fileName); ~IniFileSTL(void); bool ReadFile(void); //使用eg: mysql ip 127.0.0.1 mysql port 3306 //其中value是数据如果读取失败时候的默认值 string ReadString( const string §ion, const string &key, const string &value ); int ReadInt( const string §ion, const string &key, int value ); bool WriteString( const string §ion, const string &key, const string &value ); bool WriteInt( const string §ion, const string &key, int value ); bool RemoveSection( const string §ion ); bool RemoveKey( const string §ion, const string &key ); bool WriteFile(void); private: static string Trim( const string &str ); static string LTrim( const string &str ); static string RTrim( const string &str ); private: string m_fileName; vector<string> m_vctLine; }; #endif // #include <stdio.h> // #include <iostream> // #include "IniFileSTL.h" // int main(int argc, char **argv) // { // IniFileSTL ini("./test.ini"); // std::cout<<"use default value:"<<std::endl; // std::cout<<ini.ReadString("system","ipaddr", "10.70.101.134")<<std::endl; // std::cout<<ini.ReadInt("system","tcpport", 7050 )<<std::endl; // std::cout<<"input data,and read again:"<<std::endl; // ini.WriteString("system","ipaddr", "10.70.101.125"); // ini.WriteInt("system","tcpport", 8080 ); // std::cout<<ini.ReadString("system","ipaddr", "10.70.101.134")<<std::endl; // std::cout<<ini.ReadInt("system","tcpport", 7050 )<<std::endl; // return 0; // }
IniFileSTL.cpp
#include "IniFileSTL.h" #include <fstream> #include <strstream> #include <string> IniFileSTL::IniFileSTL(const string &fileName) :m_fileName(fileName) { ReadFile(); } IniFileSTL::~IniFileSTL(void) { WriteFile(); } bool IniFileSTL::ReadFile( void ) { ifstream in(m_fileName.c_str()); bool bopen = in.is_open(); string line; while( getline(in,line) ) { m_vctLine.push_back(line); } return true; } string IniFileSTL::ReadString( const string §ion, const string &key, const string &value ) { for( size_t i = 0;i < m_vctLine.size(); ++i ) { string §ion_line = m_vctLine[i]; size_t sec_begin_pos = section_line.find('['); if( sec_begin_pos == string::npos || sec_begin_pos != 0 ) { continue; } size_t sec_end_pos = section_line.find( ']', sec_begin_pos ); if( sec_end_pos == string::npos ) { continue; } if( section != Trim(section_line.substr(sec_begin_pos+1, sec_end_pos-sec_begin_pos-1) ) ) { continue; } //find key, ÕâÀïµÄϱ껹ÊÇi for( ++i; i < m_vctLine.size(); ++i ) { string &key_line = m_vctLine[i]; size_t sec_pos = key_line.find('['); if( sec_pos != string::npos && sec_pos == 0 ) { --i; //reback a step,find again break;//the line is section line } size_t begin_key_pos = key_line.find(key); if( begin_key_pos == string::npos ) { continue; } size_t equal_pos = key_line.find( '=', begin_key_pos ); if( equal_pos == string::npos ) { continue; } if( key != Trim(key_line.substr( begin_key_pos, equal_pos - begin_key_pos) ) ) { continue; } size_t comment_pos = key_line.find( "#", equal_pos + 1 ); if( comment_pos != string::npos ) { //²»Òª×¢ÊÍ return Trim(key_line.substr( equal_pos + 1, comment_pos - equal_pos - 1 )); } return Trim(key_line.substr( equal_pos + 1 )); } } return value; } bool IniFileSTL::WriteString( const string §ion, const string &key, const string &value ) { for( size_t i = 0;i < m_vctLine.size(); ++i ) { string §ion_line = m_vctLine[i]; size_t sec_begin_pos = section_line.find('['); if( sec_begin_pos == string::npos || sec_begin_pos != 0 ) { continue; } size_t sec_end_pos = section_line.find( ']', sec_begin_pos ); if( sec_end_pos == string::npos ) { continue; } if( section != Trim(section_line.substr(sec_begin_pos+1, sec_end_pos-sec_begin_pos-1)) ) { continue; } //find key, ÕâÀïµÄϱ껹ÊÇi for( ++i; i < m_vctLine.size(); ++i ) { string &key_line = m_vctLine[i]; size_t sec_pos = key_line.find('['); if( sec_pos != string::npos && sec_pos == 0 ) { --i; //reback a step,find again break;//the line is section line } size_t begin_key_pos = key_line.find(key); if( begin_key_pos == string::npos ) { continue; } size_t equal_pos = key_line.find( '=', begin_key_pos ); if( equal_pos == string::npos ) { continue; } if( key != Trim(key_line.substr( begin_key_pos, equal_pos - begin_key_pos )) ) { continue; } size_t comment_pos = key_line.find( "#", equal_pos + 1 ); string new_line = key_line.substr( 0, equal_pos + 1 ) + value; if( comment_pos != string::npos ) { new_line += key_line.substr( comment_pos ); } key_line = new_line; return true; } //δÕÒµ½key m_vctLine.insert( m_vctLine.begin() + i, key + "=" + value ); return true; } //sectionºÍkey¶¼²»´æÔÚ m_vctLine.insert( m_vctLine.end(), "" ); m_vctLine.insert( m_vctLine.end(), "[" + section + "]" ); m_vctLine.insert( m_vctLine.end(), key + "=" + value ); return true; } bool IniFileSTL::RemoveSection( const string §ion ) { for( size_t i = 0;i < m_vctLine.size(); ++i ) { string §ion_line = m_vctLine[i]; size_t sec_begin_pos = section_line.find('['); if( sec_begin_pos == string::npos || sec_begin_pos != 0 ) { continue; } size_t sec_end_pos = section_line.find( ']', sec_begin_pos ); if( sec_end_pos == string::npos ) { continue; } if( section != Trim(section_line.substr(sec_begin_pos+1, sec_end_pos-sec_begin_pos-1)) ) { continue; } //ÕÒµ½ÁËsection size_t del_begin = i; for( ++i ; i < m_vctLine.size(); ++i ) { string &next_section = m_vctLine[i]; size_t next_pos = next_section.find('['); if( next_pos == string::npos || next_pos != 0 ) { continue; } break; } m_vctLine.erase( m_vctLine.begin() + del_begin, m_vctLine.begin()+i ); return true; } return false; } bool IniFileSTL::RemoveKey( const string §ion, const string &key ) { for( size_t i = 0;i < m_vctLine.size(); ++i ) { string §ion_line = m_vctLine[i]; size_t sec_begin_pos = section_line.find('['); if( sec_begin_pos == string::npos || sec_begin_pos != 0 ) { continue; } size_t sec_end_pos = section_line.find( ']', sec_begin_pos ); if( sec_end_pos == string::npos ) { continue; } if( section != Trim(section_line.substr(sec_begin_pos+1, sec_end_pos-sec_begin_pos-1)) ) { continue; } //ÕÒµ½ÁËsection for( ++i ; i < m_vctLine.size(); ++i ) { string &key_line = m_vctLine[i]; size_t key_pos = key_line.find( key ); if( key_pos == string::npos || key_pos != 0 ) { continue; } else { m_vctLine.erase( m_vctLine.begin() + i ); return true; } } } return false; } bool IniFileSTL::WriteFile( void ) { ofstream out(m_fileName.c_str()); for( size_t i = 0; i < m_vctLine.size(); ++i ) { out<<m_vctLine[i]<<endl; } return true; } int IniFileSTL::ReadInt( const string §ion, const string &key, int value ) { string str = ReadString( section, key, "" ); if( "" == str ) { return value; } istrstream in( str.c_str() ); int ret = 0; in>>ret; return ret; } bool IniFileSTL::WriteInt( const string §ion, const string &key, int value ) { ostrstream out; out<<value; //char buffer[64] = ""; //itoa( value, buffer, 10 ); return WriteString( section, key, out.str() ); } string IniFileSTL::LTrim( const string &str ) { size_t pos = 0; while( pos != str.size() ) { if( ' ' == str[pos] ) { ++pos; } else { break; } } return str.substr(pos); } string IniFileSTL::RTrim( const string &str ) { size_t pos = str.size() - 1; while( pos >= 0 ) { if(' ' == str[pos]) { --pos; } else { break; } } return str.substr( 0, pos + 1 ); } string IniFileSTL::Trim(const string &str) { return LTrim( RTrim(str) ); }
main.cpp
#include <stdio.h> #include <iostream> #include "IniFileSTL.h" int main(int argc, char **argv) { IniFileSTL ini("./test.ini"); std::cout<<"use default value:"<<std::endl; std::cout<<ini.ReadString("system","ipaddr", "10.70.101.134")<<std::endl; std::cout<<ini.ReadInt("system","tcpport", 7050 )<<std::endl; std::cout<<"input data,and read again:"<<std::endl; ini.WriteString("system","ipaddr", "10.70.101.125"); ini.WriteInt("system","tcpport", 8080 ); std::cout<<ini.ReadString("system","ipaddr", "10.70.101.134")<<std::endl; std::cout<<ini.ReadInt("system","tcpport", 7050 )<<std::endl; return 0; }