BaseFun.cpp 8.2 KB

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