Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
parser.h File Reference
#include <map>
#include <string>
#include <vector>
#include <optional>
#include <iostream>

Go to the source code of this file.

Classes

class  Parser
 
struct  Waveform< T >
 

Functions

template<typename T >
std::optional< Waveform< T > > parseWaveformSamples (const std::string &adf_waveform_line)
 

Function Documentation

◆ parseWaveformSamples()

template<typename T >
std::optional< Waveform< T > > parseWaveformSamples ( const std::string & adf_waveform_line)

Definition at line 46 of file parser.cc.

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}