forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryUtils.cpp
More file actions
89 lines (74 loc) · 2.4 KB
/
DirectoryUtils.cpp
File metadata and controls
89 lines (74 loc) · 2.4 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
/*
Directory utility functions that are common to all posix and posix-like platforms.
*/
#include "il2cpp-config.h"
#include "StringUtils.h"
#include "DirectoryUtils.h"
namespace il2cpp
{
namespace utils
{
bool Match(const std::string name, size_t nameIndex, const std::string& pattern, const size_t patternIndex)
{
const size_t nameLength = name.length();
for (size_t i = patternIndex, patternLength = pattern.length(); i < patternLength; ++i)
{
const char c = pattern[i];
if (c == '*')
{
if (i + 1 == patternLength) // Star is last character, match everything.
return true;
do
{
// Check that we match the rest of the pattern against name.
if (Match(name, nameIndex, pattern, i + 1))
return true;
}
while (nameIndex++ < nameLength);
return false;
}
else if (c == '?')
{
if (nameIndex == nameLength)
return false;
nameIndex++;
}
else
{
if (nameIndex == nameLength)
{
// A pattern ending with .* should match a file with no extension
// The pattern "file.*" should match "file"
if (c == '.' && i + 2 == patternLength && pattern[i + 1] == '*')
return true;
return false;
}
else if (name[nameIndex] != c)
{
return false;
}
nameIndex++;
}
}
// All characters matched
return nameIndex == nameLength;
}
bool Match(const std::string name, const std::string& pattern)
{
return Match(name, 0, pattern, 0);
}
std::string CollapseAdjacentStars(const std::string& pattern)
{
std::string matchPattern;
matchPattern.reserve(pattern.length());
// Collapse adjacent stars into one
for (size_t i = 0, length = pattern.length(); i < length; ++i)
{
if (i > 0 && pattern[i] == '*' && pattern[i - 1] == '*')
continue;
matchPattern.append(1, pattern[i]);
}
return matchPattern;
}
}
}