SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
filesystem.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <filesystem>
4 #include <iostream>
5 #include <vector>
6 
7 #include "../logging.h"
8 
9 namespace benchmax {
10 
11 /// Computes the common prefix of two paths.
12 inline std::filesystem::path common_prefix(const std::filesystem::path& p1, const std::filesystem::path& p2) {
13  std::filesystem::path result;
14  for (
15  auto it1 = p1.begin(), it2 = p2.begin();
16  it1 != p1.end() && it2 != p2.end();
17  ++it1, ++it2)
18  {
19  if (*it1 != *it2) break;
20  result /= *it1;
21  }
22  return result;
23 }
24 
25 /**
26  * Computes the common prefix of a list of paths.
27  * skip_last specifies whether the last part if the path (usually the filename) may be part of the prefix.
28  */
29 inline std::filesystem::path common_prefix(const std::vector<std::filesystem::path>& s, bool skip_last = true) {
30  if (s.empty()) return std::filesystem::path();
31  auto cur = s.front();
32  if (skip_last) {
33  cur = cur.parent_path();
34  }
35  for (std::size_t i = 1; i < s.size(); i++) {
36  cur = common_prefix(cur, s[i]);
37  }
38  return cur;
39 }
40 
41 /**
42  * Computes the common prefix of multiple lists of paths.
43  * skip_last specifies whether the last part if the path (usually the filename) may be part of the prefix.
44  */
45 inline std::filesystem::path common_prefix(const std::initializer_list<std::vector<std::filesystem::path>>& s, bool skip_last = true) {
46  std::vector<std::filesystem::path> all;
47  for (const auto& sv: s) {
48  all.insert(all.end(), sv.begin(), sv.end());
49  }
50  return common_prefix(all, skip_last);
51 }
52 
53 /// Remove a prefix from a path.
54 inline std::filesystem::path remove_prefix(const std::filesystem::path& s, const std::filesystem::path& prefix) {
55  return std::filesystem::relative(s, prefix);
56  //return s.substr(prefix.length());
57 }
58 
59 /// Checks whether the extension of the filename is as specified.
60 inline bool is_extension(const std::filesystem::path& path, const std::string& extension) {
61  if (!std::filesystem::is_regular_file(path)) return false;
62  return path.extension() == extension;
63 }
64 
65 }
std::filesystem::path remove_prefix(const std::filesystem::path &s, const std::filesystem::path &prefix)
Remove a prefix from a path.
Definition: filesystem.h:54
std::filesystem::path common_prefix(const std::filesystem::path &p1, const std::filesystem::path &p2)
Computes the common prefix of two paths.
Definition: filesystem.h:12
bool is_extension(const std::filesystem::path &path, const std::string &extension)
Checks whether the extension of the filename is as specified.
Definition: filesystem.h:60
std::vector< T > prefix(const std::vector< T > vars, std::size_t prefixSize)
Definition: utils.h:349