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 <sstream>
29 #include <utf.h>
30 #include <wx/translation.h>
31 #include <wx/wx.h>
32 
33 
34 const CFB::COMPOUND_FILE_ENTRY* FindStream(
35  const CFB::CompoundFileReader& aReader, const char* aStreamName )
36 {
37  const CFB::COMPOUND_FILE_ENTRY* ret = nullptr;
38  aReader.EnumFiles( aReader.GetRootEntry(), -1,
39  [&]( const CFB::COMPOUND_FILE_ENTRY* aEntry, const CFB::utf16string& aU16dir,
40  int level ) -> void {
41  if( aReader.IsStream( aEntry ) )
42  {
43  std::string name = UTF16ToUTF8( aEntry->name );
44  if( aU16dir.length() > 0 )
45  {
46  std::string dir = UTF16ToUTF8( aU16dir.c_str() );
47  if( strncmp( aStreamName, dir.c_str(), dir.length() ) == 0
48  && aStreamName[dir.length()] == '\\'
49  && strcmp( aStreamName + dir.length() + 1, name.c_str() ) == 0 )
50  {
51  ret = aEntry;
52  }
53  }
54  else
55  {
56  if( strcmp( aStreamName, name.c_str() ) == 0 )
57  {
58  ret = aEntry;
59  }
60  }
61  }
62  } );
63  return ret;
64 }
65 
66 
68  const CFB::CompoundFileReader& aReader, const CFB::COMPOUND_FILE_ENTRY* aEntry )
69 {
70  m_subrecord_end = nullptr;
71  m_size = static_cast<size_t>( aEntry->size );
72  m_error = false;
73  m_content.reset( new char[m_size] );
74  m_pos = m_content.get();
75 
76  // read file into buffer
77  aReader.ReadFile( aEntry, 0, m_content.get(), m_size );
78 }
79 
80 
81 std::map<wxString, wxString> ALTIUM_PARSER::ReadProperties()
82 {
83  std::map<wxString, wxString> kv;
84 
85  uint32_t length = Read<uint32_t>();
86  if( length > GetRemainingBytes() )
87  {
88  m_error = true;
89  return kv;
90  }
91 
92  // There is one case by kliment where Board6 ends with "|NEARDISTANCE=1000mi".
93  // Both the 'l' and the null-byte are missing, which looks like Altium swallowed two bytes.
94  if( m_pos[length - 1] != '\0' )
95  {
96  wxLogError( "For Altium import, we assumes a null byte at the end of a list of properties. "
97  "Because this is missing, imported data might be malformed or missing." );
98  }
99 
100  //we use std::string because std::string can handle NULL-bytes
101  //wxString would end the string at the first NULL-byte
102  std::string str = std::string( m_pos, length - 1 );
103  m_pos += length;
104 
105  std::size_t token_end = 0;
106  while( token_end < str.size() && token_end != std::string::npos )
107  {
108  std::size_t token_start = str.find( '|', token_end );
109  std::size_t token_equal = str.find( '=', token_start );
110  token_end = str.find( '|', token_equal );
111 
112  std::string keyS = str.substr( token_start + 1, token_equal - token_start - 1 );
113  std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
114 
115  //convert the strings to wxStrings, since we use them everywhere
116  //value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
117  wxString key( keyS.c_str(), wxConvISO8859_1 );
118  wxString value( valueS.c_str(), wxConvISO8859_1 );
119 
120  // Altium stores keys either in Upper, or in CamelCase. Lets unify it.
121  kv.insert( { key.Trim().MakeUpper(), value.Trim() } );
122  }
123 
124  return kv;
125 }
126 
128  const std::map<wxString, wxString>& aProperties, const wxString& aKey, int aDefault )
129 {
130  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
131  return value == aProperties.end() ? aDefault : wxAtoi( value->second );
132 }
133 
135  const std::map<wxString, wxString>& aProperties, const wxString& aKey, double aDefault )
136 {
137  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
138  if( value == aProperties.end() )
139  {
140  return aDefault;
141  }
142 
143  // Locale independent str -> double conversation
144  std::istringstream istr( (const char*) value->second.mb_str() );
145  istr.imbue( std::locale::classic() );
146 
147  double doubleValue;
148  istr >> doubleValue;
149  return doubleValue;
150 }
151 
153  const std::map<wxString, wxString>& aProperties, const wxString& aKey, bool aDefault )
154 {
155  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
156  if( value == aProperties.end() )
157  return aDefault;
158  else
159  return value->second == "T" || value->second == "TRUE";
160 }
161 
162 int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxString>& aProperties,
163  const wxString& aKey, const wxString& aDefault )
164 {
165  const wxString& value = PropertiesReadString( aProperties, aKey, aDefault );
166 
167  wxString prefix;
168  if( !value.EndsWith( "mil", &prefix ) )
169  {
170  wxLogError( wxString::Format( "Unit '%s' does not end with mil", value ) );
171  return 0;
172  }
173 
174  double mils;
175  if( !prefix.ToCDouble( &mils ) )
176  {
177  wxLogError( wxString::Format( "Cannot convert '%s' into double", prefix ) );
178  return 0;
179  }
180 
181  return ConvertToKicadUnit( mils * 10000 );
182 }
183 
184 wxString ALTIUM_PARSER::PropertiesReadString( const std::map<wxString, wxString>& aProperties,
185  const wxString& aKey, const wxString& aDefault )
186 {
187  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
188  return value == aProperties.end() ? aDefault : value->second;
189 }
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)
static int32_t ConvertToKicadUnit(const double aValue)
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
static wxString PropertiesReadString(const std::map< wxString, wxString > &aProperties, const wxString &aKey, const wxString &aDefault)
std::unique_ptr< char[]> m_content