BaseFun.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "stdafx.h"
  2. #include "BaseFun.h"
  3. #include "array.h"
  4. #ifdef RVC_OS_WIN
  5. #include <ShlDisp.h>
  6. #include <direct.h>
  7. #include <io.h>
  8. #include <string.h>
  9. #else
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #endif // RVC_OS_WIN
  14. #ifndef RVC_MAX_INI_LEN
  15. #define RVC_MAX_INI_LEN 1024*16
  16. #endif // !RVC_MAX_INI_LEN
  17. void split(const string& src, const string& separator, vector<string>& dest)
  18. {
  19. string str = src;
  20. string substring;
  21. string::size_type start = 0, index;
  22. do
  23. {
  24. index = str.find_first_of(separator, start);
  25. if (index != string::npos)
  26. {
  27. substring = str.substr(start, index - start);
  28. dest.push_back(substring);
  29. start = str.find_first_not_of(separator, index);
  30. if (start == string::npos) return;
  31. }
  32. } while (index != string::npos);
  33. //the last token
  34. substring = str.substr(start);
  35. dest.push_back(substring);
  36. }
  37. bool checkDirExist(const string &strPath)
  38. {
  39. if (ExistsDir(strPath.c_str())){
  40. return true;
  41. }
  42. else{
  43. return false;
  44. }
  45. }
  46. bool checkFileExist(string fileName)
  47. {
  48. if (ExistsFile(fileName.c_str())){
  49. return true;
  50. }
  51. else{
  52. return false;
  53. }
  54. }
  55. //TODO: CrossPlaform [Gifur@2025730]
  56. void Wchar_tToString(std::string& szDst, wchar_t *wchar)
  57. {
  58. #ifdef RVC_OS_WIN
  59. wchar_t* wText = wchar;
  60. DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);// WideCharToMultiByte的运用
  61. char* psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
  62. psText = new char[dwNum];
  63. WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);// WideCharToMultiByte的再次运用
  64. szDst = psText;// std::string赋值
  65. delete[]psText;// psText的清除
  66. #else
  67. std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
  68. setlocale(LC_ALL, "chs");
  69. size_t _Dsize = 2 * wcslen(wchar) + 1;
  70. char* _Dest = new char[_Dsize];
  71. memset(_Dest, 0, _Dsize);
  72. wcstombs(_Dest, wchar, _Dsize);
  73. std::string result = _Dest;
  74. delete[]_Dest;
  75. setlocale(LC_ALL, curLocale.c_str());
  76. szDst = result;
  77. #endif
  78. }
  79. //TODO: CrossPlaform [Gifur@2025730]
  80. void StringToWstring(std::wstring& szDst, std::string str)
  81. {
  82. #ifdef RVC_OS_WIN
  83. std::string temp = str;
  84. int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0);
  85. wchar_t* wszUtf8 = new wchar_t[len + 1];
  86. memset(wszUtf8, 0, len * 2 + 2);
  87. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len);
  88. szDst = wszUtf8;
  89. std::wstring r = wszUtf8;
  90. delete[] wszUtf8;
  91. #else
  92. setlocale(LC_ALL, "chs");
  93. const char* _Source = str.c_str();
  94. size_t _Dsize = str.size() + 1;
  95. wchar_t* _Dest = new wchar_t[_Dsize];
  96. wmemset(_Dest, 0, _Dsize);
  97. mbstowcs(_Dest, _Source, _Dsize);
  98. std::wstring result = _Dest;
  99. delete[]_Dest;
  100. setlocale(LC_ALL, "C");
  101. szDst = result;
  102. #endif
  103. }
  104. //TODO: CrossPlaform [Gifur@2025730]
  105. bool createDir(const string &filePath)
  106. {
  107. #ifdef RVC_OS_WIN
  108. return 0 == _mkdir(filePath.c_str());
  109. #else
  110. if (CreateDirRecursive(filePath.c_str()))
  111. {
  112. return true;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. #endif
  119. }
  120. //TODO: CrossPlaform 采用框架统一的函数 [Gifur@2025728]
  121. BOOL IsDirectory(const char *pDir)
  122. {
  123. char szCurPath[MAX_PATH*2] = {0};
  124. #ifdef RVC_OS_WIN
  125. _snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir);
  126. WIN32_FIND_DATAA FindFileData = {0};
  127. //ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
  128. HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
  129. if (hFile == INVALID_HANDLE_VALUE)
  130. {
  131. FindClose(hFile);
  132. return FALSE; /** 如果不能找到第一个文件,那么没有目录 */
  133. }
  134. else
  135. {
  136. FindClose(hFile);
  137. return TRUE;
  138. }
  139. #else
  140. snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir);
  141. struct stat buf;
  142. if (0 == stat(szCurPath, &buf))
  143. {
  144. return S_ISDIR(buf.st_mode);
  145. }
  146. else
  147. {
  148. return FALSE;
  149. }
  150. #endif // RVC_OS_WIN
  151. }
  152. void getDirs(string path, vector<string> &ownname)
  153. {
  154. #ifdef RVC_OS_WIN
  155. long hFile = 0;
  156. struct _finddata_t fileinfo;
  157. string p;
  158. if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
  159. {
  160. do
  161. {
  162. //如果是目录加入列表
  163. if ((fileinfo.attrib & _A_SUBDIR) && strcmp(fileinfo.name, ".") && strcmp(fileinfo.name, ".."))
  164. ownname.push_back(fileinfo.name);
  165. } while (_findnext(hFile, &fileinfo) == 0);
  166. _findclose(hFile);
  167. }
  168. #else
  169. array_header_t* subdirs = fileutil_get_sub_files(path.c_str());
  170. int i;
  171. for (i = 0; i < subdirs->nelts; ++i) {
  172. char* strsubdir = ARRAY_IDX(subdirs, i, char*);
  173. ownname.push_back(strsubdir);
  174. }
  175. #endif // RVC_OS_WIN
  176. }