#include "stdafx.h" #include "ToolUnits.h" #include CToolUnits::CToolUnits() { } CToolUnits::~CToolUnits() { } std::string CToolUnits::timeToString(ULONGLONG time) { ULONGLONG time1, time2; time1 = time / 1000000000; time2 = time % 1000000000; char buffer1[256], buffer[128]; struct tm timeinfo; time_t t = time_t(time1); localtime_s(&timeinfo, &t); strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", &timeinfo); sprintf_s(buffer1, 256, "%s.%lld", buffer, time2); return std::string(buffer1); } std::string CToolUnits::timeToString2(ULONGLONG time) { ULONGLONG time1; time1 = time / 1000; char buffer[256]; struct tm timeinfo; time_t t = time_t(time1); localtime_s(&timeinfo, &t); strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", &timeinfo); return std::string(buffer); } std::string CToolUnits::timeToString3(ULONGLONG time) { ULONGLONG time1; int ms; time1 = time / 1000; ms = time % 1000; char buffer1[256], buffer[128]; struct tm timeinfo; time_t t = time_t(time1); localtime_s(&timeinfo, &t); strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", &timeinfo); sprintf_s(buffer1, 256, "%s.%03d", buffer, ms); return std::string(buffer1); } ULONGLONG CToolUnits::stringToTime(const char* pszTime) { struct tm tm; memset(&tm, 0, sizeof(tm)); sscanf_s(pszTime, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec); tm.tm_year -= 1900; tm.tm_mon--; return mktime(&tm) * 1000; } ULONGLONG CToolUnits::getTimestamp() { auto now = std::chrono::system_clock::now(); auto duration_in_milliseconds = std::chrono::duration_cast(now.time_since_epoch()); uint64_t timestamp = duration_in_milliseconds.count(); return timestamp; } void CToolUnits::createDir(const char* pszDir) { if (isDirectory(std::string(pszDir))) { return; } CString strDir = pszDir; int lastIndex = 0; int index = strDir.Find(_T("\\"), lastIndex); while (index > 0) { CString strTempDir = strDir.Left(index); CreateDirectory(strTempDir, NULL); lastIndex = index + 1; index = strDir.Find(_T("\\"), lastIndex); } CreateDirectory(strDir, NULL); } CString& CToolUnits::floatToString1(float value, CString& strOut) { strOut.Format(_T("%.1f"), value); return strOut; } CString& CToolUnits::floatToString3(float value, CString& strOut) { strOut.Format(_T("%.3f"), value); return strOut; } BOOL CToolUnits::copyTextToClipboard(CWnd* pWnd, const CString& strText) { if (OpenClipboard(pWnd->GetSafeHwnd())) { EmptyClipboard(); HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strText.GetLength() + 1) * sizeof(TCHAR)); if (hglbCopy == NULL) { CloseClipboard(); return FALSE; } LPTSTR lptstrCopy = (LPTSTR)GlobalLock(hglbCopy); strcpy_s(lptstrCopy, strText.GetLength()+1, strText); GlobalUnlock(hglbCopy); SetClipboardData(CF_TEXT, hglbCopy); CloseClipboard(); GlobalFree(hglbCopy); return TRUE; } return FALSE; } std::string CToolUnits::getCurrentExePath() { char path[MAX_PATH]; GetModuleFileName(NULL, path, MAX_PATH); std::string exePath(path); return exePath.substr(0, exePath.find_last_of("\\/")); } bool CToolUnits::isFile(const std::string& path) { DWORD attributes = GetFileAttributes(path.c_str()); return (attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY)); } bool CToolUnits::isDirectory(const std::string& path) { DWORD attributes = GetFileAttributes(path.c_str()); return (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY)); } int CToolUnits::toInt32(const char* pBuffer) { return (pBuffer[0] & 0xff) | (pBuffer[1] & 0xff) << 8 | (pBuffer[2] & 0xff) << 16 | (pBuffer[3] & 0xff) << 24; } int CToolUnits::toInt16(const char* pBuffer) { return (pBuffer[0] & 0xff) | (pBuffer[1] & 0xff) << 8; } BOOL CToolUnits::getBit(const char c, int index) { switch (index) { case 0: return c & 0x01; break; case 1: return c & 0x02; break; case 2: return c & 0x04; break; case 3: return c & 0x08; break; case 4: return c & 0x10; break; case 5: return c & 0x20; break; case 6: return c & 0x40; break; case 7: return c & 0x80; break; default: break; } return FALSE; } void CToolUnits::setBit(char* p, int index) { int byteIndex = 0; byte b = 0; if (index >= 8) byteIndex = 1; switch (index) { case 0: case 8: b = 0x1; break; case 1: case 9: b = 0x2; break; case 2: case 0xA: b = 0x4; break; case 3: case 0xB: b = 0x8; break; case 4: case 0xC: b = 0x10; break; case 5: case 0xD: b = 0x20; break; case 6: case 0xE: b = 0x40; break; case 7: case 0xF: b = 0x80; break; default: break; } p[byteIndex] = b; } void CToolUnits::setDlgItemDouble(CWnd* pWnd, int nCtrlId, double value) { CString strText; strText.Format(_T("%.03f"), value); pWnd->SetDlgItemText(nCtrlId, strText); } std::vector CToolUnits::GetFileNamesInDirectory(const CString& strFolderPath, const CString& strExtension) { std::vector fileNames; // È·±£Ä¿Â¼Â·¾¶×îºóÓз´Ð±¸Ü CString strSearchPath = strFolderPath; if (strSearchPath[strSearchPath.GetLength() - 1] != '\\') { strSearchPath += '\\'; } CString finalExtension = strExtension; if (finalExtension.Find('.') == -1) { finalExtension = '.' + finalExtension; } strSearchPath += "*" + finalExtension; std::unique_ptr finder = std::make_unique(); BOOL bWorking = finder->FindFile(strSearchPath); // ±éÀúÎļþ¼Ð while (bWorking) { bWorking = finder->FindNextFile(); if (!finder->IsDirectory()) { CString fileName = finder->GetFileName(); int dotPos = fileName.ReverseFind('.'); if (dotPos != -1) { fileName = fileName.Left(dotPos); } fileNames.push_back(fileName); } } return fileNames; } std::string CToolUnits::getRecipePath() { return getCurrentExePath() + "\\Recipe"; } std::string CToolUnits::getCurrentTimeString() { struct tm ltm; time_t now = time(0); localtime_s(<m, &now); // ʹÓð²È«µÄ localtime_s º¯Êý char buffer[256]; sprintf_s(buffer, sizeof(buffer), "%04d-%02d-%02d %02d:%02d:%02d", ltm.tm_year + 1900, ltm.tm_mon + 1, ltm.tm_mday, ltm.tm_hour, ltm.tm_min, ltm.tm_sec); return std::string(buffer); }