summaryrefslogtreecommitdiffhomepage
path: root/file.cpp
blob: 3149f63a94ab3ea67f052da2161352e6c83b2022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "file.h"

#include <unordered_set>

#include <libreichwein/stringhelper.h>

#include "env.h"

namespace fs = std::filesystem;

const fs::path YMakefile{"YMakefile"};

// type of file can be built from dependencies
bool is_buildable_by_extension(const fs::path& p) {
 fs::path ext{p.extension()};
 return ext.empty() || ext == ".o";
}

namespace {
 std::unordered_set<fs::path> compile_unit_source_types{".cpp", ".c", ".cc", ".S", ".rs"};
}

// type of file is source of compile unit (no included types like headers)
bool is_compile_unit_source_by_extension(const fs::path& p) {
 fs::path ext{p.extension()};
 return compile_unit_source_types.find(ext) != compile_unit_source_types.end();
}

std::filesystem::path simplified_path(const std::filesystem::path& p)
{
 if (p.string().substr(0, 2) == "./") {
  return p.string().substr(2);
 }
 return p;
}

bool is_executable(const std::string& file) {
 std::vector<std::string> paths {Reichwein::Stringhelper::split(env_value("PATH"), ":")};

 for (const auto& i: paths) {
  fs::path path{i + "/" + file};
  if (fs::exists(path) && (fs::status(path).permissions() & std::filesystem::perms::others_exec) != std::filesystem::perms::none) {
   return true;
  }
 }

 return false;
}

std::string find_executable(const std::vector<std::string>& list) {
 std::vector<std::string> paths {Reichwein::Stringhelper::split(env_value("PATH"), ":")};

 for (const auto& i: list) {
  for (const auto& j: paths) {
   fs::path path{j + "/" + i};
   if (fs::exists(path) && (fs::status(path).permissions() & std::filesystem::perms::others_exec) != std::filesystem::perms::none) {
    return i;
   }
  }
 }

 return {};
}