KiCad PCB EDA Suite
FP_CACHE Class Reference

Public Member Functions

 FP_CACHE (PCB_IO *aOwner, const wxString &aLibraryPath)
 Map of footprint file name per MODULE*. More...
 
wxString GetPath () const
 
wxDateTime GetLastModificationTime () const
 
bool IsWritable () const
 
MODULE_MAPGetModules ()
 
void Save ()
 save the entire legacy library to m_lib_name; More...
 
void Load ()
 
void Remove (const wxString &aFootprintName)
 
wxDateTime GetLibModificationTime () const
 
bool IsModified (const wxString &aLibPath, const wxString &aFootprintName=wxEmptyString) const
 Function IsModified check if the footprint cache has been modified relative to aLibPath and aFootprintName. More...
 
bool IsPath (const wxString &aPath) const
 Function IsPath checks if aPath is the same as the current cache path. More...
 

Private Attributes

PCB_IOm_owner
 
wxFileName m_lib_path
 Plugin object that owns the cache. More...
 
wxDateTime m_mod_time
 The path of the library. More...
 
MODULE_MAP m_modules
 Footprint library path modified time stamp. More...
 

Detailed Description

Definition at line 143 of file kicad_plugin.cpp.

Constructor & Destructor Documentation

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

Map of footprint file name per MODULE*.

Definition at line 201 of file kicad_plugin.cpp.

References m_lib_path, and m_owner.

202 {
203  m_owner = aOwner;
204  m_lib_path.SetPath( aLibraryPath );
205 }
wxFileName m_lib_path
Plugin object that owns the cache.
PCB_IO * m_owner

Member Function Documentation

wxDateTime FP_CACHE::GetLastModificationTime ( ) const
inline

Definition at line 154 of file kicad_plugin.cpp.

References m_mod_time.

154 { return m_mod_time; }
wxDateTime m_mod_time
The path of the library.
wxDateTime FP_CACHE::GetLibModificationTime ( ) const

Definition at line 208 of file kicad_plugin.cpp.

References m_lib_path.

Referenced by Load(), and Save().

209 {
210  return m_lib_path.GetModificationTime();
211 }
wxFileName m_lib_path
Plugin object that owns the cache.
MODULE_MAP& FP_CACHE::GetModules ( )
inline

Definition at line 156 of file kicad_plugin.cpp.

References m_modules.

Referenced by PCB_IO::FootprintEnumerate(), PCB_IO::FootprintLoad(), and PCB_IO::FootprintSave().

156 { return m_modules; }
MODULE_MAP m_modules
Footprint library path modified time stamp.
wxString FP_CACHE::GetPath ( ) const
inline

Definition at line 153 of file kicad_plugin.cpp.

153 { return m_lib_path.GetPath(); }
wxFileName m_lib_path
Plugin object that owns the cache.
bool FP_CACHE::IsModified ( const wxString &  aLibPath,
const wxString &  aFootprintName = wxEmptyString 
) const

Function IsModified check if the footprint cache has been modified relative to aLibPath and aFootprintName.

Parameters
aLibPathis a path to test the current cache library path against.
aFootprintNameis the footprint name in the cache to test. If the footprint name is empty, the all the footprint files in the library are checked to see if they have been modified.
Returns
true if the cache has been modified.

Definition at line 368 of file kicad_plugin.cpp.

References IsPath(), KiCadFootprintFileExtension, m_lib_path, m_modules, and traceFootprintLibrary.

Referenced by PCB_IO::cacheLib().

369 {
370  // The library is modified if the library path got deleted or changed.
371  if( !m_lib_path.DirExists() || !IsPath( aLibPath ) )
372  return true;
373 
374  // If no footprint was specified, check every file modification time against the time
375  // it was loaded.
376  if( aFootprintName.IsEmpty() )
377  {
378  for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
379  {
380  wxFileName fn = m_lib_path;
381 
382  fn.SetName( it->second->GetFileName().GetName() );
383  fn.SetExt( KiCadFootprintFileExtension );
384 
385  if( !fn.FileExists() )
386  {
387  wxLogTrace( traceFootprintLibrary,
388  wxT( "Footprint cache file '%s' does not exist." ),
389  fn.GetFullPath().GetData() );
390  return true;
391  }
392 
393  if( it->second->IsModified() )
394  {
395  wxLogTrace( traceFootprintLibrary,
396  wxT( "Footprint cache file '%s' has been modified." ),
397  fn.GetFullPath().GetData() );
398  return true;
399  }
400  }
401  }
402  else
403  {
404  MODULE_CITER it = m_modules.find( aFootprintName );
405 
406  if( it == m_modules.end() || it->second->IsModified() )
407  return true;
408  }
409 
410  return false;
411 }
const wxString KiCadFootprintFileExtension
wxFileName m_lib_path
Plugin object that owns the cache.
MODULE_MAP m_modules
Footprint library path modified time stamp.
bool IsPath(const wxString &aPath) const
Function IsPath checks if aPath is the same as the current cache path.
MODULE_MAP::const_iterator MODULE_CITER
static const wxString traceFootprintLibrary
Flag to enable KiCad PCB plugin debug output.
bool FP_CACHE::IsPath ( const wxString &  aPath) const

Function IsPath checks 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 358 of file kicad_plugin.cpp.

References m_lib_path.

Referenced by PCB_IO::FootprintLibDelete(), and IsModified().

359 {
360  // Converts path separators to native path separators
361  wxFileName newPath;
362  newPath.AssignDir( aPath );
363 
364  return m_lib_path == newPath;
365 }
wxFileName m_lib_path
Plugin object that owns the cache.
bool FP_CACHE::IsWritable ( ) const
inline

Definition at line 155 of file kicad_plugin.cpp.

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

155 { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
wxFileName m_lib_path
Plugin object that owns the cache.
void FP_CACHE::Load ( )

Definition at line 276 of file kicad_plugin.cpp.

References Format(), GetChars(), GetLibModificationTime(), KiCadFootprintFileExtension, m_lib_path, m_mod_time, m_modules, m_owner, PCB_IO::m_parser, PCB_PARSER::Parse(), MODULE::SetFPID(), PCB_PARSER::SetLineReader(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by PCB_IO::cacheLib().

277 {
278  wxDir dir( m_lib_path.GetPath() );
279 
280  if( !dir.IsOpened() )
281  {
282  wxString msg = wxString::Format(
283  _( "Footprint library path '%s' does not exist" ),
284  GetChars( m_lib_path.GetPath() )
285  );
286 
287  THROW_IO_ERROR( msg );
288  }
289 
290  wxString fpFileName;
291  wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
292 
293  if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
294  {
295  wxString cacheError;
296 
297  do
298  {
299  // prepend the libpath into fullPath
300  wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
301 
302  // Queue I/O errors so only files that fail to parse don't get loaded.
303  try
304  {
305  FILE_LINE_READER reader( fullPath.GetFullPath() );
306 
307  m_owner->m_parser->SetLineReader( &reader );
308 
309  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
310 
311  // The footprint name is the file name without the extension.
312  wxString fpName = fullPath.GetName();
313 
314  footprint->SetFPID( LIB_ID( fpName ) );
315  m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fullPath ) );
316  }
317  catch( const IO_ERROR& ioe )
318  {
319  if( !cacheError.IsEmpty() )
320  cacheError += "\n\n";
321 
322  cacheError += ioe.What();
323  }
324  } while( dir.GetNext( &fpFileName ) );
325 
326  // Remember the file modification time of library file when the
327  // cache snapshot was made, so that in a networked environment we will
328  // reload the cache as needed.
330 
331  if( !cacheError.IsEmpty() )
332  THROW_IO_ERROR( cacheError );
333  }
334 }
const wxString KiCadFootprintFileExtension
wxFileName m_lib_path
Plugin object that owns the cache.
Class LIB_ID.
Definition: lib_id.h:56
MODULE_MAP m_modules
Footprint library path modified time stamp.
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
wxDateTime m_mod_time
The path of the library.
PCB_IO * m_owner
PCB_PARSER * m_parser
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:405
Class FP_CACHE_ITEM is helper class for creating a footprint library cache.
LINE_READER * SetLineReader(LINE_READER *aReader)
Function SetLineReader sets aLineReader into the parser, and returns the previous one...
Definition: pcb_parser.h:290
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:165
wxDateTime GetLibModificationTime() const
void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 337 of file kicad_plugin.cpp.

References Format(), GetChars(), m_lib_path, m_modules, and THROW_IO_ERROR.

Referenced by PCB_IO::FootprintDelete().

338 {
339  MODULE_CITER it = m_modules.find( aFootprintName );
340 
341  if( it == m_modules.end() )
342  {
343  wxString msg = wxString::Format(
344  _( "library '%s' has no footprint '%s' to delete" ),
345  GetChars( m_lib_path.GetPath() ),
346  GetChars( aFootprintName )
347  );
348  THROW_IO_ERROR( msg );
349  }
350 
351  // Remove the module from the cache and delete the module file from the library.
352  wxString fullPath = it->second->GetFileName().GetFullPath();
353  m_modules.erase( aFootprintName );
354  wxRemoveFile( fullPath );
355 }
wxFileName m_lib_path
Plugin object that owns the cache.
MODULE_MAP m_modules
Footprint library path modified time stamp.
MODULE_MAP::const_iterator MODULE_CITER
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void FP_CACHE::Save ( )

save the entire legacy library to m_lib_name;

Definition at line 214 of file kicad_plugin.cpp.

References PCB_IO::Format(), Format(), GetChars(), GetLibModificationTime(), m_lib_path, m_mod_time, m_modules, m_owner, PCB_IO::SetOutputFormatter(), THROW_IO_ERROR, and traceFootprintLibrary.

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

215 {
216  if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
217  {
218  THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library path '%s'" ),
219  m_lib_path.GetPath().GetData() ) );
220  }
221 
222  if( !m_lib_path.IsDirWritable() )
223  {
224  THROW_IO_ERROR( wxString::Format( _( "Footprint library path '%s' is read only" ),
225  GetChars( m_lib_path.GetPath() ) ) );
226  }
227 
228  for( MODULE_ITER it = m_modules.begin(); it != m_modules.end(); ++it )
229  {
230  wxFileName fn = it->second->GetFileName();
231 
232  if( fn.FileExists() && !it->second->IsModified() )
233  continue;
234 
235  wxString tempFileName =
236 #ifdef USE_TMP_FILE
237  fn.CreateTempFileName( fn.GetPath() );
238 #else
239  fn.GetFullPath();
240 #endif
241  // Allow file output stream to go out of scope to close the file stream before
242  // renaming the file.
243  {
244  wxLogTrace( traceFootprintLibrary, wxT( "Creating temporary library file %s" ),
245  GetChars( tempFileName ) );
246 
247  FILE_OUTPUTFORMATTER formatter( tempFileName );
248 
249  m_owner->SetOutputFormatter( &formatter );
250  m_owner->Format( (BOARD_ITEM*) it->second->GetModule() );
251  }
252 
253 #ifdef USE_TMP_FILE
254  wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
255 
256  // Even on linux you can see an _intermittent_ error when calling wxRename(),
257  // and it is fully inexplicable. See if this dodges the error.
258  wxMilliSleep( 250L );
259 
260  if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
261  {
262  wxString msg = wxString::Format(
263  _( "Cannot rename temporary file '%s' to footprint library file '%s'" ),
264  GetChars( tempFileName ),
265  GetChars( fn.GetFullPath() )
266  );
267  THROW_IO_ERROR( msg );
268  }
269 #endif
270  it->second->UpdateModificationTime();
272  }
273 }
MODULE_MAP::iterator MODULE_ITER
wxFileName m_lib_path
Plugin object that owns the cache.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
void SetOutputFormatter(OUTPUTFORMATTER *aFormatter)
MODULE_MAP m_modules
Footprint library path modified time stamp.
wxDateTime m_mod_time
The path of the library.
PCB_IO * m_owner
void Format(BOARD_ITEM *aItem, int aNestLevel=0) const
Function Format outputs aItem to aFormatter in s-expression format.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static const wxString traceFootprintLibrary
Flag to enable KiCad PCB plugin debug output.
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
wxDateTime GetLibModificationTime() const

Member Data Documentation

wxFileName FP_CACHE::m_lib_path
private

Plugin object that owns the cache.

Definition at line 146 of file kicad_plugin.cpp.

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

wxDateTime FP_CACHE::m_mod_time
private

The path of the library.

Definition at line 147 of file kicad_plugin.cpp.

Referenced by GetLastModificationTime(), Load(), and Save().

MODULE_MAP FP_CACHE::m_modules
private

Footprint library path modified time stamp.

Definition at line 148 of file kicad_plugin.cpp.

Referenced by GetModules(), IsModified(), Load(), Remove(), and Save().

PCB_IO* FP_CACHE::m_owner
private

Definition at line 145 of file kicad_plugin.cpp.

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


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