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 141 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 199 of file kicad_plugin.cpp.

References m_lib_path, and m_owner.

200 {
201  m_owner = aOwner;
202  m_lib_path.SetPath( aLibraryPath );
203 }
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 152 of file kicad_plugin.cpp.

References m_mod_time.

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

Definition at line 206 of file kicad_plugin.cpp.

References m_lib_path.

Referenced by Load(), and Save().

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

Definition at line 154 of file kicad_plugin.cpp.

References m_modules.

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

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

Definition at line 151 of file kicad_plugin.cpp.

151 { 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 367 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::cacheLib().

368 {
369  // The library is modified if the library path got deleted or changed.
370  if( !m_lib_path.DirExists() || !IsPath( aLibPath ) )
371  return true;
372 
373  // If no footprint was specified, check every file modification time against the time
374  // it was loaded.
375  if( aFootprintName.IsEmpty() )
376  {
377  for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
378  {
379  wxFileName fn = m_lib_path;
380 
381  fn.SetName( it->second->GetFileName().GetName() );
382  fn.SetExt( KiCadFootprintFileExtension );
383 
384  if( !fn.FileExists() )
385  {
386  wxLogTrace( traceFootprintLibrary,
387  wxT( "Footprint cache file '%s' does not exist." ),
388  fn.GetFullPath().GetData() );
389  return true;
390  }
391 
392  if( it->second->IsModified() )
393  {
394  wxLogTrace( traceFootprintLibrary,
395  wxT( "Footprint cache file '%s' has been modified." ),
396  fn.GetFullPath().GetData() );
397  return true;
398  }
399  }
400  }
401  else
402  {
403  MODULE_CITER it = m_modules.find( TO_UTF8( aFootprintName ) );
404 
405  if( it == m_modules.end() || it->second->IsModified() )
406  return true;
407  }
408 
409  return false;
410 }
const wxString KiCadFootprintFileExtension
wxFileName m_lib_path
Plugin object that owns the cache.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
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.
static const wxString traceFootprintLibrary(wxT("KicadFootprintLib"))
Definition for enabling and disabling footprint library trace output.
MODULE_MAP::const_iterator MODULE_CITER
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 357 of file kicad_plugin.cpp.

References m_lib_path.

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

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

Definition at line 153 of file kicad_plugin.cpp.

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

153 { 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 274 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::cacheLib().

275 {
276  wxDir dir( m_lib_path.GetPath() );
277 
278  if( !dir.IsOpened() )
279  {
280  wxString msg = wxString::Format(
281  _( "Footprint library path '%s' does not exist" ),
282  GetChars( m_lib_path.GetPath() )
283  );
284 
285  THROW_IO_ERROR( msg );
286  }
287 
288  wxString fpFileName;
289  wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
290 
291  if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
292  {
293  wxString cacheError;
294 
295  do
296  {
297  // prepend the libpath into fullPath
298  wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
299 
300  // Queue I/O errors so only files that fail to parse don't get loaded.
301  try
302  {
303  FILE_LINE_READER reader( fullPath.GetFullPath() );
304 
305  m_owner->m_parser->SetLineReader( &reader );
306 
307  std::string name = TO_UTF8( fullPath.GetName() );
308  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
309 
310  // The footprint name is the file name without the extension.
311  footprint->SetFPID( LIB_ID( fullPath.GetName() ) );
312  m_modules.insert( name, new FP_CACHE_ITEM( footprint, fullPath ) );
313  }
314  catch( const IO_ERROR& ioe )
315  {
316  if( !cacheError.IsEmpty() )
317  cacheError += "\n\n";
318 
319  cacheError += ioe.What();
320  }
321  } while( dir.GetNext( &fpFileName ) );
322 
323  // Remember the file modification time of library file when the
324  // cache snapshot was made, so that in a networked environment we will
325  // reload the cache as needed.
327 
328  if( !cacheError.IsEmpty() )
329  THROW_IO_ERROR( cacheError );
330  }
331 }
const wxString KiCadFootprintFileExtension
wxFileName m_lib_path
Plugin object that owns the cache.
Class LIB_ID.
Definition: lib_id.h:56
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
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
#define THROW_IO_ERROR(x)
Definition: utf8.cpp:60
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:296
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
const char * name
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
wxDateTime GetLibModificationTime() const
void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 334 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::FootprintDelete().

335 {
336  std::string footprintName = TO_UTF8( aFootprintName );
337 
338  MODULE_CITER it = m_modules.find( footprintName );
339 
340  if( it == m_modules.end() )
341  {
342  wxString msg = wxString::Format(
343  _( "library '%s' has no footprint '%s' to delete" ),
344  GetChars( m_lib_path.GetPath() ),
345  GetChars( aFootprintName )
346  );
347  THROW_IO_ERROR( msg );
348  }
349 
350  // Remove the module from the cache and delete the module file from the library.
351  wxString fullPath = it->second->GetFileName().GetFullPath();
352  m_modules.erase( footprintName );
353  wxRemoveFile( fullPath );
354 }
wxFileName m_lib_path
Plugin object that owns the cache.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
MODULE_MAP m_modules
Footprint library path modified time stamp.
#define THROW_IO_ERROR(x)
Definition: utf8.cpp:60
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
void FP_CACHE::Save ( )

save the entire legacy library to m_lib_name;

Definition at line 212 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().

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

Member Data Documentation

wxFileName FP_CACHE::m_lib_path
private

Plugin object that owns the cache.

Definition at line 144 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 145 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 146 of file kicad_plugin.cpp.

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

PCB_IO* FP_CACHE::m_owner
private

Definition at line 143 of file kicad_plugin.cpp.

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


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