FireSTARR
Loading...
Searching...
No Matches
Util.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_UTIL_H
3#define FS_UTIL_H
4#include "stdafx.h"
5#include <filesystem>
6#include "Radians.h"
7namespace fs
8{
9class FileList : public vector<string>
10{
11public:
12 using vector::vector;
13// HACK: this is in C++23 but needs gcc 15
14#ifndef __cpp_lib_containers_ranges
15 void append_range(const FileList& rhs) noexcept { insert(end(), rhs.cbegin(), rhs.cend()); }
16#endif
17};
24template <class T>
25[[nodiscard]] T no_convert(int value, int) noexcept
26{
27 return static_cast<T>(value);
28}
39template <class Elem, class Traits, class Alloc>
40std::basic_istream<Elem, Traits>& getline(
41 std::basic_istream<Elem, Traits>* stream,
42 std::basic_string<Elem, Traits, Alloc>* str,
43 const Elem delimiter
44)
45{
46 // make template so we can use pointers directly
47 return getline(*stream, *str, delimiter);
48}
54[[nodiscard]] bool directory_exists(const char* dir) noexcept;
60[[nodiscard]] bool file_exists(const char* path) noexcept;
68[[nodiscard]] FileList read_directory(
69 const string_view name,
70 const string_view match = "*",
71 const bool for_files = true
72);
79[[nodiscard]] FileList find_rasters(const string_view dir, YearSize year);
84void make_directory(const char* dir) noexcept;
89void make_directory_recursive(string dir) noexcept;
95template <class T>
96[[nodiscard]] inline constexpr MathSize sq(const T& x)
97{
98 return static_cast<MathSize>(x) * static_cast<MathSize>(x);
99}
107template <unsigned int N, class T>
108[[nodiscard]] constexpr T pow_int(const T& base)
109{
110 // https://stackoverflow.com/questions/16443682/c-power-of-integer-template-meta-programming
111 return N == 0 ? 1
112 : N % 2 == 0 ? pow_int<N / 2, T>(base) * pow_int<N / 2, T>(base)
113 : base * pow_int<(N - 1) / 2, T>(base) * pow_int<(N - 1) / 2, T>(base);
114}
121template <unsigned int N, class T>
122[[nodiscard]] constexpr T bit_mask()
123{
124 return static_cast<T>(pow_int<N, int64_t>(2) - 1);
125}
132template <unsigned int N>
133[[nodiscard]] MathSize round_to_precision(const MathSize value) noexcept
134{
135 // HACK: this can't actually make the value be the precision we want due to
136 // floating point storage, but we can round it to what it would be if it were
137 // that precision
138 static const auto b = pow_int<N, int64_t>(10);
139 return round(value * b) / b;
140}
145{
149 atomic<size_t> count_;
153 string for_what_;
154
155public:
156 ~UsageCount();
161 explicit UsageCount(string for_what) noexcept;
162 UsageCount(UsageCount&& rhs) noexcept = delete;
163 UsageCount(const UsageCount& rhs) noexcept = delete;
164 UsageCount& operator=(UsageCount&& rhs) noexcept = delete;
165 UsageCount& operator=(const UsageCount& rhs) noexcept = delete;
170 UsageCount& operator++() noexcept;
171};
172// https://stackoverflow.com/questions/15843525/how-do-you-insert-the-value-in-a-sorted-vector
180template <typename T>
181[[nodiscard]] typename std::vector<T>::iterator insert_sorted(std::vector<T>* vec, T const& item)
182{
183 return vec->insert(std::upper_bound(vec->begin(), vec->end(), item), item);
184}
191template <typename T>
192void insert_unique(std::vector<T>* vec, T const& item)
193{
194 const auto i = std::lower_bound(vec->begin(), vec->end(), item);
195 if (i == vec->end() || *i != item)
196 {
197 vec->insert(i, item);
198 }
199}
211template <typename T, typename V>
212T binary_find(const T lower, const T upper, const V value, const std::function<V(T)>& fct)
213{
214 const auto mid = lower + (upper - lower) / 2;
215 if (lower == upper)
216 {
217 return lower;
218 }
219 if (fct(mid) < value)
220 {
221 return binary_find(lower, mid, value, fct);
222 }
223 return binary_find(mid + 1, upper, value, fct);
224}
236template <typename T, typename V>
237T binary_find_checked(const T lower, const T upper, const V value, const std::function<V(T)>& fct)
238{
239 if (fct(lower) < value)
240 {
241 return lower;
242 }
243 if (fct(upper) >= value)
244 {
245 return upper;
246 }
247 return binary_find(lower, upper, value, fct);
248}
250{
251 using is_transparent = void;
252 [[nodiscard]] size_t operator()(const char* txt) const
253 {
254 return std::hash<std::string_view>{}(txt);
255 }
256 [[nodiscard]] size_t operator()(std::string_view txt) const
257 {
258 return std::hash<std::string_view>{}(txt);
259 }
260 [[nodiscard]] size_t operator()(const std::string& txt) const
261 {
262 return std::hash<std::string>{}(txt);
263 }
264};
265template <class V>
266using string_map = unordered_map<string, V, string_hash, std::equal_to<>>;
273[[nodiscard]] inline Radians ellipse_angle(const MathSize length_to_breadth, const Radians& theta)
274{
275 return Radians{atan2(sin(theta) / length_to_breadth, cos(theta))}.fix();
276}
277// make a set of shared pointers that compares the underlying objects
278template <class T>
280{
281 bool operator()(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) const
282 {
283 if (!lhs || !rhs)
284 {
285 return !lhs && rhs;
286 }
287 return *lhs < *rhs;
288 }
289};
290template <class T>
291class sp_set : public std::set<shared_ptr<T>, sp_less<T>>
292{
293 using base = std::set<shared_ptr<T>, sp_less<T>>;
294
295public:
296 template <class... Args>
297 auto sp_emplace(Args&&... args)
298 {
299 // make it so emplace will work on object type and not shared_ptr
300 return base::emplace(new T(std::forward<Args>(args)...));
301 }
302};
303inline string find_value(
304 const string_view key,
305 const string_view within,
306 const string fallback = ""
307)
308{
309 const auto c = within.find(key);
310 if (c != string::npos)
311 {
312 const string str = &within.at(c + string(key).length());
313 return str.substr(0, str.find(' '));
314 }
315 return fallback;
316}
317inline int str_to_int(const string str) { return stoi(str); }
318inline MathSize str_to_value(const string str) { return static_cast<MathSize>(stod(str)); }
319template <class T>
320inline bool find_value(
321 const string_view key,
322 const string_view within,
323 T* result,
324 T (*convert)(const string_view str)
325)
326{
327 const auto str = find_value(key, within);
328 if (!str.empty())
329 {
330 *result = convert(str);
331 // logging::extensive("{:s} '{:s}'\n", key, str);
332 return true;
333 }
334 return false;
335}
336string get_canonical_path(const char* const dir_root, string path);
338{
339public:
340 LazyPath() = default;
341 LazyPath(const string dir_root, const string path)
342 : dir_root_{[&]() {
343 // have to convert to absolute path or this makes no sense if current path changes
344 return std::filesystem::absolute(dir_root).generic_string();
345 }()},
346 path_{path}
347 { }
348 LazyPath(const string_view dir_root, const string_view path)
349 : LazyPath(string(dir_root), string(path))
350 { }
351 bool empty() const { return path_.empty(); }
352 string intended_path() const noexcept { return std::format("{}/{}", dir_root_, path_); }
353 const char* canonical() const
354 {
355 if (nullptr == canonical_path_)
356 {
357 canonical_path_ = make_unique<string>(get_canonical_path(dir_root_.c_str(), path_));
358 }
359 return canonical_path_->c_str();
360 }
361 LazyPath& operator=(const LazyPath& rhs) noexcept
362 {
363 dir_root_ = rhs.dir_root_;
364 path_ = rhs.path_;
365 canonical_path_ = nullptr;
366 return *this;
367 }
368 LazyPath& operator=(LazyPath&& rhs) noexcept
369 {
370 dir_root_ = std::move(rhs.dir_root_);
371 path_ = std::move(rhs.path_);
372 canonical_path_ = std::move(rhs.canonical_path_);
373 rhs.canonical_path_ = nullptr;
374 return *this;
375 }
376 LazyPath& operator=(const string& path) noexcept
377 {
378 path_ = path;
379 canonical_path_ = nullptr;
380 return *this;
381 }
382
383protected:
384 string dir_root_;
385 string path_;
386 mutable unique_ptr<string> canonical_path_{nullptr};
387};
388// convert string to lower case
389string tolower(string value);
390// convert string to upper case
391string toupper(string value);
392template <class V>
393std::ostream& operator<<(std::ostream& os, const std::optional<V>& v)
394{
395 if (v.has_value())
396 {
397 os << v.value();
398 }
399 return os;
400}
401// go into directory and return to previous location when object is deleted
402class pushd
403{
404public:
405 const string previous_directory;
406 const string current_directory;
407 pushd(string path) noexcept
408 : previous_directory{std::filesystem::current_path().generic_string()},
409 current_directory{[&]() {
410 // HACK: empty is current, but still do rest of parsing so results match that process
411 if (path.empty())
412 {
413 path = ".";
414 }
415 auto p_rel = std::filesystem::relative(path);
416 auto p_abs = std::filesystem::absolute(p_rel);
417 auto p = p_abs.lexically_normal();
418 std::filesystem::current_path(p);
419 return p.generic_string();
420 }()}
421 { }
422 ~pushd() noexcept { std::filesystem::current_path(previous_directory); }
423 pushd& operator=(const pushd& rhs) noexcept = delete;
424 pushd& operator=(pushd&& rhs) noexcept = delete;
425 pushd(const pushd& rhs) noexcept = delete;
426 pushd(pushd&& rhs) noexcept = delete;
427};
428}
429#endif
Definition Util.h:10
Definition Util.h:338
Provides the ability to determine how many times something is used during a simulation.
Definition Util.h:145
string for_what_
What this is tracking usage of.
Definition Util.h:153
atomic< size_t > count_
How many times this has been used.
Definition Util.h:149
UsageCount & operator++() noexcept
Increment operator.
Definition Util.cpp:109
UsageCount(string for_what) noexcept
Constructor.
Definition Util.cpp:108
Definition Util.h:403
Definition Util.h:292
Definition Radians.h:21
Definition Util.h:280
Definition Util.h:250