Explanation: It's just a rookie, the purpose of posting is to hope that heroes point, deficiencies, C learning time is not long, huh. Or help test, and make it together.
head File:
/ / =========================================================================================================================================================================================== =========
//
// CopyRight (C) 2000-2005 TT Technologies, Co. LTD.
// All Rights Reserved.
//
// Class: CTTXMLFILE
//
// Product: TTLIBRARY
// file: TTXMLFILE.H
// Author: http://blog.9cbs.net/waterpub
// created: 2005.03.07 11:30
//
// Description:
// ValueAdded Main Program for TTLibrary.
//Undact: shenzhen @ guangdong
// Waterpub@mail.9cbs.net
//
/ / =========================================================================================================================================================================================== =========
#pragma overce
#include "string"
#include "vector"
#include "utility"
#include "algorithm"
Using namespace std;
Namespace TTLIBRARY
{
/ / Maintain an XML entry class
Class CTTXMLITEM
{
PUBLIC:
CTTXMLITEM (Const string & sitemname);
~ Cttxmlitem ();
Inline Bool SetKeyValue (Const String & Skey);
Inline Bool SetKeyValue (Const String & Skey, Const Int & nvalue);
Inline Bool GetKeyValuestring (Const String & Skey, String & Svalue) Const;
Inline Bool GetKeyValueint (Const String & Skey, Int & nvalue) Const;
Inline Bool Mergeanother; Inline String GetItemName () const {return m_sitemname;}
#ifdef _Debug
Void debug ();
#ENDIF
Private:
Const vector
Private:
String m_sitemname; // 条 Name
Vector
Friend class cttxmlfile;
Private:
Class cttxmlitemfind // lookup key value of imitation functions
{
PUBLIC:
CTTXMLITEMFIND (Const String & Skey)
{
m_skey = SKEY;
}
Bool Operator () (Const Pair
{
IF (xmlitemdata.first == m_skey)
{
Return True;
}
Else
{
Return False;
}
}
Private:
String m_skey;
}
}
/ / Load XML and resolve the class
Class CTTXMLFILE
{
PUBLIC:
CTTXMLFILE (VOID);
~ Cttxmlfile (void);
PUBLIC:
Bool clear ();
Bool Load (const string & sfilename);
Bool Save (const string & sfilename = ");
Bool AddItem (Const Cttxmlitem & Xi);
Bool RemoveItem (Const string & sitemname);
#ifdef _Debug
Void debug ();
#ENDIF
Private:
Bool ParseItem (Char * & PSTR);
Bool Parsekey (Char * & PSTR, CTTXMLITEM & XI);
Bool Skipblank (Char * & PSTR);
Bool Skipequalchar (Char * & PSTR);
Bool Findword (Char * & PSTR, String & Sword);
Private:
String m_sfilename; // Loaded file name
Vector
Private:
Class CTTXMLITEMFIND / / Find the imitation function of the entry that is the same name in the XML entry list
{
PUBLIC:
CTTXMLITEMFIND (Const string & sitemname)
{
m_sitemname = sitemname;
}
Bool Operator () (Const Cttxmlitem & Xi)
{
IF (xi.getitemname () == m_sitemname)
{
Return True;
}
Else
{
Return False;
}
}
Private:
String m_sitemname;
}
}
}
Implement file:
#include "stdafx.h"
#include "TTXMLFILE.H" Namespace TTLIBRARY
{
CTTXMLITEM :: CTTXMLITEM (Const string & sitemname)
{
m_sitemname = sitemname;
}
CTTXMLITEM :: ~ cttxmlitem ()
{
}
Bool Cttxmlitem :: SetKeyValue (Const String & Stey)
{
Vector
CTTXMLITEMFIND (SKEY));
IF (it! = m_keyvector.end ())
{
(* it). Second = svalue;
}
Else
{
Pair
XMLITEMDATA.FIRST = SKEY;
Xmlitemdata.second = svalue;
M_keyvector.push_back (xmlitemdata);
}
Return True;
}
Bool Cttxmlitem :: SetKeyValue (Const String & Skey, Const Int & nvalue)
{
String svalue;
Char szvalue [26];
ITOA (NValue, SzValue, 10);
Svalue = SzValue;
SetKeyValue (SKEY, SVALUE);
Return True;
}
Bool CTTXMLITEM :: GetKeyValuestring (Const String & Skey, String & Svalue) Const
{
Vector
CTTXMLITEMFIND (SKEY));
IF (it! = m_keyvector.end ())
{
Svalue = (* it). Second;
Return True;
}
Else
{
Return False;
}
}
Bool CTTXMLITEM :: GetKeyValueint (Const String & Skey, Int & nvalue) Const
{
Vector
CTTXMLITEMFIND (SKEY));
IF (it! = m_keyvector.end ())
{
Nvalue = atoi ((* it). Second.c_str ());
Return True;
}
Else
{
Return False;
}
}
Const vector
{
Return & M_KeyVector;
}
Bool Cttxmlitem :: Mergeanother (Const Cttxmlitem & Xi)
{
IF (xi.getitemname () == m_sitemname) {
Vector
For (; it! = (xi.getKeyVector ()) -> end (); it )
{
SetKeyValue ((* it) .first, (* it). Second);
}
}
Return True;
}
#ifdef _Debug
Void cttxmlitem :: Debug ()
{
Xtrace ("============================= / n");
Xtrace (RGB (0, 0, 255), "Article Name:% S / N", m_sitemname.c_str ());
Vector
For (; it! = m_keyvector.end (); it )
{
Xtrace ("Key:% S, Value:% S / N", (* it) .first.c_str (), (* it). Second.c_str ());
}
}
#ENDIF
CTTXMLFILE :: CTTXMLFile (void)
{
}
CTTXMLFILE :: ~ cttxmlfile (void)
{
}
Bool cttxmlfile :: AddItem (const cttxmlitem & xi)
{
Vector
CTTXMLITEMFIND (Xi.GetItemName ()));
IF (it! = m_itemvector.end ())
{
(* it) .mergenother (xi);
Return False;
}
Else
{
// No this entry added
m_itemvector.push_back (xi);
Return True;
}
}
Bool CttxmlFile :: RemoveItem (const string& sitemname)
{
Vector
CTTXMLITEMFIND (SITEMNAME));
IF (it! = m_itemvector.end ())
{
// Take this entry to delete
m_itemvector.erase (IT);
Return True;
}
Else
{
Return False;
}
}
Bool cttxmlfile :: clear ()
{
m_itemvector.clear ();
m_sfilename.clear ();
Return True;
}
Bool CTTXMLFILE :: Load (const string & sfilename)
{
IF (sfilename.empty ()) Return False;
CHAR SZDIRBUF [MAX_PATH] = ""; getCurrentDirectory (max_path, szdirbuf);
Strncat (SZDirbuf, "////////////////////////////////////////////////////////////////////>
Strncat (szdirbuf, sfilename.c_str (), sfilename.size ());
Clear ();
m_sfilename = szdirbuf;
File * hfile = fopen (m_sfilename.c_str (), "r");
IF (null == hfile) Return False;
String strText;
Char * szbuf = new char [1024 1];
While (! feof (hfile))
{
INT NREAD = (int) Fread (SZBUF, 1, 1024, HFILE);
IF (NREAD> 0)
{
SZBUF [NREAD] = NULL;
Strtext = SZBUF;
}
}
Fclose (HFILE);
Char * pstr = (char *) strText.c_str ();
// analyse file
While (PSTR! = NULL)
{
IF (! PARSEITEM (PSTR)) BREAK;
}
#ifdef _Debug
DEBUG ();
#ENDIF
Delete [] szbuf;
Return True;
}
Bool CTTXMLFILE :: Save (const string & sfilename)
{
Return True;
}
// Resolution from the file out an entry
Bool CttxmlFile :: ParseItem (Char * & PSTR)
{
Char * pstart = PSTR;
While (* pstart! = '<' && * pstart! = null)
PStart ;
IF (* pstart == null)
{
PSTR = PStart;
Return false; // parsing the end of the file
}
Else
{
PStart ; // starts from '<'
Skipblank (PStart);
// Analytical bar name
STRING SITEMNAME;
Findword (pstart, sitemname))
{
CTTXMLITEM XI (Sitemname);
// Resolution key value
ParseKey (Pstart, Xi);
#ifdef _Debug
// xi.debug ();
#ENDIF
m_itemvector.push_back (xi);
}
/ / Eliminate the corresponding '>' character
Skipblank (PStart);
IF (* pstart == '>) PStart ;
PSTR = PStart;
Return True;
}
}
Bool CTTXMLFILE :: Parsekey (Char * & PSTR, CTTXMLITEM & XI)
{
String sKey;
String svalue;
While (Findword (pstr, skey))
{
IF (SkipequalChar (PSTR))
{
IF (Findword (PSTR, SVALUE))
{
Xi.setKeyValue (SKEY, SVALUE);
}
}
}
Skipblank (PSTR);
Return True;}
Bool CttxmlFile :: Skipblank (Char * & PSTR)
{
While (* pstr> 0 && * pstr <33)))
PSTR ;
Return True;
}
Bool cttxmlfile :: Skipequalcha (Char * & PSTR)
{
Skipblank (PSTR);
IF (* pstr == '=')
{
PSTR ;
Return True;
}
Else
Return False;
}
// String, cannot be '<' or '>' or '='
Bool cttxmlfile :: Findword (Char * & PSTR, String & Sword)
{
Skipblank (PSTR);
Char * pstart = PSTR;
While (* pstart> = 33 || * pstart <0) && * pstart! = '<' && * pstart! = '> && * pstart! =' = ')
PStart ;
IF (PStart == PSTR)
{
PSTR = PStart;
Return False;
}
Else
{
String Strtemp (PSTR, PSTART);
Sword = strTemp;
PSTR = PStart;
Return True;
}
}
#ifdef _Debug
Void cttxmlfile :: debug ()
{
Xtrace ("**************************************************** / N ");
Xtrace (RGB (255, 0, 0), "File Name:% S / N", M_SFileName.c_STR ());
Xtrace ("**************************************************** / N ");
Vector
For (; it! = m_itemvector.end (); it )
{
(* it) .debug ();
}
}
#ENDIF
}
Test file: Test.ini, as follows (written is very chaotic, as a strengthening test,):
FDSAFDFDSAFDS
<> <>>
Test code: CTTXMLFILE TTXF; TTXF.LOAD ("Skin2.ini");