123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #include "pch.h"
- #include "CUtils.h"
- CUtils::CUtils()
- {
-
- }
- CUtils::~CUtils()
- {
-
- }
- int CUtils::MultiByToUtf8(const char* multi, char*& utf8)
- {
- int size = 0;
- size = MultiByteToWideChar(CP_ACP, NULL, multi, -1, NULL, 0);
- wchar_t* buff = NULL;
- buff = (wchar_t*)malloc(sizeof(wchar_t) * (size + 1));
- if (!buff)
- {
- return 0;
- }
- wmemset(buff, 0, size + 1);
- MultiByteToWideChar(CP_ACP, NULL, multi, -1, buff, size);
- int len = 0;
- len = WideCharToMultiByte(CP_UTF8, NULL, buff, size, NULL, 0, NULL, NULL);
- utf8 = NULL;
- utf8 = (char*)malloc(sizeof(char) * (len + 1));
- if (!utf8)
- {
- free(buff);
- return 0;
- }
- memset(utf8, 0, len + 1);
- WideCharToMultiByte(CP_UTF8, NULL, buff, size, utf8, len, NULL, NULL);
- free(buff);
- return len;
- }
- int CUtils::Utf8ToMultiBy(const char* utf8, char*& multi)
- {
- int size = 0;
- size = MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, NULL, 0);
- wchar_t* buff = NULL;
- buff = (wchar_t*)malloc(sizeof(wchar_t) * (size + 1));
- if (!buff)
- {
- return 0;
- }
- wmemset(buff, 0, size + 1);
- MultiByteToWideChar(CP_ACP, NULL, utf8, -1, buff, size);
- int len = 0;
- len = WideCharToMultiByte(CP_ACP, NULL, buff, size, NULL, 0, NULL, NULL);
- multi = NULL;
- multi = (char*)malloc(sizeof(char) * (len + 1));
- if (!multi)
- {
- free(buff);
- return 0;
- }
- memset(multi, 0, len + 1);
- WideCharToMultiByte(CP_ACP, NULL, buff, size, multi, len, NULL, NULL);
- free(buff);
- return len;
- }
- //utf8תunicode
- int CUtils::Utf8ToUnicode(const char* utf8, wchar_t*& unicode)
- {
- int len = 0;
- len = MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, NULL, 0);
- unicode = NULL;
- unicode = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1));
- if (!unicode)
- {
- return 0;
- }
- wmemset(unicode, 0, len + 1);
- MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, unicode, len);
- return len;
- }
- //unicodeתutf8
- int CUtils::UnicodeToUtf8(const wchar_t* unicode, char*& utf8)
- {
- int len = 0;
- len = WideCharToMultiByte(CP_UTF8, NULL, unicode, -1, NULL, 0, NULL, NULL);
- utf8 = NULL;
- utf8 = (char*)malloc(sizeof(char) * (len + 1));
- memset(utf8, 0, len + 1);
- if (!utf8)
- {
- return 0;
- }
- WideCharToMultiByte(CP_UTF8, NULL, unicode, -1, utf8, len, NULL, NULL);
- return len;
- }
- //¶à×Ö½Úתunicode
- int CUtils::MultiByToUnicode(const char* multi, wchar_t*& unicode)
- {
- int len = 0;
- len = MultiByteToWideChar(CP_ACP, NULL, multi, -1, NULL, 0);
- unicode = NULL;
- unicode = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1));
- if (!unicode)
- {
- return 0;
- }
- wmemset(unicode, 0, len + 1);
- MultiByteToWideChar(CP_ACP, NULL, multi, -1, unicode, len);
- return len;
- }
- //unicodeת¶à×Ö½Ú
- int CUtils::UnicodeToMultiBy(const wchar_t* unicode, char*& multi)
- {
- int len = 0;
- len = WideCharToMultiByte(CP_ACP, NULL, unicode, -1, NULL, 0, NULL, NULL);
- multi = NULL;
- multi = (char*)malloc(sizeof(char) * (len + 1));
- memset(multi, 0, len + 1);
- if (!multi)
- {
- return 0;
- }
- WideCharToMultiByte(CP_ACP, NULL, unicode, -1, multi, len, NULL, NULL);
- return len;
- }
- int CUtils::fnmatch_win(const char* pattern, const char* name, int flags)
- {
- if (SafeFNMatch(pattern, strlen(pattern), name, strlen(name)))
- return 0;
- else
- return FNM_NOMATCH;
- }
- /**copy from Google-glog*/
- bool CUtils::SafeFNMatch(const char* pattern, size_t patt_len, const char* str, size_t str_len)
- {
- size_t p = 0;
- size_t s = 0;
- while (1)
- {
- if (p == patt_len && s == str_len)
- return true;
- if (p == patt_len)
- return false;
- if (s == str_len)
- return p + 1 == patt_len && pattern[p] == '*';
- if (pattern[p] == str[s] || pattern[p] == '?')
- {
- p += 1;
- s += 1;
- continue;
- }
- if (pattern[p] == '*')
- {
- if (p + 1 == patt_len) return true;
- do
- {
- if (SafeFNMatch(pattern + (p + 1), patt_len - (p + 1), str + s, str_len - s))
- {
- return true;
- }
- s += 1;
- } while (s != str_len);
- return false;
- }
- return false;
- }
- }
- std::string CUtils::CvtA2A(const
- std::string& str, unsigned int cpFrom/*=CP_UTF8*/, unsigned int cpTo/*=CP_ACP*/)
- {
- if (cpTo == cpFrom)
- return str;
- std::wstring strw = CvtA2W(str, cpFrom);
- return CvtW2A(strw, cpTo);
- }
- std::wstring CUtils::CvtW2W(const std::wstring& str, unsigned int cp)
- {
- return str;
- }
- std::wstring CUtils::CvtA2W(const std::string& str, unsigned int cp, unsigned int cp2)
- {
- UNREFERENCED_PARAMETER(cp2);
- wchar_t szBuf[1024];
- int nRet = MultiByteToWideChar(cp, 0, str.c_str(), str.length(), szBuf, 1024);
- if (nRet > 0)
- {
- return std::wstring(szBuf, nRet);
- }
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- int nRet = MultiByteToWideChar(cp, 0, str.c_str(), str.length(), NULL, 0);
- if (nRet > 0)
- {
- wchar_t* pBuf = new wchar_t[nRet];
- MultiByteToWideChar(cp, 0, str.c_str(), str.length(), pBuf, nRet);
- std::wstring strRet(pBuf, nRet);
- delete[]pBuf;
- return strRet;
- }
- }
- return L"";
- }
- std::string CUtils::CvtW2A(const std::wstring& str, unsigned int cp/*=CP_ACP*/)
- {
- char szBuf[1024];
- int nRet = WideCharToMultiByte(cp, 0, str.c_str(), str.length(), szBuf, 1024, NULL, NULL);
- if (nRet > 0) return std::string(szBuf, nRet);
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
- {
- int nRet = WideCharToMultiByte(cp, 0, str.c_str(), str.length(), NULL, 0, NULL, NULL);
- if (nRet > 0)
- {
- char* pBuf = new char[nRet];
- WideCharToMultiByte(cp, 0, str.c_str(), str.length(), pBuf, nRet, NULL, NULL);
- std::string strRet(pBuf, nRet);
- delete[]pBuf;
- return strRet;
- }
- }
- return "";
- }
|