forked from realsenseai/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrs-config.cpp
More file actions
126 lines (107 loc) · 2.42 KB
/
Copy pathrs-config.cpp
File metadata and controls
126 lines (107 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 RealSense, Inc. All Rights Reserved.
#include "rs-config.h"
#include <librealsense2/rs.h>
#include <rsutils/os/special-folder.h>
#include <rsutils/json.h>
#include <rsutils/json-config.h>
#include <fstream>
using json = rsutils::json;
using namespace rs2;
void config_file::set(const char* key, const char* value)
{
_j[key] = value;
save();
}
void config_file::set_default(const char* key, const char* calculate)
{
_defaults[key] = calculate;
}
void config_file::remove(const char* key)
{
_j.erase(key);
save();
}
void config_file::reset()
{
_j = json::object();
save();
}
std::string config_file::get(const char* key, const char* def) const
{
auto it = _j.find(key);
if (it != _j.end() && it->is_string())
{
return it->string_ref();
}
return get_default(key, def);
}
bool config_file::contains(const char* key) const
{
auto it = _j.find(key);
return it != _j.end() && it->is_string();
}
std::string config_file::get_default(const char* key, const char* def) const
{
auto it = _defaults.find(key);
if (it == _defaults.end()) return def;
return it->second;
}
config_value config_file::get(const char* key) const
{
if (!contains(key)) return config_value(get_default(key, ""));
return config_value(get(key, ""));
}
void config_file::save(const char* filename)
{
try
{
std::ofstream out(filename);
out << std::setw( 2 ) << _j;
out.close();
}
catch (...)
{
}
}
config_file& config_file::instance()
{
static config_file inst( rsutils::os::get_special_folder( rsutils::os::special_folder::app_data )
+ RS2_CONFIG_FILENAME );
return inst;
}
config_file::config_file( std::string const & filename )
: _filename( filename )
{
try
{
auto j = rsutils::json_config::load_from_file( filename );
if( j.exists() )
_j = std::move( j );
}
catch(...)
{
}
}
void config_file::save()
{
save(_filename.c_str());
}
config_file::config_file()
: _j( rsutils::json::object() )
{
}
config_file& config_file::operator=(const config_file& other)
{
if (this != &other)
{
_j = other._j;
_defaults = other._defaults;
save();
}
return *this;
}
bool config_file::operator==(const config_file& other) const
{
return _j == other._j;
}