C++ STL string 格式化方法以及读取配置文件的实现

#ifndef _READCONFIG_H_
#define _READCONFIG_H_

#include <string>
#include <map>
#include <vector>
#include <iostram>
#include <fstream>

#define MAX_BUFFLEN 1024

typedef map< string, string, less<string> > strMap;
typedef strMap::iterator strmapIt;

const char * const MIDDLESTRING = "__**__";

using namespace std;

// 此类用于格式化string字符串
class SString : public string
{
public:

SString &Format(const char *_format, ...)
{
char szBuffer[MAX_BUFFLEN];
memset(szBuffer, 0x00, sizeof(szBuffer));

va_list ap;
va_start(ap, _format);

try
{
vsnprintf(szBuffer, MAX_BUFFLEN, _format, ap);
}
catch(...)
{
cout<<"ERROR: format the string failed..."<<endl;
return *this;
}

va_end(ap);
this->append(szBuffer);
return *this;
}

};

// 该类用于根据传入的数据,将数据存入map中
class CAnalyzeValue
{
private:
string m_strSect;
strMap *m_pMap;

public:
CAnalyzeValue(strMap &strmap):m_pMap(&strmap)
{
m_strSect = "";
}

~CAnalyzeValue()
{

}

void operator () (const string &strInput) const
{
int iFirst = strInput.find('[');
int iLast = strInput.find(']');

if (iFirst != string::npos && iLast != string::npos && iFirst != iLast + 1)
{
m_strSect = strInput.substr(iFirst + 1, iLast - iFirst - 1);
return;
}

if (m_strSect.empty())
{
return;
}

iFirst = strInput.find('=');
if (iFirst == string::npos)
{
return;
}

string strTemp1 = strInput.substr(0, iFirst);
string strTemp2 = strInput.substr(iFirst + 1, string::npos);

iFirst = strTemp1.find_first_not_of(" /t");
iLast = strTemp1.find_last_not_of(" /t");
if (iFirst == string::npos || iLast == string::npos)
{
return;
}

string strKey = strTemp1.substr(iFirst + 1, iLast - iFirst + 1);
iFirst = strTemp2.find_first_not_of(" /t");
if (((iLast = strTemp2.find("/t#", iFirst)) != string::npos) ||
((iLast = strTemp2.find(" #", iFirst)) != string::npos) ||
((iLast = strTemp2.find("/t//", iFirst)) != string::npos) ||
((iLast = strTemp2.find(" //", iFirst)) != string::npos))
{
strTemp2 = strTemp2.substr(0, iLast - iFirst);
}

iLast = strTemp2.find_last_not_of(" /t");
if (iFirst == string::npos || iLast == string::npos)
{
return;
}

string strValue = strTemp2.substr(iFirst, iLast - iFirst + 1);
string strMapkey = m_strSect + MIDDLESTRING;
strMapkey += strKey;
(*m_pMap)[strMapkey] = strValue;
return;

}
};

// 此类用于读取配置文件
class CReadConfig
{
public:
CReadConfig()
{
}

~CReadConfig()
{
}

bool OpenFile(const char *FileName)
{
return
}

string ReadCfg(const char * pSect, const char * pKey)
{
string strMapKey = pSect;
strMapKey += MIDDLESTRING;
strMapKey += pKey;

strmapIt iter;

iter = mapcfg.find(strMapKey);

if (iter == mapcfg.end())
{
return "";
}
else
{
return iter->second;
}
}

protected:

bool Do_OpenFile(const char *FileName)
{
ifstream fin(FileName);

if (!fin.is_open())
{
return false;
}

vector<string> strVect;

while (!fin.eof())
{
string strInput = "";
getline(fin, strInput, '/n');

strVect.push_back(strInput);
}

if (strVect.empty())
{
return false;
}

for_each(strVect.begin(), strVect.end(), CAnalyzeValue(mapcfg));
}

private:
strMap mapcfg;
};

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值