CIniParse类并不能在ASCII环境中编译通过。只是wince是UNICODE的系统,所以这点在平时使用中倒不会引起很大的麻烦。当然,如果你是打算移植到VC6.0中,那么可能有一些函数就必须要修正了。再一点就是,该类没有详细地进行测试,所以应该还会有或多或少的问题,如果你发现了,也希望我更正,期待你的指出。
惯例,先罗列出CIniParse的完整代码:
头文件:
//Class Name
// CIniParse
//
//Version:
// 1.0.0
//
//Date:
// 2008.12.22
//
//Author:
// norains
//
#pragma once
#include "windows.h"
#include <string>
#include <vector>
#include <map>
#ifdef UNICODE
#ifndef TSTRING
#define TSTRING std::wstring
#endif
#else
#ifndef TSTRING
#define TSTRING std::string
#endif
#endif
class CIniParse
{
public:
//-------------------------------------------------------------------------------
//Description:
// Open the ini file.
//-------------------------------------------------------------------------------
BOOL Open(TSTRING strFile);
//-------------------------------------------------------------------------------
//Description:
// Get the profile value as string type
//-------------------------------------------------------------------------------
TSTRING GetPrivateProfileString(const TSTRING &strSection,const TSTRING &strKey);
//-------------------------------------------------------------------------------
//Description:
// Get the profile value as int type
//-------------------------------------------------------------------------------
int GetPrivateProfileInt(const TSTRING &strSection,const TSTRING &strKey);
//-------------------------------------------------------------------------------
//Description:
// Set the profile value as string type.The function wouldn't save the data to file
//but memory. If you want to save to the file, you must call Flush function
//-------------------------------------------------------------------------------
BOOL SetPrivateProfileString(const TSTRING &strSection,const TSTRING &strKey,TSTRING strSet);
//-------------------------------------------------------------------------------
//Description:
// Set the profile value as int type.The function wouldn't save the data to file
//but memory. If you want to save to the file, you must call Flush function
//-------------------------------------------------------------------------------
BOOL SetPrivateProfileInt(const TSTRING &strSection,const TSTRING &strKey,int iSet);
//-------------------------------------------------------------------------------
//Description:
// Flush the memory buffer to the file
//-------------------------------------------------------------------------------
BOOL Flush();
public:
CIniParse();
virtual ~CIniParse();
private:
//-------------------------------------------------------------------------------
//Description:
// Get the line value base on the current offset. After calling, the offset value
//would move to the head of next line
//-------------------------------------------------------------------------------
TSTRING GetLine();
//-------------------------------------------------------------------------------
//Description:
// Check the string value of one line whether is comment or not
//-------------------------------------------------------------------------------
static BOOL IsCommentLine(const TSTRING & strLine);
//-------------------------------------------------------------------------------
//Description:
// Check the string value of one line whether is the section.
//Parameters:
// strLine : [in] The string value buffer.
//-------------------------------------------------------------------------------
static BOOL IsSectionLine(const TSTRING & strLine);
//-------------------------------------------------------------------------------
//Description:
// Get the key value from one line
//Parameters:
// strLine : [in] The buffer to find
//-------------------------------------------------------------------------------
static TSTRING GetKeyValueFromLine(const TSTRING & strLine);
//-------------------------------------------------------------------------------