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 
31 LIB_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 
44 wxString 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  return converted;
77 }
78 
79 
80 // https://www.altium.com/documentation/altium-designer/sch-obj-textstringtext-string-ad#!special-strings
81 wxString AltiumSpecialStringsToKiCadVariables( const wxString& aString,
82  const std::map<wxString, wxString>& aOverrides )
83 {
84  if( aString.IsEmpty() || aString.at( 0 ) != '=' )
85  {
86  return aString;
87  }
88 
89  wxString result;
90 
91  size_t start = 1;
92  size_t delimiter = 0;
93  size_t escaping_start = 0;
94  do
95  {
96  delimiter = aString.find( "+", start );
97  escaping_start = aString.find( "'", start );
98 
99  if( escaping_start < delimiter )
100  {
101  size_t text_start = escaping_start + 1;
102  size_t escaping_end = aString.find( "'", text_start );
103 
104  if( escaping_end == wxString::npos )
105  {
106  escaping_end = aString.size();
107  }
108 
109  result += aString.substr( text_start, escaping_end - text_start );
110 
111  start = escaping_end + 1;
112  }
113  else
114  {
115  wxString specialString = aString.substr( start, delimiter - start ).Trim().Trim( false );
116 
117  if( specialString.StartsWith( "\"" ) && specialString.EndsWith( "\"" ) )
118  specialString = specialString.Mid( 1, specialString.Length() - 2 );
119 
120  if( !specialString.IsEmpty() )
121  {
122  // Note: Altium variable references are case-insensitive. KiCad matches
123  // case-senstive OR to all-upper-case, so make the references all-upper-case.
124  specialString.UpperCase();
125 
126  auto overrideIt = aOverrides.find( specialString );
127 
128  if( overrideIt != aOverrides.end() )
129  specialString = overrideIt->second;
130 
131  result += wxString::Format( wxT( "${%s}" ), specialString );
132  }
133 
134  start = delimiter + 1;
135  }
136  } while( delimiter != wxString::npos );
137 
138  return result;
139 }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
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
LIB_ID AltiumToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:49
wxString AltiumPropertyToKiCadString(const wxString &aString)
wxString AltiumSpecialStringsToKiCadVariables(const wxString &aString, const std::map< wxString, wxString > &aOverrides)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...