-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCUtilsFile.h
232 lines (205 loc) · 4.8 KB
/
RCUtilsFile.h
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
#pragma once
#include "RCUtilsBase.h"
namespace RCUtils
{
inline std::vector<char> LoadFileToArray(const char* Filename, bool* OutSuccess = nullptr)
{
std::vector<char> OutData;
bool bSuccess = false;
FILE* File = nullptr;
fopen_s(&File, Filename, "rb");
if (File)
{
fseek(File, 0, SEEK_END);
long Size = ftell(File);
fseek(File, 0, SEEK_SET);
check(Size > 0);
OutData.resize(Size);
fread(&OutData[0], 1, Size, File);
fclose(File);
bSuccess = true;
}
if (OutSuccess)
{
*OutSuccess = bSuccess;
}
return OutData;
}
inline std::string LoadFileToString(const char* Filename, bool* OutSuccess = nullptr)
{
bool bSuccess = false;
std::string OutString;
FILE* File;
fopen_s(&File, Filename, "rb");
if (File)
{
fseek(File, 0, SEEK_END);
long Size = ftell(File);
check(Size > 0);
std::vector<char> Data(Size);
fseek(File, 0, SEEK_SET);
fread(&Data[0], 1, Size, File);
fclose(File);
OutString.resize(Size);
OutString = &Data[0];
bSuccess = true;
}
if (OutSuccess)
{
*OutSuccess = bSuccess;
}
return OutString;
}
// Returns Extension
inline std::string SplitPath(const std::string& FullPathToFilename, std::string& OutPath, std::string& OutFilename, bool bIncludeExtension)
{
char Buffer[1024];
char* PtrFilename = nullptr;
::GetFullPathNameA(FullPathToFilename.c_str(), sizeof(Buffer), Buffer, &PtrFilename);
OutPath = Buffer;
if (PtrFilename)
{
OutPath.resize(PtrFilename - Buffer);
OutFilename = PtrFilename;
}
std::string Extension;
auto ExtensionFound = OutFilename.rfind('.');
if (ExtensionFound != std::string::npos)
{
Extension = OutFilename.substr(ExtensionFound + 1);
if (!bIncludeExtension)
{
OutFilename.resize(ExtensionFound);
}
}
return Extension;
}
inline std::string GetBaseName(const std::string& FullPathToFilename, bool bExtension)
{
std::string Path;
std::string Filename;
SplitPath(FullPathToFilename, Path, Filename, bExtension);
return Filename;
}
inline std::string GetPath(const std::string& FullPathToFilename, bool bExtension)
{
std::string Path;
std::string Filename;
SplitPath(FullPathToFilename, Path, Filename, bExtension);
return Path;
}
inline void RemoveQuotes(std::string& Path)
{
if (Path.size() > 2 && Path.front() == '"')
{
check(Path.back() == '"');
Path.pop_back();
Path = Path.substr(1);
}
}
inline std::string MakePath(const std::string& Root, const std::string& DirOrFile)
{
std::string Out;
if (!Root.empty())
{
Out = Root;
RemoveQuotes(Out);
if (Root.back() != '\\')
{
Out += '\\';
}
}
Out += DirOrFile;
return Out;
}
inline std::string AddQuotes(const std::string& InPath)
{
std::string Path = InPath;
if (Path.size() > 2 && Path.front() == '"')
{
check(Path.back() == '"');
}
else
{
Path = "\"" + Path;
Path +="\"";
}
return Path;
}
inline std::vector<std::string> GetFilenamesInDir(const char* Path, bool bFullFilename)
{
std::vector<std::string> FileList;
if (Path && *Path)
{
std::string Filename = Path;
Filename += "\\*.*";
::WIN32_FIND_DATAA FindData;
::HANDLE Found = INVALID_HANDLE_VALUE;
Found = ::FindFirstFileA(Filename.c_str(), &FindData);
if (Found != INVALID_HANDLE_VALUE)
{
do
{
if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
std::string ItemName;
if (bFullFilename)
{
ItemName = Path;
ItemName += "\\";
}
ItemName += FindData.cFileName;
FileList.push_back(ItemName);
}
}
while (::FindNextFileA(Found, &FindData));
::FindClose(Found);
}
}
return FileList;
}
// Returns true is Src is newer than Dst or if Dst doesn't exist
inline bool IsNewerThan(const std::string& Src, const std::string& Dst)
{
HANDLE SrcHandle = ::CreateFileA(Src.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (SrcHandle == INVALID_HANDLE_VALUE)
{
return false;
}
HANDLE DstHandle = ::CreateFileA(Dst.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (DstHandle == INVALID_HANDLE_VALUE)
{
::CloseHandle(SrcHandle);
return true;
}
FILETIME SrcTime, DstTime;
if (!::GetFileTime(SrcHandle, nullptr, nullptr, &SrcTime))
{
auto Error = ::GetLastError();
check(0);
return false;
}
if (!::GetFileTime(DstHandle, nullptr, nullptr, &DstTime))
{
auto Error = ::GetLastError();
check(0);
return false;
}
bool bResult = false;
if (SrcTime.dwHighDateTime < DstTime.dwHighDateTime)
{
bResult = false;
}
else if (SrcTime.dwHighDateTime == DstTime.dwHighDateTime)
{
bResult = SrcTime.dwLowDateTime > DstTime.dwLowDateTime;
}
else
{
bResult = true;
}
::CloseHandle(DstHandle);
::CloseHandle(SrcHandle);
return bResult;
}
}