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() || m_pos[length - 1] != '\0' )
87  {
88  m_error = true;
89  return kv;
90  }
91 
92  //we use std::string because std::string can handle NULL-bytes
93  //wxString would end the string at the first NULL-byte
94  std::string str = std::string( m_pos, length - 1 );
95  m_pos += length;
96 
97  std::size_t token_end = 0;
98  while( token_end < str.size() && token_end != std::string::npos )
99  {
100  std::size_t token_start = str.find( '|', token_end );
101  std::size_t token_equal = str.find( '=', token_start );
102  token_end = str.find( '|', token_equal );
103 
104  std::string keyS = str.substr( token_start + 1, token_equal - token_start - 1 );
105  std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
106 
107  //convert the strings to wxStrings, since we use them everywhere
108  //value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
109  wxString key( keyS.c_str(), wxConvISO8859_1 );
110  wxString value( valueS.c_str(), wxConvISO8859_1 );
111 
112  kv.insert( { key.Trim(), value.Trim() } );
113  }
114 
115  return kv;
116 }
117 
119  const std::map<wxString, wxString>& aProperties, const wxString& aKey, int aDefault )
120 {
121  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
122  return value == aProperties.end() ? aDefault : wxAtoi( value->second );
123 }
124 
126  const std::map<wxString, wxString>& aProperties, const wxString& aKey, double aDefault )
127 {
128  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
129  if( value == aProperties.end() )
130  {
131  return aDefault;
132  }
133 
134  // Locale independent str -> double conversation
135  std::istringstream istr( (const char*) value->second.mb_str() );
136  istr.imbue( std::locale( "C" ) );
137 
138  double doubleValue;
139  istr >> doubleValue;
140  return doubleValue;
141 }
142 
144  const std::map<wxString, wxString>& aProperties, const wxString& aKey, bool aDefault )
145 {
146  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
147  return value == aProperties.end() ? aDefault : value->second == "TRUE";
148 }
149 
150 int32_t ALTIUM_PARSER::PropertiesReadKicadUnit( const std::map<wxString, wxString>& aProperties,
151  const wxString& aKey, const wxString& aDefault )
152 {
153  const wxString& value = PropertiesReadString( aProperties, aKey, aDefault );
154 
155  size_t decimal_point = value.find( '.' );
156  size_t value_end = value.find_first_not_of( "+-0123456789." );
157 
158  wxString before_decimal_str = value.Left( decimal_point );
159  int before_decimal = wxAtoi( before_decimal_str );
160  int after_decimal = 0;
161  size_t after_decimal_digits = 0;
162  if( decimal_point != wxString::npos )
163  {
164  if( value_end != wxString::npos )
165  {
166  after_decimal_digits = value_end - ( decimal_point + 1 );
167  }
168  else
169  {
170  after_decimal_digits = value.size() - ( decimal_point + 1 ); // TODO: correct?
171  }
172  wxString after_decimal_str = value.Mid( decimal_point + 1, after_decimal_digits );
173  after_decimal = wxAtoi( after_decimal_str );
174  }
175 
176  if( value.length() > 3 && value.compare( value.length() - 3, 3, "mil" ) == 0 )
177  {
178  // ensure after_decimal is formatted to base 1000
179  int after_decimal_1000;
180  if( after_decimal_digits <= 4 )
181  {
182  after_decimal_1000 =
183  static_cast<int>( after_decimal * std::pow( 10, 4 - after_decimal_digits ) );
184  }
185  else
186  {
187  after_decimal_1000 =
188  static_cast<int>( after_decimal / std::pow( 10, after_decimal_digits - 4 ) );
189  }
190 
191  int32_t mils = before_decimal * 10000 + after_decimal_1000;
192  return ConvertToKicadUnit( mils );
193  }
194 
195  wxLogError( wxString::Format( "Unit '%s' does not end with mils" ), value );
196  return 0;
197 }
198 
199 wxString ALTIUM_PARSER::PropertiesReadString( const std::map<wxString, wxString>& aProperties,
200  const wxString& aKey, const wxString& aDefault )
201 {
202  const std::map<wxString, wxString>::const_iterator& value = aProperties.find( aKey );
203  return value == aProperties.end() ? aDefault : value->second;
204 }
static int PropertiesReadInt(const std::map< wxString, wxString > &aProperties, const wxString &aKey, int aDefault)
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)
#define kv
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)
ALTIUM_PARSER(const CFB::CompoundFileReader &aReader, const CFB::COMPOUND_FILE_ENTRY *aEntry)
const char * name
Definition: DXF_plotter.cpp:60
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
static wxString PropertiesReadString(const std::map< wxString, wxString > &aProperties, const wxString &aKey, const wxString &aDefault)
std::unique_ptr< char[]> m_content