BaseFun.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 StringToWstring(std::wstring& szDst, std::string str)
  57. {
  58. #ifdef RVC_OS_WIN
  59. std::string temp = str;
  60. int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0);
  61. wchar_t* wszUtf8 = new wchar_t[len + 1];
  62. memset(wszUtf8, 0, len * 2 + 2);
  63. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len);
  64. szDst = wszUtf8;
  65. std::wstring r = wszUtf8;
  66. delete[] wszUtf8;
  67. #else
  68. setlocale(LC_ALL, "chs");
  69. const char* _Source = str.c_str();
  70. size_t _Dsize = str.size() + 1;
  71. wchar_t* _Dest = new wchar_t[_Dsize];
  72. wmemset(_Dest, 0, _Dsize);
  73. mbstowcs(_Dest, _Source, _Dsize);
  74. std::wstring result = _Dest;
  75. delete[]_Dest;
  76. setlocale(LC_ALL, "C");
  77. szDst = result;
  78. #endif
  79. }
  80. //TODO: CrossPlaform [Gifur@2025730]
  81. bool createDir(const string &filePath)
  82. {
  83. #ifdef RVC_OS_WIN
  84. return 0 == _mkdir(filePath.c_str());
  85. #else
  86. if (CreateDirRecursive(filePath.c_str()))
  87. {
  88. return true;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. #endif
  95. }
  96. //TODO: CrossPlaform 采用框架统一的函数 [Gifur@2025728]
  97. BOOL IsDirectory(const char *pDir)
  98. {
  99. char szCurPath[MAX_PATH*2] = {0};
  100. #ifdef RVC_OS_WIN
  101. _snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir);
  102. WIN32_FIND_DATAA FindFileData = {0};
  103. //ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
  104. HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
  105. if (hFile == INVALID_HANDLE_VALUE)
  106. {
  107. FindClose(hFile);
  108. return FALSE; /** 如果不能找到第一个文件,那么没有目录 */
  109. }
  110. else
  111. {
  112. FindClose(hFile);
  113. return TRUE;
  114. }
  115. #else
  116. snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir);
  117. struct stat buf;
  118. if (0 == stat(szCurPath, &buf))
  119. {
  120. return S_ISDIR(buf.st_mode);
  121. }
  122. else
  123. {
  124. return FALSE;
  125. }
  126. #endif // RVC_OS_WIN
  127. }
  128. void getDirs(string path, vector<string> &ownname)
  129. {
  130. #ifdef RVC_OS_WIN
  131. long hFile = 0;
  132. struct _finddata_t fileinfo;
  133. string p;
  134. if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
  135. {
  136. do
  137. {
  138. //如果是目录加入列表
  139. if ((fileinfo.attrib & _A_SUBDIR) && strcmp(fileinfo.name, ".") && strcmp(fileinfo.name, ".."))
  140. ownname.push_back(fileinfo.name);
  141. } while (_findnext(hFile, &fileinfo) == 0);
  142. _findclose(hFile);
  143. }
  144. #else
  145. array_header_t* subdirs = fileutil_get_sub_files(path.c_str());
  146. int i;
  147. for (i = 0; i < subdirs->nelts; ++i) {
  148. char* strsubdir = ARRAY_IDX(subdirs, i, char*);
  149. ownname.push_back(strsubdir);
  150. }
  151. #endif // RVC_OS_WIN
  152. }