KiCad PCB EDA Suite
altium_parser.h
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  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef ALTIUM_PARSER_H
26 #define ALTIUM_PARSER_H
27 
28 #include <map>
29 #include <memory>
30 
31 #include <wx/gdicmn.h>
32 #include <vector>
33 
34 
35 namespace CFB
36 {
37 class CompoundFileReader;
38 struct COMPOUND_FILE_ENTRY;
39 } // namespace CFB
40 
41 // Helper method to find file inside compound file
42 const CFB::COMPOUND_FILE_ENTRY* FindStream(
43  const CFB::CompoundFileReader& aReader, const char* aStreamName );
44 
45 
47 {
48 public:
49  ALTIUM_PARSER( const CFB::CompoundFileReader& aReader, const CFB::COMPOUND_FILE_ENTRY* aEntry );
50  ALTIUM_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize );
51  ~ALTIUM_PARSER() = default;
52 
53  template <typename Type>
54  Type Read()
55  {
56  if( GetRemainingBytes() >= sizeof( Type ) )
57  {
58  Type val = *(Type*) ( m_pos );
59  m_pos += sizeof( Type );
60  return val;
61  }
62  else
63  {
64  m_error = true;
65  return 0;
66  }
67  }
68 
69  wxString ReadWxString()
70  {
71  uint8_t len = Read<uint8_t>();
72  if( GetRemainingBytes() >= len )
73  {
74 
75  //altium uses LATIN1/ISO 8859-1, convert it
76  wxString val = wxString( m_pos, wxConvISO8859_1, len );
77  m_pos += len;
78  return val;
79  }
80  else
81  {
82  m_error = true;
83  return wxString( "" );
84  }
85  }
86 
87  std::vector<char> ReadVector( size_t aSize )
88  {
89  if( aSize > GetRemainingBytes() )
90  {
91  m_error = true;
92  return {};
93  }
94  else
95  {
96  std::vector<char> data( m_pos, m_pos + aSize );
97  m_pos += aSize;
98  return data;
99  }
100  }
101 
102  int32_t ReadKicadUnit()
103  {
104  return ConvertToKicadUnit( Read<int32_t>() );
105  }
106 
107  int32_t ReadKicadUnitX()
108  {
109  return ReadKicadUnit();
110  }
111 
112  int32_t ReadKicadUnitY()
113  {
114  return -ReadKicadUnit();
115  }
116 
117  wxPoint ReadWxPoint()
118  {
119  int32_t x = ReadKicadUnitX();
120  int32_t y = ReadKicadUnitY();
121  return { x, y };
122  }
123 
124  wxSize ReadWxSize()
125  {
126  int32_t x = ReadKicadUnit();
127  int32_t y = ReadKicadUnit();
128  return { x, y };
129  }
130 
132  {
133  uint32_t length = Read<uint32_t>();
134  m_subrecord_end = m_pos + length;
135  return length;
136  }
137 
138  std::map<wxString, wxString> ReadProperties();
139 
140  static int32_t ConvertToKicadUnit( const double aValue );
141 
142  static int PropertiesReadInt(
143  const std::map<wxString, wxString>& aProperties, const wxString& aKey, int aDefault );
144 
145  static double PropertiesReadDouble( const std::map<wxString, wxString>& aProperties,
146  const wxString& aKey, double aDefault );
147 
148  static bool PropertiesReadBool(
149  const std::map<wxString, wxString>& aProperties, const wxString& aKey, bool aDefault );
150 
151  static int32_t PropertiesReadKicadUnit( const std::map<wxString, wxString>& aProperties,
152  const wxString& aKey, const wxString& aDefault );
153 
154  static wxString PropertiesReadString( const std::map<wxString, wxString>& aProperties,
155  const wxString& aKey, const wxString& aDefault );
156 
157  void Skip( size_t aLength )
158  {
159  if( GetRemainingBytes() >= aLength )
160  {
161  m_pos += aLength;
162  }
163  else
164  {
165  m_error = true;
166  }
167  }
168 
170  {
171  if( m_subrecord_end == nullptr || m_subrecord_end < m_pos )
172  {
173  m_error = true;
174  }
175  else
176  {
178  }
179  };
180 
181  size_t GetRemainingBytes() const
182  {
183  return m_pos == nullptr ? 0 : m_size - ( m_pos - m_content.get() );
184  }
185 
187  {
188  return m_pos == nullptr || m_subrecord_end == nullptr || m_subrecord_end <= m_pos ?
189  0 :
191  };
192 
194  {
195  return m_error;
196  }
197 
198 private:
199  std::unique_ptr<char[]> m_content;
200  size_t m_size;
201 
202  char* m_pos; // current read pointer
203  char* m_subrecord_end; // pointer which points to next subrecord start
204  bool m_error;
205 };
206 
207 
208 #endif //ALTIUM_PARSER_H
static int PropertiesReadInt(const std::map< wxString, wxString > &aProperties, const wxString &aKey, int aDefault)
~ALTIUM_PARSER()=default
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)
void Skip(size_t aLength)
size_t GetRemainingSubrecordBytes() const
wxSize ReadWxSize()
std::vector< char > ReadVector(size_t aSize)
Definition: altium_parser.h:87
size_t ReadAndSetSubrecordLength()
std::map< wxString, wxString > ReadProperties()
wxPoint ReadWxPoint()
static double PropertiesReadDouble(const std::map< wxString, wxString > &aProperties, const wxString &aKey, double aDefault)
int32_t ReadKicadUnit()
ALTIUM_PARSER(const CFB::CompoundFileReader &aReader, const CFB::COMPOUND_FILE_ENTRY *aEntry)
wxString ReadWxString()
Definition: altium_parser.h:69
int32_t ReadKicadUnitX()
void SkipSubrecord()
const CFB::COMPOUND_FILE_ENTRY * FindStream(const CFB::CompoundFileReader &aReader, const char *aStreamName)
int32_t ReadKicadUnitY()
static wxString PropertiesReadString(const std::map< wxString, wxString > &aProperties, const wxString &aKey, const wxString &aDefault)
std::unique_ptr< char[]> m_content
bool HasParsingError()
static int32_t ConvertToKicadUnit(const double aValue)