KiCad PCB EDA Suite
altium_parser_utils.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) 2021 Thomas Pointhuber <[email protected]>
5 * Copyright (C) 2021 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_parser_utils.h"
26
27#include <string_utils.h>
28#include <lib_id.h>
29
30
31LIB_ID AltiumToKiCadLibID( const wxString& aLibName, const wxString& aLibReference )
32{
33 wxString libReference = EscapeString( aLibReference, CTX_LIBID );
34
35 wxString key = !aLibName.empty() ? ( aLibName + ":" + libReference ) : libReference;
36
37 LIB_ID libId;
38 libId.Parse( key, true );
39
40 return libId;
41}
42
43
44wxString AltiumPropertyToKiCadString( const wxString& aString )
45{
46 wxString converted;
47 bool inOverbar = false;
48
49 for( wxString::const_iterator chIt = aString.begin(); chIt != aString.end(); ++chIt )
50 {
51 wxString::const_iterator lookahead = chIt + 1;
52
53 if( lookahead != aString.end() && *lookahead == '\\' )
54 {
55 if( !inOverbar )
56 {
57 converted += "~{";
58 inOverbar = true;
59 }
60
61 converted += *chIt;
62 chIt = lookahead;
63 }
64 else
65 {
66 if( inOverbar )
67 {
68 converted += "}";
69 inOverbar = false;
70 }
71
72 converted += *chIt;
73 }
74 }
75
76 if( inOverbar )
77 converted += "}";
78
79 return converted;
80}
81
82
83// https://www.altium.com/documentation/altium-designer/sch-obj-textstringtext-string-ad#!special-strings
84wxString AltiumSpecialStringsToKiCadVariables( const wxString& aString,
85 const std::map<wxString, wxString>& aOverrides )
86{
87 if( aString.IsEmpty() || aString.at( 0 ) != '=' )
88 {
89 return aString;
90 }
91
92 wxString result;
93
94 size_t start = 1;
95 size_t delimiter = 0;
96 size_t escaping_start = 0;
97 do
98 {
99 delimiter = aString.find( "+", start );
100 escaping_start = aString.find( "'", start );
101
102 if( escaping_start < delimiter )
103 {
104 size_t text_start = escaping_start + 1;
105 size_t escaping_end = aString.find( "'", text_start );
106
107 if( escaping_end == wxString::npos )
108 {
109 escaping_end = aString.size();
110 }
111
112 result += aString.substr( text_start, escaping_end - text_start );
113
114 start = escaping_end + 1;
115 }
116 else
117 {
118 wxString specialString = aString.substr( start, delimiter - start ).Trim().Trim( false );
119
120 if( specialString.StartsWith( "\"" ) && specialString.EndsWith( "\"" ) )
121 specialString = specialString.Mid( 1, specialString.Length() - 2 );
122
123 if( !specialString.IsEmpty() )
124 {
125 // Note: Altium variable references are case-insensitive. KiCad matches
126 // case-sensitive OR to all-upper-case, so make the references all-upper-case.
127 specialString.UpperCase();
128
129 auto overrideIt = aOverrides.find( specialString );
130
131 if( overrideIt != aOverrides.end() )
132 specialString = overrideIt->second;
133
134 result += wxString::Format( wxT( "${%s}" ), specialString );
135 }
136
137 start = delimiter + 1;
138 }
139 } while( delimiter != wxString::npos );
140
141 return result;
142}
wxString AltiumPropertyToKiCadString(const wxString &aString)
LIB_ID AltiumToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
wxString AltiumSpecialStringsToKiCadVariables(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:50
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
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID
Definition: string_utils.h:55