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