2020-04-03 23:22:24 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019-2020 Thomas Pointhuber <thomas.pointhuber@gmx.at>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "altium_parser.h"
|
2021-07-01 18:41:46 +00:00
|
|
|
#include "altium_parser_utils.h"
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
|
|
#include <compoundfilereader.h>
|
|
|
|
#include <ki_exception.h>
|
2021-06-02 04:50:46 +00:00
|
|
|
#include <math/util.h>
|
2020-04-03 23:22:24 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <utf.h>
|
2021-06-07 22:38:35 +00:00
|
|
|
#include <wx/log.h>
|
2021-06-26 19:21:30 +00:00
|
|
|
#include <wx/translation.h>
|
2020-04-03 23:22:24 +00:00
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
const CFB::COMPOUND_FILE_ENTRY* FindStream( const CFB::CompoundFileReader& aReader,
|
|
|
|
const char* aStreamName )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* ret = nullptr;
|
|
|
|
aReader.EnumFiles( aReader.GetRootEntry(), -1,
|
|
|
|
[&]( const CFB::COMPOUND_FILE_ENTRY* aEntry, const CFB::utf16string& aU16dir,
|
2021-07-01 18:41:46 +00:00
|
|
|
int level ) -> void
|
|
|
|
{
|
2020-04-03 23:22:24 +00:00
|
|
|
if( aReader.IsStream( aEntry ) )
|
|
|
|
{
|
|
|
|
std::string name = UTF16ToUTF8( aEntry->name );
|
|
|
|
if( aU16dir.length() > 0 )
|
|
|
|
{
|
|
|
|
std::string dir = UTF16ToUTF8( aU16dir.c_str() );
|
|
|
|
if( strncmp( aStreamName, dir.c_str(), dir.length() ) == 0
|
|
|
|
&& aStreamName[dir.length()] == '\\'
|
|
|
|
&& strcmp( aStreamName + dir.length() + 1, name.c_str() ) == 0 )
|
|
|
|
{
|
|
|
|
ret = aEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( strcmp( aStreamName, name.c_str() ) == 0 )
|
|
|
|
{
|
|
|
|
ret = aEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
ALTIUM_PARSER::ALTIUM_PARSER( const CFB::CompoundFileReader& aReader,
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* aEntry )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
|
|
|
m_subrecord_end = nullptr;
|
2020-08-23 19:01:08 +00:00
|
|
|
m_size = static_cast<size_t>( aEntry->size );
|
|
|
|
m_error = false;
|
2020-04-03 23:22:24 +00:00
|
|
|
m_content.reset( new char[m_size] );
|
|
|
|
m_pos = m_content.get();
|
|
|
|
|
|
|
|
// read file into buffer
|
|
|
|
aReader.ReadFile( aEntry, 0, m_content.get(), m_size );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
ALTIUM_PARSER::ALTIUM_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize )
|
|
|
|
{
|
|
|
|
m_subrecord_end = nullptr;
|
|
|
|
m_size = aSize;
|
|
|
|
m_error = false;
|
|
|
|
m_content = std::move( aContent );
|
|
|
|
m_pos = m_content.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
std::map<wxString, wxString> ALTIUM_PARSER::ReadProperties()
|
|
|
|
{
|
|
|
|
std::map<wxString, wxString> kv;
|
|
|
|
|
|
|
|
uint32_t length = Read<uint32_t>();
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2021-02-07 15:51:05 +00:00
|
|
|
if( length > GetRemainingBytes() )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
|
|
|
m_error = true;
|
|
|
|
return kv;
|
|
|
|
}
|
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
if( length == 0 )
|
|
|
|
{
|
|
|
|
return kv;
|
|
|
|
}
|
|
|
|
|
2021-02-07 15:51:05 +00:00
|
|
|
// There is one case by kliment where Board6 ends with "|NEARDISTANCE=1000mi".
|
|
|
|
// Both the 'l' and the null-byte are missing, which looks like Altium swallowed two bytes.
|
2021-03-08 22:02:05 +00:00
|
|
|
bool hasNullByte = m_pos[length - 1] == '\0';
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
if( !hasNullByte )
|
2021-02-07 15:51:05 +00:00
|
|
|
{
|
2021-07-10 15:37:19 +00:00
|
|
|
wxLogError( _( "Missing null byte at end of property list. Imported data might be "
|
|
|
|
"malformed or missing." ) );
|
2021-02-07 15:51:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
// we use std::string because std::string can handle NULL-bytes
|
|
|
|
// wxString would end the string at the first NULL-byte
|
|
|
|
std::string str = std::string( m_pos, length - ( hasNullByte ? 1 : 0 ) );
|
2020-04-03 23:22:24 +00:00
|
|
|
m_pos += length;
|
|
|
|
|
|
|
|
std::size_t token_end = 0;
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
while( token_end < str.size() && token_end != std::string::npos )
|
|
|
|
{
|
|
|
|
std::size_t token_start = str.find( '|', token_end );
|
|
|
|
std::size_t token_equal = str.find( '=', token_start );
|
2021-03-08 22:02:05 +00:00
|
|
|
token_end = str.find( '|', token_start + 1 );
|
|
|
|
|
|
|
|
if( token_equal >= token_end )
|
|
|
|
{
|
|
|
|
continue; // this looks like an error: skip the entry. Also matches on std::string::npos
|
|
|
|
}
|
|
|
|
|
|
|
|
if( token_end == std::string::npos )
|
|
|
|
{
|
|
|
|
token_end = str.size() + 1; // this is the correct offset
|
|
|
|
}
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
|
|
std::string keyS = str.substr( token_start + 1, token_equal - token_start - 1 );
|
|
|
|
std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
|
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
// convert the strings to wxStrings, since we use them everywhere
|
|
|
|
// value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
|
2020-04-03 23:22:24 +00:00
|
|
|
wxString key( keyS.c_str(), wxConvISO8859_1 );
|
2020-10-18 13:23:07 +00:00
|
|
|
// Altium stores keys either in Upper, or in CamelCase. Lets unify it.
|
2021-06-26 11:58:56 +00:00
|
|
|
wxString canonicalKey = key.Trim( false ).Trim( true ).MakeUpper();
|
|
|
|
// If the key starts with '%UTF8%' we have to parse the value using UTF8
|
|
|
|
wxString value;
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2021-06-26 11:58:56 +00:00
|
|
|
if( canonicalKey.StartsWith( "%UTF8%" ) )
|
|
|
|
value = wxString( valueS.c_str(), wxConvUTF8 );
|
|
|
|
else
|
|
|
|
value = wxString( valueS.c_str(), wxConvISO8859_1 );
|
|
|
|
|
2021-07-08 14:52:09 +00:00
|
|
|
if( canonicalKey == wxT( "DESIGNATOR" )
|
|
|
|
|| canonicalKey == wxT( "NAME" )
|
|
|
|
|| canonicalKey == wxT( "TEXT" ) )
|
|
|
|
{
|
2021-07-06 10:33:41 +00:00
|
|
|
value = AltiumPropertyToKiCadString( value.Trim() );
|
2021-07-08 14:52:09 +00:00
|
|
|
}
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2021-06-26 11:58:56 +00:00
|
|
|
kv.insert( { canonicalKey, value.Trim() } );
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return kv;
|
|
|
|
}
|
|
|
|
|
2021-06-02 04:50:46 +00:00
|
|
|
|
|
|
|
int32_t ALTIUM_PARSER::ConvertToKicadUnit( const double aValue )
|
|
|
|
{
|
|
|
|
const double int_limit = ( std::numeric_limits<int>::max() - 1 ) / 2.54;
|
|
|
|
|
|
|
|
int32_t iu = KiROUND( Clamp<double>( -int_limit, aValue, int_limit ) * 2.54 );
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
// Altium stores metric units up to 0.001mm (1000nm) in accuracy. This code fixes rounding
|
|
|
|
// errors. Because imperial units > 0.01mil are always even, this workaround should never
|
|
|
|
// trigger for them.
|
2021-06-02 04:50:46 +00:00
|
|
|
switch( iu % 1000 )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case -999:
|
|
|
|
return iu - 1;
|
|
|
|
case 999:
|
|
|
|
case -1:
|
|
|
|
return iu + 1;
|
|
|
|
default:
|
|
|
|
return iu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
int ALTIUM_PARSER::PropertiesReadInt( const std::map<wxString, wxString>& aProperties,
|
|
|
|
const wxString& aKey, int aDefault )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
2020-04-06 15:38:26 +00:00
|
|
|
const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
|
|
|
|
return value == aProperties.end() ? aDefault : wxAtoi( value->second );
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 04:50:46 +00:00
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
double ALTIUM_PARSER::PropertiesReadDouble( const std::map<wxString, wxString>& aProperties,
|
|
|
|
const wxString& aKey, double aDefault )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
2020-04-06 15:38:26 +00:00
|
|
|
const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
|
|
|
|
if( value == aProperties.end() )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
|
|
|
return aDefault;
|
|
|
|
}
|
2020-04-06 15:38:26 +00:00
|
|
|
|
|
|
|
// Locale independent str -> double conversation
|
|
|
|
std::istringstream istr( (const char*) value->second.mb_str() );
|
2021-02-27 01:21:55 +00:00
|
|
|
istr.imbue( std::locale::classic() );
|
2020-04-06 15:38:26 +00:00
|
|
|
|
|
|
|
double doubleValue;
|
|
|
|
istr >> doubleValue;
|
|
|
|
return doubleValue;
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
|
|
|
|
bool ALTIUM_PARSER::PropertiesReadBool( const std::map<wxString, wxString>& aProperties,
|
|
|
|
const wxString& aKey, bool aDefault )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
2020-04-06 15:38:26 +00:00
|
|
|
const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
|
2020-10-14 19:04:53 +00:00
|
|
|
if( value == aProperties.end() )
|
|
|
|
return aDefault;
|
|
|
|
else
|
|
|
|
return value->second == "T" || value->second == "TRUE";
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxString>& aProperties,
|
2021-07-01 18:41:46 +00:00
|
|
|
const wxString& aKey, const wxString& aDefault )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
|
|
|
const wxString& value = PropertiesReadString( aProperties, aKey, aDefault );
|
|
|
|
|
2021-01-31 12:50:17 +00:00
|
|
|
wxString prefix;
|
2021-06-26 19:21:30 +00:00
|
|
|
|
2021-01-31 12:50:17 +00:00
|
|
|
if( !value.EndsWith( "mil", &prefix ) )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
2021-06-26 19:21:30 +00:00
|
|
|
wxLogError( _( "Unit '%s' does not end with 'mil'." ), value );
|
2021-01-31 12:50:17 +00:00
|
|
|
return 0;
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 12:50:17 +00:00
|
|
|
double mils;
|
2021-06-26 19:21:30 +00:00
|
|
|
|
2021-01-31 12:50:17 +00:00
|
|
|
if( !prefix.ToCDouble( &mils ) )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
2021-06-26 19:21:30 +00:00
|
|
|
wxLogError( _( "Cannot convert '%s' to double." ), prefix );
|
2021-01-31 12:50:17 +00:00
|
|
|
return 0;
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 12:50:17 +00:00
|
|
|
return ConvertToKicadUnit( mils * 10000 );
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
wxString ALTIUM_PARSER::PropertiesReadString( const std::map<wxString, wxString>& aProperties,
|
2021-07-01 18:41:46 +00:00
|
|
|
const wxString& aKey, const wxString& aDefault )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
2021-07-01 18:41:46 +00:00
|
|
|
const auto& utf8Value = aProperties.find( wxString( "%UTF8%" ) + aKey );
|
|
|
|
|
2021-06-26 11:58:56 +00:00
|
|
|
if( utf8Value != aProperties.end() )
|
|
|
|
return utf8Value->second;
|
|
|
|
|
2021-07-01 18:41:46 +00:00
|
|
|
const auto& value = aProperties.find( aKey );
|
|
|
|
|
|
|
|
if( value != aProperties.end() )
|
|
|
|
return value->second;
|
|
|
|
|
|
|
|
return aDefault;
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|