Everyone should know that the read configuration file can use the system API getPrivateProfileString () (the prototype is as follows), I originally packaged it into a class, but when I was used today (I didn't have an error before.) I tried a few times or there was no effect, and I wrote one of myself.
DWORD GETPRIVATEPROFILESTRING (LPCTSTSTR LPAPPNAME, LPCTSTR LPKEYNAME, LPCTSTSTR LPDEFAULT, LPTSTSTSTSTSTSTSTSTSTSTER LPRETURNEDSTRING, DWORD NSIZE, LPCTSTSTR LPFILENAME)
Oh, boring people. The factors considered are:
1. Some people like to write Key = Value, and some people like Key = Value, and the intermediate space must be handled.
2. In some configuration files, each section plus an empty line, this is not considered.
3. In order to increase the speed, I reduced the number of read files. I originally thought that when I read a key, the record keys and their key values, but the test results show that it is better to read the fast (sacrifice space To achieve performance - a very routine practice)
The code is posted below, the test passes:
/ ************************************************** ***************************************************************** PrivateProfile.hwritten by DS in 09.03.2005this class is buy to simplified read the profiles **** *********************************************************** *********************** /
#ifndef darkstar_initialize_file_h # Define Darkstar_initialize_file_h # include "AfxColl.h" #include "AfxTempl.h" #include "afx.h"
Class CPrivateProfile {public: CPRIVATEPROFILE (VOID); ~ CPRIVATEPROFILE (VOID);
CPRIVATEPROFILE (LPCTSTR file); int setprofile (LPCTSTR file);
int GetInt (LPCTSTR section, LPCTSTR key, int nDefault = -1); CString GetString (LPCTSTR section, LPCTSTR key, CString const & strDefault = "");
Void getAllKeys (); // void getAllKeys (cmapstringtostring & map); void getsectionKeys (lpctstr section); // void getsectionKeys (LPCTSTSTSTSITION, CMAPSTRINGTOSTRING & MAP);
Int gettotalKeys (); protected: cfile profile;
CLIST
Bool binit;}; # endif // darkstar_initialize_file_h
//PrivateProfile.cpp#include "StdAfx.h" #include "PrivateProfile.h" #include
CPRIVATEPROFILE :: ~ CPRIVATEPROFILE (VOID) {profile.close (); keymap.removeall ();
CPRIVATEPROFILE: CPRIVATEPROFILE (LPCTSTR FILE) {if (! Profile.open (file, cfile :: modeReadwrite) EXIT (-1); keyMap.removeall ();
INT CPRIVATEPROFILE :: SetProfile (LPCTSTR file) {if (! profile.open (file, cfile :: modeReadwrite) Return -1; keyMap.removeall (); getAllKeys (); return 0;}
void CPrivateProfile :: GetAllKeys () {proFile.SeekToBegin (); int size = 2048; char * buf; int len; // Read all context of the file into the memory while (true) {proFile.SeekToBegin (); buf = New char [size]; memset (buf, 0, size); len = profile.read (buf, size); if (len 1 Line.lle (tpos); key.trim (); value = line.right (line.getLength () - TPOS - 1); value.trim (); // cout << "key:" << (lpctstr) Key << "-> value:" << (lpctstr) value << Endl; KeyMap.Setat (key, value);} binit = true;} void cprivateprofile :: getsectionKeys (lpctstr section) {profile.seektobegin (); INT size = 2048; char * buf; int Len; // read all context of the file {TRUE) {profile.seektobe (); buf = new char [size]; MEMSET (BUF, 0, SIZE) Leen = profile (buf, size); if (len INT POS = P - BUF 1; int offset = POS; CSTRING key; cstring value; while (buf [pos]! = 0) {IF (buf [pOS] == '[') // stop, When Ge To The next section break; if (buf [pOS]! = '/ n') {// now line terminator POS ; Continue;} IF (POS == Offset) {// Null line offset ; POS ; Continue;} // line cstring line (buf offset, pos - offset); POS ; offset = POS; line.trim (); int tpos = line .Find ('='); if (-1 == TPOS || 0 == TPOS || line.getlength () - 1 == TPOS) {// invalid position of '=' amount;} key = section; Key = "-"; key = line.Left (tpos); value = line.right (line.getLength () - TPOS - 1); keyMap.set (key, value);} CString CPrivateProfile :: GetString (LPCTSTR section, LPCTSTR key, CString const & strDefault) {if GetAllKeys (); CString s = section; s = '-'; (bInit!) S = key; CString value; if (keyMap. LOOKUP (S, Value) Return Value; Return StrDefault;} INT CPRIVATEPROFILE :: Get (LPCTSTSTSTSTSTSTST (LPCTSTSTSTSTSTSTSTSTESTSTSTSTSTSTESTSTSTR Key, INT NDEFAULT) {if (! binit) getAllKeys (); cstring s = section; s = '-'; s = key; CString Value; if (KeyMap.Lookup (S, Value)) Return ATOI ((LPCTSTR) Value; Return NDEFAULT;} INT CPRIVATEPROFILE :: gettotalKeys () {if (! binit) getAllKeys (); return keyMap.getsize (); The main function of the test was changed n times. Basically, it is testing all kinds of situations, nothing is worth it.