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