LAPTOP-SNT8I5JK\Boounion
2025-07-28 bfe14e41fa5b07771d78af4511ba18d706bc23cc
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#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)
{
    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<CString> CToolUnits::GetFileNamesInDirectory(const CString& strFolderPath, const CString& strExtension)
{
    std::vector<CString> fileNames;
 
    // È·±£Ä¿Â¼Â·¾¶×îºóÓз´Ð±¸Ü
    CString strSearchPath = strFolderPath;
    if (strSearchPath[strSearchPath.GetLength() - 1] != '\\') {
        strSearchPath += '\\';
    }
 
    CString finalExtension = strExtension;
    if (finalExtension.Find('.') == -1) {
        finalExtension = '.' + finalExtension;
    }
    strSearchPath += "*" + finalExtension;
 
    std::unique_ptr<CFileFind> finder = std::make_unique<CFileFind>();
    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(&ltm, &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);
}