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
23#include <cstdint>
24#include <limits>
25
26#include <kiplatform/io.h>
29#include <richio.h>
30#include <string_utils.h>
31#include <trace_helpers.h>
32#include <wx_filename.h>
33#include <xnode.h>
34#include <ki_exception.h>
36
37#include <wx/buffer.h>
38#include <wx/ffile.h>
39
40
41const wxString LIBRARY_TABLE_ROW::TABLE_TYPE_NAME = wxT( "Table" );
42
43
45{
46 return m_scope == aOther.m_scope
47 && m_nickname == aOther.m_nickname
48 && m_uri == aOther.m_uri
49 && m_type == aOther.m_type
50 && m_options == aOther.m_options
51 && m_description == aOther.m_description
52 && m_disabled == aOther.m_disabled
53 && m_hidden == aOther.m_hidden;
54}
55
56
57std::map<std::string, UTF8> LIBRARY_TABLE_ROW::GetOptionsMap() const
58{
60}
61
62
63LIBRARY_TABLE::LIBRARY_TABLE( const wxFileName &aPath, LIBRARY_TABLE_SCOPE aScope, LIBRARY_TABLE_TYPE aExpectedType ) :
64 m_scope( aScope )
65{
67
68 wxFileName fn( aPath );
70 m_path = fn.GetAbsolutePath();
71
72 if( !fn.FileExists() )
73 {
74 m_ok = false;
75 m_errorDescription = wxString::Format( _( "The library table path '%s' does not exist" ),
76 fn.GetFullPath() );
77 return;
78 }
79
80 if( fn.GetSize() <= 1 ) // test for an empty file, 1 byte allowed for BOM
81 {
82 m_ok = true;
83 m_type = aExpectedType;
84 return;
85 }
86
87 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> ir = parser.Parse( m_path.ToStdString() );
88
89 if( ir.has_value() )
90 {
91 if( aExpectedType != LIBRARY_TABLE_TYPE::UNINITIALIZED && ir->type != aExpectedType )
92 {
93 m_ok = false;
94 m_errorDescription = _( "The library table is of wrong type" );
95 return;
96 }
97
98 m_ok = initFromIR( *ir );
99 }
100 else
101 {
102 m_ok = false;
103 m_errorDescription = ir.error().description;
104 }
105}
106
107
112LIBRARY_TABLE::LIBRARY_TABLE( bool aFromClipboard, const wxString &aBuffer, LIBRARY_TABLE_SCOPE aScope ) :
113 m_path( wxEmptyString ),
114 m_scope( aScope )
115{
117
118 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> ir = parser.ParseBuffer( aBuffer.ToStdString() );
119
120 if( ir.has_value() )
121 {
122 m_ok = initFromIR( *ir );
123 }
124 else
125 {
126 m_ok = false;
127 m_errorDescription = ir.error().description;
128 }
129}
130
131
132bool LIBRARY_TABLE::operator==( const LIBRARY_TABLE& aOther ) const
133{
134 return m_path == aOther.m_path
135 && m_scope == aOther.m_scope
136 && m_type == aOther.m_type
137 && m_version == aOther.m_version
138 && m_rows == aOther.m_rows;
139}
140
141
143{
144 m_type = aIR.type;
145
146 try
147 {
148 m_version = boost::lexical_cast<int>( aIR.version );
149 }
150 catch( const boost::bad_lexical_cast & )
151 {
152 m_version = std::nullopt;
153 }
154
155 for( const LIBRARY_TABLE_ROW_IR& row : aIR.rows )
156 addRowFromIR( row );
157
158 return true;
159}
160
161
163{
165
166 row.m_nickname = wxString::FromUTF8( aIR.nickname );
167 row.m_uri = wxString::FromUTF8( aIR.uri );
168 row.m_type = wxString::FromUTF8( aIR.type );
169 row.m_options = wxString::FromUTF8( aIR.options );
170 row.m_description = wxString::FromUTF8( aIR.description );
171 row.m_hidden = aIR.hidden;
172 row.m_disabled = aIR.disabled;
173 row.m_ok = true;
174 row.m_scope = m_scope;
175
176 m_rows.emplace_back( row );
177 return true;
178}
179
180
182{
183 static const std::map<LIBRARY_TABLE_TYPE, wxString> types = {
184 { LIBRARY_TABLE_TYPE::SYMBOL, "sym_lib_table" },
185 { LIBRARY_TABLE_TYPE::FOOTPRINT, "fp_lib_table" },
186 { LIBRARY_TABLE_TYPE::DESIGN_BLOCK, "design_block_lib_table" }
187 };
188
189 if( !types.contains( Type() ) )
190 {
191 THROW_IO_ERROR( "Unknown library table type: " + std::to_string( static_cast<int>( Type() ) ) );
192 }
193
194 XNODE self( wxXML_ELEMENT_NODE, types.at( Type() ) );
195
196 // TODO(JE) library tables - version management?
197 self.AddAttribute( "version", 7 );
198
199 for( const LIBRARY_TABLE_ROW& row : Rows() )
200 {
201 wxString uri = row.URI();
202 uri.Replace( '\\', '/' );
203
204 XNODE* rowNode = new XNODE( wxXML_ELEMENT_NODE, "lib" );
205 rowNode->AddAttribute( "name", row.Nickname() );
206 rowNode->AddAttribute( "type", row.Type() );
207 rowNode->AddAttribute( "uri", uri );
208 rowNode->AddAttribute( "options", row.Options() );
209 rowNode->AddAttribute( "descr", row.Description() );
210
211 if( row.Disabled() )
212 rowNode->AddChild( new XNODE( wxXML_ELEMENT_NODE, "disabled" ) );
213
214 if( row.Hidden() )
215 rowNode->AddChild( new XNODE( wxXML_ELEMENT_NODE, "hidden" ) );
216
217 self.AddChild( rowNode );
218 }
219
220 self.Format( aOutput );
221}
222
223
225{
226 LIBRARY_TABLE_ROW row = {};
227
228 row.SetScope( m_scope );
229 row.SetOk();
230
231 return row;
232}
233
234
236{
237 return Rows().emplace_back( MakeRow() );
238}
239
240
241bool LIBRARY_TABLE::HasRow( const wxString& aNickname ) const
242{
243 for( const LIBRARY_TABLE_ROW& row : m_rows )
244 {
245 if( row.Nickname() == aNickname )
246 return true;
247 }
248
249 return false;
250}
251
252
253bool LIBRARY_TABLE::HasRowWithURI( const wxString& aUri, const PROJECT& aProject,
254 bool aSubstituted ) const
255{
256 for( const LIBRARY_TABLE_ROW& row : m_rows )
257 {
258 if( !aSubstituted && row.URI() == aUri )
259 return true;
260
261 if( aSubstituted && LIBRARY_MANAGER::ExpandURI( row.URI(), aProject ) == aUri )
262 return true;
263 }
264
265 return false;
266}
267
268
269std::optional<LIBRARY_TABLE_ROW*> LIBRARY_TABLE::Row( const wxString& aNickname )
270{
271 for( LIBRARY_TABLE_ROW& row : m_rows )
272 {
273 if( row.Nickname() == aNickname )
274 return &row;
275 }
276
277 return std::nullopt;
278}
279
280
281std::optional<const LIBRARY_TABLE_ROW*> LIBRARY_TABLE::Row( const wxString& aNickname ) const
282{
283 for( const LIBRARY_TABLE_ROW& row : m_rows )
284 {
285 if( row.Nickname() == aNickname )
286 return &row;
287 }
288
289 return std::nullopt;
290}
291
292
294{
295 if( m_path.IsEmpty() )
296 return false;
297
298 wxFileName fn( m_path );
299
300 return fn.FileExists() && !fn.IsFileWritable();
301}
302
303
305{
306 if( IsReadOnly() )
307 {
308 return tl::unexpected( LIBRARY_ERROR(
309 wxString::Format( _( "Library table '%s' is read-only" ), Path() ) ) );
310 }
311
312 wxLogTrace( traceLibraries, "Saving %s", Path() );
313 wxFileName fn( Path() );
314 // This should already be normalized, but just in case...
315 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
316
317 // Global user data with no other recovery path: keep a rotating .bak sibling so the
318 // user can recover from logical corruption outside our fsync window.
319 wxFFile existing( fn.GetFullPath(), wxT( "rb" ) );
320
321 if( existing.IsOpened() )
322 {
323 wxFileOffset rawLen = existing.Length();
324
325 if( rawLen >= 0
326 && static_cast<uint64_t>( rawLen ) <= std::numeric_limits<size_t>::max() )
327 {
328 size_t len = static_cast<size_t>( rawLen );
329 wxMemoryBuffer buf;
330 void* dst = len > 0 ? buf.GetWriteBuf( len ) : nullptr;
331
332 if( len == 0 || existing.Read( dst, len ) == len )
333 {
334 buf.SetDataLen( len );
335 existing.Close();
336
337 wxString bakPath = fn.GetFullPath() + wxT( ".bak" );
338 wxString bakError;
339
340 if( !KIPLATFORM::IO::AtomicWriteFile( bakPath, buf.GetData(), len, &bakError ) )
341 {
342 // Non-fatal: the original is still on disk and the atomic save below
343 // is safe.
344 wxLogTrace( traceLibraries,
345 "Could not rotate library table backup to '%s': %s", bakPath,
346 bakError );
347 }
348 }
349 }
350 }
351
352 try
353 {
354 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( fn.GetFullPath(), KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
355 Format( &formatter );
356 formatter.Finish();
357 }
358 catch( IO_ERROR& e )
359 {
360 wxLogTrace( traceLibraries, "Exception while saving: %s", e.What() );
361 return tl::unexpected( LIBRARY_ERROR( e.What() ) );
362 }
363
364 return LIBRARY_RESULT<void>();
365}
366
367
368#define OPT_SEP '|'
369
370std::map<std::string, UTF8> LIBRARY_TABLE::ParseOptions( const std::string& aOptionsList )
371{
372 std::map<std::string, UTF8> props;
373
374 if( aOptionsList.size() )
375 {
376 const char* cp = &aOptionsList[0];
377 const char* end = cp + aOptionsList.size();
378
379 std::string pair;
380
381 // Parse all name=value pairs
382 while( cp < end )
383 {
384 pair.clear();
385
386 // Skip leading white space.
387 while( cp < end && isspace( *cp ) )
388 ++cp;
389
390 // Find the end of pair/field
391 while( cp < end )
392 {
393 if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
394 {
395 ++cp; // skip the escape
396 pair += *cp++; // add the separator
397 }
398 else if( *cp == OPT_SEP )
399 {
400 ++cp; // skip the separator
401 break; // process the pair
402 }
403 else
404 {
405 pair += *cp++;
406 }
407 }
408
409 // stash the pair
410 if( pair.size() )
411 {
412 // first equals sign separates 'name' and 'value'.
413 size_t eqNdx = pair.find( '=' );
414
415 if( eqNdx != pair.npos )
416 {
417 std::string name = pair.substr( 0, eqNdx );
418 std::string value = pair.substr( eqNdx + 1 );
419 props[name] = value;
420 }
421 else
422 {
423 props[pair] = ""; // property is present, but with no value.
424 }
425 }
426 }
427 }
428
429 return props;
430}
431
432
433UTF8 LIBRARY_TABLE::FormatOptions( const std::map<std::string, UTF8>* aProperties )
434{
435 UTF8 ret;
436
437 if( aProperties )
438 {
439 for( std::map<std::string, UTF8>::const_iterator it = aProperties->begin();
440 it != aProperties->end(); ++it )
441 {
442 const std::string& name = it->first;
443
444 const UTF8& value = it->second;
445
446 if( ret.size() )
447 ret += OPT_SEP;
448
449 ret += name;
450
451 // the separation between name and value is '='
452 if( value.size() )
453 {
454 ret += '=';
455
456 for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
457 {
458 // escape any separator in the value.
459 if( *si == OPT_SEP )
460 ret += '\\';
461
462 ret += *si;
463 }
464 }
465 }
466 }
467
468 return ret;
469}
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
bool IsReadOnly() const
Returns true if the underlying file exists but is not writable.
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)
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
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.
const std::deque< LIBRARY_TABLE_ROW > & Rows() const
LIBRARY_TABLE(const wxFileName &aPath, LIBRARY_TABLE_SCOPE aScope, LIBRARY_TABLE_TYPE aExpectedType=LIBRARY_TABLE_TYPE::UNINITIALIZED)
Creates a library table from a file on disk.
std::optional< int > m_version
The format version, if present in the parsed file.
std::deque< LIBRARY_TABLE_ROW > m_rows
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
bool Finish() override
Runs prettification over the buffered bytes, writes them to the sibling temp file,...
Definition richio.cpp:700
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 THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define OPT_SEP
options separator character
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
LIBRARY_TABLE_TYPE
LIBRARY_TABLE_SCOPE
bool AtomicWriteFile(const wxString &aTargetPath, const void *aData, size_t aSize, wxString *aError=nullptr)
Writes aData to aTargetPath via a sibling temp file, fsyncs the data and directory,...
#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