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 
13 std::ifstream make_ifstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::in);
14 std::ofstream make_ofstream(const std::string &filename_utf8, std::ios_base::openmode mode = std::ios_base::out);
15 
16 void save_json_to_file(const std::string &filename, const json &j);
17 json load_json_from_file(const std::string &filename);
18 int orientation_to_angle(Orientation o);
19 std::string get_exe_dir();
20 void allow_set_foreground_window(int pid);
21 std::string coord_to_string(const Coordf &c, bool delta = false);
22 std::string dim_to_string(int64_t x, bool with_sign = true);
23 std::string angle_to_string(int angle, bool pos_only = true);
24 void setup_locale();
25 const std::locale &get_locale();
26 
27 int64_t round_multiple(int64_t x, int64_t mul);
28 
29 template <typename T, typename U> std::vector<T> dynamic_cast_vector(const std::vector<U> &cin)
30 {
31  std::vector<T> out;
32  out.reserve(cin.size());
33  std::transform(cin.begin(), cin.end(), std::back_inserter(out), [](auto x) { return dynamic_cast<T>(x); });
34  return out;
35 }
36 
37 template <typename Map, typename F> static void map_erase_if(Map &m, F pred)
38 {
39  for (typename Map::iterator i = m.begin(); (i = std::find_if(i, m.end(), pred)) != m.end(); m.erase(i++))
40  ;
41 }
42 
43 bool endswith(const std::string &haystack, const std::string &needle);
44 
45 template <typename T> int sgn(T val)
46 {
47  return (T(0) < val) - (val < T(0));
48 }
49 
50 int strcmp_natural(const std::string &a, const std::string &b);
51 int strcmp_natural(const char *a, const char *b);
52 void create_config_dir();
53 std::string get_config_dir();
54 
55 void replace_backslash(std::string &path);
56 json json_from_resource(const std::string &rsrc);
57 bool compare_files(const std::string &filename_a, const std::string &filename_b);
58 void find_files_recursive(const std::string &base_path, std::function<void(const std::string &)> cb,
59  const std::string &path = "");
60 
61 Color color_from_json(const json &j);
62 json color_to_json(const Color &c);
63 
64 std::string format_m_of_n(unsigned int m, unsigned int n);
65 std::string format_digits(unsigned int m, unsigned int digits_max);
66 double parse_si(const std::string &inps);
67 
68 } // namespace horizon
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:161
libzip::int64_t
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103