Jarquafelmu

Untitled

Mar 17th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1.  
  2. /// <summary>
  3. /// Reads the in configuration.
  4. /// </summary>
  5. /// <param name="config_file">The configuration file.</param>
  6. void Analyze::readInConfig( istream& config_file ) {
  7.   // create stringstream
  8.   stringstream localStream;
  9.  
  10.   localStream << config_file.rdbuf ();
  11.  
  12.   enum {VT, WIDTH, PULSE_DELTA, DROP_RATIO, BELOW_DROP_RATIO, NUM_FIELDS};
  13.   vector<string> field_names {"vt", "width", "pulse_delta", "drop_ratio", "below_drop_ratio"};
  14.   map<string, string> properties {};
  15.  
  16.   string line;
  17.   while ( localStream && getline ( localStream, line, '='  ) ) {
  18.     // lower case the line
  19.     STRING_HELPER.lower ( line );
  20.     auto property { line };
  21.    
  22.     // ignore comments
  23.     auto skip { false };
  24.     while ( property.find_first_of ( '#' ) != string::npos ) {
  25.       auto const comment_end { property.find_first_of ( '\n' ) + 1 };
  26.       property = property.substr ( comment_end, property.length ());
  27.       if ( property.empty () )
  28.         skip = true;
  29.     }
  30.  
  31.     if ( skip ) continue;
  32.  
  33.     property = STRING_HELPER.trim ( property );
  34.  
  35.     getline ( localStream, line, '\n');
  36.     auto value { line };
  37.  
  38.     properties.insert ( {property, value} );
  39.   }
  40.  
  41.   auto fail { false };
  42.  
  43.   for ( auto const& name : field_names) {
  44.     auto it = properties.find ( name );
  45.     if (it == properties.end ()) {
  46.       throw runtime_error ( "Config file is missing a required property." );
  47.     }
  48.   }
  49.  
  50.   if (properties.size () != NUM_FIELDS) throw runtime_error ( "Config file contains an invalid property." );
  51.  
  52.   try {
  53.     config_.below_drop_ratio = stod ( properties.find ( field_names [static_cast< int >( BELOW_DROP_RATIO )] )->second );
  54.     config_.drop_ratio = stod ( properties.find ( field_names [static_cast< int >( DROP_RATIO )] )->second );
  55.     config_.pulse_delta = stod ( properties.find ( field_names [static_cast< int >( PULSE_DELTA )] )->second );
  56.     config_.threshold = stod ( properties.find ( field_names [static_cast< int >( VT )] )->second );
  57.     config_.width = stod ( properties.find ( field_names [static_cast< int >( WIDTH )] )->second );
  58.   }
  59.   catch ( invalid_argument const& )
  60.   {
  61.     throw runtime_error ( "Config file contains illegal characters."  );
  62.   }
  63.   catch ( out_of_range const& ) {
  64.     throw runtime_error ( "Config file contains illegal characters." );
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment