Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Reads the in configuration.
- /// </summary>
- /// <param name="config_file">The configuration file.</param>
- void Analyze::readInConfig( istream& config_file ) {
- // create stringstream
- stringstream localStream;
- localStream << config_file.rdbuf ();
- enum {VT, WIDTH, PULSE_DELTA, DROP_RATIO, BELOW_DROP_RATIO, NUM_FIELDS};
- vector<string> field_names {"vt", "width", "pulse_delta", "drop_ratio", "below_drop_ratio"};
- map<string, string> properties {};
- string line;
- while ( localStream && getline ( localStream, line, '=' ) ) {
- // lower case the line
- STRING_HELPER.lower ( line );
- auto property { line };
- // ignore comments
- auto skip { false };
- while ( property.find_first_of ( '#' ) != string::npos ) {
- auto const comment_end { property.find_first_of ( '\n' ) + 1 };
- property = property.substr ( comment_end, property.length ());
- if ( property.empty () )
- skip = true;
- }
- if ( skip ) continue;
- property = STRING_HELPER.trim ( property );
- getline ( localStream, line, '\n');
- auto value { line };
- properties.insert ( {property, value} );
- }
- auto fail { false };
- for ( auto const& name : field_names) {
- auto it = properties.find ( name );
- if (it == properties.end ()) {
- throw runtime_error ( "Config file is missing a required property." );
- }
- }
- if (properties.size () != NUM_FIELDS) throw runtime_error ( "Config file contains an invalid property." );
- try {
- config_.below_drop_ratio = stod ( properties.find ( field_names [static_cast< int >( BELOW_DROP_RATIO )] )->second );
- config_.drop_ratio = stod ( properties.find ( field_names [static_cast< int >( DROP_RATIO )] )->second );
- config_.pulse_delta = stod ( properties.find ( field_names [static_cast< int >( PULSE_DELTA )] )->second );
- config_.threshold = stod ( properties.find ( field_names [static_cast< int >( VT )] )->second );
- config_.width = stod ( properties.find ( field_names [static_cast< int >( WIDTH )] )->second );
- }
- catch ( invalid_argument const& )
- {
- throw runtime_error ( "Config file contains illegal characters." );
- }
- catch ( out_of_range const& ) {
- throw runtime_error ( "Config file contains illegal characters." );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment