altium: improve property reading code, do not rely on exceptions

This commit is contained in:
Thomas Pointhuber 2020-04-06 17:38:26 +02:00 committed by Wayne Stambaugh
parent 2401917776
commit a433d9ea55
1 changed files with 16 additions and 41 deletions

View File

@ -118,52 +118,33 @@ std::map<wxString, wxString> ALTIUM_PARSER::ReadProperties()
int ALTIUM_PARSER::PropertiesReadInt( int ALTIUM_PARSER::PropertiesReadInt(
const std::map<wxString, wxString>& aProperties, const wxString& aKey, int aDefault ) const std::map<wxString, wxString>& aProperties, const wxString& aKey, int aDefault )
{ {
try const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
{ return value == aProperties.end() ? aDefault : wxAtoi( value->second );
const wxString& value = aProperties.at( aKey );
return wxAtoi( value );
}
catch( const std::out_of_range& oor )
{
return aDefault;
}
} }
double ALTIUM_PARSER::PropertiesReadDouble( double ALTIUM_PARSER::PropertiesReadDouble(
const std::map<wxString, wxString>& aProperties, const wxString& aKey, double aDefault ) const std::map<wxString, wxString>& aProperties, const wxString& aKey, double aDefault )
{ {
try const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
{ if( value == aProperties.end() )
const wxString& value = aProperties.at( aKey );
// Locale independent str -> double conversation
std::istringstream istr( (const char*) value.mb_str() );
istr.imbue( std::locale( "C" ) );
double doubleValue;
istr >> doubleValue;
return doubleValue;
}
catch( const std::out_of_range& oor )
{ {
return aDefault; return aDefault;
} }
// Locale independent str -> double conversation
std::istringstream istr( (const char*) value->second.mb_str() );
istr.imbue( std::locale( "C" ) );
double doubleValue;
istr >> doubleValue;
return doubleValue;
} }
bool ALTIUM_PARSER::PropertiesReadBool( bool ALTIUM_PARSER::PropertiesReadBool(
const std::map<wxString, wxString>& aProperties, const wxString& aKey, bool aDefault ) const std::map<wxString, wxString>& aProperties, const wxString& aKey, bool aDefault )
{ {
try const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
{ return value == aProperties.end() ? aDefault : value->second == "TRUE";
const wxString& value = aProperties.at( aKey );
return value == "TRUE";
}
catch( const std::out_of_range& oor )
{
return aDefault;
}
} }
int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxString>& aProperties, int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxString>& aProperties,
@ -218,12 +199,6 @@ int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxStrin
wxString ALTIUM_PARSER::PropertiesReadString( const std::map<wxString, wxString>& aProperties, wxString ALTIUM_PARSER::PropertiesReadString( const std::map<wxString, wxString>& aProperties,
const wxString& aKey, const wxString& aDefault ) const wxString& aKey, const wxString& aDefault )
{ {
try const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
{ return value == aProperties.end() ? aDefault : value->second;
return aProperties.at( aKey );
}
catch( const std::out_of_range& oor )
{
return aDefault;
}
} }