LAPTOP-T815PCOQ\25526
2024-11-25 6d106eb1bb92dc235bcbda976ae232729bf52c7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "stdafx.h"
#include "ToolUnits.h"
#include <chrono>
 
 
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<std::chrono::milliseconds>(now.time_since_epoch());
    uint64_t timestamp = duration_in_milliseconds.count();
    return timestamp;
}
 
void CToolUnits::createDir(const char* pszDir)
{
    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));
}
 
double CToolUnits::toInt32(const char* pBuffer)
{
    return (pBuffer[0] & 0xff) | (pBuffer[1] & 0xff) << 8 | (pBuffer[2] & 0xff) << 16 | (pBuffer[3] & 0xff) << 24;
}
 
double CToolUnits::toInt16(const char* pBuffer)
{
    return (pBuffer[0] & 0xff) | (pBuffer[1] & 0xff) << 8;
}
 
void CToolUnits::setDlgItemDouble(CWnd* pWnd, int nCtrlId, double value)
{
    CString strText;
    strText.Format(_T("%.03f"), value);
    pWnd->SetDlgItemText(nCtrlId, strText);
}