Jpp test-rotations-old
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 std::cout << "line=" << line << " nsamples=" << nsamples << "\n";
78 }
79 catch (std::exception const& ex) {
80 std::cerr << "could not parse the waveform_id:" << ex.what() << "\n";
81 return std::nullopt;
82 }
83
84 waveform_samples.reserve(nsamples);
85
86 while (std::getline(ins, line, ' ')) {
87 T sample = atof(line.c_str());
88 waveform_samples.push_back(sample);
89 }
90 if (waveform_samples.size() != nsamples) {
91 std::cerr << "mismatched number of samples : expecting " << nsamples << " but got " << waveform_samples.size() << "\n";
92 return std::nullopt;
93 }
94
95 return Waveform<T>{waveform_id, waveform_samples};
96}
97
98// instanciate the only two versions of parseWaveformSamples
99// that we really support
100template std::optional<Waveform<float>> parseWaveformSamples(const std::string& adf_waveform_line);
101template std::optional<Waveform<double>> parseWaveformSamples(const std::string& adf_waveform_line);
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
std::optional< Waveform< T > > parseWaveformSamples(const std::string &adf_waveform_line)
Definition parser.cc:46