`
tulunta
  • 浏览: 360258 次
文章分类
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics