#include "stdafx.h" #include "PubFun.h" BOOL IsDir(LPCTSTR lpFile) { DWORD attr = GetFileAttributes(lpFile); if ( (INVALID_FILE_ATTRIBUTES != attr) && (attr & FILE_ATTRIBUTE_DIRECTORY)) { return TRUE; } return FALSE; } BOOL IsFileExist(LPCTSTR lpFile) { if (GetFileAttributes(lpFile) != INVALID_FILE_ATTRIBUTES) { return TRUE; } return FALSE; } BOOL CopyFileOrDir(LPCTSTR lpSrcFile, LPCTSTR lpDstPath) { if (!IsFileExist(lpSrcFile) || !IsFileExist(lpDstPath) || !IsDir(lpDstPath)) return FALSE; if (IsDir(lpSrcFile)) { tstring strDstRootDir = JoinPath(lpDstPath, GetFileName(lpSrcFile).c_str()); if (!IsFileExist(strDstRootDir.c_str()) && !VCreateDirectory(strDstRootDir.c_str())) { return FALSE; } tstring strFind = JoinPath(lpSrcFile, _T("*")); WIN32_FIND_DATA ffd; HANDLE hFind = FindFirstFile(strFind.c_str(), &ffd); if (INVALID_HANDLE_VALUE == hFind) { return FALSE; } do { if (_tcsicmp(ffd.cFileName, _T(".")) == 0 || _tcsicmp(ffd.cFileName, _T("..")) == 0) { continue; } tstring strSrcSubFile = JoinPath(lpSrcFile, ffd.cFileName); if (!CopyFileOrDir(strSrcSubFile.c_str(), strDstRootDir.c_str())) { FindClose(hFind); return FALSE; } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); return TRUE; } else { return VCopyFile(lpSrcFile, lpDstPath); } return FALSE; } BOOL VCopyFile(LPCTSTR lpSrcFile, LPCTSTR lpDstPath) { if (!IsFileExist(lpSrcFile) || !IsFileExist(lpDstPath) || IsDir(lpSrcFile)) return FALSE; tstring strFileSrc = FormatPath(lpSrcFile); tstring strFileName = GetFileName(lpSrcFile); tstring strFileDst = JoinPath(FormatPath(lpDstPath).c_str(), strFileName.c_str()); return CopyFile(strFileSrc.c_str(), strFileDst.c_str(), FALSE); } tstring JoinPath(LPCTSTR lpPath1, LPCTSTR lpPath2) { tstring strPath = FormatPath(lpPath1) + _T("\\") + FormatPath(lpPath2); return strPath; } tstring FormatPath(LPCTSTR lpPath) { tstring strPath = lpPath; tstring::size_type pos = strPath.find(_T("/")); while (pos != tstring::npos) { strPath.replace(pos, 1, _T("\\")); pos = strPath.find(_T("/")); } pos = strPath.find(_T("\\\\")); while (pos != tstring::npos) { strPath.replace(pos, 2, _T("\\")); pos = strPath.find(_T("\\\\")); } pos = strPath.find(_T("\\")); if (pos != tstring::npos && pos == 0) { strPath.erase(pos, 1); } pos = strPath.rfind(_T("\\")); if (pos != tstring::npos && pos == strPath.size() - 1) { strPath.erase(pos, 1); } return strPath; } tstring GetFileName(LPCTSTR lpPath) { tstring strPath = FormatPath(lpPath); tstring strFileName; if (strPath.rfind(_T("\\")) != tstring::npos) { strFileName = strPath.substr(strPath.rfind(_T("\\"))); } else { strFileName = strPath; } return strFileName; } tstring UTF8ToTString(LPCSTR lpUTF8) { tstring strRst; int len = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, NULL, 0); if (len == 0) return strRst; wchar_t* wszBuf = new wchar_t[len]; if (MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, wszBuf, len) == 0) { delete[] wszBuf; return strRst; } #ifdef _UNICODE strRst = wszBuf; delete[] wszBuf; #else len = WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, NULL, 0, NULL, NULL); if (len == 0) { delete[] wszBuf; return strRst; } char* szBuf = new char[len]; if (WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, szBuf, len, NULL, NULL) == 0) { delete[] wszBuf; delete[] szBuf; return strRst; } delete[] wszBuf; strRst = szBuf; delete[] szBuf; #endif return strRst; } string TStringToUTF8(LPCTSTR lpTStr) { string strRst; #ifdef _UNICODE int len = WideCharToMultiByte(CP_UTF8, 0, lpTStr, -1, NULL, 0, NULL, NULL); if (len == 0) { return strRst; } char* szBuf = new char[len]; if (WideCharToMultiByte(CP_UTF8, 0, lpTStr, -1, szBuf, len, NULL, NULL) == 0) { delete[] szBuf; return strRst; } strRst = szBuf; delete[] szBuf; #else int len = MultiByteToWideChar(CP_ACP, 0, lpTStr, -1, NULL, 0); if (len == 0) return strRst; wchar_t* wszBuf = new wchar_t[len]; if (MultiByteToWideChar(CP_ACP, 0, lpTStr, -1, wszBuf, len) == 0) { delete[] wszBuf; return strRst; } len = WideCharToMultiByte(CP_UTF8, 0, wszBuf, -1, NULL, 0, NULL, NULL); if (len == 0) { delete[] wszBuf; return strRst; } char* szBuf = new char[len]; if (WideCharToMultiByte(CP_UTF8, 0, wszBuf, -1, szBuf, len, NULL, NULL) == 0) { delete[] wszBuf; delete[] szBuf; return strRst; } delete[] wszBuf; strRst = szBuf; delete[] szBuf; #endif return strRst; } DWORD BKDRHash(LPCTSTR lp) { DWORD seed = 131; // 31 131 1313 13131 131313 etc.. DWORD hash = 0; while (*lp) { hash = hash * seed + (*lp++); } return (hash & 0x7FFFFFFF); } tstring GetModulePath() { tstring strPath; TCHAR szBuf[MAX_PATH] = { 0 }; GetModuleFileName(NULL, szBuf, MAX_PATH); strPath = FormatPath(szBuf); strPath = strPath.substr(0, strPath.rfind(_T("\\"))); return strPath; } BOOL VCreateDirectory(LPCTSTR dir) { tstring strPath = FormatPath(dir); if (!IsFileExist(strPath.c_str())) { if (strPath.rfind(_T("\\")) != tstring::npos) { tstring strParentPath = strPath.substr(0, strPath.rfind(_T("\\"))); if (!VCreateDirectory(strParentPath.c_str())) return FALSE; } return CreateDirectory(strPath.c_str(), NULL); } else if (!IsDir(strPath.c_str())) { return FALSE; } return TRUE; } BOOL VCreateFile(LPCTSTR file) { HANDLE fh = CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (fh == INVALID_HANDLE_VALUE) return FALSE; else CloseHandle(fh); return TRUE; } BOOL DeleteFileOrDir(LPCTSTR lpFile) { if (!IsFileExist(lpFile)) return TRUE; if (IsDir(lpFile)) { tstring strFind = JoinPath(lpFile, _T("*")); WIN32_FIND_DATA ffd; HANDLE hFind = FindFirstFile(strFind.c_str(), &ffd); if (INVALID_HANDLE_VALUE == hFind) { return FALSE; } do { if (_tcsicmp(ffd.cFileName, _T(".")) == 0 || _tcsicmp(ffd.cFileName, _T("..")) == 0) { continue; } tstring strSubFile = JoinPath(lpFile, ffd.cFileName); if (!DeleteFileOrDir(strSubFile.c_str())) { FindClose(hFind); return FALSE; } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); return RemoveDirectory(lpFile); } else { return DeleteFile(lpFile); } return FALSE; } tstring AStringToTString(LPCSTR lpStr) { tstring strRst; #ifdef _UNICODE int len = MultiByteToWideChar(CP_ACP, 0, lpStr, -1, NULL, 0); if (len == 0) return strRst; wchar_t* wszBuf = new wchar_t[len]; if (MultiByteToWideChar(CP_ACP, 0, lpStr, -1, wszBuf, len) == 0) { delete[] wszBuf; return strRst; } strRst = wszBuf; delete[] wszBuf; #else strRst = lpStr; #endif return strRst; } string TStringToAString(LPCTSTR lpTStr) { string strRst; #ifdef _UNICODE int len = WideCharToMultiByte(CP_ACP, 0, lpTStr, -1, NULL, 0, NULL, NULL); if (len == 0) { return strRst; } char* szBuf = new char[len]; if (WideCharToMultiByte(CP_ACP, 0, lpTStr, -1, szBuf, len, NULL, NULL) == 0) { delete[] szBuf; return strRst; } strRst = szBuf; delete[] szBuf; #else strRst = lpTStr; #endif return strRst; } BOOL OpenFileByDefault(LPCTSTR lpFile) { TCHAR szExe[MAX_PATH] = { 0 }; _stprintf_s(szExe, MAX_PATH, _T("explorer.exe %s"), lpFile); STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); if (CreateProcess(NULL, szExe, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return TRUE; } return FALSE; } tstring BufToString(LPVOID buf, int len) { tstring rst = _T(""); if (buf == NULL || len <= 0) return rst; LPBYTE p = (LPBYTE)buf; for (int i = 0; i < len; ++i) { TCHAR str[10] = { 0 }; _stprintf_s(str, _T("%02X "), p[i]); rst += str; } return rst; } bool StringToBuf(LPCTSTR lpstr, LPVOID& buf, int& len) { if (lpstr == NULL || _tcslen(lpstr) == 0) return false; buf = NULL; len = 0; int slen = _tcslen(lpstr); vector vecByte; BYTE n = 0, m = 0; bool add = false; for (int i = 0; i < slen; ++i) { TCHAR ch = lpstr[i]; if (ch >= _T('0') && ch <= _T('9')) { m = ch - _T('0'); } else if (ch >= _T('A') && ch <= _T('F')) { m = 0x0A + ch - _T('A'); } else if (ch >= _T('a') && ch <= _T('f')) { m = 0x0A + ch - _T('a'); } else { continue; } if (add) { n = (n << 4) + m; vecByte.push_back(n); ++len; } n = m; add = !add; } if (len == 0) { return false; } buf = new BYTE[len]; for (int i = 0; i < len; ++i) { ((LPBYTE)buf)[i] = vecByte[i]; } return true; } vector VFindFiles(LPCTSTR str) { vector vec; WIN32_FIND_DATA ffd; ZeroMemory(&ffd, sizeof(ffd)); HANDLE hFind = FindFirstFile(str, &ffd); if (hFind != INVALID_HANDLE_VALUE) { do { tstring file = ffd.cFileName; vec.push_back(file); } while (FindNextFile(hFind, &ffd)); FindClose(hFind); } return vec; } //CString GetExeRunPath() //{ // TCHAR cSAMSRunPath[MAX_PATH] = { 0 }; // GetModuleFileName(NULL, cSAMSRunPath, MAX_PATH); //获取可执行模块的路径 // CString strPath = cSAMSRunPath; // int nEnd = strPath.ReverseFind('\\'); //取最后的"\"号之前地址 // strPath = strPath.Left(nEnd + 1); // return strPath; //} void WriteLog(std::string strLog, int n/* = 0xFFFFFFF*/) { HANDLE hOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);//获得控制台输出句柄 if (hOutputHandle) { if (n != 0xFFFFFFF) { char szParam[20]; sprintf_s(szParam, "%d\n", n); strLog += szParam; } else strLog += "\n"; DWORD nRet = 0; WriteConsole(hOutputHandle, strLog.c_str(), strLog.size(), &nRet, NULL); } }