altium: Improve parsing of UTF-8 data

This is an implementation based on d4ccec05 from @fxtentacle
This commit is contained in:
Thomas Pointhuber 2021-06-26 13:58:56 +02:00
parent e6c047896c
commit e50eaed7b9
3 changed files with 17 additions and 3 deletions

View File

@ -140,10 +140,16 @@ std::map<wxString, wxString> ALTIUM_PARSER::ReadProperties()
// convert the strings to wxStrings, since we use them everywhere
// value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
wxString key( keyS.c_str(), wxConvISO8859_1 );
wxString value( valueS.c_str(), wxConvISO8859_1 );
// Altium stores keys either in Upper, or in CamelCase. Lets unify it.
kv.insert( { key.Trim( false ).Trim( true ).MakeUpper(), value.Trim() } );
wxString canonicalKey = key.Trim( false ).Trim( true ).MakeUpper();
// If the key starts with '%UTF8%' we have to parse the value using UTF8
wxString value;
if( canonicalKey.StartsWith( "%UTF8%" ) )
value = wxString( valueS.c_str(), wxConvUTF8 );
else
value = wxString( valueS.c_str(), wxConvISO8859_1 );
kv.insert( { canonicalKey, value.Trim() } );
}
return kv;
@ -233,6 +239,11 @@ int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxStrin
wxString ALTIUM_PARSER::PropertiesReadString( const std::map<wxString, wxString>& aProperties,
const wxString& aKey, const wxString& aDefault )
{
const std::map<wxString, wxString>::const_iterator& utf8Value =
aProperties.find( wxString( "%UTF8%" ) + aKey );
if( utf8Value != aProperties.end() )
return utf8Value->second;
const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
return value == aProperties.end() ? aDefault : value->second;
}

View File

@ -93,6 +93,7 @@ class PROGRESS_REPORTER;
*
* @param aBoard board the pcb should be appended to
* @param aFileName file name of board file
* @param aProgressReporter report import progress, might be a nullptr.
* @param aFileMapping mapping how altium stream names are mapped
*/
void ParseAltiumPcb( BOARD* aBoard, const wxString& aFileName, PROGRESS_REPORTER* aProgressReporter,

View File

@ -263,6 +263,8 @@ static const std::vector<READ_PROPERTIES_CASE> read_properties = {
{ "|A=a\na\0", { { "A", "a\na" } } },
{ "|A=a\ta\0", { { "A", "a\ta" } } },
// Encoding, TODO: extend
{ "|%UTF8%A=abc\0", { { "%UTF8%A", "abc" } } },
{ "|%UTF8%A=\xc2\xa6\0", { { "%UTF8%A", { "\xc2\xa6", wxConvUTF8 } } } }, // Convert to '|' ?
// Correct reading errors
{ "|A|B=C\0", { { "B", "C" } } },
{ "|A=B|C\0", { { "A", "B" } } },