blob: 12e505bb4e00c749150ba0c95509adf0ba5d352b (
plain)
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
|
/*
*/
#include "path.h"
#if _WIN32
# include <Windows.h>
#else
# include <unistd.h> // getcwd/chdir
#endif
helpers::path::path(const char* s):
m_str(s)
{
// 1. Normalise path separators to the system specified separator
for(size_t i = 0; i < m_str.size(); i ++)
{
if( m_str[i] == '/' || m_str[i] == '\\' )
m_str[i] = SEP;
}
// 2. Remove any trailing separators
if( !m_str.empty() )
{
while(!m_str.empty() && m_str.back() == SEP )
m_str.pop_back();
if(m_str.empty())
{
m_str.push_back(SEP);
}
}
else
{
throw ::std::runtime_error("Empty path being constructed");
}
}
helpers::path helpers::path::to_absolute() const
{
if(!this->is_valid())
throw ::std::runtime_error("Calling to_absolute() on an invalid path");
if(this->m_str[0] == SEP)
return *this;
#if _WIN32
char cwd[1024];
GetCurrentDirectoryA(sizeof(cwd), cwd);
#else
char cwd[1024];
if( !getcwd(cwd, sizeof(cwd)) )
throw ::std::runtime_error("Calling getcwd() failed in path::to_absolute()");
#endif
auto rv = path(cwd);
for(auto comp : *this)
{
if(comp == ".")
;
else if( comp == ".." )
rv.pop_component();
else
rv /= comp;
}
#if _WIN32
#else
#endif
return rv;
}
helpers::path helpers::path::normalise() const
{
path rv;
rv.m_str.reserve( m_str.size()+1 );
for(auto comp : *this)
{
if( comp == "." ) {
// Ignore.
}
else if( comp == ".." )
{
// If the path is empty, OR the last element is a "..", push the element
if( rv.m_str.empty()
|| (rv.m_str.size() == 3 && rv.m_str[0] == '.' && rv.m_str[1] == '.' && rv.m_str[2] == SEP)
|| (rv.m_str.size() > 4 && *(rv.m_str.end()-4) == SEP && *(rv.m_str.end()-3) == '.' && *(rv.m_str.end()-2) == '.' && *(rv.m_str.end()-1) == SEP )
)
{
// Push
rv.m_str += comp;
rv.m_str += SEP;
}
else
{
rv.m_str.pop_back();
auto pos = rv.m_str.find_last_of(SEP);
if(pos == ::std::string::npos)
{
rv.m_str.resize(0);
}
else if( pos == 0 )
{
// Keep.
}
else
{
rv.m_str.resize(pos+1);
}
}
}
else
{
rv.m_str += comp;
rv.m_str += SEP;
}
}
rv.m_str.pop_back();
return rv;
}
#if 0
void helpers::path::normalise_in_place()
{
size_t insert_point = 0;
for(size_t read_pos = 0; read_pos < m_str.size(); read_pos ++)
{
auto pos = m_str.find_first_of(SEP, read_pos);
if(pos == ::std::string::npos)
pos = m_str.size();
auto comp = string_view(m_str.c_str() + read_pos, pos - read_pos);
bool append;
if(comp == ".")
{
// Advance read without touching insert
append = false;
}
else if( comp == ".." )
{
// Consume parent (if not a relative component already)
// Move insertion point back to the previous separator
auto pos = m_str.find_last_of(SEP, insert_point);
if(pos == ::std::string::npos)
{
// Only one component currently (or empty)
append = true;
}
else if(string_view(m_str.c_str() + pos+1, insert_point - pos-1) == "..")
{
// Last component is ".." - keep adding
append = true;
}
else
{
insert_point = pos;
append = false;
}
}
else
{
append = true;
}
if(append)
{
if( read_pos != insert_point )
{
//assert(read_pos > insert_point);
while(read_pos < pos)
{
m_str[insert_point++] = m_str[read_pos++];
}
}
}
else
{
read_pos = pos;
}
}
}
#endif
void helpers::path::ComponentsIter::operator++()
{
if(end == p.m_str.size())
{
pos = end;
}
else
{
pos = end+1;
end = p.m_str.find(SEP, pos);
if(end == ::std::string::npos)
end = p.m_str.size();
}
}
|