#include <Shlwapi.h>
#pragma comment (lib , "shlwapi.lib" )
#define nullptr NULL
inline BYTE toHex(const BYTE &x)
{
return x > 9 ? x + 55: x + 48;
}
inline CString UrlEncode(CString sIn)
{
CString sOut;
const int nLen = sIn.GetLength() + 1;
register LPBYTE pOutTmp = NULL;
LPBYTE pOutBuf = NULL;
register LPBYTE pInTmp = NULL;
LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
BYTE b = 0;
//alloc out buffer
pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen * 3];
if(pOutBuf)
{
pInTmp = pInBuf;
pOutTmp = pOutBuf;
// do encoding
while (*pInTmp)
{
if(isalnum(*pInTmp))
*pOutTmp++ = *pInTmp;
else
if(isspace(*pInTmp))
*pOutTmp++ = '+';
else
{
*pOutTmp++ = '%';
*pOutTmp++ = toHex(*pInTmp>>4);
*pOutTmp++ = toHex(*pInTmp%16);
}
pInTmp++;
}
*pOutTmp = '\0';
//sOut=pOutBuf;
//delete [] pOutBuf;
sOut.ReleaseBuffer();
}
sIn.ReleaseBuffer();
return sOut;
}
inline int SplitString(CString & str, TCHAR cTok, CStringArray& aryItem)
{
TCHAR* p = str.GetBuffer(0);
TCHAR* e = p;
TCHAR cEnd = *e;
int nCount = 0;
while (cEnd)
{
if (*e == _T('\0'))
cEnd = *e;
else if (*e == cTok)
*e = _T('\0');
if (*e)
e++;
else
{
if (*p != _T('\0'))
{
aryItem.Add(p);
nCount++;
}
p = ++e;
}
}
return nCount;
}
inline void MakeSureCreateDir(CString basePath, CString path)
{
CStringArray arr;
SplitString(path, '\\', arr);
for (int i = 0; i < arr.GetCount(); i++)
{
basePath += L"\\" + arr[i];
if (!PathFileExists(basePath))
::CreateDirectory(basePath, NULL);
}
}
本文为“技术点滴”的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。