KiCad PCB EDA Suite
Loading...
Searching...
No Matches
library_table.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <boost/lexical_cast.hpp>
22
25#include <richio.h>
26#include <string_utils.h>
27#include <trace_helpers.h>
28#include <wx_filename.h>
29#include <xnode.h>
31
32
33const wxString LIBRARY_TABLE_ROW::TABLE_TYPE_NAME = wxT( "Table" );
34
35
37{
38 return m_scope == aOther.m_scope
39 && m_nickname == aOther.m_nickname
40 && m_uri == aOther.m_uri
41 && m_type == aOther.m_type
42 && m_options == aOther.m_options
43 && m_description == aOther.m_description
44 && m_disabled == aOther.m_disabled
45 && m_hidden == aOther.m_hidden;
46}
47
48
49std::map<std::string, UTF8> LIBRARY_TABLE_ROW::GetOptionsMap() const
50{
52}
53
54
55LIBRARY_TABLE::LIBRARY_TABLE( const wxFileName &aPath, LIBRARY_TABLE_SCOPE aScope ) :
56 m_scope( aScope )
57{
59
60 wxFileName fn( aPath );
62 m_path = fn.GetAbsolutePath();
63
64 if( !fn.FileExists() )
65 {
66 m_ok = false;
67 m_errorDescription = wxString::Format( _( "The library table path '%s' does not exist" ),
68 fn.GetFullPath() );
69 return;
70 }
71
72 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> ir = parser.Parse( m_path.ToStdString() );
73
74 if( ir.has_value() )
75 {
76 m_ok = initFromIR( *ir );
77 }
78 else
79 {
80 m_ok = false;
81 m_errorDescription = ir.error().description;
82 }
83}
84
85
90LIBRARY_TABLE::LIBRARY_TABLE( bool aFromClipboard, const wxString &aBuffer, LIBRARY_TABLE_SCOPE aScope ) :
91 m_path( wxEmptyString ),
92 m_scope( aScope )
93{
95
96 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> ir = parser.ParseBuffer( aBuffer.ToStdString() );
97
98 if( ir.has_value() )
99 {
100 m_ok = initFromIR( *ir );
101 }
102 else
103 {
104 m_ok = false;
105 m_errorDescription = ir.error().description;
106 }
107}
108
109
110bool LIBRARY_TABLE::operator==( const LIBRARY_TABLE& aOther ) const
111{
112 return m_path == aOther.m_path
113 && m_scope == aOther.m_scope
114 && m_type == aOther.m_type
115 && m_version == aOther.m_version
116 && m_rows == aOther.m_rows;
117}
118
119
121{
122 m_type = aIR.type;
123
124 try
125 {
126 m_version = boost::lexical_cast<int>( aIR.version );
127 }
128 catch( const boost::bad_lexical_cast & )
129 {
130 m_version = std::nullopt;
131 }
132
133 for( const LIBRARY_TABLE_ROW_IR& row : aIR.rows )
134 addRowFromIR( row );
135
136 return true;
137}
138
139
141{
143
144 row.m_nickname = wxString::FromUTF8( aIR.nickname );
145 row.m_uri = wxString::FromUTF8( aIR.uri );
146 row.m_type = wxString::FromUTF8( aIR.type );
147 row.m_options = wxString::FromUTF8( aIR.options );
148 row.m_description = wxString::FromUTF8( aIR.description );
149 row.m_hidden = aIR.hidden;
150 row.m_disabled = aIR.disabled;
151 row.m_ok = true;
152 row.m_scope = m_scope;
153
154 m_rows.emplace_back( row );
155 return true;
156}
157
158
160{
161 static const std::map<LIBRARY_TABLE_TYPE, wxString> types = {
162 { LIBRARY_TABLE_TYPE::SYMBOL, "sym_lib_table" },
163 { LIBRARY_TABLE_TYPE::FOOTPRINT, "fp_lib_table" },
164 { LIBRARY_TABLE_TYPE::DESIGN_BLOCK, "design_block_lib_table" }
165 };
166
167 wxCHECK( types.contains( Type() ), /* void */ );
168
169 XNODE self( wxXML_ELEMENT_NODE, types.at( Type() ) );
170
171 // TODO(JE) library tables - version management?
172 self.AddAttribute( "version", 7 );
173
174 for( const LIBRARY_TABLE_ROW& row : Rows() )
175 {
176 wxString uri = row.URI();
177 uri.Replace( '\\', '/' );
178
179 XNODE* rowNode = new XNODE( wxXML_ELEMENT_NODE, "lib" );
180 rowNode->AddAttribute( "name", row.Nickname() );
181 rowNode->AddAttribute( "type", row.Type() );
182 rowNode->AddAttribute( "uri", uri );
183 rowNode->AddAttribute( "options", row.Options() );
184 rowNode->AddAttribute( "descr", row.Description() );
185
186 if( row.Disabled() )
187 rowNode->AddChild( new XNODE( wxXML_ELEMENT_NODE, "disabled" ) );
188
189 if( row.Hidden() )
190 rowNode->AddChild( new XNODE( wxXML_ELEMENT_NODE, "hidden" ) );
191
192 self.AddChild( rowNode );
193 }
194
195 self.Format( aOutput );
196}
197
198
200{
201 LIBRARY_TABLE_ROW row = {};
202
203 row.SetScope( m_scope );
204 row.SetOk();
205
206 return row;
207}
208
209
211{
212 return Rows().emplace_back( MakeRow() );
213}
214
215
216bool LIBRARY_TABLE::HasRow( const wxString& aNickname ) const
217{
218 for( const LIBRARY_TABLE_ROW& row : m_rows )
219 {
220 if( row.Nickname() == aNickname )
221 return true;
222 }
223
224 return false;
225}
226
227
228bool LIBRARY_TABLE::HasRowWithURI( const wxString& aUri, const PROJECT& aProject,
229 bool aSubstituted ) const
230{
231 for( const LIBRARY_TABLE_ROW& row : m_rows )
232 {
233 if( !aSubstituted && row.URI() == aUri )
234 return true;
235
236 if( aSubstituted && LIBRARY_MANAGER::ExpandURI( row.URI(), aProject ) == aUri )
237 return true;
238 }
239
240 return false;
241}
242
243
244std::optional<LIBRARY_TABLE_ROW*> LIBRARY_TABLE::Row( const wxString& aNickname )
245{
246 for( LIBRARY_TABLE_ROW& row : m_rows )
247 {
248 if( row.Nickname() == aNickname )
249 return &row;
250 }
251
252 return std::nullopt;
253}
254
255
256std::optional<const LIBRARY_TABLE_ROW*> LIBRARY_TABLE::Row( const wxString& aNickname ) const
257{
258 for( const LIBRARY_TABLE_ROW& row : m_rows )
259 {
260 if( row.Nickname() == aNickname )
261 return &row;
262 }
263
264 return std::nullopt;
265}
266
267
269{
270 wxLogTrace( traceLibraries, "Saving %s", Path() );
271 wxFileName fn( Path() );
272 // This should already be normalized, but just in case...
273 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
274
275 try
276 {
277 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( fn.GetFullPath(), KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
278 Format( &formatter );
279 }
280 catch( IO_ERROR& e )
281 {
282 wxLogTrace( traceLibraries, "Exception while saving: %s", e.What() );
283 return tl::unexpected( LIBRARY_ERROR( e.What() ) );
284 }
285
286 return LIBRARY_RESULT<void>();
287}
288
289
290#define OPT_SEP '|'
291
292std::map<std::string, UTF8> LIBRARY_TABLE::ParseOptions( const std::string& aOptionsList )
293{
294 std::map<std::string, UTF8> props;
295
296 if( aOptionsList.size() )
297 {
298 const char* cp = &aOptionsList[0];
299 const char* end = cp + aOptionsList.size();
300
301 std::string pair;
302
303 // Parse all name=value pairs
304 while( cp < end )
305 {
306 pair.clear();
307
308 // Skip leading white space.
309 while( cp < end && isspace( *cp ) )
310 ++cp;
311
312 // Find the end of pair/field
313 while( cp < end )
314 {
315 if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
316 {
317 ++cp; // skip the escape
318 pair += *cp++; // add the separator
319 }
320 else if( *cp == OPT_SEP )
321 {
322 ++cp; // skip the separator
323 break; // process the pair
324 }
325 else
326 {
327 pair += *cp++;
328 }
329 }
330
331 // stash the pair
332 if( pair.size() )
333 {
334 // first equals sign separates 'name' and 'value'.
335 size_t eqNdx = pair.find( '=' );
336
337 if( eqNdx != pair.npos )
338 {
339 std::string name = pair.substr( 0, eqNdx );
340 std::string value = pair.substr( eqNdx + 1 );
341 props[name] = value;
342 }
343 else
344 {
345 props[pair] = ""; // property is present, but with no value.
346 }
347 }
348 }
349 }
350
351 return props;
352}
353
354
355UTF8 LIBRARY_TABLE::FormatOptions( const std::map<std::string, UTF8>* aProperties )
356{
357 UTF8 ret;
358
359 if( aProperties )
360 {
361 for( std::map<std::string, UTF8>::const_iterator it = aProperties->begin();
362 it != aProperties->end(); ++it )
363 {
364 const std::string& name = it->first;
365
366 const UTF8& value = it->second;
367
368 if( ret.size() )
369 ret += OPT_SEP;
370
371 ret += name;
372
373 // the separation between name and value is '='
374 if( value.size() )
375 {
376 ret += '=';
377
378 for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
379 {
380 // escape any separator in the value.
381 if( *si == OPT_SEP )
382 ret += '\\';
383
384 ret += *si;
385 }
386 }
387 }
388 }
389
390 return ret;
391}
const char * name
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
static wxString ExpandURI(const wxString &aShortURI, const PROJECT &aProject)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > ParseBuffer(const std::string &aBuffer)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
LIBRARY_TABLE_ROW()=default
void SetOk(bool aOk=true)
bool operator==(const LIBRARY_TABLE_ROW &aOther) const
std::map< std::string, UTF8 > GetOptionsMap() const
static const wxString TABLE_TYPE_NAME
void SetScope(LIBRARY_TABLE_SCOPE aScope)
LIBRARY_TABLE_SCOPE m_scope
LIBRARY_RESULT< void > Save()
bool operator==(const LIBRARY_TABLE &aOther) const
void Format(OUTPUTFORMATTER *aOutput) const
static std::map< std::string, UTF8 > ParseOptions(const std::string &aOptionsList)
LIBRARY_TABLE_TYPE Type() const
LIBRARY_TABLE_TYPE m_type
What type of content this table contains (footprint, symbol, design block, etc)
std::optional< LIBRARY_TABLE_ROW * > Row(const wxString &aNickname)
std::vector< LIBRARY_TABLE_ROW > m_rows
LIBRARY_TABLE_ROW & InsertRow()
Builds a new row and inserts it at the end of the table; returning a reference to the row.
static UTF8 FormatOptions(const std::map< std::string, UTF8 > *aProperties)
const wxString & Path() const
wxString m_path
The full path to the file this table was parsed from, if any.
wxString m_errorDescription
LIBRARY_TABLE_ROW MakeRow() const
Builds a new row that is suitable for this table (does not insert it)
bool HasRow(const wxString &aNickname) const
LIBRARY_TABLE(const wxFileName &aPath, LIBRARY_TABLE_SCOPE aScope)
Creates a library table from a file on disk.
bool HasRowWithURI(const wxString &aUri, const PROJECT &aProject, bool aSubstituted=false) const
Returns true if the given (fully-expanded) URI exists as a library in this table.
std::optional< int > m_version
The format version, if present in the parsed file.
const std::vector< LIBRARY_TABLE_ROW > & Rows() const
bool addRowFromIR(const LIBRARY_TABLE_ROW_IR &aIR)
LIBRARY_TABLE_SCOPE m_scope
bool initFromIR(const LIBRARY_TABLE_IR &aIR)
An interface used to output 8 bit text in a convenient way.
Definition richio.h:295
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:71
std::string::const_iterator begin() const
Definition utf8.h:219
std::string::size_type size() const
Definition utf8.h:116
std::string::const_iterator end() const
Definition utf8.h:220
static void ResolvePossibleSymlinks(wxFileName &aFilename)
An extension of wxXmlNode that can format its contents as KiCad-style s-expressions.
Definition xnode.h:71
void Format(OUTPUTFORMATTER *out) const
Write this object as UTF8 out to an OUTPUTFORMATTER as an S-expression.
Definition xnode.cpp:113
void AddAttribute(const wxString &aName, const wxString &aValue) override
Definition xnode.cpp:92
#define _(s)
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
#define OPT_SEP
options separator character
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
LIBRARY_TABLE_SCOPE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
The intermediate representation that a library table is parsed into.
std::vector< LIBRARY_TABLE_ROW_IR > rows
LIBRARY_TABLE_TYPE type
VECTOR2I end
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:39