BaseFun.cpp 7.9 KB

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