27
27
#include < fstream>
28
28
29
29
using namespace std ;
30
+ using namespace digidoc ;
30
31
using namespace digidoc ::util;
31
32
using namespace xercesc ;
32
33
using namespace xml_schema ;
@@ -38,7 +39,7 @@ template <class A>
38
39
class XmlConfParam : public unique_ptr <A>
39
40
{
40
41
public:
41
- XmlConfParam (string _name, A def = {}): name(std::move(_name)), _def (std::move(def)) {}
42
+ XmlConfParam (string && _name, A && def = {}): name(std::move(_name)), defaultValue (std::move(def)) {}
42
43
43
44
bool setValue (const Configuration::ParamType &p, bool global)
44
45
{
@@ -59,13 +60,13 @@ class XmlConfParam: public unique_ptr<A>
59
60
60
61
XmlConfParam &operator =(const A &other)
61
62
{
62
- unique_ptr<A>::reset (_def != other ? new A (other) : nullptr );
63
+ unique_ptr<A>::reset (defaultValue != other ? new A (other) : nullptr );
63
64
return *this ;
64
65
}
65
66
66
67
operator A () const
67
68
{
68
- return value (_def );
69
+ return value (defaultValue );
69
70
}
70
71
71
72
A value (const A &def) const
@@ -74,7 +75,7 @@ class XmlConfParam: public unique_ptr<A>
74
75
}
75
76
76
77
const string name;
77
- A _def ;
78
+ const A defaultValue ;
78
79
bool locked = false ;
79
80
};
80
81
@@ -86,10 +87,7 @@ class XmlConf::Private
86
87
void init (const string &path, bool global);
87
88
unique_ptr<Configuration> read (const string &path) const ;
88
89
template <class A >
89
- void setUserConf (XmlConfParam<A> ¶m, const A &defined, const A &value);
90
- static string to_string (bool val) { return val ? " true" : " false" ; }
91
- static string to_string (const string &val) { return val; }
92
-
90
+ void setUserConf (XmlConfParam<A> ¶m, A &&defined, const A &value);
93
91
94
92
XmlConfParam<int > logLevel{" log.level" , 3 };
95
93
XmlConfParam<string> logFile{" log.file" };
@@ -112,11 +110,11 @@ class XmlConf::Private
112
110
std::set<std::string> ocspTMProfiles;
113
111
114
112
string SCHEMA_LOC;
115
- string USER_CONF_LOC;
113
+ static const string USER_CONF_LOC;
116
114
};
117
115
}
118
116
119
- using namespace digidoc ;
117
+ const string XmlConf::Private::USER_CONF_LOC = File::path(File::digidocppPath(), " digidocpp.conf " ) ;
120
118
121
119
XmlConf::Private::Private (const string &path, string schema)
122
120
: SCHEMA_LOC(std::move(schema))
@@ -128,13 +126,12 @@ XmlConf::Private::Private(const string &path, string schema)
128
126
catch (const XMLException &e) {
129
127
try {
130
128
string result = xsd::cxx::xml::transcode<char >(e.getMessage ());
131
- THROW (" Error during initialisation of Xerces: %s" , result.c_str ());
129
+ THROW (" Error during initialization of Xerces: %s" , result.c_str ());
132
130
} catch (const xsd::cxx::xml::invalid_utf16_string & /* ex */ ) {
133
- THROW (" Error during initialisation of Xerces" );
131
+ THROW (" Error during initialization of Xerces" );
134
132
}
135
133
}
136
134
137
- USER_CONF_LOC = File::path (File::digidocppPath (), " digidocpp.conf" );
138
135
if (path.empty ())
139
136
{
140
137
init (File::path (File::confPath (), " digidocpp.conf" ), true );
@@ -151,46 +148,35 @@ XmlConf::Private::Private(const string &path, string schema)
151
148
void XmlConf::Private::init (const string& path, bool global)
152
149
{
153
150
DEBUG (" XmlConfPrivate::init(%s, %u)" , path.c_str (), global);
154
- try
155
- {
156
- unique_ptr<Configuration> conf = read (path);
157
- for (const Configuration::ParamType &p: conf->param ())
158
- {
159
- if (logLevel.setValue (p, global) ||
160
- logFile.setValue (p, global) ||
161
- digestUri.setValue (p, global) ||
162
- signatureDigestUri.setValue (p, global) ||
163
- PKCS11Driver.setValue (p, global) ||
164
- proxyForceSSL.setValue (p, global) ||
165
- proxyTunnelSSL.setValue (p, global) ||
166
- proxyHost.setValue (p, global) ||
167
- proxyPort.setValue (p, global) ||
168
- proxyUser.setValue (p, global) ||
169
- proxyPass.setValue (p, global) ||
170
- TSUrl.setValue (p, global) ||
171
- TSLAutoUpdate.setValue (p, global) ||
172
- TSLCache.setValue (p, global) ||
173
- TSLOnlineDigest.setValue (p, global) ||
174
- TSLTimeOut.setValue (p, global) ||
175
- verifyServiceUri.setValue (p, global))
176
- continue ;
177
- if (p.name () == " ocsp.tm.profile" && global)
178
- ocspTMProfiles.emplace (p);
179
- else
180
- WARN (" Unknown configuration parameter %s" , p.name ().c_str ());
181
- }
182
-
183
- for (const Configuration::OcspType &o: conf->ocsp ())
184
- ocsp[o.issuer ()] = o;
185
- }
186
- catch (const Exception &e)
187
- {
188
- WARN (" Failed to parse configuration: %s %d %s" , path.c_str (), global, e.msg ().c_str ());
189
- }
190
- catch (const xml_schema::Exception &e)
151
+ unique_ptr<Configuration> conf = read (path);
152
+ for (const Configuration::ParamType &p: conf->param ())
191
153
{
192
- WARN (" Failed to parse configuration: %s %d %s" , path.c_str (), global, e.what ());
154
+ if (logLevel.setValue (p, global) ||
155
+ logFile.setValue (p, global) ||
156
+ digestUri.setValue (p, global) ||
157
+ signatureDigestUri.setValue (p, global) ||
158
+ PKCS11Driver.setValue (p, global) ||
159
+ proxyForceSSL.setValue (p, global) ||
160
+ proxyTunnelSSL.setValue (p, global) ||
161
+ proxyHost.setValue (p, global) ||
162
+ proxyPort.setValue (p, global) ||
163
+ proxyUser.setValue (p, global) ||
164
+ proxyPass.setValue (p, global) ||
165
+ TSUrl.setValue (p, global) ||
166
+ TSLAutoUpdate.setValue (p, global) ||
167
+ TSLCache.setValue (p, global) ||
168
+ TSLOnlineDigest.setValue (p, global) ||
169
+ TSLTimeOut.setValue (p, global) ||
170
+ verifyServiceUri.setValue (p, global))
171
+ continue ;
172
+ if (p.name () == " ocsp.tm.profile" && global)
173
+ ocspTMProfiles.emplace (p);
174
+ else
175
+ WARN (" Unknown configuration parameter %s" , p.name ().c_str ());
193
176
}
177
+
178
+ for (const Configuration::OcspType &o: conf->ocsp ())
179
+ ocsp[o.issuer ()] = o;
194
180
}
195
181
196
182
/* *
@@ -202,7 +188,7 @@ unique_ptr<Configuration> XmlConf::Private::read(const string &path) const
202
188
{
203
189
try
204
190
{
205
- if (File::fileExists (path))
191
+ if (File::fileExists (path) && File::fileSize (path) > 0 )
206
192
{
207
193
Properties props;
208
194
props.no_namespace_schema_location (SCHEMA_LOC);
@@ -211,22 +197,22 @@ unique_ptr<Configuration> XmlConf::Private::read(const string &path) const
211
197
}
212
198
catch (const xml_schema::Exception& e)
213
199
{
214
- THROW (" Failed to parse configuration: %s (%s) - %s" ,
200
+ WARN (" Failed to parse configuration: %s (%s) - %s" ,
215
201
path.c_str (), SCHEMA_LOC.c_str (), e.what ());
216
202
}
217
203
catch (const xsd::cxx::xml::properties<char >::argument & /* e */ )
218
204
{
219
- THROW (" Failed to parse configuration: %s (%s)" ,
205
+ WARN (" Failed to parse configuration: %s (%s)" ,
220
206
path.c_str (), SCHEMA_LOC.c_str ());
221
207
}
222
208
catch (const xsd::cxx::xml::invalid_utf8_string & /* e */ )
223
209
{
224
- THROW (" Failed to parse configuration: %s (%s)" ,
210
+ WARN (" Failed to parse configuration: %s (%s)" ,
225
211
path.c_str (), SCHEMA_LOC.c_str ());
226
212
}
227
213
catch (const xsd::cxx::xml::invalid_utf16_string & /* e */ )
228
214
{
229
- THROW (" Failed to parse configuration: %s (%s)" ,
215
+ WARN (" Failed to parse configuration: %s (%s)" ,
230
216
path.c_str (), SCHEMA_LOC.c_str ());
231
217
}
232
218
return make_unique<Configuration>();
@@ -241,38 +227,38 @@ unique_ptr<Configuration> XmlConf::Private::read(const string &path) const
241
227
* @throws Exception exception is thrown if reading, writing or creating of a user configuration file fails.
242
228
*/
243
229
template <class A >
244
- void XmlConf::Private::setUserConf (XmlConfParam<A> ¶m, const A &defined, const A &value)
230
+ void XmlConf::Private::setUserConf (XmlConfParam<A> ¶m, A & &defined, const A &value)
245
231
{
246
232
if (param.locked )
247
233
return ;
248
234
param = value;
249
235
unique_ptr<Configuration> conf = read (USER_CONF_LOC);
250
- try
236
+ Configuration::ParamSequence ¶mSeq = conf->param ();
237
+ for (auto it = paramSeq.begin (); it != paramSeq.end (); ++it)
251
238
{
252
- Configuration::ParamSequence ¶mSeq = conf->param ();
253
- for (Configuration::ParamSequence::iterator it = paramSeq.begin (); it != paramSeq.end (); ++it)
239
+ if (param.name == it->name ())
254
240
{
255
- if (param.name == it->name ())
256
- {
257
- paramSeq.erase (it);
258
- break ;
259
- }
241
+ paramSeq.erase (it);
242
+ break ;
260
243
}
261
- if (defined != value) // if it's a new parameter
262
- paramSeq.push_back (make_unique<Param>(to_string (value), param.name ));
263
244
}
264
- catch ( const xml_schema::Exception& e)
245
+ if (defined != value) // if it's a new parameter
265
246
{
266
- THROW (" (in set %s) Failed to parse configuration: %s" , param.name .c_str (), e.what ());
247
+ if constexpr (is_same<A,bool >::value)
248
+ paramSeq.push_back (make_unique<Param>(value ? " true" : " false" , param.name ));
249
+ else if constexpr (is_integral<A>::value)
250
+ paramSeq.push_back (make_unique<Param>(to_string (value), param.name ));
251
+ else
252
+ paramSeq.push_back (make_unique<Param>(std::move (value), param.name ));
267
253
}
268
254
269
255
File::createDirectory (File::directory (USER_CONF_LOC));
270
256
ofstream ofs (File::encodeName (USER_CONF_LOC));
271
257
if (ofs.fail ())
272
258
THROW (" Failed to open configuration: %s" , USER_CONF_LOC.c_str ());
273
- NamespaceInfomap map;
274
- map[{}]. name = {};
275
- map[{}]. schema = SCHEMA_LOC ;
259
+ NamespaceInfomap map{{
260
+ {{}, {{}, SCHEMA_LOC}}
261
+ }} ;
276
262
configuration (ofs, *conf, map, " UTF-8" , Flags::dont_initialize);
277
263
}
278
264
0 commit comments