CommonFun.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "stdafx.h"
  2. #include <winbase.h>
  3. #include <algorithm>
  4. #include <assert.h>
  5. #include "CommonFun.h"
  6. namespace CCommonFun
  7. {
  8. void Tokenize(const std::wstring& str,
  9. std::vector<std::wstring>& tokens,
  10. const std::wstring& delimiters)
  11. {
  12. // Skip delimiters at beginning.
  13. std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0);
  14. // Find first "non-delimiter".
  15. std::wstring::size_type pos = str.find_first_of(delimiters, lastPos);
  16. while (std::wstring::npos != pos || std::wstring::npos != lastPos)
  17. {
  18. // Found a token, add it to the vector.
  19. tokens.push_back(str.substr(lastPos, pos - lastPos));
  20. // Skip delimiters. Note the "not_of"
  21. lastPos = str.find_first_not_of(delimiters, pos);
  22. // Find next "non-delimiter"
  23. pos = str.find_first_of(delimiters, lastPos);
  24. }
  25. }
  26. std::string wc2mb( const wchar_t* wcstr)
  27. {
  28. std::string strVal;
  29. int size = WideCharToMultiByte(CP_UTF8, 0, wcstr, -1, NULL, 0, NULL, NULL);
  30. char* mbstr = new char[size+1];
  31. if (mbstr)
  32. {
  33. memset(mbstr, 0, size * sizeof(char));
  34. int ret = WideCharToMultiByte(CP_UTF8, 0, wcstr, -1, mbstr, size, NULL, NULL);
  35. if (ret != 0) // MultiByteToWideChar returns 0 if it does not succeed.
  36. {
  37. strVal = mbstr;
  38. }
  39. delete[] mbstr;
  40. mbstr = NULL;
  41. }
  42. return strVal;
  43. }
  44. std::wstring mb2wc(const char* mbstr)
  45. {
  46. std::wstring strVal;
  47. int size = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, NULL, 0);
  48. wchar_t* wcstr = new wchar_t[size+1];
  49. if (wcstr)
  50. {
  51. memset(wcstr, 0, size * sizeof(wchar_t));
  52. int ret = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, wcstr, size);
  53. if (ret != 0) // MultiByteToWideChar returns 0 if it does not succeed.
  54. {
  55. strVal = wcstr;
  56. }
  57. delete[] wcstr;
  58. wcstr = NULL;
  59. }
  60. return strVal;
  61. }
  62. std::string GetAppPath(void)
  63. {
  64. char FullPath[MAX_PATH];
  65. std::string strAppPath = "";
  66. memset(FullPath,0,MAX_PATH);
  67. int nPos = GetModuleFileNameA(NULL , FullPath, MAX_PATH);
  68. if (nPos > 0)
  69. {
  70. for (int i=nPos;i>=0;i--)
  71. {
  72. if (FullPath[i] != '\\')
  73. {
  74. FullPath[i] = 0;
  75. }
  76. else
  77. {
  78. FullPath[i] = 0;
  79. break;
  80. }
  81. }
  82. }
  83. strAppPath = FullPath;
  84. return strAppPath;
  85. }
  86. std::wstring GetAppPathW(void)
  87. {
  88. std::string strPath = GetAppPath();
  89. std::wstring wstrPath = mb2wc(strPath.c_str());
  90. return wstrPath;
  91. }
  92. //ÔÝÍ£µÈ´ý
  93. void Pausetime( int dwSpan )
  94. {
  95. DWORD dtstart,dwnow;
  96. dtstart = dwnow = ::GetTickCount();
  97. assert(0);//don't use like this.
  98. while((int)(dwnow - dtstart) < dwSpan)
  99. {
  100. MSG msg;
  101. while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  102. {
  103. ::TranslateMessage(&msg);
  104. ::DispatchMessage(&msg);
  105. }
  106. dwnow = ::GetTickCount();
  107. }
  108. }
  109. }