1 //===-- Path.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 header file implements the operating system Path concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Path.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/FileSystem.h"
24 using support::ulittle32_t;
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only TRULY operating system
29 //=== independent code.
30 //===----------------------------------------------------------------------===//
32 bool Path::operator==(const Path &that) const {
33 return path == that.path;
36 bool Path::operator<(const Path& that) const {
37 return path < that.path;
41 Path::isArchive() const {
43 if (fs::identify_magic(str(), type))
45 return type == fs::file_magic::archive;
49 Path::isDynamicLibrary() const {
51 if (fs::identify_magic(str(), type))
54 default: return false;
55 case fs::file_magic::macho_fixed_virtual_memory_shared_lib:
56 case fs::file_magic::macho_dynamically_linked_shared_lib:
57 case fs::file_magic::macho_dynamically_linked_shared_lib_stub:
58 case fs::file_magic::elf_shared_object:
59 case fs::file_magic::pecoff_executable: return true;
64 Path::isObjectFile() const {
66 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
71 StringRef Path::GetDLLSuffix() {
72 return &(LTDL_SHLIB_EXT[1]);
76 Path::appendSuffix(StringRef suffix) {
77 if (!suffix.empty()) {
84 Path::isBitcodeFile() const {
86 if (fs::identify_magic(str(), type))
88 return type == fs::file_magic::bitcode;
91 bool Path::hasMagicNumber(StringRef Magic) const {
92 std::string actualMagic;
93 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
94 return Magic == actualMagic;
98 static void getPathList(const char*path, std::vector<Path>& Paths) {
99 const char* at = path;
100 const char* delim = strchr(at, PathSeparator);
103 std::string tmp(at, size_t(delim-at));
104 if (tmpPath.set(tmp))
105 if (tmpPath.canRead())
106 Paths.push_back(tmpPath);
108 delim = strchr(at, PathSeparator);
112 if (tmpPath.set(std::string(at)))
113 if (tmpPath.canRead())
114 Paths.push_back(tmpPath);
117 static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
118 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
119 "Sep must be a 1-character string literal.");
123 // If the path is all slashes, return a single slash.
124 // Otherwise, remove all trailing slashes.
126 signed pos = static_cast<signed>(path.size()) - 1;
128 while (pos >= 0 && path[pos] == Sep[0])
132 return path[0] == Sep[0] ? Sep : ".";
137 while (i < pos && path[i] != Sep[0])
140 if (i == pos) // No slashes? Return "."
143 // There is at least one slash left. Remove all trailing non-slashes.
144 while (pos >= 0 && path[pos] != Sep[0])
147 // Remove any trailing slashes.
148 while (pos >= 0 && path[pos] == Sep[0])
152 return path[0] == Sep[0] ? Sep : ".";
154 return path.substr(0, pos+1);
157 // Include the truly platform-specific parts of this class.
158 #if defined(LLVM_ON_UNIX)
159 #include "Unix/Path.inc"
161 #if defined(LLVM_ON_WIN32)
162 #include "Windows/Path.inc"