KiCad PCB EDA Suite
Loading...
Searching...
No Matches
altium_ascii_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 <[email protected]>
5 * Copyright The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "altium_ascii_parser.h"
22#include "altium_parser_utils.h"
23
24
25ALTIUM_ASCII_PARSER::ALTIUM_ASCII_PARSER( const wxString& aInputFile ) :
26 m_fileInput( aInputFile.fn_str() )
27{
28}
29
30
31std::map<wxString, wxString> ALTIUM_ASCII_PARSER::ReadProperties()
32{
33 std::map<wxString, wxString> kv;
34 std::string str;
35 std::string line;
36
37 // Lines ending with |> continue on the next line
38 do
39 {
40 if( !std::getline( m_fileInput, line ) )
41 {
42 m_error = true;
43 return kv;
44 }
45
46 if( ( line.size() > 2 && line[line.size() - 2] == '|' && line[line.size() - 1] == '>' ) )
47 {
48 str.append( line, 0, line.size() - 2 );
49 }
50 else
51 {
52 str.append( line );
53 break;
54 }
55 } while( true );
56
57 std::size_t token_end = 0;
58
59 while( token_end < str.size() && token_end != std::string::npos )
60 {
61 std::size_t token_start = str.find( '|', token_end );
62 std::size_t token_equal = str.find( '=', token_end );
63 std::size_t key_start;
64
65 if( token_start <= token_equal )
66 {
67 key_start = token_start + 1;
68 }
69 else
70 {
71 // Leading "|" before "RECORD=28" may be missing in older schematic versions.
72 key_start = token_end;
73 }
74
75 token_end = str.find( '|', key_start );
76
77 if( token_equal >= token_end )
78 {
79 continue; // this looks like an error: skip the entry. Also matches on std::string::npos
80 }
81
82 if( token_end == std::string::npos )
83 {
84 token_end = str.size() + 1; // this is the correct offset
85 }
86
87 std::string keyS = str.substr( key_start, token_equal - key_start );
88 std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
89
90 // convert the strings to wxStrings, since we use them everywhere
91 // value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
92 wxString key( keyS.c_str(), wxConvISO8859_1 );
93
94 // Altium stores keys either in Upper, or in CamelCase. Lets unify it.
95 wxString canonicalKey = key.Trim( false ).Trim( true ).MakeUpper();
96
97 // If the key starts with '%UTF8%' we have to parse the value using UTF8
98 wxString value;
99
100 if( canonicalKey.StartsWith( "%UTF8%" ) )
101 value = wxString( valueS.c_str(), wxConvUTF8 );
102 else
103 value = wxString( valueS.c_str(), wxConvISO8859_1 );
104
105 if( canonicalKey != wxS( "PATTERN" ) && canonicalKey != wxS( "SOURCEFOOTPRINTLIBRARY" ) )
106 {
107 // Breathless hack because I haven't a clue what the story is here (but this character
108 // appears in a lot of radial dimensions and is rendered by Altium as a space).
109 value.Replace( wxT( "ÿ" ), wxT( " " ) );
110 }
111
112 // Storage binary data do not need conversion.
113 if( str.rfind( "|BINARY", 0 ) != 0 )
114 {
115 if( canonicalKey == wxT( "DESIGNATOR" ) || canonicalKey == wxT( "NAME" )
116 || canonicalKey == wxT( "TEXT" ) )
117 {
118 wxString recordType = kv[ wxT( "RECORD" ) ];
119
120 // RECORD=33 (FILE_NAME) stores Windows-formatted paths; keep the backslashes intact.
121 if( recordType != wxT( "4" ) && recordType != wxT( "33" ) )
122 value = AltiumPropertyToKiCadString( value );
123 }
124 }
125
126 kv.insert( { canonicalKey, value.Trim() } );
127 }
128
129 return kv;
130}
131
132
134{
135 return m_fileInput && m_fileInput.peek() != std::ifstream::traits_type::eof();
136}
wxString AltiumPropertyToKiCadString(const wxString &aString)
std::map< wxString, wxString > ReadProperties()
ALTIUM_ASCII_PARSER(const wxString &aInputFile)
#define kv