-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
259 lines (217 loc) · 6.61 KB
/
Utils.cpp
File metadata and controls
259 lines (217 loc) · 6.61 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
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
/*
Copyright (c) 2024-2025 Toksisitee. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, refer to license.md or https://opensource.org/licenses/MIT
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
#include <windows.h>
#include <fstream>
#include <algorithm>
#include "App.h"
#include "Utils.h"
namespace Util
{
namespace FileSystem
{
void CreateFolder( const std::string& sFolderPath )
{
DWORD attrib = GetFileAttributesA( sFolderPath.c_str() );
if ( attrib == INVALID_FILE_ATTRIBUTES ) {
CreateDirectoryA( sFolderPath.c_str(), NULL );
}
}
std::string GetApplicationDirectory()
{
static std::string sFinalPath;
if ( sFinalPath.empty() ) {
wchar_t wszPath[MAX_PATH] = { 0 };
GetModuleFileNameW( NULL, wszPath, MAX_PATH );
wchar_t* pwszLastSlash = wcsrchr( wszPath, L'\\' );
if ( pwszLastSlash != nullptr ) {
*pwszLastSlash = L'\0';
}
int nSize = WideCharToMultiByte( CP_UTF8, 0, wszPath, -1, NULL, 0, NULL, NULL );
if ( nSize > 0 ) {
std::string str( nSize - 1, 0 );
WideCharToMultiByte( CP_UTF8, 0, wszPath, -1, &str[0], nSize, NULL, NULL );
sFinalPath = str;
}
}
return sFinalPath;
}
std::string GetAssetsDirectory()
{
static std::string sDataPath;
if ( sDataPath.empty() ) {
sDataPath = GetApplicationDirectory();
sDataPath += "\\Assets\\";
}
return sDataPath;
}
std::string GetCurrentDir()
{
char szBuffer[MAX_PATH];
if ( GetModuleFileNameA( NULL, szBuffer, MAX_PATH ) == 0 ) {
return "";
}
std::string fullPath( szBuffer );
size_t uSlash = fullPath.find_last_of( "\\/" );
if ( uSlash != std::string::npos ) {
return fullPath.substr( 0, uSlash );
}
return fullPath;
}
std::string FormatPath( const char* pFileName, const char* pFilePath )
{
char szDirBuffer[MAX_PATH];
char szPathBuffer[MAX_PATH];
if ( !pFilePath )
sprintf_s( szDirBuffer, MAX_PATH, GetCurrentDir().c_str() );
else
sprintf_s( szDirBuffer, MAX_PATH, pFilePath );
sprintf_s( szPathBuffer, MAX_PATH, "%s\\%s", szDirBuffer, pFileName );
return std::string( szPathBuffer );
}
const std::string& GetExportDirectory()
{
return g_Editor.GetExportDirectory();
}
std::string FormatExportPathFromFilePath( const std::string& sFilePath )
{
auto sFileName = GetFileName( sFilePath );
return FormatPath( sFileName.c_str(), GetExportDirectory().c_str() );
}
std::string FormatExportPathFromFileName( const std::string& sFileName )
{
return FormatPath( sFileName.c_str(), GetExportDirectory().c_str() );
}
[[nodiscard]] bool FileExists( const std::string& sFilePath )
{
struct stat buffer;
return (stat( sFilePath.c_str(), &buffer ) == 0);
}
[[nodiscard]] std::string GetFolderName( const std::string& sPath )
{
size_t uSlash = sPath.find_last_of( "/\\" );
if ( uSlash == std::string::npos ) {
return sPath;
}
return sPath.substr( uSlash + 1 );
}
[[nodiscard]] std::vector<uint8_t> ReadFileMagic( const std::string& sFilePath, size_t uMagicSize )
{
std::ifstream file( sFilePath, std::ios::binary );
if ( !file.is_open() ) {
return {};
}
std::vector<uint8_t> vecMagic( uMagicSize );
file.read( reinterpret_cast<char*>(vecMagic.data()), uMagicSize );
file.close();
return vecMagic;
}
[[nodiscard]] std::string GetFileExtension( const std::string& sFilePath )
{
size_t uDot = sFilePath.find_last_of( '.' );
if ( uDot == std::string::npos || uDot == sFilePath.length() - 1 ) {
return "";
}
return sFilePath.substr( uDot );
}
[[nodiscard]] std::string GetFileName( const std::string& sFilePath )
{
size_t uSlash = sFilePath.find_last_of( "/\\" );
if ( uSlash == std::string::npos ) {
return sFilePath;
}
return sFilePath.substr( uSlash + 1 );
}
[[nodiscard]] size_t GetFileSize( const std::string& sFilePath )
{
std::ifstream file( sFilePath, std::ios::binary | std::ios::ate );
if ( !file.is_open() ) {
return 0;
}
std::streamoff fileSize = file.tellg();
if ( fileSize < 0 ) {
return 0;
}
return static_cast<size_t>(fileSize);
}
[[nodiscard]] std::string GetLastCharacterInFilePath( const std::string& sFilePath )
{
size_t uDotPos = sFilePath.find_last_of( '.' );
if ( uDotPos == std::string::npos || uDotPos == 0 ) return "";
size_t uCharPos = uDotPos - 1;
if ( uCharPos < sFilePath.size() ) {
return std::string( 1, sFilePath[uCharPos] );
}
return "";
}
[[nodiscard]] std::string RemoveFileExtension( const std::string& sFilePath )
{
size_t uDot = sFilePath.find_last_of( '.' );
size_t uSlash = sFilePath.find_last_of( "/\\" );
if ( uDot != std::string::npos && (uSlash == std::string::npos || uDot > uSlash) ) {
return sFilePath.substr( 0, uDot );
}
return sFilePath;
}
[[nodiscard]] std::string GetParentDirectory( const std::string& sFilePath )
{
size_t uLastSlash = sFilePath.find_last_of( "/\\" );
if ( uLastSlash == std::string::npos ) {
return "";
}
size_t uSecondLastSlash = sFilePath.find_last_of( "/\\", uLastSlash - 1 );
if ( uSecondLastSlash == std::string::npos ) {
return "";
}
return sFilePath.substr( 0, uSecondLastSlash + 1 );
}
[[nodiscard]] bool PathExists( const std::string& sPath )
{
DWORD dwAttr = GetFileAttributesA( sPath.c_str() );
return (dwAttr != INVALID_FILE_ATTRIBUTES && (dwAttr & FILE_ATTRIBUTE_DIRECTORY));
}
void RemoveFileSpec( char* pszPath )
{
if ( !pszPath || !*pszPath )
return;
size_t uLen = strlen( pszPath );
for ( size_t i = uLen; i > 0; --i ) {
if ( pszPath[i] == '\\' || pszPath[i] == '/' ) {
pszPath[i] = '\0';
break;
}
}
}
}
std::string StringToLowerCopy( const std::string& sStr )
{
std::string sLower = sStr;
std::transform( sLower.begin(), sLower.end(), sLower.begin(), []( unsigned char c ) { return std::tolower( c ); } );
return sLower;
}
std::string wcstombs( void* pwszStr, size_t uBufferSize )
{
size_t i;
char* pszBuffer = (char*)malloc( uBufferSize );
wcstombs_s( &i, pszBuffer, uBufferSize, static_cast<PWSTR>(pwszStr), uBufferSize - 1 );
if ( pszBuffer ) {
auto sStr = std::string( pszBuffer );
free( pszBuffer );
return sStr;
}
return "";
}
void OpenDirectory( const std::string& sPath )
{
ShellExecuteA( 0, "open", sPath.c_str(), NULL, NULL, SW_SHOWNORMAL );
}
void OpenBrowser( const char* pszUrl )
{
ShellExecuteA( 0, "open", pszUrl, NULL, NULL, SW_SHOWNORMAL );
}
}