Jpp 19.3.0
the software that should make you happy
Loading...
Searching...
No Matches
parser.cc
Go to the documentation of this file.
1#include "parser.h"
2#include <exception>
3#include <sstream>
4#include <boost/algorithm/string.hpp>
5#include <iostream>
6
7Parser::Parser(const std::string& d, char t)
8{
9 std::istringstream iss(d);
10 std::string token;
11
12 while (std::getline(iss, token, t)) {
13 boost::algorithm::trim(token);
14 size_t pos = token.find('=');
15 std::string key = token.substr(0, pos);
16 std::string value = token.substr(pos + 1);
17 boost::algorithm::trim(key);
18 boost::algorithm::trim(value);
19 mmap.insert(std::pair<std::string, std::string>(key, value));
20 }
21}
22
24{
26 auto i = mmap.equal_range(tag);
27 for (auto it = i.first; it != i.second; ++it) {
28 r.push_back(it->second);
29 }
30
31 return r;
32}
33
34std::string Parser::Find(const std::string& tag, size_t index,
35 std::string value)
36{
37 std::vector<std::string> result = Find(tag);
38
39 if (result.size() > index) {
40 return result[index];
41 }
42 return value;
43}
44
45template<typename T>
46std::optional<Waveform<T>> parseWaveformSamples(const std::string& adf_waveform_line)
47{
48 // remove multiple spaces, if any
49 std::string str(adf_waveform_line);
50
51 std::string::iterator new_end = std::unique(str.begin(), str.end(), [](char a, char b)
52 { return a == b && a == ' '; });
53
54 str.erase(new_end, str.end());
55
56 // now decode the string
57 std::istringstream ins(str);
58 std::string line;
59
60 std::vector<T> waveform_samples;
61 int waveform_id{};
62
63 std::getline(ins, line, ' ');
64 try {
65 waveform_id = std::stoi(line);
66 }
67 catch (std::exception const& ex) {
68 std::cerr << "could not parse the waveform_id:" << ex.what() << "\n";
69 return std::nullopt;
70 }
71
72 size_t nsamples{};
73
74 std::getline(ins, line, ' ');
75 try {
76 nsamples = std::stoi(line);
77 }
78 catch (std::exception const& ex) {
79 std::cerr << "could not parse the waveform_id:" << ex.what() << "\n";
80 return std::nullopt;
81 }
82
83 waveform_samples.reserve(nsamples);
84
85 while (std::getline(ins, line, ' ')) {
86 T sample = atof(line.c_str());
87 waveform_samples.push_back(sample);
88 }
89 if (waveform_samples.size() != nsamples) {
90 std::cerr << "mismatched number of samples : expecting " << nsamples << " but got " << waveform_samples.size() << "\n";
91 return std::nullopt;
92 }
93
94 return Waveform<T>{waveform_id, waveform_samples};
95}
96
97// instanciate the only two versions of parseWaveformSamples
98// that we really support
99template std::optional<Waveform<float>> parseWaveformSamples(const std::string& adf_waveform_line);
100template std::optional<Waveform<double>> parseWaveformSamples(const std::string& adf_waveform_line);
101
102std::optional<DOMConfig> parseDOMConfiguration(const std::string& dom_configuration_line)
103{
104 if (dom_configuration_line.empty()) {
105 return std::nullopt;
106 }
107 std::stringstream iss(dom_configuration_line);
108 int dom_id;
109 iss >> dom_id;
110 if (iss.eof()) {
111 return std::nullopt;
112 }
113 int number_of_probes;
114 iss >> number_of_probes;
115 if (iss.eof() || number_of_probes <= 0) {
116 return std::nullopt;
117 }
118 std::vector<ProbeConfig> probe_configs;
119 while (probe_configs.size() < number_of_probes) {
120 int id;
121 iss >> id;
122 if (iss.eof()) {
123 return std::nullopt;
124 }
125 int threshold;
126 iss >> threshold;
127 if (iss.eof()) {
128 return std::nullopt;
129 }
130 probe_configs.push_back(ProbeConfig{id, threshold});
131 }
132 if (iss.eof()) {
133 return std::nullopt;
134 }
135 int debug_write;
136 iss >> debug_write;
137 if (!iss.eof()) {
138 return std::nullopt;
139 }
140 return DOMConfig{dom_id, probe_configs, debug_write};
141}
std::vector< std::string > Find(const std::string &tag)
Definition parser.cc:23
Parser(const std::string &d, char t)
Definition parser.cc:7
std::multimap< std::string, std::string > mmap
Definition parser.h:23
uint32_t dom_id(frame_idx_t idx)
std::optional< DOMConfig > parseDOMConfiguration(const std::string &dom_configuration_line)
Parse a line that should contain the ADF configuration for a single DOM.
Definition parser.cc:102
std::optional< Waveform< T > > parseWaveformSamples(const std::string &adf_waveform_line)
Definition parser.cc:46
The configuration information for one DOM.
Definition parser.h:66
A pair (id,threshold) representing basic information about one probe.
Definition parser.h:58