/************************************************************************************************** 版本所有(C), 2012-2013, 西安XXXXX公司 *************************************************************************************************** 文 件 名:CommonFunc.cpp 版 本 号: 1.00初版 作 者: HYF 生成日期:2013-3-20 最近修改: 功能描述: 公共函數定義 函数列表: 修改历史: 1.日 期 : 2012-3-20 作 者 : HYF 修改内容 : 创建文件 **************************************************************************************************/ #include "pch.h" #include #include #include #include #include "Utils.h" using namespace std; Utils::Utils() { } Utils::~Utils() { } /************************************************************************************************** 函 数 名 :GetExecDir 功能描述 :获取当前目录 输出参数 :无 返 回 值 :路径 **************************************************************************************************/ CString Utils::GetExecDir(void) { CString path; GetModuleFileName(NULL, path.GetBufferSetLength(MAX_PATH + 1), MAX_PATH); path.ReleaseBuffer(); int pos = path.ReverseFind('\\'); return path.Left(pos); } BOOL Utils::IsFileExist(LPCTSTR lpFileName) { if (NULL == lpFileName) { return FALSE; } BOOL bExist = TRUE; HANDLE hFind = INVALID_HANDLE_VALUE; WIN32_FIND_DATA DataFind; hFind = FindFirstFile(lpFileName, &DataFind); if (INVALID_HANDLE_VALUE != hFind) { return true; } FindClose(hFind); return FALSE; } BOOL Utils::DirectoryExists(LPCTSTR szFilePath) { if (::GetFileAttributes(szFilePath) == FILE_ATTRIBUTE_DIRECTORY) return TRUE; return FALSE; } /************************************************************************************************** 函 数 名 :GetLocalUrlDir 功能描述 :获取当前目录 输出参数 :无 返 回 值 :路径 **************************************************************************************************/ CString Utils::GetLocalUrlDir(void) { CString path; GetModuleFileName(NULL, path.GetBufferSetLength(MAX_PATH + 1), MAX_PATH); path.ReleaseBuffer(); int pos = path.ReverseFind('\\'); CString urlPath = path.Left(pos); urlPath.Replace('\\', '\/'); return urlPath; } /************************************************************************************************** 函 数 名 :GetCurrentTimeStamp 功能描述 :获取当前时间Tick数目 输出参数 :无 返 回 值 :INT64 **************************************************************************************************/ INT64 Utils::GetCurrentTimeStamp() { SYSTEMTIME tmSys; GetLocalTime(&tmSys); CTime tm3(tmSys); INT64 tmDst = (tm3.GetTime()) * 1000 + tmSys.wMilliseconds; return tmDst; } /************************************************************************************************** 函 数 名 :TimeStampToString 功能描述 :时间戳转北京时间字串 输出参数 :无 返 回 值 :INT64 **************************************************************************************************/ CString Utils::TimeStampToString(INT64 timestamp) { int dtime = (int)(timestamp / 1000); dtime += 28800;//GTM偏移8个时区得到北京时间 tm p; gmtime_s(&p, (time_t *)&dtime); char s[80]; strftime(s, 80, "%Y-%m-%d %H:%M:%S", &p); return CString(s); } /************************************************************************************************** 函 数 名 :TimeStampToString 功能描述 :北京时间字串转时间戳 输出参数 :无 返 回 值 :INT64 **************************************************************************************************/ INT64 Utils::StringToTimeStamp(CString strInputTime) { COleVariant vtime(strInputTime); vtime.ChangeType(VT_DATE); COleDateTime cOleTime = vtime; SYSTEMTIME systime; VariantTimeToSystemTime(cOleTime, &systime); //时间戳最小值为北京时间:1970-01-01 08:00:00 if (systime.wYear <= 1970 && systime.wMonth <= 1 && systime.wDay <= 1 && systime.wHour <= 7 && systime.wMinute <= 59 && systime.wSecond <= 59) return 0; CTime cTimeFromDB(systime); INT64 timestamp = cTimeFromDB.GetTime();//CTime->时间戳 return timestamp; } /************************************************************************************************** 函 数 名 :ConverANSI2Unicode 功能描述 :ANSI转Unicode 输出参数 :略 返 回 值 :略 **************************************************************************************************/ std::wstring Utils::ConverANSI2Unicode(const std::string str) { int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t *pUnicode = new wchar_t[unicodeLen + 1]; memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen); std::wstring wstr = pUnicode; delete[] pUnicode; return wstr; } /************************************************************************************************** 函 数 名 :ConverUTF82Unicode 功能描述 :UTF8转Unicode 输出参数 :略 返 回 值 :略 **************************************************************************************************/ std::wstring Utils::ConverUTF82Unicode(const std::string str) { int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t *pUnicode = new wchar_t[unicodeLen + 1]; memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen); std::wstring wstr = pUnicode; delete[] pUnicode; return wstr; } /************************************************************************************************** 函 数 名 :ConverUnicode2UTF8 功能描述 :Unicode转UTF8 输出参数 :略 返 回 值 :略 **************************************************************************************************/ std::string Utils::ConverUnicode2UTF8(const std::wstring wstr) { int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL); char *pUtf8 = new char[utf8Len+1]; memset(pUtf8,0, utf8Len+1); WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pUtf8, utf8Len, NULL, NULL); string str = pUtf8; delete[] pUtf8; return str; } /************************************************************************************************** 函 数 名 :ConverUnicode2ANSI 功能描述 :Unicode转ANSI 输出参数 :略 返 回 值 :略 **************************************************************************************************/ std::string Utils::ConverUnicode2ANSI(const std::wstring wstr) { int ansiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL); char *pAnsi = new char[ansiLen+1]; memset(pAnsi,0, ansiLen+1); WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAnsi, ansiLen, NULL, NULL); string str = pAnsi; delete[] pAnsi; return str; } /************************************************************************************************** 函 数 名 :ConverANSI2UTF8 功能描述 :ANSI转UTF8 输出参数 :略 返 回 值 :略 **************************************************************************************************/ std::string Utils::ConverANSI2UTF8(const std::string & str) { int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2); ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.length(), pwBuf, nwLen); int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char * pBuf = new char[nLen + 1]; ZeroMemory(pBuf, nLen + 1); ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr(pBuf); delete []pwBuf; delete []pBuf; pwBuf = NULL; pBuf = NULL; return retStr; } /************************************************************************************************** 函 数 名 :ConverUTF82ANSI 功能描述 :UTF8转ANSI 输出参数 :略 返 回 值 :略 **************************************************************************************************/ std::string Utils::ConverUTF82ANSI(const std::string str) { int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t *pUnicode = new wchar_t[unicodeLen + 1]; memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen); int ansiLen = WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, NULL, 0, NULL, NULL); char *pAnsi = new char[ansiLen+1]; memset(pAnsi,0, ansiLen+1); WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsi, ansiLen, NULL, NULL); string str1 = pAnsi; delete[] pUnicode; delete[] pAnsi; return str1; } /************************************************************************************************** 函 数 名 :DeleteFolder 功能描述 :删除文件夹 输出参数 :略 返 回 值 :略 **************************************************************************************************/ BOOL Utils::DeleteFolder(const CString& strDir) { if(strDir.IsEmpty()) { BOOL bRet = RemoveDirectory(strDir); if (!bRet) { LOG::E("删除文件夹失败:%d", GetLastError()); return FALSE; } return TRUE; } CFileFind ff; BOOL bFound = ff.FindFile(strDir + _T("\\*"),0); while(bFound) { bFound = ff.FindNextFile(); if(ff.GetFileName()== _T(".")||ff.GetFileName()== _T("..")) { continue; } //去掉文件(夹)只读等属性 SetFileAttributes(ff.GetFilePath(),FILE_ATTRIBUTE_NORMAL); if(ff.IsDirectory()) { //递归删除子文件夹 DeleteFolder(ff.GetFilePath()); RemoveDirectory(ff.GetFilePath()); }else { DeleteFile(ff.GetFilePath()); //删除文件 } } ff.Close(); //然后删除该文件夹 BOOL bRet = RemoveDirectory(strDir); if (!bRet) { LOG::E("删除文件夹失败:%d", GetLastError()); } return bRet; } BOOL Utils::SHDeleteFolder(const CString& szDir) { char sourceFolder[MAX_PATH] = ""; strcpy(sourceFolder, szDir); SHFILEOPSTRUCT lpFile; lpFile.wFunc = FO_DELETE; lpFile.pFrom = sourceFolder; lpFile.pTo = NULL; lpFile.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; lpFile.fAnyOperationsAborted = false; lpFile.hNameMappings = NULL; lpFile.lpszProgressTitle = NULL; int iRet = SHFileOperation(&lpFile); if (iRet == 0) return FALSE; return TRUE; } /************************************************************************************************** 函 数 名 :DeleteFolder 功能描述 :删除文件夹 输出参数 :略 返 回 值 :略 **************************************************************************************************/ void Utils::MoveFolder(const CString& szSrc, const CString& szDes) { SHFILEOPSTRUCT sfo; sfo.hwnd = NULL; sfo.wFunc = FO_MOVE; sfo.pFrom = szSrc; sfo.pTo = szDes; sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR; SHFileOperation(&sfo); } /************************************************************************************************** 函 数 名 :IP_STR2DW 功能描述 :IP地址转换 输出参数 :略 返 回 值 :略 **************************************************************************************************/ DWORD Utils::IP_STR2DW(LPCTSTR szIP) { int len= (int)_tcslen(szIP); if( len <7 || len>15 ) return 0 ; DWORD dwip = 0; char *p ; BYTE dd; char field[4]; int i=0; int j = 3; for (p =(char *)szIP; *p!= NULL ; p++) { if ((*p >='0') && (*p <='9')) { field[i++] = * p; } if ((*(p+1) == '.') || (*(p+1) == 0 )) { field[i] = 0; i = 0; dd = (BYTE)atoi(field); dwip = dwip | ((DWORD) dd << (j--)*8); } } if (j!= -1) return 0; return dwip; } /************************************************************************************************** 函 数 名 :DMS_CreateMsg 功能描述 :创建消息包 输出参数 :略 返 回 值 :略 **************************************************************************************************/ DMSResponse* Utils::DMS_CreateMsg() { DMSResponse *pMSG = new DMSResponse(); memset(pMSG->szMsg, 0, DMS_MSG_SIZE); pMSG->bRet = TRUE; pMSG->iCode = 0; return pMSG; } /************************************************************************************************** 函 数 名 :DMS_FreeMsg 功能描述 :释放消息包 输出参数 :略 返 回 值 :略 **************************************************************************************************/ void Utils::DMS_FreeMsg(DMSResponse *pMSG) { delete pMSG; return ; } /************************************************************************************************** 函 数 名 :StrSplit 功能描述 :字符串分割成数组 输出参数 :略 返 回 值 :略 **************************************************************************************************/ int Utils::StrSplit(const CString& strLine, char split, CStringArray &strArray) { strArray.RemoveAll();//自带清空属性 CString temp = strLine; int tag = 0; while (1) { tag = temp.Find(split); if (tag >= 0) { CString substr = temp.Left(tag); substr.Trim(); if (substr.GetLength()>0) strArray.Add(substr); temp = temp.Right(temp.GetLength() - tag - 1); } else { break; } } strArray.Add(temp); return (int)strArray.GetSize(); } /************************************************************************************************** 函 数 名 :StrSplit 功能描述 :字符串分割成数组 输出参数 :略 返 回 值 :略 **************************************************************************************************/ int Utils::StrSplit(const char *str, int iLen, char split, CStringArray &strArray) { strArray.RemoveAll();//自带清空属性 int iStart = 0; for (int iPos = 0; iPos < iLen; iPos++) { if (str[iPos] == '\0') { CString szTemp(str+ iStart); if (!szTemp.IsEmpty()) strArray.Add(szTemp); iStart = iPos+1; } } return (int)strArray.GetSize(); } /************************************************************************************************** 函 数 名 :StrSplit 功能描述 :字符串分割成数组 输出参数 :略 返 回 值 :略 **************************************************************************************************/ int Utils::StrSplit(const CString& strLine, LPCTSTR split, CStringArray &strArray) { strArray.RemoveAll();//自带清空属性 CString temp = strLine; CString szSplit(split); int isplitsize = szSplit.GetLength(); int tag = 0; while (1) { tag = temp.Find(split); if (tag >= 0) { CString substr = temp.Left(tag); substr.Trim(); if (substr.GetLength()>0) strArray.Add(substr); temp = temp.Right(temp.GetLength() - (tag+isplitsize)); } else { break; } } strArray.Add(temp); return (int)strArray.GetSize(); } //资源加载 BOOL Utils::LoadImageFromResource(CImage *pImage, UINT nResID, LPCTSTR lpTyp) { if (pImage == NULL) return false; pImage->Destroy(); // 查找资源 HRSRC hRsrc = ::FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(nResID), lpTyp); if (hRsrc == NULL) return false; // 加载资源 HGLOBAL hImgData = ::LoadResource(AfxGetResourceHandle(), hRsrc); if (hImgData == NULL) { ::FreeResource(hImgData); return false; } // 锁定内存中的指定资源 LPVOID lpVoid = ::LockResource(hImgData); LPSTREAM pStream = NULL; DWORD dwSize = ::SizeofResource(AfxGetResourceHandle(), hRsrc); HGLOBAL hNew = ::GlobalAlloc(GHND, dwSize); LPBYTE lpByte = (LPBYTE)::GlobalLock(hNew); ::memcpy(lpByte, lpVoid, dwSize); // 解除内存中的指定资源 ::GlobalUnlock(hNew); // 从指定内存创建流对象 HRESULT ht = ::CreateStreamOnHGlobal(hNew, TRUE, &pStream); if (ht != S_OK) { GlobalFree(hNew); } else { // 加载图片 pImage->Load(pStream); GlobalFree(hNew); } // 释放资源 ::FreeResource(hImgData); return true; }