SigPack - the C++ signal processing library
parser.h
Go to the documentation of this file.
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 #ifndef SP_PARSER_H
5 #define SP_PARSER_H
6 #include <map>
7 
8 namespace sp
9 {
14 
20 class parser
21 {
22 private:
23  std::map<std::string, std::string>
25 
31  bool valid_key(const std::string &key)
32  {
33  if (par_map.find(key) == par_map.end())
34  {
35  std::cout << "SigPack: Parameter " + key + " not found!" << std::endl;
36  return false;
37  }
38  return true;
39  }
40 
49  std::complex<double> parse_cx(std::string str)
50  {
51  double re, im;
52  char i_ch;
53  std::stringstream iss(str);
54 
55  // Parse full ...
56  if (iss >> re >> im >> i_ch && (i_ch == 'i' || i_ch == 'j'))
57  return std::complex<double>(re, im);
58 
59  // ... or only imag
60  iss.clear();
61  iss.seekg(0, iss.beg);
62  if (iss >> im >> i_ch && (i_ch == 'i' || i_ch == 'j'))
63  return std::complex<double>(0.0, im);
64 
65  // .. or only real
66  iss.clear();
67  iss.seekg(0, iss.beg);
68  if (iss >> re)
69  return std::complex<double>(re, 0.0);
70 
71  // ... otherwise
72  err_handler("Could not parse complex number!");
73  }
74 
75 public:
82  parser(const std::string &fname)
83  {
84  std::ifstream fh;
85  std::string line;
86  size_t mark = 0;
87 
88  // Clear parameter map
89  par_map.clear();
90 
91  // Open file
92  fh.open(fname.c_str());
93  if (!fh)
94  {
95  wrn_handler("Could not find " + fname);
96  }
97  else
98  {
99  // Parse
100  while (std::getline(fh, line))
101  {
102  std::string keyS = "";
103  std::string dataS = "";
104 
105  // Skip empty lines
106  if (line.empty())
107  continue;
108 
109  // Skip lines with only whitespace
110  if (line.find_first_not_of("\t ") == std::string::npos)
111  continue;
112 
113  // Remove comment
114  mark = line.find("%");
115  if (mark != std::string::npos)
116  line.erase(mark, line.length());
117 
118  // Do we have a '='
119  mark = line.find("=");
120  if (mark != std::string::npos)
121  {
122  // Find key
123  keyS = line.substr(line.find_first_not_of("\t "),
124  mark - line.find_first_not_of("\t "));
125  keyS = keyS.substr(0, keyS.find_last_not_of("\t ") + 1);
126 
127  // Find data
128  dataS = line.substr(mark + 1, line.length());
129  dataS = dataS.substr(0, dataS.find_last_not_of("\t ") + 1);
130 
131  // Do we have a string
132  mark = dataS.find("\"");
133  if (mark != std::string::npos)
134  {
135  dataS = dataS.substr(mark + 1, dataS.length());
136  dataS = dataS.substr(0, dataS.find_last_of("\""));
137  }
138  // Do we have a vector/matrix
139  mark = dataS.find("[");
140  if (mark != std::string::npos)
141  {
142  dataS = dataS.substr(mark + 1, dataS.length());
143  dataS = dataS.substr(0, dataS.find_last_of("]"));
144  }
145 
146  // Insert to map
147  par_map.insert(std::pair<std::string, std::string>(keyS, dataS));
148  }
149  }
150 
151  // Close file
152  fh.close();
153  }
154  }
155 
159  ~parser() {}
160 
168  template <typename T> T getParam(const std::string key, const T def_val)
169  {
170  if (!valid_key(key))
171  {
172  std::cout << "Setting default " << def_val << std::endl;
173  return def_val;
174  }
175  std::istringstream iss(par_map.find(key)->second);
176  T out;
177  iss >> out;
178  return out;
179  }
180 
188  std::string getString(const std::string key, const std::string def_val)
189  {
190  if (!valid_key(key))
191  {
192  std::cout << "Setting default " << def_val << std::endl;
193  return def_val;
194  }
195  return par_map.find(key)->second;
196  }
197 
204  template <typename T>
205  arma::Col<T> getCol(const std::string key, const arma::Col<T> def_val)
206  {
207  if (!valid_key(key))
208  {
209  std::cout << "Setting default \n" << def_val << std::endl;
210  return def_val;
211  }
212 
213  std::string row, str = par_map.find(key)->second;
214  std::istringstream full_str(str);
215  int K = static_cast<int>(std::count(str.begin(), str.end(), ';') + 1);
216  arma::Col<T> x(K);
217  for (int k = 0; k < K; k++)
218  {
219  std::getline(full_str, row, ';');
220  std::stringstream iss(row);
221  iss >> x(k);
222  }
223  return x;
224  }
225 
233  arma::cx_vec getCxCol(const std::string key, const arma::cx_vec def_val)
234  {
235  if (!valid_key(key))
236  {
237  std::cout << "Setting default \n" << def_val << std::endl;
238  return def_val;
239  }
240 
241  std::string row, str = par_map.find(key)->second;
242  std::istringstream full_str(str);
243  int K = static_cast<int>(std::count(str.begin(), str.end(), ';') + 1);
244  arma::cx_vec x(K);
245  for (int k = 0; k < K; k++)
246  {
247  std::getline(full_str, row, ';');
248  x(k) = parse_cx(row);
249  }
250  return x;
251  }
252 
259  template <typename T>
260  arma::Row<T> getRow(const std::string key, const arma::Row<T> def_val)
261  {
262  if (!valid_key(key))
263  {
264  std::cout << "Setting default \n" << def_val << std::endl;
265  return def_val;
266  }
267 
268  std::string col, str = par_map.find(key)->second;
269  std::istringstream full_str(str);
270  int K = static_cast<int>(std::count(str.begin(), str.end(), ',') + 1);
271  arma::Row<T> x(K);
272  for (int k = 0; k < K; k++)
273  {
274  std::getline(full_str, col, ',');
275  std::stringstream iss(col);
276  iss >> x(k);
277  }
278  return x;
279  }
280 
288  arma::cx_rowvec getCxRow(const std::string key, const arma::cx_rowvec def_val)
289  {
290  if (!valid_key(key))
291  {
292  std::cout << "Setting default \n" << def_val << std::endl;
293  return def_val;
294  }
295 
296  std::string col, str = par_map.find(key)->second;
297  std::istringstream full_str(str);
298  int K = static_cast<int>(std::count(str.begin(), str.end(), ',') + 1);
299  arma::cx_rowvec x(K);
300  for (int k = 0; k < K; k++)
301  {
302  std::getline(full_str, col, ',');
303  x(k) = parse_cx(col);
304  }
305  return x;
306  }
307 
314  template <typename T>
315  arma::Mat<T> getMat(const std::string key, const arma::Mat<T> def_val)
316  {
317  if (!valid_key(key))
318  {
319  std::cout << "Setting default \n" << def_val << std::endl;
320  return def_val;
321  }
322  std::string full_str, row, col;
323  std::istringstream iss_full;
324 
325  full_str = par_map.find(key)->second;
326  int R =
327  static_cast<int>(std::count(full_str.begin(), full_str.end(), ';') + 1);
328 
329  iss_full.str(full_str);
330  std::getline(iss_full, row, ';');
331  int C = static_cast<int>(std::count(row.begin(), row.end(), ',') + 1);
332 
333  arma::Mat<T> x(R, C);
334 
335  iss_full.seekg(0, iss_full.beg);
336  for (int r = 0; r < R; r++)
337  {
338  std::getline(iss_full, row, ';');
339  std::istringstream iss_row(row);
340  for (int c = 0; c < C; c++)
341  {
342  std::getline(iss_row, col, ',');
343  std::istringstream iss_col(col);
344  iss_col >> x(r, c);
345  }
346  }
347  return x;
348  }
349 
357  arma::cx_mat getCxMat(const std::string key, const arma::cx_mat def_val)
358  {
359  if (!valid_key(key))
360  {
361  std::cout << "Setting default \n" << def_val << std::endl;
362  return def_val;
363  }
364  std::string full_str, row, col;
365  std::istringstream iss_full;
366 
367  full_str = par_map.find(key)->second;
368  int R =
369  static_cast<int>(std::count(full_str.begin(), full_str.end(), ';') + 1);
370 
371  iss_full.str(full_str);
372  std::getline(iss_full, row, ';');
373  int C = static_cast<int>(std::count(row.begin(), row.end(), ',') + 1);
374 
375  arma::cx_mat x(R, C);
376 
377  iss_full.seekg(0, iss_full.beg);
378  for (int r = 0; r < R; r++)
379  {
380  std::getline(iss_full, row, ';');
381  std::istringstream iss_row(row);
382  for (int c = 0; c < C; c++)
383  {
384  std::getline(iss_row, col, ',');
385  x(r, c) = parse_cx(col);
386  }
387  }
388  return x;
389  }
390 
391 }; // end Class
393 
394 } // namespace sp
395 #endif
T getParam(const std::string key, const T def_val)
Generic type get function.
Definition: parser.h:168
arma::cx_mat getCxMat(const std::string key, const arma::cx_mat def_val)
cx_mat type get function.
Definition: parser.h:357
std::string getString(const std::string key, const std::string def_val)
String type get function.
Definition: parser.h:188
#define err_handler(msg)
Definition: base.h:213
Definition: base.h:7
arma::cx_vec getCxCol(const std::string key, const arma::cx_vec def_val)
cx_vec type get function.
Definition: parser.h:233
arma::cx_rowvec getCxRow(const std::string key, const arma::cx_rowvec def_val)
cx_rowvec type get function.
Definition: parser.h:288
arma::Mat< T > getMat(const std::string key, const arma::Mat< T > def_val)
Mat type get function.
Definition: parser.h:315
bool valid_key(const std::string &key)
Check if key is valid.
Definition: parser.h:31
A parser class.
Definition: parser.h:20
~parser()
Destructor.
Definition: parser.h:159
#define wrn_handler(msg)
Definition: base.h:224
std::map< std::string, std::string > par_map
Map structure to store parameter and value.
Definition: parser.h:24
parser(const std::string &fname)
Constructor.
Definition: parser.h:82
arma::Col< T > getCol(const std::string key, const arma::Col< T > def_val)
Col type get function.
Definition: parser.h:205
std::complex< double > parse_cx(std::string str)
Parse complex value.
Definition: parser.h:49
arma::Row< T > getRow(const std::string key, const arma::Row< T > def_val)
Row type get function.
Definition: parser.h:260