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 #include <optional>
10 
11 namespace horizon {
12 using json = nlohmann::json;
13 enum class InToolActionID;
14 
15 std::ifstream make_ifstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::in);
16 std::ofstream make_ofstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::out);
17 
18 void save_json_to_file(const std::string &filename, const json &j);
19 json load_json_from_file(const std::string &filename);
20 std::string get_exe_dir();
21 void allow_set_foreground_window(int pid);
22 void setup_locale();
23 const std::locale &get_locale();
24 
25 template <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 
33 template <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 
39 template <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 
51 bool endswith(const std::string &haystack, const std::string &needle);
52 
53 int strcmp_natural(const std::string &a, const std::string &b);
54 int strcmp_natural(const char *a, const char *b);
55 void create_config_dir();
56 std::string get_config_dir();
57 
58 void replace_backslash(std::string &path);
59 json json_from_resource(const std::string &rsrc);
60 bool compare_files(const std::string &filename_a, const std::string &filename_b);
61 void find_files_recursive(const std::string &base_path, std::function<void(const std::string &)> cb,
62  const std::string &path = "");
63 
64 Color color_from_json(const json &j);
65 json color_to_json(const Color &c);
66 
67 ColorI colori_from_json(const json &j);
68 json colori_to_json(const ColorI &c);
69 
70 std::string format_m_of_n(unsigned int m, unsigned int n);
71 std::string format_digits(unsigned int m, unsigned int digits_max);
72 double parse_si(const std::string &inps);
73 
74 void rmdir_recursive(const std::string &dir_name);
75 std::string interpolate_text(const std::string &str,
76  std::function<std::optional<std::string>(const std::string &s)> interpolator);
77 
78 std::pair<Coordi, bool> dir_from_action(InToolActionID a);
79 
80 
81 template <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 
86 void check_object_type(const json &j, ObjectType type);
87 void ensure_parent_dir(const std::string &path);
88 
89 std::string append_dot_json(const std::string &s);
90 
91 Orientation get_pin_orientation_for_placement(Orientation pin_orientation, const class Placement &placement);
92 
93 } // namespace horizon
Definition: common.hpp:270
a class to store JSON values
Definition: json.hpp:177
basic_json<> json
default JSON class
Definition: json_fwd.hpp:62