#include "stdafx.h" #include "BaseFun.h" #include "array.h" #ifdef RVC_OS_WIN #include #include #include #include #else #include #include #include #endif // RVC_OS_WIN #ifndef RVC_MAX_INI_LEN #define RVC_MAX_INI_LEN 1024*16 #endif // !RVC_MAX_INI_LEN bool Unzip2Folder(BSTR lpZipFile, BSTR lpFolder) { #ifdef RVC_OS_WIN IShellDispatch* pISD; Folder* pZippedFile = 0L; Folder* pDestination = 0L; long FilesCount = 0; IDispatch* pItem = 0L; FolderItems* pFilesInside = 0L; VARIANT Options, OutFolder, InZipFile, Item; CoInitialize(NULL); __try { if (CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)& pISD) != S_OK) return 1; InZipFile.vt = VT_BSTR; InZipFile.bstrVal = lpZipFile; pISD->NameSpace(InZipFile, &pZippedFile); if (!pZippedFile) { pISD->Release(); return 1; } OutFolder.vt = VT_BSTR; OutFolder.bstrVal = lpFolder; pISD->NameSpace(OutFolder, &pDestination); if (!pDestination) { pZippedFile->Release(); pISD->Release(); return 1; } pZippedFile->Items(&pFilesInside); if (!pFilesInside) { pDestination->Release(); pZippedFile->Release(); pISD->Release(); return 1; } pFilesInside->get_Count(&FilesCount); if (FilesCount < 1) { pFilesInside->Release(); pDestination->Release(); pZippedFile->Release(); pISD->Release(); return 0; } pFilesInside->QueryInterface(IID_IDispatch, (void**)& pItem); Item.vt = VT_DISPATCH; Item.pdispVal = pItem; Options.vt = VT_I4; Options.lVal = 1024 | 512 | 16 | 4; bool retval = pDestination->CopyHere(Item, Options) == S_OK; pItem->Release(); pItem = 0L; pFilesInside->Release(); pFilesInside = 0L; pDestination->Release(); pDestination = 0L; pZippedFile->Release(); pZippedFile = 0L; pISD->Release(); pISD = 0L; return retval; } __finally { CoUninitialize(); } #else return true; #endif } void split(const string& src, const string& separator, vector& dest) { string str = src; string substring; string::size_type start = 0, index; do { index = str.find_first_of(separator, start); if (index != string::npos) { substring = str.substr(start, index - start); dest.push_back(substring); start = str.find_first_not_of(separator, index); if (start == string::npos) return; } } while (index != string::npos); //the last token substring = str.substr(start); dest.push_back(substring); } bool checkDirExist(const string &strPath) { #ifdef RVC_OS_WIN WIN32_FIND_DATA wfd; bool rValue = false; HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd); if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) rValue = true; FindClose(hFind); return rValue; #else if (ExistsDir(strPath.c_str())) { return true; } else { return false; } #endif } bool checkFileExist(string fileName) { #ifdef RVC_OS_WIN WIN32_FIND_DATA FindFileData; HANDLE hFind; hFind = FindFirstFile(fileName.c_str(), &FindFileData); if (hFind == INVALID_HANDLE_VALUE) return false; FindClose(hFind); return true; #else if (ExistsFile(fileName.c_str())) { return true; } else { return false; } #endif } void Wchar_tToString(std::string& szDst, wchar_t *wchar) { #ifdef RVC_OS_WIN wchar_t* wText = wchar; DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);// WideCharToMultiByte的运用 char* psText; // psText为char*的临时数组,作为赋值给std::string的中间变量 psText = new char[dwNum]; WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);// WideCharToMultiByte的再次运用 szDst = psText;// std::string赋值 delete[]psText;// psText的清除 #else std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C"; setlocale(LC_ALL, "chs"); size_t _Dsize = 2 * wcslen(wchar) + 1; char* _Dest = new char[_Dsize]; memset(_Dest, 0, _Dsize); wcstombs(_Dest, wchar, _Dsize); std::string result = _Dest; delete[]_Dest; setlocale(LC_ALL, curLocale.c_str()); szDst = result; #endif } // string to wstring void StringToWstring(std::wstring& szDst, std::string str) { #ifdef RVC_OS_WIN std::string temp = str; int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0); wchar_t* wszUtf8 = new wchar_t[len + 1]; memset(wszUtf8, 0, len * 2 + 2); MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len); szDst = wszUtf8; std::wstring r = wszUtf8; delete[] wszUtf8; #else setlocale(LC_ALL, "chs"); const char* _Source = str.c_str(); size_t _Dsize = str.size() + 1; wchar_t* _Dest = new wchar_t[_Dsize]; wmemset(_Dest, 0, _Dsize); mbstowcs(_Dest, _Source, _Dsize); std::wstring result = _Dest; delete[]_Dest; setlocale(LC_ALL, "C"); szDst = result; #endif } bool createDir(const string &filePath) { #ifdef RVC_OS_WIN return 0 == _mkdir(filePath.c_str()); #else if (CreateDirRecursive(filePath.c_str())) { return true; } else { return false; } #endif } BOOL IsDirectory(const char *pDir) { char szCurPath[MAX_PATH*2] = {0}; #ifdef RVC_OS_WIN _snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir); WIN32_FIND_DATAA FindFileData = {0}; //ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA)); HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */ if (hFile == INVALID_HANDLE_VALUE) { FindClose(hFile); return FALSE; /** 如果不能找到第一个文件,那么没有目录 */ } else { FindClose(hFile); return TRUE; } #else snprintf(szCurPath, MAX_PATH * 2, "%s//*", pDir); struct stat buf; if (0 == stat(szCurPath, &buf)) { return S_ISDIR(buf.st_mode); } else { return FALSE; } #endif // RVC_OS_WIN } BOOL DeleteDirectory(const char * DirName) { #ifdef RVC_OS_WIN // CFileFind tempFind; //声明一个CFileFind类变量,以用来搜索 char szCurPath[MAX_PATH]; //用于定义搜索格式 _snprintf(szCurPath, MAX_PATH, "%s//*.*", DirName); //匹配格式为*.*,即该目录下的所有文件 WIN32_FIND_DATAA FindFileData; memset(&FindFileData, 0, sizeof(WIN32_FIND_DATAA)); HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); BOOL IsFinded = TRUE; while (IsFinded) { IsFinded = FindNextFileA(hFile, &FindFileData); //递归搜索其他的文件 if (strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) //如果不是"." ".."目录 { std::string strFileName = ""; strFileName = strFileName + DirName + "//" + FindFileData.cFileName; std::string strTemp; strTemp = strFileName; if (IsDirectory(strFileName.c_str())) //如果是目录,则递归地调用 { printf("目录为:%s/n", strFileName.c_str()); DeleteDirectory(strTemp.c_str()); } else { DeleteFileA(strTemp.c_str()); } } } FindClose(hFile); BOOL bRet = RemoveDirectoryA(DirName); if (bRet == 0) //删除目录 { printf("删除%s目录失败!/n", DirName); return FALSE; } return TRUE; #else return RemoveDirRecursive(DirName); #endif // RVC_OS_WIN } bool removeDir(const string &filePaht) { return DeleteDirectory(filePaht.c_str()); } void stopForDebug() { #ifdef RVC_OS_WIN DWORD processId = GetCurrentProcessId(); char showMsg[100] = ""; sprintf_s(showMsg, "Current pross id is %d", processId); MessageBox(NULL, showMsg, NULL, 0); #else #endif // RVC_OS_WIN } void getDirs(string path, vector &ownname) { #ifdef RVC_OS_WIN long hFile = 0; struct _finddata_t fileinfo; string p; if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do { //如果是目录加入列表 if ((fileinfo.attrib & _A_SUBDIR) && strcmp(fileinfo.name, ".") && strcmp(fileinfo.name, "..")) ownname.push_back(fileinfo.name); } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } #else array_header_t* subdirs = fileutil_get_sub_files(path.c_str()); int i; for (i = 0; i < subdirs->nelts; ++i) { char* strsubdir = ARRAY_IDX(subdirs, i, char*); ownname.push_back(strsubdir); } #endif // RVC_OS_WIN } bool getUniqueDir(string path, string &dirName) { vector dirs; getDirs(path, dirs); if (1 == dirs.size()) { dirName = dirs[0]; return true; } return false; }