forked from ValleyBell/in_vgm-libvgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
224 lines (174 loc) · 4.1 KB
/
utils.cpp
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
#include "utils.hpp"
#include <stddef.h>
#include <string.h>
#include <string>
#include <stdio.h>
#include <vector>
static const char* GetLastDirSeparator(const char* filePath)
{
if (strncmp(filePath, "\\\\", 2))
{
filePath += 2; // skip Windows network prefix
}
const char* sepPos1 = strrchr(filePath, '/');
const char* sepPos2 = strrchr(filePath, '\\');
if (!sepPos1)
{
return sepPos2;
}
else if (!sepPos2)
{
return sepPos1;
}
else
{
return (sepPos1 < sepPos2) ? sepPos2 : sepPos1;
}
}
const char* GetFileTitle(const char* filePath)
{
const char* dirSepPos;
dirSepPos = GetLastDirSeparator(filePath);
return (dirSepPos) ? &dirSepPos[1] : filePath;
}
const char* GetFileExtension(const char* filePath)
{
const char* dirSepPos;
const char* extDotPos;
dirSepPos = GetLastDirSeparator(filePath);
if (!dirSepPos)
{
dirSepPos = filePath;
}
extDotPos = strrchr(dirSepPos, '.');
return (!extDotPos) ? nullptr : (extDotPos + 1);
}
static const wchar_t* GetLastDirSeparator(const wchar_t* filePath)
{
if (wcsncmp(filePath, L"\\\\", 2))
{
filePath += 2; // skip Windows network prefix
}
const wchar_t* sepPos1 = wcsrchr(filePath, L'/');
const wchar_t* sepPos2 = wcsrchr(filePath, L'\\');
if (sepPos1 == NULL)
{
return sepPos2;
}
else if (sepPos2 == NULL)
{
return sepPos1;
}
else
{
return (sepPos1 < sepPos2) ? sepPos2 : sepPos1;
}
}
const wchar_t* GetFileTitle(const wchar_t* filePath)
{
const wchar_t* dirSepPos = GetLastDirSeparator(filePath);
return (dirSepPos) ? &dirSepPos[1] : filePath;
}
const wchar_t* GetFileExtension(const wchar_t* filePath)
{
const wchar_t* dirSepPos = GetLastDirSeparator(filePath);
if (dirSepPos == NULL)
{
dirSepPos = filePath;
}
const wchar_t* extDotPos = wcsrchr(dirSepPos, L'.');
return (!extDotPos) ? nullptr : (extDotPos + 1);
}
void StandardizeDirSeparators(std::string& filePath)
{
size_t curChr = 0;
if (! filePath.compare(curChr, 2, "\\\\"))
{
curChr += 2; // skip Windows network prefix
}
for (; curChr < filePath.length(); curChr ++)
{
if (filePath[curChr] == '\\')
{
filePath[curChr] = '/';
}
}
return;
}
static bool IsAbsolutePath(const char* filePath)
{
if (filePath[0] == '\0')
{
return false; // empty string
}
if (filePath[0] == '/' || filePath[0] == '\\')
{
return true; // absolute UNIX path / device-relative Windows path
}
if (filePath[1] == ':')
{
if ((filePath[0] >= 'A' && filePath[0] <= 'Z') ||
(filePath[0] >= 'a' && filePath[0] <= 'z'))
{
return true; // Windows device path: C:\path
}
}
if (! strncmp(filePath, "\\\\", 2))
{
return true; // Windows network path: \\computername\path
}
return false;
}
std::string CombinePaths(const std::string& basePath, const std::string& addPath)
{
if (basePath.empty() || IsAbsolutePath(addPath.c_str()))
{
return addPath;
}
char lastChr = basePath[basePath.length() - 1];
if (lastChr == '/' || lastChr == '\\')
{
return basePath + addPath;
}
else
{
return basePath + '/' + addPath;
}
}
std::string FindFile_List(const std::vector<std::string>& fileList, const std::vector<std::string>& pathList)
{
std::vector<std::string>::const_reverse_iterator pathIt;
std::vector<std::string>::const_reverse_iterator fileIt;
std::string fullName;
FILE* hFile;
for (pathIt = pathList.rbegin(); pathIt != pathList.rend(); ++pathIt)
{
for (fileIt = fileList.rbegin(); fileIt != fileList.rend(); ++fileIt)
{
fullName = CombinePaths(*pathIt, *fileIt);
hFile = fopen(fullName.c_str(), "r");
if (hFile != NULL)
{
fclose(hFile);
return fullName;
}
}
}
return "";
}
std::string FindFile_Single(const std::string& fileName, const std::vector<std::string>& pathList)
{
std::vector<std::string> fileList;
fileList.push_back(fileName);
return FindFile_List(fileList, pathList);
}
std::vector<std::string> CombineBaseWithList(const std::string base, const std::vector<std::string>& postfixes)
{
std::vector<std::string> result;
std::vector<std::string>::const_iterator pfIt;
for (pfIt = postfixes.begin(); pfIt != postfixes.end(); ++pfIt)
{
result.push_back(base + *pfIt);
}
return result;
}