Euphoria
vfs_path.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 
5 #include <optional>
6 #include <tuple>
7 #include <type_traits>
8 
9 namespace eu::io
10 {
11  // here lies the definitions of a virtual path
12  // can either be a file or a directory
13  // (prefer directory to folder, since it can autocomplete from 1 char)
14  //
15  // a path is case sensitive
16  // a path should be lowercase, but this is not required
17  // a path should only contain ascii characters but this is not required
18  // a slash (/) separates folders or directories
19  //
20  // File specifications
21  // filename or extension cannot contain slashes (/)
22  // filenames cannot contain dots (.)
23  // extension is either empty, or a string like txt or tar.gz and
24  // can't start/end with a dot (.)
25  //
26  // Directory specifications
27  // tilde (~) represents the root folder (absolute path)
28  // a single dot (.) represents the "current" folder (relative path)
29  // two dots (..) represents the parent folder (relative path)
30 
31  struct FilePath;
32  struct DirPath;
33 
34 
35  struct FilePath
36  {
39  std::string path;
40 
41  explicit
42  FilePath(const std::string& p);
43 
45  static std::optional<FilePath> from_script(const std::string& path);
46 
48  static std::optional<FilePath> from_dirty_source(const std::string& path);
49 
51  static std::optional<FilePath> from_script_or_empty(const std::string& path);
52 
53  [[nodiscard]] FilePath set_extension_copy(const std::string& ext) const;
54  [[nodiscard]] FilePath extend_extension_copy(const std::string& ext) const;
55 
56  [[nodiscard]] std::tuple<DirPath, std::string> split_directories_and_file() const;
57  [[nodiscard]] DirPath get_directory() const;
58  [[nodiscard]] std::string get_file_with_extension() const;
59  [[nodiscard]] std::string get_filename_without_extension() const;
60  [[nodiscard]] std::string get_extension() const;
61  };
62 
63  std::string to_string(const FilePath& p);
64 
65 
66  struct DirPath
67  {
69  std::string path;
70 
71  explicit DirPath(const std::string& p);
72 
73  [[nodiscard]] static DirPath from_root();
74  [[nodiscard]] static DirPath from_relative();
75  [[nodiscard]] static DirPath from_dirs(const std::vector<std::string>& dirs);
76 
77  [[nodiscard]] FilePath get_file(const std::string& filename) const;
78  [[nodiscard]] DirPath get_directory(const std::string& single) const;
79 
80  [[nodiscard]] bool is_relative() const;
81  [[nodiscard]] bool contains_relative() const;
82  [[nodiscard]] DirPath get_parent_directory() const;
83  [[nodiscard]] std::string get_name() const;
84  [[nodiscard]] std::vector<std::string> split_directories() const;
85  };
86 
87  std::string to_string(const DirPath& p);
88 
89 
90  std::optional<DirPath>
92 
93 
94  std::optional<DirPath>
95  resolve_relative(const DirPath& base, const DirPath& root);
96 
97 
98  std::optional<FilePath>
100 
101 
102  std::optional<FilePath>
103  resolve_relative(const FilePath& base, const DirPath& root);
104 
105 
106  DirPath
107  join(const DirPath& lhs, const DirPath& rhs);
108 
109 
110  FilePath
111  join(const DirPath& lhs, const FilePath& rhs);
112 
113 
114  bool
115  operator==(const DirPath& lhs, const DirPath& rhs);
116 
117 
118  bool
119  operator==(const FilePath& lhs, const FilePath& rhs);
120 
121 
122  bool
123  operator!=(const DirPath& lhs, const DirPath& rhs);
124 
125 
126  bool
127  operator!=(const FilePath& lhs, const FilePath& rhs);
128 
129 
130  bool
131  operator<(const DirPath& lhs, const DirPath& rhs);
132 
133 
134  bool
135  operator<(const FilePath& lhs, const FilePath& rhs);
136 }
137 
ParserBase * base
Definition: argparse.cc:887
Definition: enum.h:8
std::string to_string(const FilePath &p)
Definition: vfs_path.cc:321
bool operator!=(const DirPath &lhs, const DirPath &rhs)
Definition: vfs_path.cc:458
std::optional< DirPath > resolve_relative(const DirPath &base)
Definition: vfs_path.cc:349
bool operator<(const DirPath &lhs, const DirPath &rhs)
Definition: vfs_path.cc:472
DirPath join(const DirPath &lhs, const DirPath &rhs)
Definition: vfs_path.cc:419
bool operator==(const DirPath &lhs, const DirPath &rhs)
Definition: vfs_path.cc:444
DirPath get_directory(const std::string &single) const
Definition: vfs_path.cc:329
bool is_relative() const
Definition: vfs_path.cc:260
DirPath(const std::string &p)
Definition: vfs_path.cc:336
std::string get_name() const
Definition: vfs_path.cc:300
DirPath get_parent_directory() const
Definition: vfs_path.cc:286
static DirPath from_dirs(const std::vector< std::string > &dirs)
Definition: vfs_path.cc:232
std::string path
contains either . or ~ at the start, / at the end
Definition: vfs_path.h:69
bool contains_relative() const
Definition: vfs_path.cc:270
static DirPath from_root()
Definition: vfs_path.cc:218
static DirPath from_relative()
Definition: vfs_path.cc:225
std::vector< std::string > split_directories() const
Definition: vfs_path.cc:309
FilePath get_file(const std::string &filename) const
Definition: vfs_path.cc:252
FilePath extend_extension_copy(const std::string &ext) const
Definition: vfs_path.cc:189
std::tuple< DirPath, std::string > split_directories_and_file() const
Definition: vfs_path.cc:116
static std::optional< FilePath > from_script_or_empty(const std::string &path)
optional or not, log if error
Definition: vfs_path.cc:102
FilePath set_extension_copy(const std::string &ext) const
Definition: vfs_path.cc:170
DirPath get_directory() const
Definition: vfs_path.cc:137
std::string path
contains either .
Definition: vfs_path.h:39
std::string get_file_with_extension() const
Definition: vfs_path.cc:144
std::string get_extension() const
Definition: vfs_path.cc:161
std::string get_filename_without_extension() const
Definition: vfs_path.cc:151
static std::optional< FilePath > from_script(const std::string &path)
apply only minor changes, return null on invalid
Definition: vfs_path.cc:46
static std::optional< FilePath > from_dirty_source(const std::string &path)
do everything possible to convert from dirty path to valid path
Definition: vfs_path.cc:75
FilePath(const std::string &p)
Definition: vfs_path.cc:205
ADD_DEFAULT_FORMATTER(eu::io::FilePath, std::string, eu::io::to_string)