| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- /**************************************************************************************************
- 版本所有(C), 2012-2013, 西安XXXXX公司
- ***************************************************************************************************
- 文 件 名:CommonFunc.cpp
- 版 本 号: 1.00初版
- 作 者: HYF
- 生成日期:2013-3-20
- 最近修改:
- 功能描述: 公共函數定義
- 函数列表:
- 修改历史:
- 1.日 期 : 2012-3-20
- 作 者 : HYF
- 修改内容 : 创建文件
- **************************************************************************************************/
- #include "pch.h"
- #include <winsock2.h>
- #include <iphlpapi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #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;
- }
|