|
10 | 10 | //
|
11 | 11 |
|
12 | 12 | #include "Path.hpp"
|
| 13 | +#include "Support/Debug.hpp" |
13 | 14 | #include <llvm/Support/FileSystem.h>
|
14 | 15 | #include <llvm/Support/Path.h>
|
| 16 | +#include <llvm/Support/MemoryBuffer.h> |
| 17 | + |
| 18 | +#include <fstream> |
15 | 19 |
|
16 | 20 | namespace clang {
|
17 | 21 | namespace mrdox {
|
18 | 22 |
|
19 |
| -//------------------------------------------------ |
| 23 | +static |
| 24 | +bool |
| 25 | +isDirsy( |
| 26 | + std::string_view nativeDirName) |
| 27 | +{ |
| 28 | + namespace path = llvm::sys::path; |
| 29 | + if(nativeDirName.empty()) |
| 30 | + return false; |
| 31 | + if(! path::is_separator( |
| 32 | + nativeDirName.back(), |
| 33 | + path::Style::windows_slash)) |
| 34 | + return false; |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +std::string_view |
| 39 | +makeDirsy( |
| 40 | + std::string& dirName) |
| 41 | +{ |
| 42 | + namespace path = llvm::sys::path; |
| 43 | + |
| 44 | + if( ! dirName.empty() && |
| 45 | + ! path::is_separator( |
| 46 | + dirName.back(), |
| 47 | + path::Style::windows_slash)) |
| 48 | + { |
| 49 | + auto const sep = path::get_separator(path::Style::native); |
| 50 | + dirName.push_back(sep.front()); |
| 51 | + } |
| 52 | + |
| 53 | + return dirName; |
| 54 | +} |
20 | 55 |
|
21 | 56 | std::string
|
22 |
| -makeFullPath( |
23 |
| - std::string_view pathName, |
| 57 | +makeAbsoluteDirectory( |
| 58 | + std::string_view dirName, |
24 | 59 | std::string_view workingDir)
|
25 | 60 | {
|
26 | 61 | namespace path = llvm::sys::path;
|
27 | 62 |
|
28 |
| - llvm::SmallString<0> result; |
| 63 | + Assert(isDirsy(workingDir)); |
29 | 64 |
|
30 |
| - if(! path::is_absolute(pathName)) |
| 65 | + if(! path::is_absolute(dirName, path::Style::windows_slash)) |
31 | 66 | {
|
32 |
| - result = workingDir; |
33 |
| - path::append(result, path::Style::posix, pathName); |
34 |
| - path::remove_dots(result, true, path::Style::posix); |
| 67 | + SmallPathString result(workingDir); |
| 68 | + path::append(result, path::Style::native, dirName); |
| 69 | + path::remove_dots(result, true, path::Style::native); |
| 70 | + makeDirsy(result); |
35 | 71 | return std::string(result);
|
36 | 72 | }
|
37 | 73 |
|
38 |
| - result = pathName; |
39 |
| - path::remove_dots(result, true); |
40 |
| - convert_to_slash(result); |
| 74 | + SmallPathString result(workingDir); |
| 75 | + path::remove_dots(result, true, path::Style::native); |
| 76 | + makeDirsy(result); |
41 | 77 | return std::string(result);
|
42 | 78 | }
|
43 | 79 |
|
| 80 | +std::string |
| 81 | +makeFilePath( |
| 82 | + std::string_view dirName, |
| 83 | + std::string_view fileName) |
| 84 | +{ |
| 85 | + namespace path = llvm::sys::path; |
| 86 | + |
| 87 | + SmallPathString result(dirName); |
| 88 | + path::append(result, path::Style::native, fileName); |
| 89 | + path::remove_dots(result, true, path::Style::native); |
| 90 | + return std::string(result); |
| 91 | +} |
44 | 92 |
|
45 | 93 | //------------------------------------------------
|
46 | 94 |
|
@@ -69,5 +117,109 @@ makeDirsy(
|
69 | 117 | }
|
70 | 118 | }
|
71 | 119 |
|
| 120 | +//------------------------------------------------ |
| 121 | + |
| 122 | +Error |
| 123 | +forEachFile( |
| 124 | + std::string_view dirPath, |
| 125 | + AnyFileVisitor& visitor) |
| 126 | +{ |
| 127 | + namespace fs = llvm::sys::fs; |
| 128 | + namespace path = llvm::sys::path; |
| 129 | + |
| 130 | + std::error_code ec; |
| 131 | + fs::directory_iterator const end{}; |
| 132 | + fs::directory_iterator it(dirPath, ec, false); |
| 133 | + if(ec) |
| 134 | + return Error("fs::directory_iterator(\"{}\") returned \"{}\"", dirPath, ec); |
| 135 | + while(it != end) |
| 136 | + { |
| 137 | + if(it->type() == fs::file_type::directory_file) |
| 138 | + { |
| 139 | + std::string s = it->path(); |
| 140 | + makeDirsy(s); |
| 141 | + auto err = visitor.visitFile(s); |
| 142 | + if(err) |
| 143 | + return err; |
| 144 | + } |
| 145 | + else if(it->type() == fs::file_type::regular_file) |
| 146 | + { |
| 147 | + auto err = visitor.visitFile(it->path()); |
| 148 | + if(err) |
| 149 | + return err; |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + // we don't handle this type |
| 154 | + } |
| 155 | + it.increment(ec); |
| 156 | + if(ec) |
| 157 | + return Error("directory_iterator::increment returned \"{}\"", ec); |
| 158 | + } |
| 159 | + return Error::success(); |
| 160 | +} |
| 161 | + |
| 162 | +//------------------------------------------------ |
| 163 | + |
| 164 | +namespace files { |
| 165 | + |
| 166 | +std::string_view |
| 167 | +getFileName( |
| 168 | + std::string_view pathName) |
| 169 | +{ |
| 170 | + namespace path = llvm::sys::path; |
| 171 | + |
| 172 | + return path::filename(pathName); |
| 173 | +} |
| 174 | + |
| 175 | +Expected<std::string> |
| 176 | +getFileText( |
| 177 | + std::string_view pathName) |
| 178 | +{ |
| 179 | + std::ifstream file((std::string(pathName))); |
| 180 | + if(! file.good()) |
| 181 | + return Error("std::ifstream(\"{}\" returned \"{}\"", |
| 182 | + pathName, std::error_code(errno, std::generic_category())); |
| 183 | + std::istreambuf_iterator<char> it(file); |
| 184 | + std::istreambuf_iterator<char> const end; |
| 185 | + std::string text(it, end); |
| 186 | + if(! file.good()) |
| 187 | + return Error("getFileText(\"{}\") returned \"{}\"", |
| 188 | + pathName, std::error_code(errno, std::generic_category())); |
| 189 | + return text; |
| 190 | +} |
| 191 | + |
| 192 | +std::string |
| 193 | +makeDirsy( |
| 194 | + std::string_view pathName) |
| 195 | +{ |
| 196 | + namespace path = llvm::sys::path; |
| 197 | + |
| 198 | + std::string result = static_cast<std::string>(pathName); |
| 199 | + if( ! result.empty() && |
| 200 | + ! path::is_separator( |
| 201 | + result.back(), |
| 202 | + path::Style::windows_slash)) |
| 203 | + { |
| 204 | + auto const sep = path::get_separator(path::Style::native); |
| 205 | + result.push_back(sep.front()); |
| 206 | + } |
| 207 | + return result; |
| 208 | +} |
| 209 | + |
| 210 | +std::string |
| 211 | +appendPath( |
| 212 | + std::string_view basePath, |
| 213 | + std::string_view pathName) |
| 214 | +{ |
| 215 | + namespace path = llvm::sys::path; |
| 216 | + |
| 217 | + SmallPathString temp(makeDirsy(basePath)); |
| 218 | + path::append(temp, pathName); |
| 219 | + return static_cast<std::string>(temp.str()); |
| 220 | +} |
| 221 | + |
| 222 | +} // files |
| 223 | + |
72 | 224 | } // mrdox
|
73 | 225 | } // clang
|
0 commit comments