KiCad PCB EDA Suite
Loading...
Searching...
No Matches
import_proj_properties.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef IMPORT_PROJ_PROPERTIES_H
21#define IMPORT_PROJ_PROPERTIES_H
22
23#include <map>
24#include <optional>
25#include <string>
26#include <vector>
27
28#include <core/utf8.h>
29#include <lib_id.h>
30#include <wx/arrstr.h>
31#include <wx/string.h>
32
41{
42inline constexpr char FP_CACHE_NICKNAME[] = "import_fp_cache_nickname";
43inline constexpr char SOURCE_FP_LIBS[] = "import_source_fp_libs";
44inline constexpr char SYM_CACHE_NICKNAME[] = "import_sym_cache_nickname";
45inline constexpr char SOURCE_SYM_LIBS[] = "import_source_sym_libs";
46inline constexpr char NET_NAME_MAP[] = "import_net_name_map";
47
49inline constexpr char PROJECT_FILE[] = "project_file";
50
52inline constexpr char LIST_SEPARATOR = '\x1f';
53
55inline constexpr char MANAGED_CACHE_KEY[] = "kicad_import_cache";
56
58inline wxString ManagedCacheOption()
59{
60 return wxString( MANAGED_CACHE_KEY ) + wxS( "=1" );
61}
62
64inline wxString JoinList( const wxArrayString& aItems )
65{
66 return wxJoin( aItems, LIST_SEPARATOR, '\0' );
67}
68
70inline std::vector<wxString> SplitList( const wxString& aValue )
71{
72 std::vector<wxString> out;
73
74 for( const wxString& item : wxSplit( aValue, LIST_SEPARATOR, '\0' ) )
75 {
76 if( !item.IsEmpty() )
77 out.push_back( item );
78 }
79
80 return out;
81}
82
85inline constexpr char NET_NAME_MAP_TAG[] = "netmap";
86
88inline constexpr char FOOTPRINT_ID_MAP_TAG[] = "fpids";
89
91inline wxString JoinTaggedMap( const char* aTag, const std::map<wxString, wxString>& aPairs )
92{
93 wxArrayString fields;
94 fields.Add( aTag );
95
96 for( const auto& [key, value] : aPairs )
97 {
98 fields.Add( key );
99 fields.Add( value );
100 }
101
102 return JoinList( fields );
103}
104
106inline std::optional<std::map<wxString, wxString>> SplitTaggedMap( const char* aTag, const wxString& aValue )
107{
108 wxArrayString fields = wxSplit( aValue, LIST_SEPARATOR, '\0' );
109
110 if( fields.IsEmpty() || fields[0] != aTag || fields.GetCount() % 2 == 0 )
111 return std::nullopt;
112
113 std::map<wxString, wxString> pairs;
114
115 for( size_t i = 1; i < fields.GetCount(); i += 2 )
116 pairs.emplace( fields[i], fields[i + 1] );
117
118 return pairs;
119}
120
122inline wxString JoinNetNameMap( const std::map<wxString, wxString>& aNames )
123{
124 return JoinTaggedMap( NET_NAME_MAP_TAG, aNames );
125}
126
128inline std::optional<std::map<wxString, wxString>> SplitNetNameMap( const wxString& aValue )
129{
130 return SplitTaggedMap( NET_NAME_MAP_TAG, aValue );
131}
132
134inline void ReadFootprintProps( const std::map<std::string, UTF8>* aProps, wxString& aCacheNickname,
135 std::vector<wxString>& aSourceFpLibs )
136{
137 if( !aProps )
138 return;
139
140 if( auto it = aProps->find( FP_CACHE_NICKNAME ); it != aProps->end() )
141 aCacheNickname = it->second.wx_str();
142
143 if( auto it = aProps->find( SOURCE_FP_LIBS ); it != aProps->end() )
144 aSourceFpLibs = SplitList( it->second.wx_str() );
145}
146
148inline void ReadSymbolProps( const std::map<std::string, UTF8>* aProps, wxString& aCacheNickname,
149 std::vector<wxString>& aSourceSymLibs )
150{
151 if( !aProps )
152 return;
153
154 if( auto it = aProps->find( SYM_CACHE_NICKNAME ); it != aProps->end() )
155 aCacheNickname = it->second.wx_str();
156
157 if( auto it = aProps->find( SOURCE_SYM_LIBS ); it != aProps->end() )
158 aSourceSymLibs = SplitList( it->second.wx_str() );
159}
160
162inline wxString MakeCacheNickname( const wxString& aStem )
163{
164 return LIB_ID::FixIllegalChars( aStem + wxS( "-import-fps" ), true ).wx_str();
165}
166
168inline wxString MakeSymbolCacheNickname( const wxString& aStem )
169{
170 return LIB_ID::FixIllegalChars( aStem + wxS( "-import-syms" ), true ).wx_str();
171}
172}
173
174#endif // IMPORT_PROJ_PROPERTIES_H
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition lib_id.cpp:205
wxString wx_str() const
Definition utf8.cpp:41
Property keys and codec threaded through IMPORT_PROJ_HELPER::m_properties to coordinate a non-KiCad p...
std::optional< std::map< wxString, wxString > > SplitTaggedMap(const char *aTag, const wxString &aValue)
Decode a tagged string map property value, or nothing when the value is not one.
void ReadSymbolProps(const std::map< std::string, UTF8 > *aProps, wxString &aCacheNickname, std::vector< wxString > &aSourceSymLibs)
Read the symbol-import coordination properties out of a properties map.
wxString ManagedCacheOption()
Options string identifying a library-table row as a generated import cache.
constexpr char SOURCE_FP_LIBS[]
constexpr char MANAGED_CACHE_KEY[]
Library-table row option key marking a row as a generated import cache.
constexpr char SYM_CACHE_NICKNAME[]
constexpr char FP_CACHE_NICKNAME[]
wxString JoinList(const wxArrayString &aItems)
Encode library nicknames into a single list-property value.
wxString JoinNetNameMap(const std::map< wxString, wxString > &aNames)
Encode source-to-imported net names as a single property value.
wxString JoinTaggedMap(const char *aTag, const std::map< wxString, wxString > &aPairs)
Encode a string map as a single property value led by a tag.
wxString MakeCacheNickname(const wxString &aStem)
Derive the generated footprint-cache nickname from a project or file stem.
constexpr char NET_NAME_MAP_TAG[]
Leading field marking a value as an encoded net-name map, so a design that maps no nets is still dist...
wxString MakeSymbolCacheNickname(const wxString &aStem)
Derive the generated symbol-cache nickname from a project or file stem.
constexpr char LIST_SEPARATOR
Separator joining a list value within a single property.
constexpr char FOOTPRINT_ID_MAP_TAG[]
Leading field marking a value as an encoded map from reference to footprint LIB_ID.
std::vector< wxString > SplitList(const wxString &aValue)
Decode a list-property value back into nicknames, dropping empties.
std::optional< std::map< wxString, wxString > > SplitNetNameMap(const wxString &aValue)
Decode a net-name map property value, or nothing when the value is not one.
constexpr char SOURCE_SYM_LIBS[]
constexpr char NET_NAME_MAP[]
void ReadFootprintProps(const std::map< std::string, UTF8 > *aProps, wxString &aCacheNickname, std::vector< wxString > &aSourceFpLibs)
Read the footprint-import coordination properties out of a properties map.
constexpr char PROJECT_FILE[]
Source project file, present when the importer is handed a project's sheets rather than one file.