Module reading and writing INI profile

xiaoxiao2021-03-05  35

/ / - * - C header - * - // ========================================= ============================================== / *** @file config.h ** Config.h, 2005/04/19 SLAYER ** @Author SLAYER * /// ========================= ============================================================================================================================================================================================================= === # ifndef _config_h_ # define _config_h_ # include "stdafx.h" #include #include #include #include using namespace std; using namespace stdext; class _key; typedef class _Value {Friend class _ini; public: _Value () {}; _value (const string & value): _ value (value) {}; inline _value & operator = (const string & value) {_value = value; return * THIS;}; inline _value & operator = (const INT & value) {_caster.clear (); _caster << value; _value = _caster.str (); return * this;}; inline _value & operator = (constling) {_caster = .clear (); _caster << value; _value = _caster.str (); return * this;}; inline _value & operator = (Const Double & Operator =

Value) {_caster.clear (); _VASTER << Value; _Value = _caster.str (); return * this;}; inline _value & operator = (const float & value) {_caster.clear (); _caster << value; _value }; Return * this;}; inline _value & operator = (const char * value) {_caster.clear (); _caster << value; _value = _caster.str (); return * this;}; inline Operator string () const {return _value;}; inline operator int () const {return atoi (_Value.c_str ());}; inline operator long () const {return atol (_Value.c_str ());}; inline Operator float () const {return static_cast (_value.c_str ()));}; inline operator double () const {return atof (_Value.c_str ());}; private: string _value; stringstream_caster ; // for control} value; Inline Ostream & Operator << (Ostream & O, Const Value & V) {O << static_cast (v); return o;}; typef class _key {typedef hash_map Valuemap Friend class _ini; public: _Key () {}; Inline void clear () {_ value.clear ();}; inline value & operator [} _values ​​[key];}; inline _key & operator = (const_key & k) {valueMap :: const_iterator i; for (i = k._values.begin (); i! = k._values.end (); i ) _values ​​[i-> first] = (string) i-> second; return * this;}; : valueMap _values;} Key; typedef class _Section {typedef hash_map keyMap; friend class _ini; public: _Section () {}; inline Key & operator [] (const string & section) {return _keys [section];} Inline void clear () {_ keys.clear ();

Inline _section & operator = (const _section & s) {keymap :: const_iterator i; for (i = s._keys.begin (); i! = S._KEYS.END (); i ) _Keys [i-> first] = I-> Second; return * this;}; private: keymap _keys;} section; type; means _ini {public: _ini (const string & file): _ file (file) {}; _ini () {_ file.clear (); Inline int load (section & secs) {return loading (second (section & secs, const string & file) {ifstream f (file.c_str (), iOS :: in); if (! F); if (! F); if (! F); if (! F); if (! F); if (! IS_OPEN ()) Return -1; SECS.CLEAR (); STRING Section, Key, Value; String line; While (! F.eof ()) {getLine (f, line); const char * s = line.c_str ); If (line.size () <3) Continue; line.find_first_not_of ("/ t")); if (line.size () <3) Continue; if (IS_COMMENT (LINE)) Continue; if (is_section (limited) Continue; if (section device ()) Continue; // unknow line if (! PARSE_LINE (LINE, Key, Value) Continue; cout << section << "," << key << "," << value << "," << Endl; SECS [Section] [key] = value; } Return 0;} int SAVE (Section & Secs, Const String & File) {OFStream F (file.c_str (), ios :: trunc); const_section :: keymap & keys = second :: keys; _section :: keymap :: const_iterator kit For (Kit = Keys.begin (); Kit! = Keys.end (); Kit ) {f << endl << "[" << kit-> first << "] << endl; const_key: : Valuemap & Values ​​=

Kit-> second._values; _key :: valuemap :: const_iterator vit; for (vit = values.begin (); vit! = values.end (); vit ) {f << vit-> first << "=" << vit-> second << Endl;}} return 0; ('='); If (n == string :: npos) return false; key = line.substr (0, n); value = line.substr (n = key.find_last_not_of ("/ t "); Key = key.substr (0, n 1); n = value.find_first_not_of (" / t "); if (n == String :: npos) Return false; value = value.substr (n); n = value.find_last_not_of ("/ t"); if (n == String :: npos) Return false; value = value.substr (0, n 1); if ((((n = value.find ("/ T # "))! = String :: npos) || ((n = value.find (" # "))! = string :: npos) || ((n = value.find (" / t / t // " )))! = String :: npos || (((n = v Alue.Find ("//"))! = string :: npos) {value = value.substr (0, n);} n = value.find_last_not_of ("/ t"); if (n == String: : NPOS) Return False; value = value.substr (0, n 1); return true;} inline bool is_comment (const string & line) {Return Line [0] == '#' || (line [0] = = '/' && line [1] == '/');} Inline Bool IS_SECTION (Const String & Line, String &

Sec) {if (Line [0]! = '[' || line [1] == ']) Return False; size_t n = line.find_first_of (']); if (n! = string :: npos ) Sec = line.substr (1, n-1); return true;} priVate: string _file;} ini; #ndif //_config_h_//test.cpp/#include "config.h" int _tmain (int Argc) , _Tchar * argv []) {INI INI; section S; String Str = "123"; s ["system"] ["count"] = 123; s ["system"] ["ip1"] = "127.0. 0.1 "; COUT << S [" system "] [" IP1 "] << Endl; section S1; S1 [" system "] [" 123 "] =" aaa "; S1 [" ABC "] [" 123 " ] = "AAA"; S = S1; // Merge Ini.save (s, "save.ini"); str = s ["system"] ["ip1"]; cout << str << endl; s "system"] ["double"] = (double) 12.311111111111111; str = s ["system"] ["int"]; cout << str << endl; float f = s ["system"] ["int" ]; Cout << f << endl; str = s ["system"] ["count"]; str = s ["system"] ["ip1"]; s.clear (); str = s ["system "] [" count "]; str = s [" system "] [" ip1 "]; ini.load (s," test.ini "); return 0;} test.ini /

#Test [Sec1] Value1 = a string value #xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxValue 2 = 123 // value_3 = ABC

[system] IP1 = 127.0.0.1port1 = 8000

#thread countrythreadcount = 50

[[] [1] [] 123 = 3221

转载请注明原文地址:https://www.9cbs.com/read-34120.html

New Post(0)