参数字符串解析类
};
StrParam::StrParam()
{
op = L’;’;
}
void StrParam::SetOp(wchar_t sop)
{
op = sop;
}
bool StrParam::InsertStr2Map(const wstring& str)
{
wstring::size_type myPos = str.find(L’=’);
bool ret = false;
if (myPos != wstring::npos && myPos != str.length()-1)
{
wstring keyStr(str,0,myPos);
wstring valueStr(str,myPos+1);
if (keyStr.length() >0)
{
mdata.insert(make_pair(keyStr,valueStr));
ret = true;
}
}
return ret;
}
bool StrParam::Init(const wstring& initStr)
{
wstring::size_type startPos = 0;
wstring::size_type myPos = wstring::npos;
do
{
myPos = initStr.find(op,startPos);
if (myPos != wstring::npos)
{
wstring tmpStr(initStr,startPos,myPos-startPos);
InsertStr2Map(tmpStr);
startPos = myPos +1;
}
else
{
break;
}
} while (true);
return true;
}
wstring StrParam::GetParam(const wstring& str)
{
wstring res;
map<wstring, wstring>::const_iterator it;
it = mdata.find(str);
if (it != mdata.end())
{
res = it ->second;
}
return res;
}
int StrParam::GetInt(const wstring& str)
{
return _wtoi(GetParam(str).c_str());
}
参数字符串的构建类
void StrParamBuild::SetOp(wchar_t sop)
{
op = sop;
}
void StrParamBuild::InsertStr(const wstring& strKey, const wstring& strValue)
{
if (strValue.length() == 0 || strKey.length() == 0)
{
return;
}
mdata.insert(make_pair(strKey, strValue));
}
int StrParamBuild::size()
{
return mdata.size();
}
void StrParamBuild::InsertInt(const wstring& strKey, int iValue)
{
wchar_t tmp[16];
swprintf(tmp,L”%d”,iValue);
wstring strValue(tmp);
InsertStr(strKey, strValue);
}
wstring StrParamBuild::GetString()
{
map<wstring, wstring>::iterator it = mdata.begin();
wstring res;
wchar_t pbuff[2048];
for (;it != mdata.end(); it++)
{
swprintf(pbuff,L”%s=%s%c”, it->first.c_str(), it->second.c_str(),op);
res += pbuff;
}
return res;
}
#define STR_TEMP_BUFF 2048
wstring StrParamBuild::wstrFormat(LPCWSTR lpszFormat,…)
{
wstring res;
wchar_t szMsg[STR_TEMP_BUFF];
va_list argList;
va_start(argList, lpszFormat);
HRESULT hr;
hr = StringCchVPrintfW(szMsg,STR_TEMP_BUFF,lpszFormat,argList);
if (hr == S_OK || hr == STRSAFE_E_INSUFFICIENT_BUFFER)
{
szMsg[STR_TEMP_BUFF – 1]=0;
res = szMsg;
}
return res;
}
StrParamBuild::StrParamBuild()
{
op = L’;’;
}