1 //===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the operating system PathV2 API.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/PathV2.h"
15 #include "llvm/Support/ErrorHandling.h"
19 using llvm::StringRef;
21 bool is_separator(const char value) {
24 case '\\': // fall through
26 case '/': return true;
27 default: return false;
32 const StringRef separators = "\\/";
33 const char prefered_separator = '\\';
35 const StringRef separators = "/";
36 const char prefered_separator = '/';
39 StringRef find_first_component(const StringRef &path) {
40 // Look for this first component in the following order.
41 // * empty (in this case we return an empty string)
42 // * either C: or {//,\\}net.
45 // * {file,directory}name
51 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
52 return StringRef(path.begin(), 2);
55 if ((path.size() > 2) &&
56 (path.startswith("\\\\") || path.startswith("//")) &&
57 (path[2] != '\\' && path[2] != '/')) {
58 // Find the next directory separator.
59 size_t end = path.find_first_of("\\/", 2);
60 if (end == StringRef::npos)
63 return StringRef(path.begin(), end);
67 if (path[0] == '\\' || path[0] == '/')
68 return StringRef(path.begin(), 1);
70 if (path.startswith(".."))
71 return StringRef(path.begin(), 2);
74 return StringRef(path.begin(), 1);
76 // * {file,directory}name
77 size_t end = path.find_first_of("\\/", 2);
78 if (end == StringRef::npos)
81 return StringRef(path.begin(), end);
91 const_iterator begin(const StringRef &path) {
94 i.Component = find_first_component(path);
99 const_iterator end(const StringRef &path) {
102 i.Position = path.size();
106 const_iterator::reference const_iterator::operator*() const {
110 const_iterator::pointer const_iterator::operator->() const {
114 const_iterator &const_iterator::operator++() {
115 assert(Position < Path.size() && "Tried to increment past end!");
117 // Increment Position to past the current component
118 Position += Component.size();
121 if (Position == Path.size()) {
122 Component = StringRef();
126 // Both POSIX and Windows treat paths that begin with exactly two separators
128 bool was_net = Component.size() > 2 &&
129 is_separator(Component[0]) &&
130 Component[1] == Component[0] &&
131 !is_separator(Component[2]);
133 // Handle separators.
134 if (is_separator(Path[Position])) {
139 || Component.endswith(":")
142 Component = StringRef(Path.begin() + Position, 1);
146 // Skip extra separators.
147 while (Position != Path.size() &&
148 is_separator(Path[Position])) {
152 // Treat trailing '/' as a '.'.
153 if (Position == Path.size()) {
160 // Find next component.
161 size_t end_pos = Path.find_first_of(separators, Position);
162 if (end_pos == StringRef::npos)
163 end_pos = Path.size();
164 Component = StringRef(Path.begin() + Position, end_pos - Position);
169 bool const_iterator::operator==(const const_iterator &RHS) const {
170 return Path.begin() == RHS.Path.begin() &&
171 Position == RHS.Position;
174 bool const_iterator::operator!=(const const_iterator &RHS) const {
175 return !(*this == RHS);
178 error_code root_path(const StringRef &path, StringRef &result) {
179 const_iterator b = begin(path),
183 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
191 if (has_net || has_drive) {
192 if ((++pos != e) && is_separator((*pos)[0])) {
193 // {C:/,//net/}, so get the first two components.
194 result = StringRef(path.begin(), b->size() + pos->size());
195 return make_error_code(errc::success);
197 // just {C:,//net}, return the first component.
199 return make_error_code(errc::success);
203 // POSIX style root directory.
204 if (is_separator((*b)[0])) {
206 return make_error_code(errc::success);
210 result = StringRef();
211 return make_error_code(errc::success);
215 result = StringRef();
216 return make_error_code(errc::success);
219 error_code root_name(const StringRef &path, StringRef &result) {
220 const_iterator b = begin(path),
223 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
231 if (has_net || has_drive) {
232 // just {C:,//net}, return the first component.
234 return make_error_code(errc::success);
238 // No path or no name.
239 result = StringRef();
240 return make_error_code(errc::success);
243 error_code root_directory(const StringRef &path, StringRef &result) {
244 const_iterator b = begin(path),
248 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
256 if ((has_net || has_drive) &&
257 // {C:,//net}, skip to the next component.
258 (++pos != e) && is_separator((*pos)[0])) {
260 return make_error_code(errc::success);
263 // POSIX style root directory.
264 if (!has_net && is_separator((*b)[0])) {
266 return make_error_code(errc::success);
270 // No path or no root.
271 result = StringRef();
272 return make_error_code(errc::success);
275 error_code has_root_name(const Twine &path, bool &result) {
276 SmallString<128> storage;
277 StringRef p = path.toStringRef(storage);
279 if (error_code ec = root_name(p, p)) return ec;
281 return make_error_code(errc::success);
284 error_code has_root_directory(const Twine &path, bool &result) {
285 SmallString<128> storage;
286 StringRef p = path.toStringRef(storage);
288 if (error_code ec = root_directory(p, p)) return ec;
290 return make_error_code(errc::success);
293 error_code relative_path(const StringRef &path, StringRef &result) {
295 if (error_code ec = root_path(path, root)) return ec;
296 result = StringRef(path.begin() + root.size(), path.size() - root.size());
297 return make_error_code(errc::success);
300 error_code append(SmallVectorImpl<char> &path, const Twine &a,
304 SmallString<32> a_storage;
305 SmallString<32> b_storage;
306 SmallString<32> c_storage;
307 SmallString<32> d_storage;
309 SmallVector<StringRef, 4> components;
310 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
311 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
312 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
313 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
315 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
316 e = components.end();
318 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
319 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
321 if (error_code ec = has_root_name(*i, is_root_name)) return ec;
324 // Strip separators from beginning of component.
325 size_t loc = i->find_first_not_of(separators);
326 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
329 path.append(c.begin(), c.end());
333 if (!component_has_sep && !(path.empty() && is_root_name)) {
335 path.push_back(prefered_separator);
338 path.append(i->begin(), i->end());
341 return make_error_code(errc::success);
344 error_code make_absolute(SmallVectorImpl<char> &path) {
345 StringRef p(path.data(), path.size());
347 bool rootName, rootDirectory;
348 if (error_code ec = has_root_name(p, rootName)) return ec;
349 if (error_code ec = has_root_directory(p, rootDirectory)) return ec;
352 if (rootName && rootDirectory)
353 return make_error_code(errc::success);
355 // All of the following conditions will need the current directory.
356 SmallString<128> current_dir;
357 if (error_code ec = current_path(current_dir)) return ec;
359 // Relative path. Prepend the current directory.
360 if (!rootName && !rootDirectory) {
361 // Append path to the current directory.
362 if (error_code ec = append(current_dir, p)) return ec;
363 // Set path to the result.
364 path.swap(current_dir);
365 return make_error_code(errc::success);
368 if (!rootName && rootDirectory) {
370 if (error_code ec = root_name(current_dir, cdrn)) return ec;
371 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
372 if (error_code ec = append(curDirRootName, p)) return ec;
373 // Set path to the result.
374 path.swap(curDirRootName);
375 return make_error_code(errc::success);
378 if (rootName && !rootDirectory) {
380 StringRef bRootDirectory;
381 StringRef bRelativePath;
382 StringRef pRelativePath;
383 if (error_code ec = root_name(p, pRootName)) return ec;
384 if (error_code ec = root_directory(current_dir, bRootDirectory)) return ec;
385 if (error_code ec = relative_path(current_dir, bRelativePath)) return ec;
386 if (error_code ec = relative_path(p, pRelativePath)) return ec;
388 SmallString<128> res;
389 if (error_code ec = append(res, pRootName, bRootDirectory,
390 bRelativePath, pRelativePath)) return ec;
392 return make_error_code(errc::success);
395 llvm_unreachable("All rootName and rootDirectory combinations should have "
403 // Include the truly platform-specific parts.
404 #if defined(LLVM_ON_UNIX)
405 #include "Unix/PathV2.inc"
407 #if defined(LLVM_ON_WIN32)
408 #include "Windows/PathV2.inc"