文件管理类
判断文件是否存在
BOOL IsFileExist(const CString& strFile)
{
DWORD dwAttrib = GetFileAttributes(strFile);
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
判断文件夹是否存在
BOOL IsDirExist(const CString& strDir)
{
DWORD dwAttrib = GetFileAttributes(strDir);
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
判断文件或文件夹是否存在
BOOL IsPathExist(const CString& strPath)
{
DWORD dwAttrib = GetFileAttributes(strPath);
return INVALID_FILE_ATTRIBUTES != dwAttrib;
}
字符串处理类
字符串分割
void SplitString(CString str, CString spliter, CStringArray& arr)
{
str.Trim();
int pos = str.Find(spliter);
while (pos != -1)
{
CString left = str.Left(pos);
left.Trim();
arr.Add(left);
str = str.Mid(pos+spliter.GetLength());
str.Trim();
pos = str.Find(spliter);
}
if (!str.IsEmpty())
{
arr.Add(str);
}
}
字符串多字节编码转UTF8编码
CString MbcsToUtf8(const char* src)
{
CString str;
WCHAR* pwchar = NULL;
CHAR* pchar = NULL;
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
int len = MultiByteToWideChar(codepage, 0, src, -1, NULL, 0);
pwchar = new WCHAR[len];
if (pwchar == NULL) return str;
len = MultiByteToWideChar(codepage, 0, src, -1, pwchar, len);
if (len != 0)
{
len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, NULL, 0, NULL, NULL);
pchar = new CHAR[len];
if (pchar != NULL)
{
len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, pchar, len, NULL, NULL);
if (len != 0)
{
str = pchar;
}
delete [] pchar;
}
}
delete [] pwchar;
return str;
}
字符串UTF8编码转多字节编码
CString Utf8ToMbcs(const char* src)
{
CString str;
WCHAR* pwchar = NULL;
CHAR* pchar = NULL;
int len = MultiByteToWideChar(CP_UTF8, 0, src, -1, NULL, 0);
pwchar = new WCHAR[len];
if (pwchar == NULL) return str;
len = MultiByteToWideChar(CP_UTF8, 0, src, -1, pwchar, len);
if (len != 0)
{
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
len = WideCharToMultiByte(codepage, 0, pwchar, -1, NULL, 0, NULL, NULL);
pchar = new CHAR[len];
if (pchar != NULL)
{
len = WideCharToMultiByte(codepage, 0, pwchar, -1, pchar, len, NULL, NULL);
if (len != 0)
{
str = pchar;
}
delete [] pchar;
}
}
delete [] pwchar;
return str;
}
CStringA转CStringW
CStringW CStrAToCStrW(const CStringA& strA)
{
CStringW strW;
WCHAR* pwchar = NULL;
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
int len = MultiByteToWideChar(codepage, 0, strA, -1, NULL, 0);
pwchar = new WCHAR[len];
if (pwchar == NULL) return strW;
len = MultiByteToWideChar(codepage, 0, strA, -1, pwchar, len);
if (len != 0)
{
strW.Format(L"%s", pwchar);
}
delete [] pwchar;
return strW;
}
CStringW转CStringA
CStringA CStrWToCStrA(const CStringW& strW)
{
CStringA strA;
CHAR* pchar = NULL;
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
int len = WideCharToMultiByte(codepage, 0, strW, -1, NULL, 0, NULL, NULL);
pchar = new char[len];
if (pchar == NULL) return strA;
len = WideCharToMultiByte(codepage, 0, strW, -1, pchar, len, NULL, NULL);
if (len != 0)
{
strA.Format("%s", pchar);
}
delete [] pchar;
return strA;
}
全角转半角
BOOL FullWidthToHalfWidth(CString& strSrc, CString& strDest)
{
if (strSrc.IsEmpty())
{
return FALSE;
}
int nSrcStrSize = strSrc.GetLength() * sizeof(TCHAR);
int nDestStrSize = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_HALFWIDTH, strSrc, nSrcStrSize, NULL, 0);
PTCHAR lpDestStr;
lpDestStr = new TCHAR[nDestStrSize + 1];
ZeroMemory(lpDestStr, nDestStrSize + 1);
if (LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_HALFWIDTH, strSrc, nSrcStrSize, lpDestStr, nDestStrSize) != 0)
{
strDest = lpDestStr;
return TRUE;
}
else
{
return FALSE;
}
}
将多个空格字符转化为一个空格字符
CString RemoveRedundantSpace(CString strSrc)
{
if (strSrc.IsEmpty())
{
return strSrc;
}
int nIndex = 0;
CStringW strDest = CStrAToCStrW(strSrc);
while (nIndex < strDest.GetLength())
{
if (strDest[nIndex] == _T(' '))
{
do
{
nIndex++;
if (strDest[nIndex] == _T(' '))
{
strDest.Delete(nIndex--);
}
} while (nIndex < strDest.GetLength() && strDest[nIndex] == _T(' '));
}
nIndex++;
}
return CStrWToCStrA(strDest);
}
去除小数点后的零
CString RemoveFloatTailZero(CString strFloat)
{
CString strText = strFloat;
int nIndex = strText.Find(_T('.'));
if (nIndex >= 0)
{
strText.TrimRight('0');
if (strText.GetLength() == nIndex + 1)
{
strText = strText.Left(nIndex);
if (strText.IsEmpty())
strText = _T('0');
}
}
return strText;
}