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 LIST_SEPARATOR = '\x1f';
50
52inline constexpr char MANAGED_CACHE_KEY[] = "kicad_import_cache";
53
55inline wxString ManagedCacheOption()
56{
57 return wxString( MANAGED_CACHE_KEY ) + wxS( "=1" );
58}
59
61inline wxString JoinList( const wxArrayString& aItems )
62{
63 return wxJoin( aItems, LIST_SEPARATOR, '\0' );
64}
65
67inline std::vector<wxString> SplitList( const wxString& aValue )
68{
69 std::vector<wxString> out;
70
71 for( const wxString& item : wxSplit( aValue, LIST_SEPARATOR, '\0' ) )
72 {
73 if( !item.IsEmpty() )
74 out.push_back( item );
75 }
76
77 return out;
78}
79
82inline constexpr char NET_NAME_MAP_TAG[] = "netmap";
83
85inline wxString JoinNetNameMap( const std::map<wxString, wxString>& aNames )
86{
87 wxArrayString fields;
88 fields.Add( NET_NAME_MAP_TAG );
89
90 for( const auto& [source, target] : aNames )
91 {
92 fields.Add( source );
93 fields.Add( target );
94 }
95
96 return JoinList( fields );
97}
98
100inline std::optional<std::map<wxString, wxString>> SplitNetNameMap( const wxString& aValue )
101{
102 wxArrayString fields = wxSplit( aValue, LIST_SEPARATOR, '\0' );
103
104 if( fields.IsEmpty() || fields[0] != NET_NAME_MAP_TAG || fields.GetCount() % 2 == 0 )
105 return std::nullopt;
106
107 std::map<wxString, wxString> names;
108
109 for( size_t i = 1; i < fields.GetCount(); i += 2 )
110 names.emplace( fields[i], fields[i + 1] );
111
112 return names;
113}
114
116inline void ReadFootprintProps( const std::map<std::string, UTF8>* aProps, wxString& aCacheNickname,
117 std::vector<wxString>& aSourceFpLibs )
118{
119 if( !aProps )
120 return;
121
122 if( auto it = aProps->find( FP_CACHE_NICKNAME ); it != aProps->end() )
123 aCacheNickname = it->second.wx_str();
124
125 if( auto it = aProps->find( SOURCE_FP_LIBS ); it != aProps->end() )
126 aSourceFpLibs = SplitList( it->second.wx_str() );
127}
128
130inline void ReadSymbolProps( const std::map<std::string, UTF8>* aProps, wxString& aCacheNickname,
131 std::vector<wxString>& aSourceSymLibs )
132{
133 if( !aProps )
134 return;
135
136 if( auto it = aProps->find( SYM_CACHE_NICKNAME ); it != aProps->end() )
137 aCacheNickname = it->second.wx_str();
138
139 if( auto it = aProps->find( SOURCE_SYM_LIBS ); it != aProps->end() )
140 aSourceSymLibs = SplitList( it->second.wx_str() );
141}
142
144inline wxString MakeCacheNickname( const wxString& aStem )
145{
146 return LIB_ID::FixIllegalChars( aStem + wxS( "-import-fps" ), true ).wx_str();
147}
148
150inline wxString MakeSymbolCacheNickname( const wxString& aStem )
151{
152 return LIB_ID::FixIllegalChars( aStem + wxS( "-import-syms" ), true ).wx_str();
153}
154}
155
156#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...
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 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.
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.