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
|
// file : butl/path.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#ifdef _WIN32
# include <cctype> // std::tolower
# include <cwctype> // std::towlower
#endif
namespace butl
{
#ifdef _WIN32
template <>
inline char path_traits<char>::
tolower (char c)
{
return std::tolower (c);
}
template <>
inline wchar_t path_traits<wchar_t>::
tolower (wchar_t c)
{
return std::towlower (c);
}
#endif
// @@ Should only enable_if P is basic_path<C, K1>.
//
template <class P, class C, class K>
inline P
path_cast (const basic_path<C, K>& p)
{
return P (p.path_, false);
}
template <class P, class C, class K>
inline P
path_cast (basic_path<C, K>&& p)
{
return P (std::move (p.path_), false);
}
template <typename C, typename K>
inline bool basic_path<C, K>::
simple () const
{
return
#ifndef _WIN32
root () ||
#endif
traits::find_separator (this->path_) == string_type::npos;
}
template <typename C, typename K>
inline bool basic_path<C, K>::
absolute () const
{
#ifdef _WIN32
return this->path_.size () > 1 && this->path_[1] == ':';
#else
return !this->path_.empty () && traits::is_separator (this->path_[0]);
#endif
}
template <typename C, typename K>
inline bool basic_path<C, K>::
root () const
{
#ifdef _WIN32
return this->path_.size () == 2 && this->path_[1] == ':';
#else
return this->path_.size () == 1 && traits::is_separator (this->path_[0]);
#endif
}
template <typename C, typename K>
inline bool basic_path<C, K>::
sub (const basic_path& p) const
{
size_type n (p.path_.size ());
if (n == 0)
return true;
size_type m (this->path_.size ());
// The second condition guards against the /foo-bar vs /foo case.
//
return m >= n &&
traits::compare (this->path_.c_str (), n, p.path_.c_str (), n) == 0 &&
(traits::is_separator (p.path_.back ()) || // p ends with a separator
m == n || // *this == p
traits::is_separator (this->path_[n])); // next char is a separator
}
template <typename C, typename K>
inline bool basic_path<C, K>::
sup (const basic_path& p) const
{
size_type n (p.path_.size ());
if (n == 0)
return true;
size_type m (this->path_.size ());
// The second condition guards against the /foo-bar vs bar case.
//
return m >= n &&
traits::compare (
this->path_.c_str () + m - n, n, p.path_.c_str (), n) == 0 &&
(m == n || // *this == p
traits::is_separator (this->path_[m - n - 1])); // prev char separator
}
template <typename C, typename K>
inline auto basic_path<C, K>::
begin () const -> iterator
{
size_type b, e;
if (this->path_.empty ())
b = e = string_type::npos;
#ifndef _WIN32
else if (root ())
{
// We want to return a single empty component. Here we return
// the begin position one past the end. Not sure if this legal.
//
b = 1;
e = string_type::npos;
}
#endif
else
{
b = 0;
e = traits::find_separator (this->path_);
}
return iterator (this->path_, b, e);
}
template <typename C, typename K>
inline auto basic_path<C, K>::
end () const -> iterator
{
return iterator (this->path_, string_type::npos, string_type::npos);
}
template <typename C, typename K>
inline basic_path<C, K>::
basic_path (const iterator& b, const iterator& e)
{
//assert (b.p_ == e.p_);
if (b != e)
{
this->path_.assign (
*b.p_, b.b_, (e.b_ != string_type::npos ? e.b_ - b.b_ - 1 : e.b_));
#ifndef _WIN32
if (this->path_.empty ())
this->path_ = '/';
#endif
// No init() should be necessary.
}
}
template <typename C, typename K>
inline basic_path<C, K>& basic_path<C, K>::
complete ()
{
if (relative ())
*this = current () / *this;
return *this;
}
template <typename C, typename K>
inline basic_path<C, K>& basic_path<C, K>::
realize ()
{
#ifdef _WIN32
complete ();
normalize ();
#else
traits::realize (this->path_);
#endif
return *this;
}
template <typename C, typename K>
inline typename basic_path<C, K>::dir_type basic_path<C, K>::
root_directory () const
{
return absolute ()
#ifdef _WIN32
// Disambiguate with dir_type(string_type,bool).
//
? dir_type (this->path_, static_cast<size_type> (2))
#else
? dir_type ("/")
#endif
: dir_type ();
}
template <typename C, typename K>
inline basic_path<C, K> basic_path<C, K>::
base () const
{
size_type p (traits::find_extension (this->path_));
return p != string_type::npos
? basic_path (this->path_.c_str (), p)
: *this;
}
template <typename C, typename K>
inline const C* basic_path<C, K>::
extension () const
{
size_type p (traits::find_extension (this->path_));
return p != string_type::npos ? this->path_.c_str () + p + 1 : nullptr;
}
#ifndef _WIN32
template <typename C, typename K>
inline typename basic_path<C, K>::string_type basic_path<C, K>::
posix_string () const
{
return string ();
}
#endif
}
|