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 144 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 202 of file kicad_plugin.cpp.

References m_lib_path, and m_owner.

203 {
204  m_owner = aOwner;
205  m_lib_path.SetPath( aLibraryPath );
206 }
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 155 of file kicad_plugin.cpp.

References m_mod_time.

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

Definition at line 209 of file kicad_plugin.cpp.

References m_lib_path.

Referenced by Load(), and Save().

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

Definition at line 157 of file kicad_plugin.cpp.

References m_modules.

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

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

Definition at line 154 of file kicad_plugin.cpp.

154 { 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 369 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::cacheLib().

370 {
371  // The library is modified if the library path got deleted or changed.
372  if( !m_lib_path.DirExists() || !IsPath( aLibPath ) )
373  return true;
374 
375  // If no footprint was specified, check every file modification time against the time
376  // it was loaded.
377  if( aFootprintName.IsEmpty() )
378  {
379  for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
380  {
381  wxFileName fn = m_lib_path;
382 
383  fn.SetName( it->second->GetFileName().GetName() );
384  fn.SetExt( KiCadFootprintFileExtension );
385 
386  if( !fn.FileExists() )
387  {
388  wxLogTrace( traceFootprintLibrary,
389  wxT( "Footprint cache file '%s' does not exist." ),
390  fn.GetFullPath().GetData() );
391  return true;
392  }
393 
394  if( it->second->IsModified() )
395  {
396  wxLogTrace( traceFootprintLibrary,
397  wxT( "Footprint cache file '%s' has been modified." ),
398  fn.GetFullPath().GetData() );
399  return true;
400  }
401  }
402  }
403  else
404  {
405  MODULE_CITER it = m_modules.find( aFootprintName );
406 
407  if( it == m_modules.end() || it->second->IsModified() )
408  return true;
409  }
410 
411  return false;
412 }
wxFileName m_lib_path
Plugin object that owns the cache.
const wxString KiCadFootprintFileExtension
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 359 of file kicad_plugin.cpp.

References m_lib_path.

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

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

Definition at line 156 of file kicad_plugin.cpp.

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

156 { 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 277 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().

278 {
279  wxDir dir( m_lib_path.GetPath() );
280 
281  if( !dir.IsOpened() )
282  {
283  wxString msg = wxString::Format(
284  _( "Footprint library path \"%s\" does not exist" ),
285  GetChars( m_lib_path.GetPath() )
286  );
287 
288  THROW_IO_ERROR( msg );
289  }
290 
291  wxString fpFileName;
292  wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
293 
294  if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
295  {
296  wxString cacheError;
297 
298  do
299  {
300  // prepend the libpath into fullPath
301  wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
302 
303  // Queue I/O errors so only files that fail to parse don't get loaded.
304  try
305  {
306  FILE_LINE_READER reader( fullPath.GetFullPath() );
307 
308  m_owner->m_parser->SetLineReader( &reader );
309 
310  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
311 
312  // The footprint name is the file name without the extension.
313  wxString fpName = fullPath.GetName();
314 
315  footprint->SetFPID( LIB_ID( fpName ) );
316  m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fullPath ) );
317  }
318  catch( const IO_ERROR& ioe )
319  {
320  if( !cacheError.IsEmpty() )
321  cacheError += "\n\n";
322 
323  cacheError += ioe.What();
324  }
325  } while( dir.GetNext( &fpFileName ) );
326 
327  // Remember the file modification time of library file when the
328  // cache snapshot was made, so that in a networked environment we will
329  // reload the cache as needed.
331 
332  if( !cacheError.IsEmpty() )
333  THROW_IO_ERROR( cacheError );
334  }
335 }
wxFileName m_lib_path
Plugin object that owns the cache.
const wxString KiCadFootprintFileExtension
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
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:427
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:289
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:185
wxDateTime GetLibModificationTime() const
void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 338 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::FootprintDelete().

339 {
340  MODULE_CITER it = m_modules.find( aFootprintName );
341 
342  if( it == m_modules.end() )
343  {
344  wxString msg = wxString::Format(
345  _( "library \"%s\" has no footprint \"%s\" to delete" ),
346  GetChars( m_lib_path.GetPath() ),
347  GetChars( aFootprintName )
348  );
349  THROW_IO_ERROR( msg );
350  }
351 
352  // Remove the module from the cache and delete the module file from the library.
353  wxString fullPath = it->second->GetFileName().GetFullPath();
354  m_modules.erase( aFootprintName );
355  wxRemoveFile( fullPath );
356 }
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 215 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().

216 {
217  if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
218  {
219  THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library path \"%s\"" ),
220  m_lib_path.GetPath().GetData() ) );
221  }
222 
223  if( !m_lib_path.IsDirWritable() )
224  {
225  THROW_IO_ERROR( wxString::Format( _( "Footprint library path \"%s\" is read only" ),
226  GetChars( m_lib_path.GetPath() ) ) );
227  }
228 
229  for( MODULE_ITER it = m_modules.begin(); it != m_modules.end(); ++it )
230  {
231  wxFileName fn = it->second->GetFileName();
232 
233  if( fn.FileExists() && !it->second->IsModified() )
234  continue;
235 
236  wxString tempFileName =
237 #ifdef USE_TMP_FILE
238  fn.CreateTempFileName( fn.GetPath() );
239 #else
240  fn.GetFullPath();
241 #endif
242  // Allow file output stream to go out of scope to close the file stream before
243  // renaming the file.
244  {
245  wxLogTrace( traceFootprintLibrary, wxT( "Creating temporary library file %s" ),
246  GetChars( tempFileName ) );
247 
248  FILE_OUTPUTFORMATTER formatter( tempFileName );
249 
250  m_owner->SetOutputFormatter( &formatter );
251  m_owner->Format( (BOARD_ITEM*) it->second->GetModule() );
252  }
253 
254 #ifdef USE_TMP_FILE
255  wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
256 
257  // Even on linux you can see an _intermittent_ error when calling wxRename(),
258  // and it is fully inexplicable. See if this dodges the error.
259  wxMilliSleep( 250L );
260 
261  if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
262  {
263  wxString msg = wxString::Format(
264  _( "Cannot rename temporary file \"%s\" to footprint library file \"%s\"" ),
265  GetChars( tempFileName ),
266  GetChars( fn.GetFullPath() )
267  );
268  THROW_IO_ERROR( msg );
269  }
270 #endif
271  it->second->UpdateModificationTime();
273  }
274 }
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 147 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 148 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 149 of file kicad_plugin.cpp.

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

PCB_IO* FP_CACHE::m_owner
private

Definition at line 146 of file kicad_plugin.cpp.

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


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