#include "stdafx.h" #include #include #include #include "CommonFun.h" namespace CCommonFun { void Tokenize(const std::wstring& str, std::vector& tokens, const std::wstring& delimiters) { // Skip delimiters at beginning. std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); while (std::wstring::npos != pos || std::wstring::npos != lastPos) { // Found a token, add it to the vector. tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } } std::string wc2mb( const wchar_t* wcstr) { std::string strVal; int size = WideCharToMultiByte(CP_UTF8, 0, wcstr, -1, NULL, 0, NULL, NULL); char* mbstr = new char[size+1]; if (mbstr) { memset(mbstr, 0, size * sizeof(char)); int ret = WideCharToMultiByte(CP_UTF8, 0, wcstr, -1, mbstr, size, NULL, NULL); if (ret != 0) // MultiByteToWideChar returns 0 if it does not succeed. { strVal = mbstr; } delete[] mbstr; mbstr = NULL; } return strVal; } std::wstring mb2wc(const char* mbstr) { std::wstring strVal; int size = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, NULL, 0); wchar_t* wcstr = new wchar_t[size+1]; if (wcstr) { memset(wcstr, 0, size * sizeof(wchar_t)); int ret = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, wcstr, size); if (ret != 0) // MultiByteToWideChar returns 0 if it does not succeed. { strVal = wcstr; } delete[] wcstr; wcstr = NULL; } return strVal; } std::string GetAppPath(void) { char FullPath[MAX_PATH]; std::string strAppPath = ""; memset(FullPath,0,MAX_PATH); int nPos = GetModuleFileNameA(NULL , FullPath, MAX_PATH); if (nPos > 0) { for (int i=nPos;i>=0;i--) { if (FullPath[i] != '\\') { FullPath[i] = 0; } else { FullPath[i] = 0; break; } } } strAppPath = FullPath; return strAppPath; } std::wstring GetAppPathW(void) { std::string strPath = GetAppPath(); std::wstring wstrPath = mb2wc(strPath.c_str()); return wstrPath; } //ÔÝÍ£µÈ´ý void Pausetime( int dwSpan ) { DWORD dtstart,dwnow; dtstart = dwnow = ::GetTickCount(); assert(0);//don't use like this. while((int)(dwnow - dtstart) < dwSpan) { MSG msg; while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } dwnow = ::GetTickCount(); } } }