KiCad PCB EDA Suite
FP_CACHE Class Reference

Public Member Functions

 FP_CACHE (PCB_PLUGIN *aOwner, const wxString &aLibraryPath)
 
wxString GetPath () const
 
bool IsWritable () const
 
bool Exists () const
 
FOOTPRINT_MAPGetFootprints ()
 
void Save (FOOTPRINT *aFootprint=nullptr)
 Save the footprint cache or a single footprint from it to disk. More...
 
void Load ()
 
void Remove (const wxString &aFootprintName)
 
bool IsModified ()
 Return true if the cache is not up-to-date. More...
 
bool IsPath (const wxString &aPath) const
 Check if aPath is the same as the current cache path. More...
 

Static Public Member Functions

static long long GetTimestamp (const wxString &aLibPath)
 Generate a timestamp representing all source files in the cache (including the parent directory). More...
 

Private Attributes

PCB_PLUGINm_owner
 
wxFileName m_lib_path
 
wxString m_lib_raw_path
 
FOOTPRINT_MAP m_footprints
 
bool m_cache_dirty
 
long long m_cache_timestamp
 

Detailed Description

Definition at line 101 of file pcb_plugin.cpp.

Constructor & Destructor Documentation

◆ FP_CACHE()

FP_CACHE::FP_CACHE ( PCB_PLUGIN aOwner,
const wxString &  aLibraryPath 
)

Definition at line 166 of file pcb_plugin.cpp.

167{
168 m_owner = aOwner;
169 m_lib_raw_path = aLibraryPath;
170 m_lib_path.SetPath( aLibraryPath );
172 m_cache_dirty = true;
173}
long long m_cache_timestamp
Definition: pcb_plugin.cpp:110
bool m_cache_dirty
Definition: pcb_plugin.cpp:108
wxString m_lib_raw_path
Definition: pcb_plugin.cpp:105
PCB_PLUGIN * m_owner
Definition: pcb_plugin.cpp:103
wxFileName m_lib_path
Definition: pcb_plugin.cpp:104

References m_cache_dirty, m_cache_timestamp, m_lib_path, m_lib_raw_path, and m_owner.

Member Function Documentation

◆ Exists()

bool FP_CACHE::Exists ( ) const
inline

Definition at line 120 of file pcb_plugin.cpp.

120{ return m_lib_path.IsOk() && m_lib_path.DirExists(); }

References m_lib_path.

Referenced by PCB_PLUGIN::FootprintSave().

◆ GetFootprints()

FOOTPRINT_MAP & FP_CACHE::GetFootprints ( )
inline

Definition at line 122 of file pcb_plugin.cpp.

122{ return m_footprints; }
FOOTPRINT_MAP m_footprints
Definition: pcb_plugin.cpp:106

References m_footprints.

Referenced by PCB_PLUGIN::FootprintEnumerate(), PCB_PLUGIN::FootprintSave(), and PCB_PLUGIN::getFootprint().

◆ GetPath()

wxString FP_CACHE::GetPath ( ) const
inline

Definition at line 116 of file pcb_plugin.cpp.

116{ return m_lib_raw_path; }

References m_lib_raw_path.

◆ GetTimestamp()

long long FP_CACHE::GetTimestamp ( const wxString &  aLibPath)
static

Generate a timestamp representing all source files in the cache (including the parent directory).

Timestamps should not be considered ordered. They either match or they don't.

Definition at line 334 of file pcb_plugin.cpp.

335{
336 wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
337
338 return TimestampDir( aLibPath, fileSpec );
339}
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:504
const std::string KiCadFootprintFileExtension

References KiCadFootprintFileExtension, and TimestampDir().

Referenced by PCB_PLUGIN::GetLibraryTimestamp(), IsModified(), and Load().

◆ IsModified()

bool FP_CACHE::IsModified ( )

Return true if the cache is not up-to-date.

Definition at line 326 of file pcb_plugin.cpp.

327{
329
330 return m_cache_dirty;
331}
static long long GetTimestamp(const wxString &aLibPath)
Generate a timestamp representing all source files in the cache (including the parent directory).
Definition: pcb_plugin.cpp:334

References GetTimestamp(), m_cache_dirty, m_cache_timestamp, and m_lib_path.

Referenced by PCB_PLUGIN::validateCache().

◆ IsPath()

bool FP_CACHE::IsPath ( const wxString &  aPath) const

Check if aPath is the same as the current cache path.

This tests paths by converting aPath using the native separators. Internally FP_CACHE stores the current path using native separators. This prevents path miscompares on Windows due to the fact that paths can be stored with / instead of \ in the footprint library table.

Parameters
aPathis the library path to test against.
Returns
true if aPath is the same as the cache path.

Definition at line 320 of file pcb_plugin.cpp.

321{
322 return aPath == m_lib_raw_path;
323}

References m_lib_raw_path.

Referenced by PCB_PLUGIN::FootprintLibDelete(), and PCB_PLUGIN::validateCache().

◆ IsWritable()

bool FP_CACHE::IsWritable ( ) const
inline

Definition at line 118 of file pcb_plugin.cpp.

118{ return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }

References m_lib_path.

Referenced by PCB_PLUGIN::FootprintDelete(), PCB_PLUGIN::FootprintSave(), and PCB_PLUGIN::IsFootprintLibWritable().

◆ Load()

void FP_CACHE::Load ( )

Definition at line 243 of file pcb_plugin.cpp.

244{
245 m_cache_dirty = false;
247
248 wxDir dir( m_lib_raw_path );
249
250 if( !dir.IsOpened() )
251 {
252 wxString msg = wxString::Format( _( "Footprint library '%s' not found." ),
254 THROW_IO_ERROR( msg );
255 }
256
257 wxString fullName;
258 wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
259
260 // wxFileName construction is egregiously slow. Construct it once and just swap out
261 // the filename thereafter.
262 WX_FILENAME fn( m_lib_raw_path, wxT( "dummyName" ) );
263
264 if( dir.GetFirst( &fullName, fileSpec ) )
265 {
266 wxString cacheError;
267
268 do
269 {
270 fn.SetFullName( fullName );
271
272 // Queue I/O errors so only files that fail to parse don't get loaded.
273 try
274 {
275 FILE_LINE_READER reader( fn.GetFullPath() );
276 PCB_PARSER parser( &reader, nullptr, nullptr );
277
278 FOOTPRINT* footprint = (FOOTPRINT*) parser.Parse();
279 wxString fpName = fn.GetName();
280
281 footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) );
282 m_footprints.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );
283 }
284 catch( const IO_ERROR& ioe )
285 {
286 if( !cacheError.IsEmpty() )
287 cacheError += wxT( "\n\n" );
288
289 cacheError += ioe.What();
290 }
291 } while( dir.GetNext( &fullName ) );
292
294
295 if( !cacheError.IsEmpty() )
296 THROW_IO_ERROR( cacheError );
297 }
298}
A LINE_READER that reads from an open file.
Definition: richio.h:173
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:208
Helper class for creating a footprint library cache.
Definition: pcb_plugin.cpp:80
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
Definition: pcb_parser.h:74
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: wx_filename.h:49
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References _, Format(), WX_FILENAME::GetFullPath(), WX_FILENAME::GetName(), GetTimestamp(), KiCadFootprintFileExtension, m_cache_dirty, m_cache_timestamp, m_footprints, m_lib_raw_path, PCB_PARSER::Parse(), FOOTPRINT::SetFPID(), WX_FILENAME::SetFullName(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by PCB_PLUGIN::validateCache().

◆ Remove()

void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 301 of file pcb_plugin.cpp.

302{
303 FOOTPRINT_MAP::const_iterator it = m_footprints.find( aFootprintName );
304
305 if( it == m_footprints.end() )
306 {
307 wxString msg = wxString::Format( _( "Library '%s' has no footprint '%s'." ),
309 aFootprintName );
310 THROW_IO_ERROR( msg );
311 }
312
313 // Remove the footprint from the cache and delete the footprint file from the library.
314 wxString fullPath = it->second->GetFileName().GetFullPath();
315 m_footprints.erase( aFootprintName );
316 wxRemoveFile( fullPath );
317}

References _, Format(), m_footprints, m_lib_raw_path, and THROW_IO_ERROR.

Referenced by PCB_PLUGIN::FootprintDelete().

◆ Save()

void FP_CACHE::Save ( FOOTPRINT aFootprint = nullptr)

Save the footprint cache or a single footprint from it to disk.

Parameters
aFootprintif set, save only this footprint, otherwise, save the full library

Definition at line 176 of file pcb_plugin.cpp.

177{
179
180 if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
181 {
182 THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library '%s'." ),
183 m_lib_raw_path ) );
184 }
185
186 if( !m_lib_path.IsDirWritable() )
187 {
188 THROW_IO_ERROR( wxString::Format( _( "Footprint library '%s' is read only." ),
189 m_lib_raw_path ) );
190 }
191
192 for( FOOTPRINT_MAP::iterator it = m_footprints.begin(); it != m_footprints.end(); ++it )
193 {
194 if( aFootprint && aFootprint != it->second->GetFootprint() )
195 continue;
196
197 WX_FILENAME fn = it->second->GetFileName();
198
199 wxString tempFileName =
200#ifdef USE_TMP_FILE
201 wxFileName::CreateTempFileName( fn.GetPath() );
202#else
203 fn.GetFullPath();
204#endif
205 // Allow file output stream to go out of scope to close the file stream before
206 // renaming the file.
207 {
208 wxLogTrace( traceKicadPcbPlugin, wxT( "Creating temporary library file '%s'." ),
209 tempFileName );
210
211 FILE_OUTPUTFORMATTER formatter( tempFileName );
212
213 m_owner->SetOutputFormatter( &formatter );
214 m_owner->Format( (BOARD_ITEM*) it->second->GetFootprint() );
215 }
216
217#ifdef USE_TMP_FILE
218 wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
219
220 // Even on Linux you can see an _intermittent_ error when calling wxRename(),
221 // and it is fully inexplicable. See if this dodges the error.
222 wxMilliSleep( 250L );
223
224 if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
225 {
226 wxString msg = wxString::Format( _( "Cannot rename temporary file '%s' to '%s'" ),
227 tempFileName,
228 fn.GetFullPath() );
229 THROW_IO_ERROR( msg );
230 }
231#endif
233 }
234
235 m_cache_timestamp += m_lib_path.GetModificationTime().GetValue().GetValue();
236
237 // If we've saved the full cache, we clear the dirty flag.
238 if( !aFootprint )
239 m_cache_dirty = false;
240}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:52
Used for text file output.
Definition: richio.h:457
void SetOutputFormatter(OUTPUTFORMATTER *aFormatter)
Definition: pcb_plugin.h:254
void Format(const BOARD_ITEM *aItem, int aNestLevel=0) const
Output aItem to aFormatter in s-expression format.
Definition: pcb_plugin.cpp:402
wxString GetPath() const
Definition: wx_filename.cpp:53
wxString GetFullPath() const
Definition: wx_filename.cpp:59
long long GetTimestamp()
Definition: wx_filename.cpp:74
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.

References _, PCB_PLUGIN::Format(), Format(), WX_FILENAME::GetFullPath(), WX_FILENAME::GetPath(), WX_FILENAME::GetTimestamp(), m_cache_dirty, m_cache_timestamp, m_footprints, m_lib_path, m_lib_raw_path, m_owner, PCB_PLUGIN::SetOutputFormatter(), THROW_IO_ERROR, and traceKicadPcbPlugin.

Referenced by PCB_PLUGIN::FootprintLibCreate(), and PCB_PLUGIN::FootprintSave().

Member Data Documentation

◆ m_cache_dirty

bool FP_CACHE::m_cache_dirty
private

Definition at line 108 of file pcb_plugin.cpp.

Referenced by FP_CACHE(), IsModified(), Load(), and Save().

◆ m_cache_timestamp

long long FP_CACHE::m_cache_timestamp
private

Definition at line 110 of file pcb_plugin.cpp.

Referenced by FP_CACHE(), IsModified(), Load(), and Save().

◆ m_footprints

FOOTPRINT_MAP FP_CACHE::m_footprints
private

Definition at line 106 of file pcb_plugin.cpp.

Referenced by GetFootprints(), Load(), Remove(), and Save().

◆ m_lib_path

wxFileName FP_CACHE::m_lib_path
private

Definition at line 104 of file pcb_plugin.cpp.

Referenced by Exists(), FP_CACHE(), IsModified(), IsWritable(), and Save().

◆ m_lib_raw_path

wxString FP_CACHE::m_lib_raw_path
private

Definition at line 105 of file pcb_plugin.cpp.

Referenced by FP_CACHE(), GetPath(), IsPath(), Load(), Remove(), and Save().

◆ m_owner

PCB_PLUGIN* FP_CACHE::m_owner
private

Definition at line 103 of file pcb_plugin.cpp.

Referenced by FP_CACHE(), and Save().


The documentation for this class was generated from the following file: