Debian Codegraph @

src/atlas/util/Config.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include <cstdarg>
12 #include <cstring>
13 #include <fstream>
14 #include <iostream>
15 #include <sstream>
16 #include <stdexcept>
17
18 #include "eckit/filesystem/PathName.h"
19 #include "eckit/log/JSON.h"
20 #include "eckit/parser/YAMLParser.h"
21
22 #include "atlas/grid/Grid.h"
23 #include "atlas/mesh/Mesh.h"
24 #include "atlas/runtime/Exception.h"
25 #include "atlas/util/Config.h"
26
27 using std::string;
28
29 namespace atlas {
30 namespace util {
31
32 namespace {
33
34 static eckit::Value yaml_from_stream(std::istream& stream) {
35 eckit::YAMLParser parser(stream);
36 return parser.parse();
37 }
38
39 static eckit::Value yaml_from_path(const eckit::PathName& path) {
40 if (!path.exists()) {
41 throw_Exception("File " + std::string(path) + " does not exist.");
42 }
43 std::ifstream file(path.localPath());
44 if (!file.is_open()) {
45 throw_Exception("Unable to open json file " + std::string(path), Here());
46 }
47 eckit::Value value = yaml_from_stream(file);
48 file.close();
49 return value;
50 }
51
52 } // namespace
53
54 Config::Config(): eckit::LocalConfiguration() {}
55
56 Config::Config(const eckit::Configuration& p): eckit::LocalConfiguration(p) {}
57
58 Config::Config(std::istream& stream, const std::string&): eckit::LocalConfiguration(yaml_from_stream(stream)) {}
59
60 Config::Config(const eckit::PathName& path): eckit::LocalConfiguration(yaml_from_path(path)) {}
61
62 Config Config::operator|(const eckit::Configuration& other) const {
63 Config config(*this);
64 config.set(other);
65 return config;
66 }
67
68 Config& Config::set(const eckit::Configuration& other) {
69 #if ATLAS_ECKIT_VERSION_AT_LEAST(1, 26, 0) || ATLAS_ECKIT_DEVELOP
70 eckit::LocalConfiguration::set(other);
71 #else
72 eckit::Value& root = const_cast<eckit::Value&>(get());
73 auto& other_root = other.get();
74 std::vector<string> other_keys;
75 eckit::fromValue(other_keys, other_root.keys());
76 for (auto& key : other_keys) {
77 root[key] = other_root[key];
78 }
79 #endif
80 return *this;
81 }
82
83 Config& Config::remove(const std::string& name) {
84 #if ATLAS_ECKIT_VERSION_AT_LEAST(1, 26, 0) || ATLAS_ECKIT_DEVELOP
85 LocalConfiguration::remove(name);
86 #else
87 eckit::Value& root = const_cast<eckit::Value&>(get());
88 root.remove(name);
89 #endif
90 return *this;
91 }
92
93 Config& Config::set(const std::string& name, const std::vector<Config>& values) {
94 std::vector<eckit::LocalConfiguration> metadatavalues(values.size());
95 for (size_t i = 0; i < metadatavalues.size(); ++i) {
96 metadatavalues[i] = values[i];
97 }
98 set(name, metadatavalues);
99 return *this;
100 }
101
102 bool Config::get(const std::string& name, std::vector<Config>& value) const {
103 bool found = has(name);
104 if (found) {
105 auto properties = getSubConfigurations(name);
106 value.resize(properties.size());
107 for (size_t i = 0; i < value.size(); ++i) {
108 value[i].set(properties[i]);
109 }
110 }
111 return found;
112 }
113
114 #if ! (ATLAS_ECKIT_VERSION_AT_LEAST(1, 26, 0) || ATLAS_ECKIT_DEVELOP)
115 std::vector<std::string> Config::keys() const {
116 std::vector<std::string> result;
117 eckit::fromValue(result, get().keys());
118 return result;
119 }
120 #endif
121
122 std::string Config::json(eckit::JSON::Formatting formatting) const {
123 std::stringstream json;
124 eckit::JSON js(json,formatting);
125 js << *this;
126 return json.str();
127 }
128
129 //==================================================================
130
131 // ------------------------------------------------------------------
132 // C wrapper interfaces to C++ routines
133
134 Config* atlas__Config__new() {
135 return new Config();
136 }
137
138 Config* atlas__Config__new_from_json(const char* json) {
139 std::stringstream s;
140 s << json;
141 return new Config(s);
142 }
143
144 Config* atlas__Config__new_from_file(const char* path) {
145 return new Config(eckit::PathName(path));
146 }
147
148 void atlas__Config__delete(Config* This) {
149 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
150 delete This;
151 }
152
153 void atlas__Config__set_config(Config* This, const char* name, const Config* value) {
154 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
155 This->set(std::string(name), *value);
156 }
157
158 void atlas__Config__set_config_list(Config* This, const char* name, const Config* value[], int size) {
159 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
160 std::vector<Config> params(size);
161 for (int i = 0; i < size; ++i) {
162 params[i] = Config(*value[i]);
163 }
164 This->set(std::string(name), params);
165 }
166
167 void atlas__Config__set_int(Config* This, const char* name, int value) {
168 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
169 This->set(std::string(name), long(value));
170 }
171 void atlas__Config__set_long(Config* This, const char* name, long value) {
172 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
173 This->set(std::string(name), value);
174 }
175 void atlas__Config__set_float(Config* This, const char* name, float value) {
176 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
177 This->set(std::string(name), double(value));
178 }
179 void atlas__Config__set_double(Config* This, const char* name, double value) {
180 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
181 This->set(std::string(name), value);
182 }
183 void atlas__Config__set_string(Config* This, const char* name, const char* value) {
184 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
185 This->set(std::string(name), std::string(value));
186 }
187 void atlas__Config__set_array_int(Config* This, const char* name, int value[], int size) {
188 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
189 std::vector<int> v;
190 v.assign(value, value + size);
191 This->set(std::string(name), v);
192 }
193 void atlas__Config__set_array_long(Config* This, const char* name, long value[], int size) {
194 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
195 std::vector<long> v;
196 v.assign(value, value + size);
197 This->set(std::string(name), v);
198 }
199 void atlas__Config__set_array_float(Config* This, const char* name, float value[], int size) {
200 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
201 std::vector<float> v;
202 v.assign(value, value + size);
203 This->set(std::string(name), v);
204 }
205 void atlas__Config__set_array_double(Config* This, const char* name, double value[], int size) {
206 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
207 std::vector<double> v;
208 v.assign(value, value + size);
209 This->set(std::string(name), v);
210 }
211
212 int atlas__Config__get_config(Config* This, const char* name, Config* value) {
213 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
214 if (!This->get(std::string(name), *value)) {
215 return false;
216 }
217 return true;
218 }
219
220 int atlas__Config__get_config_list(Config* This, const char* name, Config**& value, int& size, int& allocated) {
221 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
222 value = nullptr;
223 std::vector<Config> vector;
224 if (!This->get(std::string(name), vector)) {
225 return false;
226 }
227 size = vector.size();
228 value = new Config*[size];
229 allocated = true;
230 for (int i = 0; i < size; ++i) {
231 value[i] = new Config(vector[i]);
232 }
233 return true;
234 }
235
236 int atlas__Config__get_int(Config* This, const char* name, int& value) {
237 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
238 long long_value = value;
239 if (!This->get(std::string(name), long_value)) {
240 return false;
241 }
242 ATLAS_ASSERT(int(long_value) == long_value);
243 value = long_value;
244 return true;
245 }
246 int atlas__Config__get_long(Config* This, const char* name, long& value) {
247 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
248 if (!This->get(std::string(name), value)) {
249 return false;
250 }
251 return true;
252 }
253 int atlas__Config__get_float(Config* This, const char* name, float& value) {
254 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
255 double double_value;
256 if (!This->get(std::string(name), double_value)) {
257 return false;
258 }
259 value = static_cast<float>(double_value);
260 return true;
261 }
262 int atlas__Config__get_double(Config* This, const char* name, double& value) {
263 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
264 if (!This->get(std::string(name), value)) {
265 return false;
266 }
267 return true;
268 }
269 int atlas__Config__get_string(Config* This, const char* name, char*& value, int& size, int& allocated) {
270 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
271 std::string s;
272 if (!This->get(std::string(name), s)) {
273 value = nullptr;
274 return false;
275 }
276 size = static_cast<int>(s.size());
277 value = new char[size + 1];
278 std::strncpy(value, s.c_str(), size + 1);
279 allocated = true;
280 return true;
281 }
282 int atlas__Config__get_array_int(Config* This, const char* name, int*& value, int& size, int& allocated) {
283 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
284 std::vector<long> v;
285 if (!This->get(std::string(name), v)) {
286 return false;
287 }
288 size = v.size();
289 value = new int[size];
290 for (size_t j = 0; j < v.size(); ++j) {
291 ATLAS_ASSERT(int(v[j]) == v[j]);
292 value[j] = v[j];
293 }
294 allocated = true;
295 return true;
296 }
297 int atlas__Config__get_array_long(Config* This, const char* name, long*& value, int& size, int& allocated) {
298 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
299 std::vector<long> v;
300 if (!This->get(std::string(name), v)) {
301 return false;
302 }
303 size = v.size();
304 value = new long[size];
305 for (size_t j = 0; j < v.size(); ++j) {
306 value[j] = v[j];
307 }
308 allocated = true;
309 return true;
310 }
311 int atlas__Config__get_array_float(Config* This, const char* name, float*& value, int& size, int& allocated) {
312 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
313 std::vector<double> v;
314 if (!This->get(std::string(name), v)) {
315 return false;
316 }
317 size = v.size();
318 value = new float[size];
319 for (size_t j = 0; j < v.size(); ++j) {
320 value[j] = static_cast<float>(v[j]);
321 }
322 allocated = true;
323 return true;
324 }
325 int atlas__Config__get_array_double(Config* This, const char* name, double*& value, int& size, int& allocated) {
326 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
327 std::vector<double> v;
328 if (!This->get(std::string(name), v)) {
329 return false;
330 }
331 size = v.size();
332 value = new double[size];
333 for (size_t j = 0; j < v.size(); ++j) {
334 value[j] = v[j];
335 }
336 allocated = true;
337 return true;
338 }
339
340 int atlas__Config__has(Config* This, const char* name) {
341 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
342 return This->has(std::string(name));
343 }
344
345 void atlas__Config__json(Config* This, char*& json, int& size, int& allocated) {
346 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Config");
347 std::stringstream s;
348 eckit::JSON j(s);
349 j.precision(16);
350 j << *This;
351 std::string json_str = s.str();
352 size = static_cast<int>(json_str.size());
353 json = new char[size + 1];
354 allocated = true;
355 std::strncpy(json, json_str.c_str(), size + 1);
356 allocated = true;
357 }
358
359 // ------------------------------------------------------------------
360
361 } // namespace util
362 } // namespace atlas