| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- #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<BYTE> 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<tstring> VFindFiles(LPCTSTR str)
- {
- vector<tstring> 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);
- }
- }
|