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 bool endswith(const std::string &haystack, const std::string &needle);
40 
41 int strcmp_natural(const std::string &a, const std::string &b);
42 int strcmp_natural(const char *a, const char *b);
43 void create_config_dir();
44 std::string get_config_dir();
45 
46 void replace_backslash(std::string &path);
47 json json_from_resource(const std::string &rsrc);
48 bool compare_files(const std::string &filename_a, const std::string &filename_b);
49 void find_files_recursive(const std::string &base_path, std::function<void(const std::string &)> cb,
50  const std::string &path = "");
51 
52 Color color_from_json(const json &j);
53 json color_to_json(const Color &c);
54 
55 ColorI colori_from_json(const json &j);
56 json colori_to_json(const ColorI &c);
57 
58 std::string format_m_of_n(unsigned int m, unsigned int n);
59 std::string format_digits(unsigned int m, unsigned int digits_max);
60 double parse_si(const std::string &inps);
61 
62 void rmdir_recursive(const std::string &dir_name);
63 std::string interpolate_text(const std::string &str,
64  std::function<std::optional<std::string>(const std::string &s)> interpolator);
65 
66 std::pair<Coordi, bool> dir_from_action(InToolActionID a);
67 
68 
69 template <typename T> constexpr bool any_of(T value, std::initializer_list<T> choices)
70 {
71  return std::count(choices.begin(), choices.end(), value);
72 }
73 
74 void check_object_type(const json &j, ObjectType type);
75 void ensure_parent_dir(const std::string &path);
76 
77 std::string append_dot_json(const std::string &s);
78 
79 Orientation get_pin_orientation_for_placement(Orientation pin_orientation, const class Placement &placement);
80 
81 } // namespace horizon
Definition: common.hpp:267
a class to store JSON values
Definition: json.hpp:177
basic_json<> json
default JSON class
Definition: json_fwd.hpp:62