-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathDeviceConfigInfo.cxx
More file actions
83 lines (73 loc) · 2.69 KB
/
DeviceConfigInfo.cxx
File metadata and controls
83 lines (73 loc) · 2.69 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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/DeviceConfigInfo.h"
#include "Framework/DeviceInfo.h"
#include <cstdlib>
#include <string_view>
#include <boost/property_tree/json_parser.hpp>
namespace o2::framework
{
// Parses a config entry in the form
//
// [CONFIG] <key>=<vaue> <timestamp> <provenance>
bool DeviceConfigHelper::parseConfig(std::string_view s, ParsedConfigMatch& match)
{
const char* begin = s.begin();
const char* end = s.end();
if (s.size() > 17 && (strncmp("[CONFIG];", begin + 17, 9) != 0)) {
return false;
}
if (s.size() < 17 + 9) {
return false;
}
match.beginKey = s.data() + 9 + 17;
match.endKey = (char const*)memchr(match.beginKey, '=', s.size() - 9);
if (match.endKey == nullptr) {
return false;
}
match.beginValue = match.endKey + 1;
match.endValue = (char const*)memchr(match.beginValue, ';', end - match.beginValue);
if (match.endValue == nullptr) {
return false;
}
char* next = nullptr;
match.timestamp = strtoll(match.endValue + 1, &next, 10);
if (!next || *next != ';') {
return false;
}
match.beginProvenance = next + 1;
match.endProvenance = end;
return true;
}
bool DeviceConfigHelper::processConfig(ParsedConfigMatch& match,
DeviceInfo& info)
{
if (match.beginKey == nullptr || match.endKey == nullptr ||
match.beginValue == nullptr || match.endValue == nullptr ||
match.beginProvenance == nullptr || match.endProvenance == nullptr) {
return false;
}
auto keyString = std::string(match.beginKey, match.endKey - match.beginKey);
auto valueString = std::string(match.beginValue, match.endValue - match.beginValue);
auto provenanceString = std::string(match.beginProvenance, match.endProvenance - match.beginProvenance);
boost::property_tree::ptree branch;
std::stringstream ss{valueString};
try {
boost::property_tree::json_parser::read_json(ss, branch);
info.currentConfig.put_child(keyString, branch);
} catch (boost::exception&) {
// in case it is not a tree but a single value
info.currentConfig.put(keyString, valueString);
}
info.currentProvenance.put(keyString, provenanceString);
return true;
}
} // namespace o2::framework