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 "../coroutine/module.hpp"
8#include "../utility/utility.hpp"
9#include "../metadata/metadata.hpp"
10#include "../macros.hpp"
11#include <filesystem>
12#include <ranges>
13#include <fstream>
14#include <string>
15#include <ranges>
16
21hi_export_module(hikogui.path.path_location : intf);
22
23namespace hi { inline namespace v1 {
24
25
26hi_export template<typename Context>
27concept path_range =
28 std::ranges::input_range<Context> and
29 std::convertible_to<std::ranges::range_value_t<std::remove_cvref_t<Context>>, std::filesystem::path> and
30 not std::convertible_to<Context, std::filesystem::path>;
31
39hi_export template<path_range Locations>
40[[nodiscard]] inline std::optional<std::filesystem::path>
41find_path(Locations &&locations, std::filesystem::path const& ref) noexcept
42{
43 if (ref.is_absolute()) {
44 if (std::filesystem::exists(ref)) {
45 return ref;
46 } else {
47 return {};
48 }
49 } else {
50 for (hilet& base : locations) {
51 auto path = base / ref;
52 if (std::filesystem::exists(path)) {
53 return path;
54 }
55 }
56 return {};
57 }
58}
59
62hi_export [[nodiscard]] std::filesystem::path executable_file() noexcept;
63
67{
68 auto tmp = executable_file();
69 tmp.remove_filename();
70 return tmp;
71}
72
75hi_export [[nodiscard]] std::filesystem::path data_dir() noexcept;
76
79hi_export [[nodiscard]] std::filesystem::path log_dir() noexcept;
80
84
87hi_export [[nodiscard]] inline generator<std::filesystem::path> resource_dirs() noexcept;
88
91hi_export [[nodiscard]] inline generator<std::filesystem::path> system_font_files() noexcept;
92
95hi_export [[nodiscard]] inline generator<std::filesystem::path> font_files() noexcept;
96
99hi_export [[nodiscard]] inline generator<std::filesystem::path> theme_files() noexcept;
100
106hi_export [[nodiscard]] inline std::optional<std::filesystem::path> source_dir() noexcept
107{
108 using namespace std::literals;
109
110 // If the cmake_install.cmake file exists then the executable is located in a build directory.
111 hilet cmake_install_path = executable_dir() / "cmake_install.cmake";
112
113 if (std::filesystem::exists(cmake_install_path)) {
114 auto line = std::string{};
115 try {
116 auto fd = std::ifstream{cmake_install_path.string()};
117 line = getline(fd, 512);
118 fd.close();
119
120 } catch (...) {
121 return std::nullopt;
122 }
123
124 hilet cmake_install_start = "# Install script for directory: "s;
125 if (not line.starts_with(cmake_install_start)) {
126 return std::nullopt;
127 }
128
129 auto source_dir = std::filesystem::path{line.substr(cmake_install_start.size())};
130 if (not std::filesystem::exists(source_dir)) {
131 return std::nullopt;
132 }
133
134 return source_dir;
135
136 } else {
137 return std::nullopt;
138 }
139}
140
146[[nodiscard]] inline std::optional<std::filesystem::path> library_install_dir() noexcept
147{
148 // path is:
149 // - /install_dir/include/hikogui/path/path_location_impl.hpp
150 // - /build_dir/src/hikogui/path/path_location_impl.hpp
151 // becomes:
152 // - /install_dir/
153 // - /build_dir/
154 auto path = std::filesystem::path{__FILE__};
155 path.replace_filename("../../..");
156 auto install_path = std::filesystem::canonical(path);
157
159 return install_path;
160 } else {
161 return std::nullopt;
162 }
163}
164
165}} // namespace hi::v1
hi_export std::optional< std::filesystem::path > find_path(Locations &&locations, std::filesystem::path const &ref) noexcept
Find a path.
Definition path_location_intf.hpp:41
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
hi_export generator< std::filesystem::path > font_files() noexcept
The directories to search for font files of both the application and system.
hi_export std::filesystem::path executable_file() noexcept
Get the full path to this executable.
hi_export std::filesystem::path data_dir() noexcept
Get the full path to the directory where the application should store its data.
hi_export std::filesystem::path log_dir() noexcept
Get the full path to the directory where the application should store its log files.
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:146
hi_export generator< std::filesystem::path > resource_dirs() noexcept
The directories to search for resource files.
hi_export generator< std::filesystem::path > system_font_files() noexcept
The directories to search for system font files.
hi_export std::filesystem::path executable_dir() noexcept
Get the full path to the directory when this executable is located.
Definition path_location_intf.hpp:66
hi_export 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:127
hi_export generator< std::filesystem::path > theme_files() noexcept
The directories to search for theme files of the application.
hi_export std::filesystem::path preferences_file() noexcept
Get the full path to application preferences file.
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
hi_export std::optional< std::filesystem::path > source_dir() noexcept
Get the full path to source code of this executable.
Definition path_location_intf.hpp:106
Definition path_location_intf.hpp:27