Read the class of the configuration file

xiaoxiao2021-03-06  74

// Author Naven 2003 for Both Win32 and UNIX OS

Class myproperties {

PUBLIC:

MyProperties ();

MyProperties (istream & IS);

MYPROPERTIES (Const string & filename);

Void Load (const string & filename);

Unsigned long size ();

String filename ();

String Get (Const String & Name, Const String & Default_Value);

Void Remove (Const string & name);

Void Set (Const String & Name, Const String & Value);

Void Add (Const String & Name, Const String & Value);

String GetName (Const Int & I);

String getValue; Const Int & I);

Void save ();

Void Save (String & FileName);

Private:

Void Init (istream & IS);

Private:

String_file;

Vector

_vals, _lms;

Unsigned long _size;

}

MyProperties :: myproperties ()

{

_size = 0;

}

MyProperties :: MyProperties (iStream & IS)

{

_size = 0;

INIT (IS);

}

MyProperties :: MyProperties (const string & filename)

{

Load (filename);

}

Void

MyProperties :: Load (const string & filename)

{

_LMS.CLEAR (); _vals.clear ();

_size = 0;

IF (filename.empty ())

Return;

IFStream is (filename.c_str ());

_file = filename;

INIT (IS);

}

Void

MyProperties :: INIT (istream & IS)

{

IF (! is) return;

Char ln [line_size 1], * pval

_LMS.CLEAR (); _vals.clear ();

_size = 0;

While (is.getline (ln, line_size) {

TrimLEFT (LN);

IF (ln [0]! = '#' && (pval = strchr (ln, '='))! = null) {

PVAL [0] = 0; PVAL ;

Trimright (ln); TRIM (PVAL);

IF (ln [0]! = 0 && pval [0]! = 0) {

_LMS.PUSH_BACK (LN);

_vals.push_back (pval);

_Size ;

}

}

}

}

Unsigned long

MyProperties :: size ()

{

Return_size;

}

String

MyProperties :: filename ()

{

Return_file;

}

String

MyProperties :: getName (const INT & I) {

IF (i> = 0 && i <_elms.size ())

Return_LMS [i];

""; "

}

String

MyProperties :: getValue (Const Int & i)

{

IF (i> = 0 && i <_vals.size ())

Return_vals [i];

""; "

}

String

MyProperties :: Get (const string & name, const string & default_value = ")

{

IF (Name.empty ())

Return Default_Value;

Vector

:: const_iterator elm_iter = _ELMS.BEGIN (), ELM_END = _LMS.END ();

Vector

:: const_iterator val_iter = _vals.begin (), VAL_END = _LMS.End ();

While (ELM_ITER! = ELM_END && VAL_ITER! = VAL_END) {

IF (* ELM_ITER) == Name)

RETURN * VAL_ITER;

ELM_ITER ;

Val_iter ;

}

Return Default_Value;

}

Void

MyProperties :: remove (const string & name)

{

IF (Name.empty ()) Return;

Vector

:: Iterator ELM_ITER = _LMS.BEGIN (), ELM_END = _LMS.END ();

Vector

:: Iterator Val_Ir = _vals.begin (), VAL_END = _LMS.END ();

While (ELM_ITER! = ELM_END && VAL_ITER! = VAL_END) {

IF (* ELM_ITER) == Name) {

_LMS.RASE (ELM_ITER);

_vals.rase (Val_Ir);

_size

}

ELM_ITER ;

Val_iter ;

}

}

Void

MyProperties :: Set (Const String & Name, Const String & Value)

{

IF (Name.empty ()) Return;

Vector

:: Iterator ELM_ITER = _ELMS.BEGIN (), ELM_END = _ELMS.END (); Vector

:: Iterator Val_iter = _vals.begin (), VAL_END = _LMS.End (); while (elm_iter! = ELM_END && VAL_ITER! = VAL_END) {ix (* ELM_ITER) == Name) * VAL_ITER = VALUE; ELM_ITER val_iter ;}} void myproperties :: add (const string & name, const string & value) {if (name.empty ()) return; _lms.push_back (name); _vals.push_back (value); _size ; } void myproperties :: save (string & filename) {if (filename.empty ()) Return; ifstream is (filename.c_str ()); if (! is) return; char ln [line_size 1], * pval; ostrStream SBUF; String Str, Str2; Bool EXIST_FLAG; VectorVec; While (is.getline (LN, LIN_SIZE)) {str = ln; trimleft (ln); exist_flag = false; if (ln [0]! = '#' && PVAL = STRCHR (Ln, '='))! = null) {PVAL [0] = 0; trimright (ln); if (ln [0]! = 0 &&! (STR2 = Get (ln)). EMPTY )) {SBUF << LN << "=" << Str2 << Endl; vec.push_back (ln); exist_flag = true;}} f (exist_flag == false) SBUF << str << endl;} vector

:: Iterator ELM_ITER = _ELMS.BEGIN (), ELM_END = _ELMS.END (); Vector

:: Iterator Val_iter = _vals.begin (), VAL_END = _LMS.END (); Vector

:: Iterator vec_itor, vec_end = vec.end (); while (elm_iter! = ELM_END && VAL_ILM = VAL_END) {vec_iter = vec.begin (); exist_flag = false; while (vec_iter! = vec_end) {ix ((* VEC_ITER) == (* ELM_ITER)) {EXIST_FLAG = true; Break;} VEC_ITER ;} if (exist_flag == false) SBUF << * ELM_ITER << "=" << * Val_Ir << Endl; ELM_ITER Val_iter ;} SBUF << Ends; OFSTREAM OS (filename.c_str ()); if (os) OS << SBUF.STR ();} void myproperties :: save () {save (_file);} / * Trim the space and tab at left * / char * Trimleft (char * s) {register int = 0; int SL = 0; char * sc = 0; if (s == null || s [0] == 0) RETURN NULL; SL = Strlen (s); / * Trim Space and Tab Char at The Left * / for (i = 0; IS) {SL = Strlen (SC); for (i = 0; i

= 0; I--) {/ * 32 IS Space, 9 IS Tab * / IF (S [i] == 32 || S [i] == 9) s [i] = 0; else break;} return S;} / * Trim Space and Tab AT LEFT AND RIGHT * / CHAR * TRIM (CHAR * S) {IF (S == NULL || S [0] == 0) Return Null; Trimright (S); Trimleft s); returnof;}

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

New Post(0)