rs-config.cpp
Go to the documentation of this file.
1 #include "rs-config.h"
2 
3 #include "../third-party/json.hpp"
4 
5 #include "model-views.h"
6 
7 #include <fstream>
8 
9 #include "os.h"
10 
12 
13 using namespace rs2;
14 
15 void config_file::set(const char* key, const char* value)
16 {
17  _values[key] = value;
18  save();
19 }
20 
21 void config_file::set_default(const char* key, const char* calculate)
22 {
23  _defaults[key] = calculate;
24 }
25 
27 {
28  _values.clear();
29  save();
30 }
31 
32 std::string config_file::get(const char* key, const char* def) const
33 {
34  auto it = _values.find(key);
35  if (it != _values.end())
36  {
37  return it->second;
38  }
39  return get_default(key, def);
40 }
41 
42 bool config_file::contains(const char* key) const
43 {
44  auto it = _values.find(key);
45  return it != _values.end();
46 }
47 
48 std::string config_file::get_default(const char* key, const char* def) const
49 {
50  auto it = _defaults.find(key);
51  if (it == _defaults.end()) return def;
52  return it->second;
53 }
54 
55 config_value config_file::get(const char* key) const
56 {
57  if (!contains(key)) return config_value(get_default(key, ""));
58  return config_value(get(key, ""));
59 }
60 
61 void config_file::save(const char* filename)
62 {
63  json j;
64  for(auto kvp : _values)
65  {
66  j[kvp.first] = kvp.second;
67  }
68  std::string s = j.dump(2);
69  try
70  {
71  std::ofstream out(filename);
72  out << s;
73  out.close();
74  }
75  catch (...)
76  {
77  }
78 }
79 
81 {
83  + std::string("realsense-config.json"));
84  return inst;
85 }
86 
88  : _filename(std::move(filename)), _values()
89 {
90  try
91  {
92 
93  std::ifstream t(_filename);
94  if (!t.good()) return;
95  std::string str((std::istreambuf_iterator<char>(t)),
96  std::istreambuf_iterator<char>());
97  auto j = json::parse(str);
98  for (json::iterator it = j.begin(); it != j.end(); ++it)
99  {
100  _values[it.key()] = it.value().get<std::string>();
101  }
102  }
103  catch(...)
104  {
105 
106  }
107 }
108 
110 {
111  save(_filename.c_str());
112 }
113 
115  : _filename(""), _values()
116 {
117 }
118 
120 {
121  if (this != &other)
122  {
123  _values = other._values;
124  _defaults = other._defaults;
125  save();
126  }
127  return *this;
128 }
129 
130 bool config_file::operator==(const config_file& other) const
131 {
132  return _values == other._values;
133 }
GLdouble s
static basic_json parse(T(&array)[N], const parser_callback_t cb=nullptr)
deserialize from an array
Definition: json.hpp:5979
static config_file & instance()
Definition: rs-config.cpp:80
a class to store JSON values
Definition: json.hpp:221
GLfloat value
std::string get_folder_path(special_folder f)
Definition: os.cpp:247
bool operator==(const config_file &other) const
Definition: rs-config.cpp:130
Definition: cah-model.h:10
GLsizei const GLchar *const * string
GLdouble t
GLuint64 key
Definition: glext.h:8966
Definition: parser.hpp:154
std::string get_default(const char *key, const char *def) const
Definition: rs-config.cpp:48
std::string get(const char *key, const char *def) const
Definition: rs-config.cpp:32
bool contains(const char *key) const
Definition: rs-config.cpp:42
GLint j
config_file & operator=(const config_file &other)
Definition: rs-config.cpp:119
void set(const char *key, const char *value)
Definition: rs-config.cpp:15
static auto it
std::map< std::string, std::string > _defaults
Definition: rs-config.h:93
string_t dump(const int indent=-1) const
serialization
Definition: json.hpp:2187
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
std::map< std::string, std::string > _values
Definition: rs-config.h:92
std::string _filename
Definition: rs-config.h:94
basic_json<> json
default JSON class
Definition: json.hpp:12124
void set_default(const char *key, const char *calculate)
Definition: rs-config.cpp:21
a template for a random access iterator for the basic_json class
Definition: json.hpp:231


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40