BaseFun.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #else
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #endif // RVC_OS_WIN
  13. bool Unzip2Folder(BSTR lpZipFile, BSTR lpFolder)
  14. {
  15. #ifdef RVC_OS_WIN
  16. IShellDispatch* pISD;
  17. Folder* pZippedFile = 0L;
  18. Folder* pDestination = 0L;
  19. long FilesCount = 0;
  20. IDispatch* pItem = 0L;
  21. FolderItems* pFilesInside = 0L;
  22. VARIANT Options, OutFolder, InZipFile, Item;
  23. CoInitialize(NULL);
  24. __try {
  25. if (CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)& pISD) != S_OK)
  26. return 1;
  27. InZipFile.vt = VT_BSTR;
  28. InZipFile.bstrVal = lpZipFile;
  29. pISD->NameSpace(InZipFile, &pZippedFile);
  30. if (!pZippedFile)
  31. {
  32. pISD->Release();
  33. return 1;
  34. }
  35. OutFolder.vt = VT_BSTR;
  36. OutFolder.bstrVal = lpFolder;
  37. pISD->NameSpace(OutFolder, &pDestination);
  38. if (!pDestination)
  39. {
  40. pZippedFile->Release();
  41. pISD->Release();
  42. return 1;
  43. }
  44. pZippedFile->Items(&pFilesInside);
  45. if (!pFilesInside)
  46. {
  47. pDestination->Release();
  48. pZippedFile->Release();
  49. pISD->Release();
  50. return 1;
  51. }
  52. pFilesInside->get_Count(&FilesCount);
  53. if (FilesCount < 1)
  54. {
  55. pFilesInside->Release();
  56. pDestination->Release();
  57. pZippedFile->Release();
  58. pISD->Release();
  59. return 0;
  60. }
  61. pFilesInside->QueryInterface(IID_IDispatch, (void**)& pItem);
  62. Item.vt = VT_DISPATCH;
  63. Item.pdispVal = pItem;
  64. Options.vt = VT_I4;
  65. Options.lVal = 1024 | 512 | 16 | 4;
  66. bool retval = pDestination->CopyHere(Item, Options) == S_OK;
  67. pItem->Release(); pItem = 0L;
  68. pFilesInside->Release(); pFilesInside = 0L;
  69. pDestination->Release(); pDestination = 0L;
  70. pZippedFile->Release(); pZippedFile = 0L;
  71. pISD->Release(); pISD = 0L;
  72. return retval;
  73. }
  74. __finally
  75. {
  76. CoUninitialize();
  77. }
  78. #else
  79. return true;
  80. #endif
  81. }
  82. void split(const string& src, const string& separator, vector<string>& dest)
  83. {
  84. string str = src;
  85. string substring;
  86. string::size_type start = 0, index;
  87. do
  88. {
  89. index = str.find_first_of(separator, start);
  90. if (index != string::npos)
  91. {
  92. substring = str.substr(start, index - start);
  93. dest.push_back(substring);
  94. start = str.find_first_not_of(separator, index);
  95. if (start == string::npos) return;
  96. }
  97. } while (index != string::npos);
  98. //the last token
  99. substring = str.substr(start);
  100. dest.push_back(substring);
  101. }
  102. bool checkDirExist(const string &strPath)
  103. {
  104. #ifdef RVC_OS_WIN
  105. WIN32_FIND_DATA wfd;
  106. bool rValue = false;
  107. HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
  108. if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  109. rValue = true;
  110. FindClose(hFind);
  111. return rValue;
  112. #else
  113. if (ExistsDir(strPath.c_str()))
  114. {
  115. return true;
  116. }
  117. else
  118. {
  119. return false;
  120. }
  121. #endif
  122. }
  123. bool checkFileExist(string fileName)
  124. {
  125. #ifdef RVC_OS_WIN
  126. WIN32_FIND_DATA FindFileData;
  127. HANDLE hFind;
  128. hFind = FindFirstFile(fileName.c_str(), &FindFileData);
  129. if (hFind == INVALID_HANDLE_VALUE)
  130. return false;
  131. FindClose(hFind);
  132. return true;
  133. #else
  134. if (ExistsFile(fileName.c_str()))
  135. {
  136. return true;
  137. }
  138. else
  139. {
  140. return false;
  141. }
  142. #endif
  143. }
  144. void Wchar_tToString(std::string& szDst, wchar_t *wchar)
  145. {
  146. #ifdef RVC_OS_WIN
  147. wchar_t* wText = wchar;
  148. DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);// WideCharToMultiByte的运用
  149. char* psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
  150. psText = new char[dwNum];
  151. WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);// WideCharToMultiByte的再次运用
  152. szDst = psText;// std::string赋值
  153. delete[]psText;// psText的清除
  154. #else
  155. std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
  156. setlocale(LC_ALL, "chs");
  157. size_t _Dsize = 2 * wcslen(wchar) + 1;
  158. char* _Dest = new char[_Dsize];
  159. memset(_Dest, 0, _Dsize);
  160. wcstombs(_Dest, wchar, _Dsize);
  161. std::string result = _Dest;
  162. delete[]_Dest;
  163. setlocale(LC_ALL, curLocale.c_str());
  164. szDst = result;
  165. #endif
  166. }
  167. // string to wstring
  168. void StringToWstring(std::wstring& szDst, std::string str)
  169. {
  170. #ifdef RVC_OS_WIN
  171. std::string temp = str;
  172. int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0);
  173. wchar_t* wszUtf8 = new wchar_t[len + 1];
  174. memset(wszUtf8, 0, len * 2 + 2);
  175. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len);
  176. szDst = wszUtf8;
  177. std::wstring r = wszUtf8;
  178. delete[] wszUtf8;
  179. #else
  180. setlocale(LC_ALL, "chs");
  181. const char* _Source = str.c_str();
  182. size_t _Dsize = str.size() + 1;
  183. wchar_t* _Dest = new wchar_t[_Dsize];
  184. wmemset(_Dest, 0, _Dsize);
  185. mbstowcs(_Dest, _Source, _Dsize);
  186. std::wstring result = _Dest;
  187. delete[]_Dest;
  188. setlocale(LC_ALL, "C");
  189. szDst = result;
  190. #endif
  191. }
  192. bool createDir(const string &filePath)
  193. {
  194. #ifdef RVC_OS_WIN
  195. return 0 == _mkdir(filePath.c_str());
  196. #else
  197. if (CreateDirRecursive(filePath.c_str()))
  198. {
  199. return true;
  200. }
  201. else
  202. {
  203. return false;
  204. }
  205. #endif
  206. }
  207. BOOL IsDirectory(const char *pDir)
  208. {
  209. char szCurPath[500] = {0};
  210. sprintf_s(szCurPath, 500, "%s//*", pDir);
  211. #ifdef RVC_OS_WIN
  212. WIN32_FIND_DATAA FindFileData;
  213. ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
  214. HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
  215. if (hFile == INVALID_HANDLE_VALUE)
  216. {
  217. FindClose(hFile);
  218. return FALSE; /** 如果不能找到第一个文件,那么没有目录 */
  219. }
  220. else
  221. {
  222. FindClose(hFile);
  223. return TRUE;
  224. }
  225. #else
  226. struct stat buf;
  227. if (0 == stat(szCurPath, &buf))
  228. {
  229. return S_ISDIR(buf.st_mode);
  230. }
  231. else
  232. {
  233. return FALSE;
  234. }
  235. #endif // RVC_OS_WIN
  236. }
  237. BOOL DeleteDirectory(const char * DirName)
  238. {
  239. #ifdef RVC_OS_WIN
  240. // CFileFind tempFind; //声明一个CFileFind类变量,以用来搜索
  241. char szCurPath[MAX_PATH]; //用于定义搜索格式
  242. _snprintf(szCurPath, MAX_PATH, "%s//*.*", DirName); //匹配格式为*.*,即该目录下的所有文件
  243. WIN32_FIND_DATAA FindFileData;
  244. ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
  245. HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData);
  246. BOOL IsFinded = TRUE;
  247. while (IsFinded)
  248. {
  249. IsFinded = FindNextFileA(hFile, &FindFileData); //递归搜索其他的文件
  250. if (strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) //如果不是"." ".."目录
  251. {
  252. std::string strFileName = "";
  253. strFileName = strFileName + DirName + "//" + FindFileData.cFileName;
  254. std::string strTemp;
  255. strTemp = strFileName;
  256. if (IsDirectory(strFileName.c_str())) //如果是目录,则递归地调用
  257. {
  258. printf("目录为:%s/n", strFileName.c_str());
  259. DeleteDirectory(strTemp.c_str());
  260. }
  261. else
  262. {
  263. DeleteFileA(strTemp.c_str());
  264. }
  265. }
  266. }
  267. FindClose(hFile);
  268. BOOL bRet = RemoveDirectoryA(DirName);
  269. if (bRet == 0) //删除目录
  270. {
  271. printf("删除%s目录失败!/n", DirName);
  272. return FALSE;
  273. }
  274. return TRUE;
  275. #else
  276. return RemoveDirRecursive(DirName);
  277. #endif // RVC_OS_WIN
  278. }
  279. bool removeDir(const string &filePaht)
  280. {
  281. return DeleteDirectory(filePaht.c_str());
  282. }
  283. void stopForDebug()
  284. {
  285. #ifdef RVC_OS_WIN
  286. DWORD processId = GetCurrentProcessId();
  287. char showMsg[100] = "";
  288. sprintf_s(showMsg, "Current pross id is %d", processId);
  289. MessageBox(NULL, showMsg, NULL, 0);
  290. #else
  291. #endif // RVC_OS_WIN
  292. }
  293. void getDirs(string path, vector<string> &ownname)
  294. {
  295. #ifdef RVC_OS_WIN
  296. long hFile = 0;
  297. struct _finddata_t fileinfo;
  298. string p;
  299. if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
  300. {
  301. do
  302. {
  303. //如果是目录加入列表
  304. if ((fileinfo.attrib & _A_SUBDIR) && strcmp(fileinfo.name, ".") && strcmp(fileinfo.name, ".."))
  305. ownname.push_back(fileinfo.name);
  306. } while (_findnext(hFile, &fileinfo) == 0);
  307. _findclose(hFile);
  308. }
  309. #else
  310. array_header_t* subdirs = fileutil_get_sub_files(path.c_str());
  311. int i;
  312. for (i = 0; i < subdirs->nelts; ++i) {
  313. char* strsubdir = ARRAY_IDX(subdirs, i, char*);
  314. ownname.push_back(strsubdir);
  315. }
  316. #endif // RVC_OS_WIN
  317. }
  318. bool getUniqueDir(string path, string &dirName)
  319. {
  320. vector<string> dirs;
  321. getDirs(path, dirs);
  322. if (1 == dirs.size())
  323. {
  324. dirName = dirs[0];
  325. return true;
  326. }
  327. return false;
  328. }