FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/ConfigProfile.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
ConfigProfile.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
168 lines (137 loc) · 5.11 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
ConfigProfile.cpp
Copy path
File metadata and controls
168 lines (137 loc) · 5.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* ConfigProfile.cpp
*
* Copyright (C) 2018 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
boost/algorithm/string.hpp
>
#
include
<
boost/property_tree/ini_parser.hpp
>
#
include
<
boost/property_tree/ptree.hpp
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
core/ConfigProfile.hpp
>
namespace
rstudio
{
namespace
core
{
using
namespace
boost
::property_tree
;
Error
ConfigProfile::validateParam
(
const
std::string& paramName,
const
std::string& paramValue)
const
{
ValidatorMap::const_iterator iter = validators_.
find
(paramName);
if
(iter == validators_.
end
())
return
Success
();
const
ValidatorFunc& validator = iter->
second
;
std::string errorMessage;
if
(!
validator
(paramValue, &errorMessage))
{
return
systemError
(boost::system::errc::protocol_error,
"
Param
"
+ paramName +
"
is not valid:
"
+ errorMessage,
ERROR_LOCATION
);
}
return
Success
();
}
Error
ConfigProfile::load
(
const
FilePath& filePath)
{
std::string contents;
Error error =
readStringFromFile
(filePath, &contents);
if
(error)
return
error;
error =
parseString
(contents);
if
(error)
{
std::string description = error.
getProperty
(
"
description
"
);
description +=
"
in file
"
+ filePath.
getAbsolutePath
();
error.
addOrUpdateProperty
(
"
description
"
, description);
}
return
error;
}
Error
ConfigProfile::parseString
(
const
std::string& profileStr)
{
std::istringstream stream{profileStr};
//
parse the profile (ini syntax)
ptree profileTree;
try
{
ini_parser::read_ini
(stream, profileTree);
}
catch
(
const
ini_parser_error& error)
{
return
systemError
(boost::system::errc::protocol_error,
"
Could not parse config profile:
"
+ error.
message
(),
ERROR_LOCATION
);
}
//
build section overrides
std::vector<LevelValues> levels;
for
(
const
ptree::value_type& child : profileTree)
{
boost::optional<Level> matchingLevel;
for
(
const
Level& level : sections_)
{
//
if the section level starts with the defined section name
//
from a prior call to addSections, we have a matching section level
if
(
boost::algorithm::starts_with
(child.
first
, level.
second
))
{
matchingLevel = level;
break
;
}
}
if
(!matchingLevel)
{
//
no matching section level - it was not specified and is thus
//
an erroneous section
return
systemError
(boost::system::errc::protocol_error,
"
Invalid config section
"
+ child.
first
,
ERROR_LOCATION
);
}
std::map<std::string, std::string> values;
for
(
const
ptree::value_type val : child.
second
)
{
//
check to see if the parameter within the section has been registered
const
std::string& paramName = val.
first
;
DefaultParamValuesMap::const_iterator defaultIter = defaultValues_.
find
(paramName);
if
(defaultIter == defaultValues_.
end
())
{
//
we require every param to have a default value / be registered so error out here
return
systemError
(boost::system::errc::protocol_error,
"
Unknown param
"
+ paramName +
"
specified
"
,
ERROR_LOCATION
);
}
std::string paramValue = val.
second
.
get_value
<std::string>();
Error error =
validateParam
(paramName, paramValue);
if
(error)
return
error;
values[paramName] = paramValue;
}
std::string levelValue = child.
first
.
substr
(matchingLevel.
get
().
second
.
size
());
levels.
push_back
({{matchingLevel.
get
().
first
, levelValue}, values});
}
//
stable sort the levels in ascending level number
//
this will preserve the order of same-level section overrides
std::stable_sort
(
levels.
begin
(),
levels.
end
(),
[](
const
LevelValues& a,
const
LevelValues& b) {
return
a.
first
.
first
< b.
first
.
first
; });
//
assign the temporary variable levels to the class member
//
this assures that we can safely call this method multiple times, preserving the last
//
good configuration in case we error out above due to invalid configuration
levels_ = levels;
return
Success
();
}
std::vector<std::string>
ConfigProfile::getLevelNames
(
uint32_t
level)
const
{
std::vector<std::string> levelNames;
for
(
const
LevelValues& value : levels_)
{
if
(value.
first
.
first
== level)
levelNames.
push_back
(value.
first
.
second
);
}
return
levelNames;
}
}
//
core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL