HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
path_location_intf.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../utility/utility.hpp"
8#include "../metadata/metadata.hpp"
9#include "../macros.hpp"
10#include <filesystem>
11#include <ranges>
12#include <fstream>
13#include <string>
14#include <ranges>
15
20hi_export_module(hikogui.path.path_location : intf);
21
22hi_export namespace hi { inline namespace v1 {
23
24
25template<typename Context>
26concept path_range =
27 std::ranges::input_range<Context> and
28 std::convertible_to<std::ranges::range_value_t<std::remove_cvref_t<Context>>, std::filesystem::path> and
29 not std::convertible_to<Context, std::filesystem::path>;
30
38template<path_range Locations>
39[[nodiscard]] hi_inline std::optional<std::filesystem::path>
40find_path(Locations &&locations, std::filesystem::path const& ref) noexcept
41{
42 if (ref.is_absolute()) {
43 if (std::filesystem::exists(ref)) {
44 return ref;
45 } else {
46 return {};
47 }
48 } else {
49 for (auto const& base : locations) {
50 auto path = base / ref;
51 if (std::filesystem::exists(path)) {
52 return path;
53 }
54 }
55 return {};
56 }
57}
58
62[[nodiscard]] std::filesystem::path executable_file() noexcept;
63
68{
69 auto tmp = executable_file();
70 tmp.remove_filename();
71 return tmp;
72}
73
77[[nodiscard]] std::filesystem::path data_dir() noexcept;
78
83
88
92[[nodiscard]] hi_inline generator<std::filesystem::path> resource_dirs() noexcept;
93
97[[nodiscard]] hi_inline generator<std::filesystem::path> system_font_dirs() noexcept;
98
102[[nodiscard]] hi_inline generator<std::filesystem::path> font_files() noexcept;
103
107[[nodiscard]] hi_inline generator<std::filesystem::path> theme_files() noexcept;
108
116{
117 if (not std::filesystem::exists(path)) {
118 return std::nullopt;
119 }
120
121 auto line = std::string{};
122 try {
123 auto fd = std::ifstream{path.string()};
124 line = getline(fd, 512);
125 fd.close();
126
127 } catch (...) {
128 return std::nullopt;
129 }
130
131 auto const cmake_install_start = std::string{"# Install script for directory: "};
132 if (not line.starts_with(cmake_install_start)) {
133 return std::nullopt;
134 }
135
136 auto source_dir = std::filesystem::path{line.substr(cmake_install_start.size())};
137 if (not std::filesystem::exists(source_dir)) {
138 return std::nullopt;
139 }
140
141 return source_dir;
142}
143
150[[nodiscard]] hi_inline std::optional<std::filesystem::path> source_dir() noexcept
151{
152 // If the cmake_install.cmake file exists then the executable is located in a build directory.
153 if (auto path = source_dir_parse_cmake_install(executable_dir() / "cmake_install.cmake")) {
154 return *path;
155 }
156
157 // When using a cmake multi-config generator, the cmake_install.cmake file is located one directory up.
158 if (auto path = source_dir_parse_cmake_install(executable_dir() / ".." / "cmake_install.cmake")) {
159 return *path;
160 }
161
162 return std::nullopt;
163}
164
171[[nodiscard]] hi_inline std::optional<std::filesystem::path> library_install_dir() noexcept
172{
173 // path is:
174 // - /install_dir/include/hikogui/path/path_location_impl.hpp
175 // - /build_dir/src/hikogui/path/path_location_impl.hpp
176 // becomes:
177 // - /install_dir/
178 // - /build_dir/
179 auto path = std::filesystem::path{__FILE__};
180 path.replace_filename("../../..");
181 auto install_path = std::filesystem::canonical(path);
182
184 return install_path;
185 } else {
186 return std::nullopt;
187 }
188}
189
190}} // namespace hi::v1
std::filesystem::path executable_file() noexcept
Get the full path to this executable.
std::filesystem::path log_dir() noexcept
Get the full path to the directory where the application should store its log files.
std::filesystem::path data_dir() noexcept
Get the full path to the directory where the application should store its data.
std::filesystem::path preferences_file() noexcept
Get the full path to application preferences file.
hi_inline std::filesystem::path executable_dir() noexcept
Get the full path to the directory when this executable is located.
Definition path_location_intf.hpp:67
hi_inline generator< std::filesystem::path > resource_dirs() noexcept
The directories to search for resource files.
hi_inline std::optional< std::filesystem::path > source_dir() noexcept
Get the full path to source code of this executable.
Definition path_location_intf.hpp:150
hi_inline generator< std::filesystem::path > font_files() noexcept
The directories to search for font files of both the application and system.
hi_inline std::optional< std::filesystem::path > library_install_dir() noexcept
The full path where HikoGUI is installed during compilation of the application.
Definition path_location_intf.hpp:171
hi_inline generator< std::filesystem::path > theme_files() noexcept
The directories to search for theme files of the application.
hi_inline generator< std::filesystem::path > system_font_dirs() noexcept
The directories to search for system font files.
hi_inline std::optional< std::filesystem::path > find_path(Locations &&locations, std::filesystem::path const &ref) noexcept
Find a path.
Definition path_location_intf.hpp:40
STL namespace.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
hi_inline std::optional< std::filesystem::path > source_dir_parse_cmake_install(std::filesystem::path path) noexcept
Parse the source dir from a cmake_install.cmake file.
Definition path_location_intf.hpp:115
hi_inline std::basic_string< CharT, Traits > getline(std::basic_istream< CharT, Traits > &in, size_t max_size) noexcept
Get a line from an input string, upto a maximum size.
Definition misc.hpp:112
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
Definition path_location_intf.hpp:26
T substr(T... args)