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 138 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 196 of file kicad_plugin.cpp.

References m_lib_path, and m_owner.

197 {
198  m_owner = aOwner;
199  m_lib_path.SetPath( aLibraryPath );
200 }
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 149 of file kicad_plugin.cpp.

References m_mod_time.

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

Definition at line 203 of file kicad_plugin.cpp.

References m_lib_path.

Referenced by Load(), and Save().

204 {
205  return m_lib_path.GetModificationTime();
206 }
wxFileName m_lib_path
Plugin object that owns the cache.
MODULE_MAP& FP_CACHE::GetModules ( )
inline

Definition at line 151 of file kicad_plugin.cpp.

References m_modules.

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

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

Definition at line 148 of file kicad_plugin.cpp.

148 { 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 349 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::cacheLib().

350 {
351  // The library is modified if the library path got deleted or changed.
352  if( !m_lib_path.DirExists() || !IsPath( aLibPath ) )
353  return true;
354 
355  // If no footprint was specified, check every file modification time against the time
356  // it was loaded.
357  if( aFootprintName.IsEmpty() )
358  {
359  for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
360  {
361  wxFileName fn = m_lib_path;
362 
363  fn.SetName( it->second->GetFileName().GetName() );
364  fn.SetExt( KiCadFootprintFileExtension );
365 
366  if( !fn.FileExists() )
367  {
368  wxLogTrace( traceFootprintLibrary,
369  wxT( "Footprint cache file '%s' does not exist." ),
370  fn.GetFullPath().GetData() );
371  return true;
372  }
373 
374  if( it->second->IsModified() )
375  {
376  wxLogTrace( traceFootprintLibrary,
377  wxT( "Footprint cache file '%s' has been modified." ),
378  fn.GetFullPath().GetData() );
379  return true;
380  }
381  }
382  }
383  else
384  {
385  MODULE_CITER it = m_modules.find( TO_UTF8( aFootprintName ) );
386 
387  if( it == m_modules.end() || it->second->IsModified() )
388  return true;
389  }
390 
391  return false;
392 }
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 339 of file kicad_plugin.cpp.

References m_lib_path.

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

340 {
341  // Converts path separators to native path separators
342  wxFileName newPath;
343  newPath.AssignDir( aPath );
344 
345  return m_lib_path == newPath;
346 }
wxFileName m_lib_path
Plugin object that owns the cache.
bool FP_CACHE::IsWritable ( ) const
inline

Definition at line 150 of file kicad_plugin.cpp.

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

150 { 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 271 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, and TO_UTF8.

Referenced by PCB_IO::cacheLib().

272 {
273  wxDir dir( m_lib_path.GetPath() );
274 
275  if( !dir.IsOpened() )
276  {
277  wxString msg = wxString::Format(
278  _( "Footprint library path '%s' does not exist" ),
279  GetChars( m_lib_path.GetPath() )
280  );
281 
282  THROW_IO_ERROR( msg );
283  }
284 
285  wxString fpFileName;
286  wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
287 
288  if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
289  {
290  do
291  {
292  // prepend the libpath into fullPath
293  wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
294 
295  FILE_LINE_READER reader( fullPath.GetFullPath() );
296 
297  m_owner->m_parser->SetLineReader( &reader );
298 
299  std::string name = TO_UTF8( fullPath.GetName() );
300  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
301 
302  // The footprint name is the file name without the extension.
303  footprint->SetFPID( LIB_ID( fullPath.GetName() ) );
304  m_modules.insert( name, new FP_CACHE_ITEM( footprint, fullPath ) );
305 
306  } while( dir.GetNext( &fpFileName ) );
307 
308  // Remember the file modification time of library file when the
309  // cache snapshot was made, so that in a networked environment we will
310  // reload the cache as needed.
312  }
313 }
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:298
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
wxDateTime GetLibModificationTime() const
void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 316 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::FootprintDelete().

317 {
318  std::string footprintName = TO_UTF8( aFootprintName );
319 
320  MODULE_CITER it = m_modules.find( footprintName );
321 
322  if( it == m_modules.end() )
323  {
324  wxString msg = wxString::Format(
325  _( "library '%s' has no footprint '%s' to delete" ),
326  GetChars( m_lib_path.GetPath() ),
327  GetChars( aFootprintName )
328  );
329  THROW_IO_ERROR( msg );
330  }
331 
332  // Remove the module from the cache and delete the module file from the library.
333  wxString fullPath = it->second->GetFileName().GetFullPath();
334  m_modules.erase( footprintName );
335  wxRemoveFile( fullPath );
336 }
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 209 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().

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

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

PCB_IO* FP_CACHE::m_owner
private

Definition at line 140 of file kicad_plugin.cpp.

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


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