-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCtrl.cpp
More file actions
229 lines (186 loc) · 5.05 KB
/
FileCtrl.cpp
File metadata and controls
229 lines (186 loc) · 5.05 KB
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
#include "FileCtrl.h"
namespace fs = std::filesystem;
CFileCtrl FileCtrl;
CFileCtrl::CFileCtrl()
{
}
CFileCtrl::~CFileCtrl()
{
}
BOOL CFileCtrl::CreateDirectory(std::string strPath)
{
auto wstrPath = StrCtrl.AnsiStringToWideString(strPath);
return CreateDirectory(wstrPath);
}
BOOL CFileCtrl::CreateDirectory(std::wstring strPath)
{
BOOL ret = fs::create_directories(strPath);
if (FALSE == ret)
{
DWORD dwErr = ::GetLastError();
SetLastError(dwErr);
}
return ret;
}
BOOL CFileCtrl::FileExists(std::string strPath)
{
auto wstrPath = StrCtrl.AnsiStringToWideString(strPath);
return FileExists(wstrPath);
}
BOOL CFileCtrl::FileExists(std::wstring strPath)
{
return fs::exists(strPath);
}
FileType CFileCtrl::GetType(std::string strPath)
{
auto wstrPath = StrCtrl.AnsiStringToWideString(strPath);
return GetType(wstrPath);
}
FileType CFileCtrl::GetType(std::wstring strPath)
{
FileType type = kNone;
if (fs::exists(strPath) == TRUE)
{
if (fs::is_directory(strPath) == TRUE)
type = FileType::kDirectory;
else
type = FileType::kFile;
}
return type;
}
BOOL CFileCtrl::CopyFile(std::string strSrc, std::string strDst)
{
return CopyFile(StrCtrl.AnsiStringToWideString(strSrc), StrCtrl.AnsiStringToWideString(strDst));
}
BOOL CFileCtrl::CopyFile(std::wstring wstrSrc, std::wstring wstrDst)
{
BOOL ret = FALSE;
if (TRUE == FileExists(wstrSrc))
ret = fs::copy_file(wstrSrc, wstrDst);
if (FALSE == ret)
{
DWORD dwErr = ::GetLastError();
SetLastError(dwErr);
}
return ret;
}
BOOL CFileCtrl::GetFiles(std::string strPath, std::vector<std::string>* files)
{
std::vector<std::wstring> tmpFiles;
BOOL ret = FALSE;
ret = GetFiles(std::wstring(strPath.begin(), strPath.end()), &tmpFiles);
if (TRUE == ret)
{
for (auto& file : tmpFiles)
files->push_back(std::string(file.begin(), file.end()));
}
return ret;
}
BOOL CFileCtrl::GetFiles(std::wstring strPath, std::vector<std::wstring>* files)
{
BOOL ret = FALSE;
if (nullptr != files && FileType::kDirectory == GetType(strPath))
{
for (const auto& entry : fs::directory_iterator(strPath))
files->push_back(entry.path());
ret = TRUE;
}
return ret;
}
BOOL CFileCtrl::RemoveFile(std::wstring wstrPath)
{
return RemoveFile(StrCtrl.WideStringToAnsiString(wstrPath));
}
BOOL CFileCtrl::RemoveFile(std::string wstrPath)
{
return fs::remove(wstrPath);
}
BOOL CFileCtrl::FileVersion(std::string& strPath, std::string& version)
{
std::wstring wstrPath = StrCtrl.AnsiStringToWideString(strPath);
std::wstring wstrVersion;
BOOL nRet = FileVersion(wstrPath, wstrVersion);
if (TRUE == nRet)
version.assign(wstrVersion.begin(), wstrVersion.end());
return nRet;
}
BOOL CFileCtrl::FileVersion(std::wstring& strPath, std::wstring& version)
{
DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSize(strPath.c_str(), &verHandle);
BOOL nRet = FALSE;
if (verSize != NULL)
{
LPSTR verData = new char[verSize];
if (GetFileVersionInfo(strPath.c_str(), verHandle, verSize, verData))
{
if (VerQueryValue(verData, L"\\", (VOID FAR * FAR*) & lpBuffer, &size))
{
if (size)
{
VS_FIXEDFILEINFO* verInfo = (VS_FIXEDFILEINFO*)lpBuffer;
if (verInfo->dwSignature == 0xfeef04bd)
{
int major = (verInfo->dwFileVersionMS >> 16) & 0xffff;
int minor = (verInfo->dwFileVersionMS >> 0) & 0xffff;
int build = (verInfo->dwFileVersionLS >> 16) & 0xffff;
int revision = (verInfo->dwFileVersionLS >> 0) & 0xffff;
version = std::format(L"{}.{}.{}.{}", major, minor, build, revision);
nRet = TRUE;
}
}
}
}
delete[] verData;
}
return nRet;
}
BOOL CFileCtrl::Is64BitFile(std::string& strPath)
{
bool is64Bit = false;
try {
// 파일 정보 가져오기
std::filesystem::path file(strPath);
std::filesystem::file_status status = std::filesystem::status(file);
// 파일이 regular 파일인 경우에만 처리
if (std::filesystem::is_regular_file(status))
{
// 파일 크기 확인
uintmax_t fileSize = std::filesystem::file_size(file);
// PE 파일 헤더 크기 확인
if (fileSize >= sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS32) &&
fileSize >= sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS64))
{
// PE 파일 헤더 읽기
std::ifstream fileStream(file, std::ios::binary);
IMAGE_DOS_HEADER dosHeader;
IMAGE_NT_HEADERS32 ntHeader32;
IMAGE_NT_HEADERS64 ntHeader64;
fileStream.read(reinterpret_cast<char*>(&dosHeader), sizeof(IMAGE_DOS_HEADER));
fileStream.read(reinterpret_cast<char*>(&ntHeader32), sizeof(IMAGE_NT_HEADERS32));
if (ntHeader32.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
// 32-bit 파일
is64Bit = false;
}
else
{
fileStream.seekg(std::ios::beg);
fileStream.read(reinterpret_cast<char*>(&ntHeader64), sizeof(IMAGE_NT_HEADERS64));
if (ntHeader64.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
// 64-bit 파일
is64Bit = true;
}
}
fileStream.close();
}
}
}
catch (const std::filesystem::filesystem_error& ex)
{
}
return is64Bit;
}