KiCad PCB EDA Suite
FP_CACHE Class Reference

Public Member Functions

 FP_CACHE (PCB_IO *aOwner, const wxString &aLibraryPath)
 
wxString GetPath () const
 
bool IsWritable () const
 
bool Exists () const
 
MODULE_MAPGetModules ()
 
void Save (MODULE *aModule=NULL)
 Function Save Save the footprint cache or a single module from it to disk. More...
 
void Load ()
 
void Remove (const wxString &aFootprintName)
 
long long GetTimestamp ()
 Function GetTimestamp Generate a timestamp representing all source files in the cache (including the parent directory). More...
 
bool IsModified ()
 Function IsModified Return true if the cache is not up-to-date. 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...
 
MODULE_MAP m_modules
 The path of the library. More...
 
bool m_cache_dirty
 Map of footprint file name per MODULE*. More...
 
long long m_cache_timestamp
 

Detailed Description

Definition at line 113 of file kicad_plugin.cpp.

Constructor & Destructor Documentation

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

Definition at line 178 of file kicad_plugin.cpp.

179 {
180  m_owner = aOwner;
181  m_lib_path.SetPath( aLibraryPath );
182  m_cache_timestamp = 0;
183  m_cache_dirty = true;
184 }
wxFileName m_lib_path
Plugin object that owns the cache.
long long m_cache_timestamp
PCB_IO * m_owner
bool m_cache_dirty
Map of footprint file name per MODULE*.

Member Function Documentation

bool FP_CACHE::Exists ( ) const
inline

Definition at line 129 of file kicad_plugin.cpp.

Referenced by PCB_IO::FootprintSave().

129 { return m_lib_path.IsOk() && m_lib_path.DirExists(); }
wxFileName m_lib_path
Plugin object that owns the cache.
MODULE_MAP& FP_CACHE::GetModules ( )
inline

Definition at line 130 of file kicad_plugin.cpp.

References Load(), and Save.

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

130 { return m_modules; }
MODULE_MAP m_modules
The path of the library.
wxString FP_CACHE::GetPath ( ) const
inline

Definition at line 127 of file kicad_plugin.cpp.

127 { return m_lib_path.GetPath(); }
wxFileName m_lib_path
Plugin object that owns the cache.
long long FP_CACHE::GetTimestamp ( )

Function GetTimestamp 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 362 of file kicad_plugin.cpp.

Referenced by PCB_IO::GetLibraryTimestamp().

363 {
364  // Avoid expensive GetModificationTime checks if we already know we're dirty
365  if( m_cache_dirty )
366  return wxDateTime::Now().GetValue().GetValue();
367 
368  long long files_timestamp = 0;
369 
370  if( m_lib_path.DirExists() )
371  {
372  files_timestamp = m_lib_path.GetModificationTime().GetValue().GetValue();
373 
374  for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
375  {
376  wxFileName moduleFile = it->second->GetFileName();
377  if( moduleFile.FileExists() )
378  files_timestamp += moduleFile.GetModificationTime().GetValue().GetValue();
379  }
380  }
381 
382  // If the new timestamp doesn't match the cache timestamp, then save ourselves the
383  // expensive calls next time
384  if( m_cache_timestamp != files_timestamp )
385  m_cache_dirty = true;
386 
387  return files_timestamp;
388 }
wxFileName m_lib_path
Plugin object that owns the cache.
long long m_cache_timestamp
MODULE_MAP m_modules
The path of the library.
MODULE_MAP::const_iterator MODULE_CITER
bool m_cache_dirty
Map of footprint file name per MODULE*.
bool FP_CACHE::IsModified ( )

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

Definition at line 353 of file kicad_plugin.cpp.

Referenced by PCB_IO::validateCache().

354 {
355  if( m_cache_dirty )
356  return true;
357  else
358  return GetTimestamp() != m_cache_timestamp;
359 }
long long GetTimestamp()
Function GetTimestamp Generate a timestamp representing all source files in the cache (including the ...
long long m_cache_timestamp
bool m_cache_dirty
Map of footprint file name per MODULE*.
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 343 of file kicad_plugin.cpp.

Referenced by PCB_IO::FootprintLibDelete(), PCB_IO::GetLibraryTimestamp(), and PCB_IO::validateCache().

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

Definition at line 128 of file kicad_plugin.cpp.

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

128 { 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 256 of file kicad_plugin.cpp.

References Format(), FP_CACHE_ITEM::FP_CACHE_ITEM(), GetChars(), KiCadFootprintFileExtension, MODULE::SetFPID(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by PCB_IO::validateCache().

257 {
258  wxDir dir( m_lib_path.GetPath() );
259 
260  if( !dir.IsOpened() )
261  {
262  m_cache_timestamp = 0;
263  m_cache_dirty = false;
264 
265  wxString msg = wxString::Format(
266  _( "Footprint library path \"%s\" does not exist" ),
267  GetChars( m_lib_path.GetPath() )
268  );
269 
270  THROW_IO_ERROR( msg );
271  }
272  else
273  {
274  m_cache_timestamp = m_lib_path.GetModificationTime().GetValue().GetValue();
275  m_cache_dirty = false;
276  }
277 
278  wxString fpFileName;
279  wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
280 
281  if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
282  {
283  wxString cacheError;
284 
285  do
286  {
287  // prepend the libpath into fullPath
288  wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
289 
290  // Queue I/O errors so only files that fail to parse don't get loaded.
291  try
292  {
293  FILE_LINE_READER reader( fullPath.GetFullPath() );
294 
295  m_owner->m_parser->SetLineReader( &reader );
296 
297  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
298 
299  // The footprint name is the file name without the extension.
300  wxString fpName = fullPath.GetName();
301 
302  footprint->SetFPID( LIB_ID( fpName ) );
303  m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fullPath ) );
304 
305  m_cache_timestamp += fullPath.GetModificationTime().GetValue().GetValue();
306  }
307  catch( const IO_ERROR& ioe )
308  {
309  if( !cacheError.IsEmpty() )
310  cacheError += "\n\n";
311 
312  cacheError += ioe.What();
313  }
314  } while( dir.GetNext( &fpFileName ) );
315 
316  if( !cacheError.IsEmpty() )
317  THROW_IO_ERROR( cacheError );
318  }
319 }
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
long long m_cache_timestamp
MODULE_MAP m_modules
The path of the library.
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
PCB_IO * m_owner
PCB_PARSER * m_parser
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:442
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
bool m_cache_dirty
Map of footprint file name per MODULE*.
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:192
void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 322 of file kicad_plugin.cpp.

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

Referenced by PCB_IO::FootprintDelete().

323 {
324  MODULE_CITER it = m_modules.find( aFootprintName );
325 
326  if( it == m_modules.end() )
327  {
328  wxString msg = wxString::Format(
329  _( "library \"%s\" has no footprint \"%s\" to delete" ),
330  GetChars( m_lib_path.GetPath() ),
331  GetChars( aFootprintName )
332  );
333  THROW_IO_ERROR( msg );
334  }
335 
336  // Remove the module from the cache and delete the module file from the library.
337  wxString fullPath = it->second->GetFileName().GetFullPath();
338  m_modules.erase( aFootprintName );
339  wxRemoveFile( fullPath );
340 }
wxFileName m_lib_path
Plugin object that owns the cache.
MODULE_MAP m_modules
The path of the library.
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 ( MODULE aModule = NULL)

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

Parameters
aModuleif set, save only this module, otherwise, save the full library

Definition at line 187 of file kicad_plugin.cpp.

References Format(), GetChars(), THROW_IO_ERROR, and traceKicadPcbPlugin.

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

188 {
189  m_cache_timestamp = 0;
190 
191  if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
192  {
193  THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library path \"%s\"" ),
194  m_lib_path.GetPath().GetData() ) );
195  }
196 
197  if( !m_lib_path.IsDirWritable() )
198  {
199  THROW_IO_ERROR( wxString::Format( _( "Footprint library path \"%s\" is read only" ),
200  GetChars( m_lib_path.GetPath() ) ) );
201  }
202 
203  for( MODULE_ITER it = m_modules.begin(); it != m_modules.end(); ++it )
204  {
205  if( aModule && aModule != it->second->GetModule() )
206  continue;
207 
208  wxFileName fn = it->second->GetFileName();
209 
210  wxString tempFileName =
211 #ifdef USE_TMP_FILE
212  fn.CreateTempFileName( fn.GetPath() );
213 #else
214  fn.GetFullPath();
215 #endif
216  // Allow file output stream to go out of scope to close the file stream before
217  // renaming the file.
218  {
219  wxLogTrace( traceKicadPcbPlugin, wxT( "Creating temporary library file %s" ),
220  GetChars( tempFileName ) );
221 
222  FILE_OUTPUTFORMATTER formatter( tempFileName );
223 
224  m_owner->SetOutputFormatter( &formatter );
225  m_owner->Format( (BOARD_ITEM*) it->second->GetModule() );
226  }
227 
228 #ifdef USE_TMP_FILE
229  wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
230 
231  // Even on linux you can see an _intermittent_ error when calling wxRename(),
232  // and it is fully inexplicable. See if this dodges the error.
233  wxMilliSleep( 250L );
234 
235  if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
236  {
237  wxString msg = wxString::Format(
238  _( "Cannot rename temporary file \"%s\" to footprint library file \"%s\"" ),
239  GetChars( tempFileName ),
240  GetChars( fn.GetFullPath() )
241  );
242  THROW_IO_ERROR( msg );
243  }
244 #endif
245  m_cache_timestamp += fn.GetModificationTime().GetValue().GetValue();
246  }
247 
248  m_cache_timestamp += m_lib_path.GetModificationTime().GetValue().GetValue();
249 
250  // If we've saved the full cache, we clear the dirty flag.
251  if( !aModule )
252  m_cache_dirty = false;
253 }
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)
long long m_cache_timestamp
MODULE_MAP m_modules
The path of the library.
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.
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
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
bool m_cache_dirty
Map of footprint file name per MODULE*.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

Member Data Documentation

bool FP_CACHE::m_cache_dirty
private

Map of footprint file name per MODULE*.

Definition at line 119 of file kicad_plugin.cpp.

long long FP_CACHE::m_cache_timestamp
private

Definition at line 121 of file kicad_plugin.cpp.

wxFileName FP_CACHE::m_lib_path
private

Plugin object that owns the cache.

Definition at line 116 of file kicad_plugin.cpp.

MODULE_MAP FP_CACHE::m_modules
private

The path of the library.

Definition at line 117 of file kicad_plugin.cpp.

PCB_IO* FP_CACHE::m_owner
private

Definition at line 115 of file kicad_plugin.cpp.


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