Horizon
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 
10 namespace horizon {
11 using json = nlohmann::json;
12 enum class InToolActionID;
13 
14 std::ifstream make_ifstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::in);
15 std::ofstream make_ofstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::out);
16 
17 void save_json_to_file(const std::string &filename, const json &j);
18 json load_json_from_file(const std::string &filename);
19 int orientation_to_angle(Orientation o);
20 std::string get_exe_dir();
21 void allow_set_foreground_window(int pid);
22 std::string coord_to_string(const Coordf &c, bool delta = false);
23 std::string dim_to_string(int64_t x, bool with_sign = true);
24 std::string angle_to_string(int angle, bool pos_only = true);
25 void setup_locale();
26 const std::locale &get_locale();
27 
28 int64_t round_multiple(int64_t x, int64_t mul);
29 
30 template <typename T, typename U> std::vector<T> dynamic_cast_vector(const std::vector<U> &cin)
31 {
32  std::vector<T> out;
33  out.reserve(cin.size());
34  std::transform(cin.begin(), cin.end(), std::back_inserter(out), [](auto x) { return dynamic_cast<T>(x); });
35  return out;
36 }
37 
38 template <typename Map, typename F> static void map_erase_if(Map &m, F pred)
39 {
40  for (typename Map::iterator i = m.begin(); (i = std::find_if(i, m.end(), pred)) != m.end(); m.erase(i++))
41  ;
42 }
43 
44 bool endswith(const std::string &haystack, const std::string &needle);
45 
46 template <typename T> int sgn(T val)
47 {
48  return (T(0) < val) - (val < T(0));
49 }
50 
51 int strcmp_natural(const std::string &a, const std::string &b);
52 int strcmp_natural(const char *a, const char *b);
53 void create_config_dir();
54 std::string get_config_dir();
55 
56 void replace_backslash(std::string &path);
57 json json_from_resource(const std::string &rsrc);
58 bool compare_files(const std::string &filename_a, const std::string &filename_b);
59 void find_files_recursive(const std::string &base_path, std::function<void(const std::string &)> cb,
60  const std::string &path = "");
61 
62 Color color_from_json(const json &j);
63 json color_to_json(const Color &c);
64 
65 ColorI colori_from_json(const json &j);
66 json colori_to_json(const ColorI &c);
67 
68 std::string format_m_of_n(unsigned int m, unsigned int n);
69 std::string format_digits(unsigned int m, unsigned int digits_max);
70 double parse_si(const std::string &inps);
71 
72 void rmdir_recursive(const std::string &dir_name);
73 std::string replace_placeholders(const std::string &s, std::function<std::string(const std::string &)> fn,
74  bool keep_empty);
75 
76 std::pair<Coordi, bool> dir_from_action(InToolActionID a);
77 
78 template <typename T> std::pair<Coord<T>, Coord<T>> pad_bbox(std::pair<Coord<T>, Coord<T>> bb, T pad)
79 {
80  bb.first.x -= pad;
81  bb.first.y -= pad;
82 
83  bb.second.x += pad;
84  bb.second.y += pad;
85  return bb;
86 }
87 Coordd project_onto_perp_bisector(const Coordd &a, const Coordd &b, const Coordd &p);
88 
89 template <typename T> constexpr bool any_of(T value, std::initializer_list<T> choices)
90 {
91  return std::count(choices.begin(), choices.end(), value);
92 }
93 
94 void check_object_type(const json &j, ObjectType type);
95 
96 } // namespace horizon
libzip::int64_t
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:166
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61