//******************************************************************************************************
// string functions
//******************************************************************************************************
//------------------------------------------------------------------------------------------------------
// CompareStringWithoutCase
//------------------------------------------------------------------------------------------------------
UTIL_API int CompareStringWithoutCase(const TSTRING & s1, const TSTRING & s2)
{
return _tcsicmp (s1.c_str(), s2.c_str());
}
//------------------------------------------------------------------------------------------------------
// FindStringWithoutCase
//------------------------------------------------------------------------------------------------------
UTIL_API size_t FindStringWithoutCase(const TSTRING & s1, const TSTRING & s2)
{
TSTRING sLower1 = stolower(s1);
TSTRING sLower2 = stolower(s2);
return sLower1.find(sLower2);
}
//------------------------------------------------------------------------------------------------------
// stoupper
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING stoupper(const TSTRING & s)
{
TSTRING upper(s);
for(size_t i=0; i<s.length(); i++)
upper[i] = _totupper(upper[i]);
return upper;
}
//------------------------------------------------------------------------------------------------------
// stolower
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING stolower(const TSTRING & s)
{
TSTRING lower(s);
for(size_t i=0; i<s.length(); i++)
lower[i] = _totlower(lower[i]);
return lower;
}
//------------------------------------------------------------------------------------------------------
// srtrim
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING srtrim(TSTRING & s)
{
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun(::isspace))).base(),
s.end());
return s;
}
//------------------------------------------------------------------------------------------------------
// sltrim
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING sltrim(TSTRING & s)
{
s.erase(s.begin(),std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun(::isspace))));
return s;
}
//------------------------------------------------------------------------------------------------------
// strim
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING strim(TSTRING & s)
{
return sltrim(srtrim(s));
}
//------------------------------------------------------------------------------------------------------
// GetRidOfDoubleQuotationMarks
//------------------------------------------------------------------------------------------------------
UTIL_API void GetRidOfDoubleQuotationMarks(TSTRING & s)
{
if (s.find(_T("\"")) == 0)
s = s.substr(1);
if (s.find(_T("\"")) == s.length() - 1)
s = s.substr(0, s.length() - 1);
}
//------------------------------------------------------------------------------------------------------
// Wstring2Char
//------------------------------------------------------------------------------------------------------
UTIL_API char * Wstring2Char(const wstring wstr, char* tch)
{
UINT codepage = GetACP();
int n = 0;
n = WideCharToMultiByte(codepage, 0, wstr.c_str(), -1, tch, MAX_PATH_IN_BYTES, NULL,NULL);
tch[n] = '\0';
return tch;
}
//------------------------------------------------------------------------------------------------------
// Bstr2String
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING Bstr2String(const BSTR bstr, TSTRING & sString)
{
TCHAR *tch = NULL;
try
{
size_t iLen = _tcslen(bstr) + 1;
tch = new TCHAR[iLen];
if (NULL != tch)
{
memset(tch, 0,iLen);
#ifdef UNICODE
_tcscpy(tch, bstr);
#else
UINT codepage = GetACP();
int n = WideCharToMultiByte(codepage, 0, bstr, -1, tch, MAX_PATH_IN_BYTES, NULL,NULL);
tch[n] = '\0';
#endif
sString = tch;
delete tch;
}
}
catch(...)
{
if (tch)
delete tch;
}
return sString;
}
//------------------------------------------------------------------------------------------------------
// String2Bstr
//------------------------------------------------------------------------------------------------------
UTIL_API BSTR String2Bstr(const TSTRING & sString, BSTR bstr)
{
#ifdef UNICODE
_tcscpy(bstr, sString.c_str());
#else
UINT codepage = GetACP();
int n = 0;
n = MultiByteToWideChar(codepage, 0, sString.c_str(), -1, bstr, MAX_PATH_IN_BYTES);
bstr[n] = '\0';
#endif
return bstr;
}
//------------------------------------------------------------------------------------------------------
// Int2String
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING Int2String(const int iValue)
{
TSTRING sString;
return Int2String(iValue, sString);;
}
//------------------------------------------------------------------------------------------------------
// Int2String
//------------------------------------------------------------------------------------------------------
UTIL_API TSTRING Int2String(const int iValue, TSTRING & sString)
{
TCHAR tchValue[20000] = {0};
_itot(iValue, tchValue, 10);
sString = tchValue;
return sString;
}
//------------------------------------------------------------------------------------------------------
// Replace
//------------------------------------------------------------------------------------------------------
UTIL_API void Replace(TSTRING & sSource, const TSTRING & sOld, const TSTRING & sNew)
{
//basic_string.find will return 0 if the string is ""
if (sOld.empty())
return;
TSTRING sLine = sSource;
TSTRING sResult;
size_t iPos = NPOS;
while (TRUE)
{
iPos = FindStringWithoutCase(sLine, sOld);
if (iPos == NPOS)
{
//get suffix file description
sResult += sLine;
break;
}
//get pre file description
sResult = sResult + sLine.substr(0, iPos) + sNew;
sLine = sLine.substr(iPos + sOld.length());
}
sSource = sResult;
}