KiCad PCB EDA Suite
altium_parser.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2019-2020 Thomas Pointhuber <thomas.pointhuber@gmx.at>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include "altium_parser.h"
25 
26 #include <compoundfilereader.h>
27 #include <ki_exception.h>
28 #include <math/util.h>
29 #include <sstream>
30 #include <utf.h>
31 #include <wx/log.h>
32 
33 const CFB::COMPOUND_FILE_ENTRY* FindStream(
34  const CFB::CompoundFileReader& aReader, const char* aStreamName )
35 {
36  const CFB::COMPOUND_FILE_ENTRY* ret = nullptr;
37  aReader.EnumFiles( aReader.GetRootEntry(), -1,
38  [&]( const CFB::COMPOUND_FILE_ENTRY* aEntry, const CFB::utf16string& aU16dir,
39  int level ) -> void {
40  if( aReader.IsStream( aEntry ) )
41  {
42  std::string name = UTF16ToUTF8( aEntry->name );
43  if( aU16dir.length() > 0 )
44  {
45  std::string dir = UTF16ToUTF8( aU16dir.c_str() );
46  if( strncmp( aStreamName, dir.c_str(), dir.length() ) == 0
47  && aStreamName[dir.length()] == '\\'
48  && strcmp( aStreamName + dir.length() + 1, name.c_str() ) == 0 )
49  {
50  ret = aEntry;
51  }
52  }
53  else
54  {
55  if( strcmp( aStreamName, name.c_str() ) == 0 )
56  {
57  ret = aEntry;
58  }
59  }
60  }
61  } );
62  return ret;
63 }
64 
65 
67  const CFB::CompoundFileReader& aReader, const CFB::COMPOUND_FILE_ENTRY* aEntry )
68 {
69  m_subrecord_end = nullptr;
70  m_size = static_cast<size_t>( aEntry->size );
71  m_error = false;
72  m_content.reset( new char[m_size] );
73  m_pos = m_content.get();
74 
75  // read file into buffer
76  aReader.ReadFile( aEntry, 0, m_content.get(), m_size );
77 }
78 
79 
80 ALTIUM_PARSER::ALTIUM_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize )
81 {
82  m_subrecord_end = nullptr;
83  m_size = aSize;
84  m_error = false;
85  m_content = std::move( aContent );
86  m_pos = m_content.get();
87 }
88 
89 
90 std::map<wxString, wxString> ALTIUM_PARSER::ReadProperties()
91 {
92  std::map<wxString, wxString> kv;
93 
94  uint32_t length = Read<uint32_t>();
95  if( length > GetRemainingBytes() )
96  {
97  m_error = true;
98  return kv;
99  }
100 
101  if( length == 0 )
102  {
103  return kv;
104  }
105 
106  // There is one case by kliment where Board6 ends with "|NEARDISTANCE=1000mi".
107  // Both the 'l' and the null-byte are missing, which looks like Altium swallowed two bytes.
108  bool hasNullByte = m_pos[length - 1] == '\0';
109  if( !hasNullByte )
110  {
111  wxLogError( "For Altium import, we assumes a null byte at the end of a list of properties. "
112  "Because this is missing, imported data might be malformed or missing." );
113  }
114 
115  // we use std::string because std::string can handle NULL-bytes
116  // wxString would end the string at the first NULL-byte
117  std::string str = std::string( m_pos, length - ( hasNullByte ? 1 : 0 ) );
118  m_pos += length;
119 
120  std::size_t token_end = 0;
121  while( token_end < str.size() && token_end != std::string::npos )
122  {
123  std::size_t token_start = str.find( '|', token_end );
124  std::size_t token_equal = str.find( '=', token_start );
125  token_end = str.find( '|', token_start + 1 );
126 
127  if( token_equal >= token_end )
128  {
129  continue; // this looks like an error: skip the entry. Also matches on std::string::npos
130  }
131 
132  if( token_end == std::string::npos )
133  {
134  token_end = str.size() + 1; // this is the correct offset
135  }
136 
137  std::string keyS = str.substr( token_start + 1, token_equal - token_start - 1 );
138  std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
139 
140  // convert the strings to wxStrings, since we use them everywhere
141  // value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
142  wxString key( keyS.c_str(), wxConvISO8859_1 );
143  wxString value( valueS.c_str(), wxConvISO8859_1 );
144 
145  // Altium stores keys either in Upper, or in CamelCase. Lets unify it.
146  kv.insert( { key.Trim( false ).Trim( true ).MakeUpper(), value.Trim() } );
147  }
148 
149  return kv;
150 }
151 
152 
153 int32_t ALTIUM_PARSER::ConvertToKicadUnit( const double aValue )
154 {
155  const double int_limit = ( std::numeric_limits<int>::max() - 1 ) / 2.54;
156 
157  int32_t iu = KiROUND( Clamp<double>( -int_limit, aValue, int_limit ) * 2.54 );
158 
159  // Altium stores metric units up to 0.001mm (1000nm) in accuracy. This code fixes rounding errors.
160  // Because imperial units > 0.01mil are always even, this workaround should never trigger for them.
161  switch( iu % 1000 )
162  {
163  case 1:
164  case -999:
165  return iu - 1;
166  case 999:
167  case -1:
168  return iu + 1;
169  default:
170  return iu;
171  }
172 }
173 
174 
176  const std::map<wxString, wxString>& aProperties, const wxString& aKey, int aDefault )
177 {
178  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
179  return value == aProperties.end() ? aDefault : wxAtoi( value->second );
180 }
181 
182 
184  const std::map<wxString, wxString>& aProperties, const wxString& aKey, double aDefault )
185 {
186  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
187  if( value == aProperties.end() )
188  {
189  return aDefault;
190  }
191 
192  // Locale independent str -> double conversation
193  std::istringstream istr( (const char*) value->second.mb_str() );
194  istr.imbue( std::locale::classic() );
195 
196  double doubleValue;
197  istr >> doubleValue;
198  return doubleValue;
199 }
200 
202  const std::map<wxString, wxString>& aProperties, const wxString& aKey, bool aDefault )
203 {
204  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
205  if( value == aProperties.end() )
206  return aDefault;
207  else
208  return value->second == "T" || value->second == "TRUE";
209 }
210 
211 int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxString>& aProperties,
212  const wxString& aKey, const wxString& aDefault )
213 {
214  const wxString& value = PropertiesReadString( aProperties, aKey, aDefault );
215 
216  wxString prefix;
217  if( !value.EndsWith( "mil", &prefix ) )
218  {
219  wxLogError( wxString::Format( "Unit '%s' does not end with mil", value ) );
220  return 0;
221  }
222 
223  double mils;
224  if( !prefix.ToCDouble( &mils ) )
225  {
226  wxLogError( wxString::Format( "Cannot convert '%s' into double", prefix ) );
227  return 0;
228  }
229 
230  return ConvertToKicadUnit( mils * 10000 );
231 }
232 
233 wxString ALTIUM_PARSER::PropertiesReadString( const std::map<wxString, wxString>& aProperties,
234  const wxString& aKey, const wxString& aDefault )
235 {
236  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
237  return value == aProperties.end() ? aDefault : value->second;
238 }
static int PropertiesReadInt(const std::map< wxString, wxString > &aProperties, const wxString &aKey, int aDefault)
#define kv
char * m_subrecord_end
size_t GetRemainingBytes() const
static bool PropertiesReadBool(const std::map< wxString, wxString > &aProperties, const wxString &aKey, bool aDefault)
static int32_t PropertiesReadKicadUnit(const std::map< wxString, wxString > &aProperties, const wxString &aKey, const wxString &aDefault)
const CFB::COMPOUND_FILE_ENTRY * FindStream(const CFB::CompoundFileReader &aReader, const char *aStreamName)
std::map< wxString, wxString > ReadProperties()
static double PropertiesReadDouble(const std::map< wxString, wxString > &aProperties, const wxString &aKey, double aDefault)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
ALTIUM_PARSER(const CFB::CompoundFileReader &aReader, const CFB::COMPOUND_FILE_ENTRY *aEntry)
const char * name
Definition: DXF_plotter.cpp:59
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:70
static wxString PropertiesReadString(const std::map< wxString, wxString > &aProperties, const wxString &aKey, const wxString &aDefault)
std::unique_ptr< char[]> m_content
static int32_t ConvertToKicadUnit(const double aValue)