//===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the operating system PathV2 API.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/PathV2.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include <cctype>
#include <cstdio>
#include <cstring>
namespace {
using llvm::StringRef;
using llvm::sys::path::is_separator;
#ifdef LLVM_ON_WIN32
const char *separators = "\\/";
const char prefered_separator = '\\';
#else
const char separators = '/';
const char prefered_separator = '/';
#endif
StringRef find_first_component(StringRef path) {
// Look for this first component in the following order.
// * empty (in this case we return an empty string)
// * either C: or {//,\\}net.
// * {/,\}
// * {.,..}
// * {file,directory}name
if (path.empty())
return path;
#ifdef LLVM_ON_WIN32
// C:
if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
return path.substr(0, 2);
#endif
// //net
if ((path.size() > 2) &&
is_separator(path[0]) &&
path[0] == path[1] &&
!is_separator(path[2])) {
// Find the next directory separator.
size_t end = path.find_first_of(separators, 2);
return path.substr(0, end);
}
// {/,\}
if (is_separator(path[0]))
return path.substr(0, 1);
if (path.startswith(".."))
return path.substr(0, 2);
if (path[0] == '.')
return path.substr(0, 1);
// * {file,directory}name
size_t end = path.find_first_of(separators, 2);
return path.substr(0, end);
}
size_t filename_pos(StringRef str) {
if (str.size() == 2 &&
is_separator(str[0]) &&
str[0] == str[1])
return 0;
if (str.size() > 0 && is_separator(str[str.size() - 1]))
return str.size() - 1;
size_t pos = str.find_last_of(separators, str.size() - 1);
#ifdef LLVM_ON_WIN32
if (pos == StringRef::npos)
pos = str.find_last_of(':', str.size() - 2);
#endif
if (pos == StringRef::npos ||
(pos == 1 && is_separator(str[0])))
return 0;
return pos + 1;
}
size_t root_dir_start(StringRef str) {
// case "c:/"
#ifdef LLVM_ON_WIN32
if (str.size() > 2 &&
str[1] == ':' &&
is_separator(str[2]))
return 2;
#endif
// case "//"
if (str.size() == 2 &&
is_separator(str[0]) &&
str[0] == str[1])
return StringRef::npos;
// case "//net"
if (str.size() > 3 &&
is_separator(str[0]) &&
str[0] == str[1] &&
!is_separator(str[2])) {
return str.find_first_of(separators, 2);
}
// case "/"
if (str.size() > 0 && is_separator(str[0]))
return 0;
return StringRef::npos;
}
size_t parent_path_end(StringRef path) {
size_t end_pos = filename_pos(path);
bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
// Skip separators except for root dir.
size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
while(end_pos > 0 &&
(end_pos - 1) != root_dir_pos &&
is_separator(path[end_pos - 1]))
--end_pos;
if (end_pos == 1 && root_dir_pos == 0 &&<