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 wxLogTrace( traceLibraries, "Saving %s", Path() );
296 wxFileName fn( Path() );
297 // This should already be normalized, but just in case...
298 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
299
300 // Global user data with no other recovery path: keep a rotating .bak sibling so the
301 // user can recover from logical corruption outside our fsync window.
302 wxFFile existing( fn.GetFullPath(), wxT( "rb" ) );
303
304 if( existing.IsOpened() )
305 {
306 wxFileOffset rawLen = existing.Length();
307
308 if( rawLen >= 0
309 && static_cast<uint64_t>( rawLen ) <= std::numeric_limits<size_t>::max() )
310 {
311 size_t len = static_cast<size_t>( rawLen );
312 wxMemoryBuffer buf;
313 void* dst = len > 0 ? buf.GetWriteBuf( len ) : nullptr;
314
315 if( len == 0 || existing.Read( dst, len ) == len )
316 {
317 buf.SetDataLen( len );
318 existing.Close();
319
320 wxString bakPath = fn.GetFullPath() + wxT( ".bak" );
321 wxString bakError;
322
323 if( !KIPLATFORM::IO::AtomicWriteFile( bakPath, buf.GetData(), len, &bakError ) )
324 {
325 // Non-fatal: the original is still on disk and the atomic save below
326 // is safe.
327 wxLogTrace( traceLibraries,
328 "Could not rotate library table backup to '%s': %s", bakPath,
329 bakError );
330 }
331 }
332 }
333 }
334
335 try
336 {
337 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( fn.GetFullPath(), KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
338 Format( &formatter );
339 formatter.Finish();
340 }
341 catch( IO_ERROR& e )
342 {
343 wxLogTrace( traceLibraries, "Exception while saving: %s", e.What() );
344 return tl::unexpected( LIBRARY_ERROR( e.What() ) );
345 }
346
347 return LIBRARY_RESULT<void>();
348}
349
350
351#define OPT_SEP '|'
352
353std::map<std::string, UTF8> LIBRARY_TABLE::ParseOptions( const std::string& aOptionsList )
354{
355 std::map<std::string, UTF8> props;
356
357 if( aOptionsList.size() )
358 {
359 const char* cp = &aOptionsList[0];
360 const char* end = cp + aOptionsList.size();
361
362 std::string pair;
363
364 // Parse all name=value pairs
365 while( cp < end )
366 {
367 pair.clear();
368
369 // Skip leading white space.
370 while( cp < end && isspace( *cp ) )
371 ++cp;
372
373 // Find the end of pair/field
374 while( cp < end )
375 {
376 if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
377 {
378 ++cp; // skip the escape
379 pair += *cp++; // add the separator
380 }
381 else if( *cp == OPT_SEP )
382 {
383 ++cp; // skip the separator
384 break; // process the pair
385 }
386 else
387 {
388 pair += *cp++;
389 }
390 }
391
392 // stash the pair
393 if( pair.size() )
394 {
395 // first equals sign separates 'name' and 'value'.
396 size_t eqNdx = pair.find( '=' );
397
398 if( eqNdx != pair.npos )
399 {
400 std::string name = pair.substr( 0, eqNdx );
401 std::string value = pair.substr( eqNdx + 1 );
402 props[name] = value;
403 }
404 else
405 {
406 props[pair] = ""; // property is present, but with no value.
407 }
408 }
409 }
410 }
411
412 return props;
413}
414
415
416UTF8 LIBRARY_TABLE::FormatOptions( const std::map<std::string, UTF8>* aProperties )
417{
418 UTF8 ret;
419
420 if( aProperties )
421 {
422 for( std::map<std::string, UTF8>::const_iterator it = aProperties->begin();
423 it != aProperties->end(); ++it )
424 {
425 const std::string& name = it->first;
426
427 const UTF8& value = it->second;
428
429 if( ret.size() )
430 ret += OPT_SEP;
431
432 ret += name;
433
434 // the separation between name and value is '='
435 if( value.size() )
436 {
437 ret += '=';
438
439 for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
440 {
441 // escape any separator in the value.
442 if( *si == OPT_SEP )
443 ret += '\\';
444
445 ret += *si;
446 }
447 }
448 }
449 }
450
451 return ret;
452}
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)
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