BaseFun.cpp 4.2 KB

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