Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1 | //===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the operating system PathV2 API. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/PathV2.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
| 16 | #include <cctype> |
| 17 | |
| 18 | namespace { |
| 19 | using llvm::StringRef; |
| 20 | |
| 21 | bool is_separator(const char value) { |
| 22 | switch(value) { |
| 23 | #ifdef LLVM_ON_WIN32 |
| 24 | case '\\': // fall through |
| 25 | #endif |
| 26 | case '/': return true; |
| 27 | default: return false; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | #ifdef LLVM_ON_WIN32 |
| 32 | const StringRef separators = "\\/"; |
| 33 | const char prefered_separator = '\\'; |
| 34 | #else |
| 35 | const StringRef separators = "/"; |
| 36 | const char prefered_separator = '/'; |
| 37 | #endif |
| 38 | |
| 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. |
| 43 | // * {/,\} |
| 44 | // * {.,..} |
| 45 | // * {file,directory}name |
| 46 | |
| 47 | if (path.empty()) |
| 48 | return path; |
| 49 | |
| 50 | // C: |
| 51 | if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':') |
| 52 | return StringRef(path.begin(), 2); |
| 53 | |
| 54 | // //net |
| 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) |
| 61 | return path; |
| 62 | else |
| 63 | return StringRef(path.begin(), end); |
| 64 | } |
| 65 | |
| 66 | // {/,\} |
| 67 | if (path[0] == '\\' || path[0] == '/') |
| 68 | return StringRef(path.begin(), 1); |
| 69 | |
| 70 | if (path.startswith("..")) |
| 71 | return StringRef(path.begin(), 2); |
| 72 | |
| 73 | if (path[0] == '.') |
| 74 | return StringRef(path.begin(), 1); |
| 75 | |
| 76 | // * {file,directory}name |
| 77 | size_t end = path.find_first_of("\\/", 2); |
| 78 | if (end == StringRef::npos) |
| 79 | return path; |
| 80 | else |
| 81 | return StringRef(path.begin(), end); |
| 82 | |
| 83 | return StringRef(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | namespace llvm { |
| 88 | namespace sys { |
| 89 | namespace path { |
| 90 | |
| 91 | const_iterator begin(const StringRef &path) { |
| 92 | const_iterator i; |
| 93 | i.Path = path; |
| 94 | i.Component = find_first_component(path); |
| 95 | i.Position = 0; |
| 96 | return i; |
| 97 | } |
| 98 | |
| 99 | const_iterator end(const StringRef &path) { |
| 100 | const_iterator i; |
| 101 | i.Path = path; |
| 102 | i.Position = path.size(); |
| 103 | return i; |
| 104 | } |
| 105 | |
| 106 | const_iterator::reference const_iterator::operator*() const { |
| 107 | return Component; |
| 108 | } |
| 109 | |
| 110 | const_iterator::pointer const_iterator::operator->() const { |
| 111 | return &Component; |
| 112 | } |
| 113 | |
| 114 | const_iterator &const_iterator::operator++() { |
| 115 | assert(Position < Path.size() && "Tried to increment past end!"); |
| 116 | |
| 117 | // Increment Position to past the current component |
| 118 | Position += Component.size(); |
| 119 | |
| 120 | // Check for end. |
| 121 | if (Position == Path.size()) { |
| 122 | Component = StringRef(); |
| 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | // Both POSIX and Windows treat paths that begin with exactly two separators |
| 127 | // specially. |
| 128 | bool was_net = Component.size() > 2 && |
| 129 | is_separator(Component[0]) && |
| 130 | Component[1] == Component[0] && |
| 131 | !is_separator(Component[2]); |
| 132 | |
| 133 | // Handle separators. |
| 134 | if (is_separator(Path[Position])) { |
| 135 | // Root dir. |
| 136 | if (was_net |
| 137 | #ifdef LLVM_ON_WIN32 |
| 138 | // c:/ |
| 139 | || Component.endswith(":") |
| 140 | #endif |
| 141 | ) { |
| 142 | Component = StringRef(Path.begin() + Position, 1); |
| 143 | return *this; |
| 144 | } |
| 145 | |
| 146 | // Skip extra separators. |
| 147 | while (Position != Path.size() && |
| 148 | is_separator(Path[Position])) { |
| 149 | ++Position; |
| 150 | } |
| 151 | |
| 152 | // Treat trailing '/' as a '.'. |
| 153 | if (Position == Path.size()) { |
| 154 | --Position; |
| 155 | Component = "."; |
| 156 | return *this; |
| 157 | } |
| 158 | } |
| 159 | |
| 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); |
| 165 | |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | bool const_iterator::operator==(const const_iterator &RHS) const { |
| 170 | return Path.begin() == RHS.Path.begin() && |
| 171 | Position == RHS.Position; |
| 172 | } |
| 173 | |
| 174 | bool const_iterator::operator!=(const const_iterator &RHS) const { |
| 175 | return !(*this == RHS); |
| 176 | } |
| 177 | |
| 178 | error_code root_path(const StringRef &path, StringRef &result) { |
| 179 | const_iterator b = begin(path), |
| 180 | pos = b, |
| 181 | e = end(path); |
| 182 | if (b != e) { |
| 183 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 184 | bool has_drive = |
| 185 | #ifdef LLVM_ON_WIN32 |
| 186 | b->endswith(":"); |
| 187 | #else |
| 188 | false; |
| 189 | #endif |
| 190 | |
| 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); |
| 196 | } else { |
| 197 | // just {C:,//net}, return the first component. |
| 198 | result = *b; |
| 199 | return make_error_code(errc::success); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // POSIX style root directory. |
| 204 | if (is_separator((*b)[0])) { |
| 205 | result = *b; |
| 206 | return make_error_code(errc::success); |
| 207 | } |
| 208 | |
| 209 | // No root_path. |
| 210 | result = StringRef(); |
| 211 | return make_error_code(errc::success); |
| 212 | } |
| 213 | |
| 214 | // No path :(. |
| 215 | result = StringRef(); |
| 216 | return make_error_code(errc::success); |
| 217 | } |
| 218 | |
| 219 | error_code root_name(const StringRef &path, StringRef &result) { |
| 220 | const_iterator b = begin(path), |
| 221 | e = end(path); |
| 222 | if (b != e) { |
| 223 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 224 | bool has_drive = |
| 225 | #ifdef LLVM_ON_WIN32 |
| 226 | b->endswith(":"); |
| 227 | #else |
| 228 | false; |
| 229 | #endif |
| 230 | |
| 231 | if (has_net || has_drive) { |
| 232 | // just {C:,//net}, return the first component. |
| 233 | result = *b; |
| 234 | return make_error_code(errc::success); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // No path or no name. |
| 239 | result = StringRef(); |
| 240 | return make_error_code(errc::success); |
| 241 | } |
| 242 | |
| 243 | error_code root_directory(const StringRef &path, StringRef &result) { |
| 244 | const_iterator b = begin(path), |
| 245 | pos = b, |
| 246 | e = end(path); |
| 247 | if (b != e) { |
| 248 | bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0]; |
| 249 | bool has_drive = |
| 250 | #ifdef LLVM_ON_WIN32 |
| 251 | b->endswith(":"); |
| 252 | #else |
| 253 | false; |
| 254 | #endif |
| 255 | |
| 256 | if ((has_net || has_drive) && |
| 257 | // {C:,//net}, skip to the next component. |
| 258 | (++pos != e) && is_separator((*pos)[0])) { |
| 259 | result = *pos; |
| 260 | return make_error_code(errc::success); |
| 261 | } |
| 262 | |
| 263 | // POSIX style root directory. |
| 264 | if (!has_net && is_separator((*b)[0])) { |
| 265 | result = *b; |
| 266 | return make_error_code(errc::success); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // No path or no root. |
| 271 | result = StringRef(); |
| 272 | return make_error_code(errc::success); |
| 273 | } |
| 274 | |
| 275 | error_code has_root_name(const Twine &path, bool &result) { |
| 276 | SmallString<128> storage; |
| 277 | StringRef p = path.toStringRef(storage); |
| 278 | |
| 279 | if (error_code ec = root_name(p, p)) return ec; |
| 280 | result = !p.empty(); |
| 281 | return make_error_code(errc::success); |
| 282 | } |
| 283 | |
| 284 | error_code has_root_directory(const Twine &path, bool &result) { |
| 285 | SmallString<128> storage; |
| 286 | StringRef p = path.toStringRef(storage); |
| 287 | |
| 288 | if (error_code ec = root_directory(p, p)) return ec; |
| 289 | result = !p.empty(); |
| 290 | return make_error_code(errc::success); |
| 291 | } |
| 292 | |
| 293 | error_code relative_path(const StringRef &path, StringRef &result) { |
| 294 | StringRef root; |
| 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); |
| 298 | } |
| 299 | |
| 300 | error_code append(SmallVectorImpl<char> &path, const Twine &a, |
| 301 | const Twine &b, |
| 302 | const Twine &c, |
| 303 | const Twine &d) { |
| 304 | SmallString<32> a_storage; |
| 305 | SmallString<32> b_storage; |
| 306 | SmallString<32> c_storage; |
| 307 | SmallString<32> d_storage; |
| 308 | |
| 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)); |
| 314 | |
| 315 | for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(), |
| 316 | e = components.end(); |
| 317 | i != e; ++i) { |
| 318 | bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]); |
| 319 | bool component_has_sep = !i->empty() && is_separator((*i)[0]); |
| 320 | bool is_root_name; |
| 321 | if (error_code ec = has_root_name(*i, is_root_name)) return ec; |
| 322 | |
| 323 | if (path_has_sep) { |
| 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); |
| 327 | |
| 328 | // Append it. |
| 329 | path.append(c.begin(), c.end()); |
| 330 | continue; |
| 331 | } |
| 332 | |
| 333 | if (!component_has_sep && !(path.empty() && is_root_name)) { |
| 334 | // Add a separator. |
| 335 | path.push_back(prefered_separator); |
| 336 | } |
| 337 | |
| 338 | path.append(i->begin(), i->end()); |
| 339 | } |
| 340 | |
| 341 | return make_error_code(errc::success); |
| 342 | } |
| 343 | |
| 344 | error_code make_absolute(SmallVectorImpl<char> &path) { |
| 345 | StringRef p(path.data(), path.size()); |
| 346 | |
| 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; |
| 350 | |
| 351 | // Already absolute. |
| 352 | if (rootName && rootDirectory) |
| 353 | return make_error_code(errc::success); |
| 354 | |
| 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; |
| 358 | |
| 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); |
| 366 | } |
| 367 | |
| 368 | if (!rootName && rootDirectory) { |
| 369 | StringRef cdrn; |
| 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); |
| 376 | } |
| 377 | |
| 378 | if (rootName && !rootDirectory) { |
| 379 | StringRef pRootName; |
| 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; |
| 387 | |
| 388 | SmallString<128> res; |
| 389 | if (error_code ec = append(res, pRootName, bRootDirectory, |
| 390 | bRelativePath, pRelativePath)) return ec; |
| 391 | path.swap(res); |
| 392 | return make_error_code(errc::success); |
| 393 | } |
| 394 | |
| 395 | llvm_unreachable("All rootName and rootDirectory combinations should have " |
| 396 | "occurred above!"); |
| 397 | } |
| 398 | |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Include the truly platform-specific parts. |
| 404 | #if defined(LLVM_ON_UNIX) |
| 405 | #include "Unix/PathV2.inc" |
| 406 | #endif |
| 407 | #if defined(LLVM_ON_WIN32) |
| 408 | #include "Windows/PathV2.inc" |
| 409 | #endif |