20#include <spdlog/spdlog.h>
23#if __has_include(<sys/utsname.h>)
24#include <sys/utsname.h>
34 explicit pipe_t(
const std::string &command,
int &result)
37#if defined(__linux) || defined(__unix) || defined(__APPLE__)
38 pipe_{popen(fmt::format(
"{} 2>&1", command).c_str(),
"r")}
40 pipe_{_popen(command.c_str(),
"r")}
45 ~pipe_t() { reset(); }
47 operator bool()
const {
return pipe_ !=
nullptr; }
49 FILE *get()
const {
return pipe_; }
56#if defined(__linux) || defined(__unix) || defined(__APPLE__)
72 constexpr size_t kBufferSize{1024};
73 std::array<char, kBufferSize> buffer{};
75 int result{EXIT_FAILURE};
77 pipe_t pipe{command, result};
80 throw std::runtime_error(
"popen() failed!");
83 while (fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr) {
84 output += buffer.data();
89 if (result != EXIT_SUCCESS) {
90 throw std::runtime_error(
91 fmt::format(
"External command '{}' failed: {}", command, output));
99 constexpr size_t kBufferSize{1024};
100 std::array<char, kBufferSize> buffer{};
101 int result{EXIT_FAILURE};
103 pipe_t pipe{command, result};
106 throw std::runtime_error(
"popen() failed!");
109 while (fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr) {
110 output += buffer.data();
115 if (result != EXIT_SUCCESS) {
116 throw std::runtime_error(
117 fmt::format(
"External command '{}' failed: {}", command, output));
123#if defined(__linux) || defined(__unix)
124 const char *value = std::getenv(name.c_str());
126 if (value ==
nullptr)
129 return std::string{value};
130#elif defined(WINDOWS) || defined(_WIN32) || defined(WIN32)
131 static constexpr auto kMaxEnvLength = 2096U;
132 static char value[kMaxEnvLength];
134 GetEnvironmentVariableA(name.c_str(), value, kMaxEnvLength);
135 if (ret == 0 || ret > kMaxEnvLength)
146 const auto env =
get_env(
"CLANGUML_GIT_COMMIT");
156 catch (std::runtime_error &e) {
164 const std::string &cmd,
const std::string &env_override)
166 auto env =
get_env(env_override);
176 catch (std::runtime_error &e) {
186 "git rev-parse --abbrev-ref HEAD",
"CLANGUML_GIT_BRANCH");
192 "git describe --tags --always",
"CLANGUML_GIT_REVISION");
203 "git rev-parse --show-toplevel",
"CLANGUML_GIT_TOPLEVEL_DIR");
209 return "Windows, 32-bit";
211 return "Windows, 64-bit";
212#elif __has_include(<sys/utsname.h>)
215 return fmt::format(
"{} {} {}", utsn.sysname, utsn.machine, utsn.release);
218#elif __APPLE__ || __MACH__
222#elif __unix__ || __unix
229std::string
ltrim(
const std::string &s)
231 const size_t start = s.find_first_not_of(
WHITESPACE);
232 return (start == std::string::npos) ?
"" : s.substr(start);
235std::string
rtrim(
const std::string &s)
237 const size_t end = s.find_last_not_of(
WHITESPACE);
238 return (end == std::string::npos) ?
"" : s.substr(0, end + 1);
244 if (res.find(
"typename ") == 0)
245 return res.substr(strlen(
"typename "));
253 const std::string &separator,
const std::string &input)
255 std::optional<std::pair<std::string, std::string>> res;
257 std::size_t pos = input.find(separator);
259 if (pos == std::string::npos) {
263 auto before = input.substr(0, pos);
264 auto after = input.substr(pos + separator.size());
266 return std::make_optional<std::pair<std::string, std::string>>(
267 std::move(before), std::move(after));
271 std::string str, std::string_view delimiter,
bool skip_empty)
273 std::vector<std::string> result;
277 result.push_back(std::move(str));
278 else if (!skip_empty)
279 result.push_back(std::move(str));
282 while (
static_cast<unsigned int>(!str.empty()) != 0U) {
283 auto index = str.find(delimiter);
284 if (index != std::string::npos) {
285 auto tok = str.substr(0, index);
287 result.push_back(std::move(tok));
288 else if (!skip_empty)
289 result.push_back(std::move(tok));
291 str = str.substr(index + delimiter.size());
295 result.push_back(std::move(str));
296 else if (!skip_empty)
297 result.push_back(std::move(str));
307 std::vector<std::string> result;
309 while (
static_cast<unsigned int>(!str.empty()) != 0U) {
310 auto index = std::find_if(
311 str.begin(), str.end(), [](
auto c) { return std::isspace(c); });
312 if (index != str.end()) {
313 auto tok = str.substr(0, std::distance(str.begin(), index));
315 result.push_back(std::move(tok));
316 str = str.substr(std::distance(str.begin(), index) + 1);
320 result.push_back(str);
328 const std::vector<std::string> &toks, std::string_view delimiter)
330 return fmt::format(
"{}", fmt::join(toks, delimiter));
333std::string
abbreviate(
const std::string &s,
const unsigned int max_length)
335 if (s.size() <= max_length)
340 res.resize(max_length);
342 if (res.size() > 3) {
343 res.replace(res.size() - 3, 3, 3,
'.');
350 const std::string &input, std::tuple<std::string, size_t, size_t> &result)
352 const std::regex alias_regex(R
"((@A\([^\).]+\)))");
355 std::sregex_iterator(input.begin(), input.end(), alias_regex);
356 auto end_it = std::sregex_iterator();
358 if (alias_it == end_it)
361 const std::smatch &match = *alias_it;
362 const std::string alias = match.str().substr(3, match.str().size() - 4);
364 std::get<0>(result) = alias;
365 std::get<1>(result) = match.position();
366 std::get<2>(result) = match.length();
372 const std::string &replace_with)
374 bool replaced{
false};
376 auto pos = input.find(pattern);
377 while (pos < input.size()) {
378 input.replace(pos, pattern.size(), replace_with);
379 pos = input.find(pattern, pos + replace_with.size());
386template <>
bool starts_with(
const std::string &s,
const std::string &prefix)
388 return s.rfind(prefix, 0) == 0;
391template <>
bool ends_with(
const std::string &value,
const std::string &suffix)
393 if (suffix.size() > value.size())
395 return std::equal(suffix.rbegin(), suffix.rend(), value.rbegin());
400 constexpr auto kSeedStart{0x6a3712b5};
401 constexpr auto kSeedShiftFirst{6};
402 constexpr auto kSeedShiftSecond{2};
404 return kSeedStart + (seed << kSeedShiftFirst) + (seed >> kSeedShiftSecond);
409 std::vector<std::string> path_tokens;
411 if (p.has_root_directory()) {
415 if (p.root_name().string().size() > 1) {
416 if (p.is_absolute()) {
417 path_tokens.push_back(std::string{
418 std::tolower(p.root_name().string().at(0), std::locale())});
426 for (; it != p.end(); it++)
427 path_tokens.push_back(it->string());
429 if (p.has_root_directory())
430 return fmt::format(
"/{}", fmt::join(path_tokens,
"/"));
432 return fmt::format(
"{}", fmt::join(path_tokens,
"/"));
436 const std::filesystem::path &p,
const std::filesystem::path &root)
441 auto result = root / p;
442 result = result.lexically_normal();
443 result.make_preferred();
449 const std::filesystem::path &child,
const std::filesystem::path &base)
451 if (child.has_root_directory() != base.has_root_directory())
454 const auto child_ln = child.lexically_normal();
455 const auto base_ln = base.lexically_normal();
457 if (child_ln == base_ln)
460 auto rel = child_ln.lexically_relative(base_ln);
461 return !rel.empty() && rel.native()[0] !=
'.';
479 unsigned current_line_length{0};
480 for (
const auto &token : tokens) {
481 if (current_line_length < width) {
486 result.back() =
'\n';
487 current_line_length = 0;
492 current_line_length += token.size() + 1;
501 const std::map<std::string, std::string> &m,
const std::string &path)
507 std::vector<std::string> keys;
508 keys.reserve(m.size());
509 for (
const auto &[key, pattern] : m) {
513 std::sort(keys.begin(), keys.end(),
514 [](
const std::string &a,
const std::string &b) {
515 return a.size() > b.size();
518 std::filesystem::path file_path{path};
520 for (
const auto &key : keys) {
521 const auto &pattern = m.at(key);
522 std::filesystem::path key_path{key};
525 return {{key, pattern}};
529 if ((path.empty() || file_path.is_relative() || path ==
".") &&
531 return {{
".", m.at(
".")}};