Horizon
Loading...
Searching...
No Matches
util.hpp
1#pragma once
2#include "common/common.hpp"
3#include "nlohmann/json_fwd.hpp"
4#include <string>
5#include <vector>
6#include <functional>
7#include <locale>
8#include <fstream>
9#include <optional>
10
11namespace horizon {
12using json = nlohmann::json;
13enum class InToolActionID;
14
15std::ifstream make_ifstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::in);
16std::ofstream make_ofstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::out);
17
18void save_json_to_file(const std::string &filename, const json &j);
19json load_json_from_file(const std::string &filename);
20std::string get_exe_dir();
21void allow_set_foreground_window(int pid);
22void setup_locale();
23const std::locale &get_locale();
24
25template <typename T, typename U> std::vector<T> dynamic_cast_vector(const std::vector<U> &cin)
26{
27 std::vector<T> out;
28 out.reserve(cin.size());
29 std::transform(cin.begin(), cin.end(), std::back_inserter(out), [](auto x) { return dynamic_cast<T>(x); });
30 return out;
31}
32
33template <typename Map, typename F> static void map_erase_if(Map &m, F pred)
34{
35 for (typename Map::iterator i = m.begin(); (i = std::find_if(i, m.end(), pred)) != m.end(); m.erase(i++))
36 ;
37}
38
39template <typename Set, typename F> static void set_erase_if(Set &m, F pred)
40{
41 for (auto it = m.begin(), last = m.end(); it != last;) {
42 if (pred(*it)) {
43 it = m.erase(it);
44 }
45 else {
46 ++it;
47 }
48 }
49}
50
51bool endswith(const std::string &haystack, const std::string &needle);
52
53int strcmp_natural(const std::string &a, const std::string &b);
54int strcmp_natural(const char *a, const char *b);
55void create_config_dir();
56std::string get_config_dir();
57
58void replace_backslash(std::string &path);
59json json_from_resource(const std::string &rsrc);
60bool compare_files(const std::string &filename_a, const std::string &filename_b);
61void find_files_recursive(const std::string &base_path, std::function<void(const std::string &)> cb,
62 const std::string &path = "");
63
64Color color_from_json(const json &j);
65json color_to_json(const Color &c);
66
67ColorI colori_from_json(const json &j);
68json colori_to_json(const ColorI &c);
69
70std::string format_m_of_n(unsigned int m, unsigned int n);
71std::string format_digits(unsigned int m, unsigned int digits_max);
72double parse_si(const std::string &inps);
73
74void rmdir_recursive(const std::string &dir_name);
75std::string interpolate_text(const std::string &str,
76 std::function<std::optional<std::string>(const std::string &s)> interpolator);
77
78std::pair<Coordi, bool> dir_from_action(InToolActionID a);
79
80
81template <typename T> constexpr bool any_of(T value, std::initializer_list<T> choices)
82{
83 return std::count(choices.begin(), choices.end(), value);
84}
85
86void check_object_type(const json &j, ObjectType type);
87void ensure_parent_dir(const std::string &path);
88std::string get_existing_path(const std::string &p);
89
90std::string append_dot_json(const std::string &s);
91
92Orientation get_pin_orientation_for_placement(Orientation pin_orientation, const class Placement &placement);
93
94} // namespace horizon
Definition common.hpp:278
a class to store JSON values
Definition json.hpp:177
not_< empty< find_if< L, Fn > > > any_of
A Boolean integral constant wrapper around true if invoke<Fn, A>::value is true for any element A in ...
Definition meta.hpp:3045
basic_json<> json
default JSON class
Definition json_fwd.hpp:62