123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #include "stdafx.h"
- #include <winbase.h>
- #include <algorithm>
- #include <assert.h>
- #include "CommonFun.h"
- namespace CCommonFun
- {
- void Tokenize(const std::wstring& str,
- std::vector<std::wstring>& 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();
- }
- }
- }
|