(The reader can click here to get the source code, pay attention to the boot.ini to the E: /, so that the program is running to find a file)
Head file cfgdata.h
// cfgdata.h: interface for the ccfgdata class.////
#if! defined (AFX_CFGDATA_H__A40CDB9A_0E44_49E6_A460_505D76BA6414__NCLUDED _) # Define AFX_CFGDATA_H__A40CDB9A_0E44_49E6_A460_505D76BA6414__NCLUDED_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
Class ccfgdata {protected: // Group maptringtoptr m_strmapstringtoptr m_StrMapMap; // Current group cstract m_strgroup; public: // Constructs configuration data ccfgdata (); // Destructure configuration data Virtual ~ ccfgdata (); // load configuration data / * parameters: name LPCTSTR strFileName loaded file returns: None * / void LoadCfgData (LPCTSTR strFileName); // configuration data is saved to the file / * parameters: name LPCTSTR strFileName save the file return value: the successful return TRUE Failure Returns false * / boolname; // Save the configuration data to the string / * Parameters: CString * PSTR To save the string pointer return value: Success Back to True Failed to return false * / bool savetostr (cstring * PSTR); // Set the currently read group / * parameter: current group name Return value: no * / void setGroup (lpctstr strgroup); // Modify or add a data / * parameter: lpctstr strkey to modify The key value of the data LPCTSTSTSTSTSTSTSTSTSTRVALUE To modify the content return value: Note: If the current group exists in the configuration data, modify or add the data of the group corresponding key value, if the current disaster configuration data does not exist, then Create this group * / bool setData (lpctstr strkey, lpctstr strval); // obtain data / * parameters of string type in the current group: LPCTSTSTSTSTRKEY The data of the data to be obtained LPCTSTSTSTRVALUE to obtain data Content Return Value: Find data to return true, otherwise return false * / virtuley, cstring & strital; // Get long-type data / * Parameters: LPCTSTSTSTSTSTSTRKEY to get the data of the data to get long Lvalue to get The value of the data returns: Find data to return true, otherwise return false * / virtual Bool getlongData (LPCTSTSTSTSTSTRKEY, Long & LVALUE);
Protected: // Release the memory / * parameter occupied by the configuration data: None return value: no * / void radovell ();};
#ndif //! Defined (AFX_CFGDATA_H__A40CDB9A_0E44_49E6_A460_505D76BA6414__INCLUDED_)
Source file cfgdata.cpp
// cfgdata.cpp: Implementation of the ccfgdata class.///#include "stdafx.h" #include "cfgdata.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
/ / // construction / destruction //// Constructive configuration data
Ccfgdata :: ccfgdata () {// Initialization Configuration Data M_StrGroup = "Settings";} // Destructor Configuration Data
Ccfgdata :: ~ ccfgdata () {transovell ();} // Release configuration data occupied by memory / * Parameters: No * / void ccfgdata :: removeall () {position POS = m_strmapmap.getStartPosition (); While (POS) {cmapstringTString * PStrMap; CString Str; M_StrMapMap.getNextAssoc (POS, STR, (Void * &) PSTRMAP); if (pstrmap! = null) {pstrmap-> removeall (); // Delete CString - > Pointer delete pstrMap;}} m_strmapmap.removeall ();} // Load configuration data / * Parameters: LPCTSTSTR STRFILENAME Load file Name Return Value: None * / Void CcfGData :: loadcfgdata (lpctstr strfilename) {INT IREADLEN = 0; CSTRING STR [3]; int tentate = 0; // 0: Reading key value 1: Reading content 2: Reading group unsigned char ch; // The characters being read / / Clear configuration data removeall (); cfile file; file.open (strfilename, cfile :: moderad); file.seek (0, cfile :: begin); str [0] = ""; // Store key value string STR [1] = ""; // Store content string STR [2] = ""; // stores group string // string to string mapping, save key value, and content cmapstringtostring * pstrMap = null; do {IReadlen = file.read (& ch, 1); if (ireadlen! = 0) {// Processing Chinese IF (CH> 0x80) // Chinese {STR [istate] = ch; IReadlen = file.read (& ch, 1); if (ireadlen! = 0) {STR [istate] = CH;}} else {switch (ch) { // Handling '[' Read Group string case '[": STR [0] .trimleft (); str [0] .trimright (); str [1] .trimleft (); str [1] .trimright (Stri); ); // confirm that the key value and the content data are empty, otherwise it may be the symbol IF in the key value and content (STR [0] == "&& Str [1] ==") {PSTRMAP = null; istate = 2 ; STR [2] = "";} else {str [istate] = ch;} BREAK; // Handle ']' group string reads Fusing Case ']': // Confirm that the read is the group character String data str [2] .trimleft (); str [2] .trimright (); if (iState == 2 && str [2]! = "
) {Istate = 0; // Newly built a key value -> content mapping, put it in this group PSTRMAP = New cmapstringTString; m_StrMapMap.Setat (STR [2], PSTRMAP);} else {str [istate] = CH;} Break; Case '=': // Start reading content iState = 1; STR [1] = ""; Break; // Process Enter Case ';': Case 0x0d: Case 0x0a: IState = 0; // Key value non-empty STR [0] .trimleft (); str [0] .trimright (); str [1] .trimleft (); str [1] .trimright (); if (Str [0 ]! = "&& pstrMap! = null) {pstrmap-> setat (lpctstr) STR [0], (lpctstr) STR [1]);} // Handling Empty Data STR [0] =" "; STR [ 1] = ""; // Continue to continue reading until the file ends or encounters the carriage return IF (CH == ';') {while ((iReadlen = file.read (& ch, 1))> 0) { / / If you encounter a carriage return, termination, and return the current location back if (CH == 0x0D || CH == 0x0a) {file.seek (-1, cfile :: current); Break;}}} Break; Default: // Ordinary characters, add to the corresponding position STR [istate] = ch; Break;}}}} while (iReadlen! = 0); file.close ();} // Set the currently read group / * Parameters: The current group name Return value: no * / void ccfgdata :: setgroup (lpctstr strgr OUP {m_strgroup = strgroup;} // Get data / * parameters of the string type in the current group: LPCTSTSTSTSTSTRKEY The data of the data to be obtained LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTS Return value: Find data to return True, otherwise returns false * /
BOOL CCfgData :: GetStrData (LPCTSTR strKey, CString & strValue) {CMapStringToString * pStrMap = NULL; // Find the current group if (m_StrMapMap.Lookup (m_strGroup, (void * &) pStrMap)) {if (pStrMap-> Lookup (strKey, STRVALUE)) Return true;} Return false;} // Get a LONG type data / * Parameters: LPCTSTSTSTSTSTSTSTSTRKEY The key value of the data to be obtained long Lvalue The value of the data to be obtained Return value: Find data to return true, Otherwise, returning false * / bool ccfgdata :: getlongdata (lpctstr strkey, long & lvalue) {cstring str; // Gets the corresponding string data IF (ccfgdata :: getStrData (strkey, str) {LVALUE = ATOI ((LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTST) STR ); Return true;}} // modify or add a data / * parameter: lpctstr strkey to modify the data of the data to modify LPCTSTSTSTSTRVALUE to modify the content return value: Remarks: In the configuration data, the data is modified or added to the corresponding key value. If the current disaster configuration data does not exist, first create the group * / bool ccfgdata :: setData (lpctstr strkey, lpctstr strval) {cmapstringTString * PSTRMAP = NULL; // If there is this group, add or modify if IF (m_strmapmap.lookup (m_strgroup, (void * &) pstrmap) {pstrmap-> setat (strkey, strval); return true;} else {// Create this group PSTRMAP = new cmapstringtostring; m_StrMapMap.Setat (m_strgroup, pstrmap); pstrmap-> setat (strkey, strval); return true;}} / / Save the configuration data to file / * Parameters: lpctstr strfilename Save Name Return Value: Successful Returns Failed to Return false * /
BOOL CCfgData :: SaveCfgData (LPCTSTR strFileName) {CFile file; if (! File.Open (strFileName, CFile :: modeCreate | CFile :: modeWrite)) return FALSE; POSITION pos = m_StrMapMap.GetStartPosition (); char ch [6] = "[] / r / n ="; // Special symbol CString Str [3]; while (pOS) {cmapstringTString * pstrMap; m_strmapmap.getnextassoc (POS, STR [2], (void * &) pstrmap; if (PSTrmap! = null) {/ write group file.write (& ch [0], 1); file.write (LPSTR) STR [2], STR [2] .getLength ()); File .Write (& Ch [1], 1); File.Write (& Ch [2], 2); Position Pos1 = pstrmap-> getStartPosition (); while (pos1) {// write key value and content PSTRMAP-> getNextAssoc (POS1, STR [0], STR [1]); File.Write (LPSTSTR) STR [0], STR [0] .getLength ()); File.Write (& Ch [4], 1) FILE.WRITE (LPCTSTR) STR [1], STR [1] .getLength ()); File.Write (& Ch [2], 2);}}} file.close (); Return True; } // Save the configuration data to the string / * Parameters: cstring * PSTR To save the string pointer return value: successful return True Failed Return False Note: The program process is basically the same, the different saves enter the string * / Bool ccfgdata :: savetostr (cstring * pstr) {if (pstr == null) Return False; * pstr = ""; position pos = m_strm Apmap.getStartPosition (); CString Str [4]; While (POS) {cmapstringtostring * pstrmap; m_StrmapMap.getNextAssoc (POS, STR [2], (Void * &) PSTRMAP; if (pstrmap! = null) {STR [ 3] = * PSTR; PSTR-> Format ("% s [% s] / r / n", str [3], str [2]); position pos1 = pstrmap-> getStartPosition (); while (pos1) { PSTRMAP-> GetNextAssoc (POS1, STR [0], STR [1]); STR [3] = * pstr; pstr-> format ("% s% s =% s / r / n", str [3], Str [0], STR [1]);}}} Return True;}