sandbox
Loading...
Searching...
No Matches
file_info.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_FILESYSTEM_FILE_INFO_HPP_
3#define LIBSBX_FILESYSTEM_FILE_INFO_HPP_
4
5#include <string>
6#include <filesystem>
7
8namespace sbx::filesystem {
9
10class file_info final {
11
12public:
13
14 file_info(const std::string& alias_path, const std::string& base_path, const std::string& file_name) {
15 _initialize(alias_path, base_path, file_name);
16 }
17
18 file_info() = delete;
19
20 ~file_info() = default;
21
22 inline auto filename() const -> const std::string& {
23 return _filename;
24 }
25
26 inline auto base_filename() const -> const std::string& {
27 return _base_filename;
28 }
29
30 inline auto extension() const -> const std::string& {
31 return _extension;
32 }
33
34 inline auto file_path() const -> const std::string& {
35 return _filepath;
36 }
37
38 inline auto virtual_path() const -> const std::string& {
39 return _virtual_path;
40 }
41
42
43 inline auto native_path() const -> const std::string& {
44 return _native_path;
45 }
46
47private:
48
49 auto _initialize(const std::string& alias_path, const std::string& base_path, const std::string& file_name) -> void {
50 auto stripped_file_name = std::string{file_name};
51
52 if (!base_path.empty() && file_name.find(base_path) == 0) {
53 stripped_file_name = file_name.substr(base_path.length());
54 } else if (!alias_path.empty() && file_name.find(alias_path) == 0) {
55 stripped_file_name = file_name.substr(alias_path.length());
56 }
57
58 while (!stripped_file_name.empty() && (stripped_file_name.front() == '/' || stripped_file_name.front() == '\\')) {
59 stripped_file_name.erase(stripped_file_name.begin());
60 }
61
62 const auto file_path = std::filesystem::path{stripped_file_name};
63
64 _filepath = file_path.generic_string();
65 _virtual_path = (std::filesystem::path{alias_path} / file_path).generic_string();
66 _native_path = (std::filesystem::path{base_path} / file_path).generic_string();
67
68 _filename = file_path.filename().string();
69 _extension = file_path.has_extension() ? file_path.extension().string() : "";
70 _base_filename = file_path.stem().string();
71 }
72
73 std::string _filename;
74 std::string _base_filename;
75 std::string _extension;
76
77 std::string _filepath;
78 std::string _virtual_path;
79 std::string _native_path;
80
81}; // class file_info
82
83inline auto operator==(const file_info& lhs, const file_info& rhs) -> bool {
84 return lhs.virtual_path() == rhs.virtual_path();
85}
86
87inline auto operator<(const file_info& lhs, const file_info& rhs) -> bool {
88 return lhs.virtual_path() < rhs.virtual_path();
89}
90
91} // namespace sbx::filesystem
92
93#endif // LIBSBX_FILESYSTEM_FILE_INFO_HPP_
Definition: file_info.hpp:10