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)
 
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...
 

Static Public Member Functions

static long long GetTimestamp (const wxString &aLibPath)
 Function GetTimestamp Generate a timestamp representing all source files in the cache (including the parent directory). More...
 

Private Attributes

PCB_IOm_owner
 
wxFileName m_lib_path
 
wxString m_lib_raw_path
 
MODULE_MAP m_modules
 
bool m_cache_dirty
 
long long m_cache_timestamp
 

Detailed Description

Definition at line 110 of file kicad_plugin.cpp.

Constructor & Destructor Documentation

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

Definition at line 176 of file kicad_plugin.cpp.

177 {
178  m_owner = aOwner;
179  m_lib_raw_path = aLibraryPath;
180  m_lib_path.SetPath( aLibraryPath );
181  m_cache_timestamp = 0;
182  m_cache_dirty = true;
183 }
wxFileName m_lib_path
wxString m_lib_raw_path
long long m_cache_timestamp
PCB_IO * m_owner
bool m_cache_dirty

Member Function Documentation

bool FP_CACHE::Exists ( ) const
inline

Definition at line 127 of file kicad_plugin.cpp.

Referenced by PCB_IO::FootprintSave().

127 { return m_lib_path.IsOk() && m_lib_path.DirExists(); }
wxFileName m_lib_path
MODULE_MAP& FP_CACHE::GetModules ( )
inline

Definition at line 128 of file kicad_plugin.cpp.

References Load(), and Save.

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

128 { return m_modules; }
MODULE_MAP m_modules
wxString FP_CACHE::GetPath ( ) const
inline

Definition at line 125 of file kicad_plugin.cpp.

125 { return m_lib_raw_path; }
wxString m_lib_raw_path
long long FP_CACHE::GetTimestamp ( const wxString &  aLibPath)
static

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

References KiCadFootprintFileExtension, and TimestampDir().

Referenced by PCB_IO::GetLibraryTimestamp().

348 {
349  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
350 
351  return TimestampDir( aLibPath, fileSpec );
352 }
const std::string KiCadFootprintFileExtension
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:804
bool FP_CACHE::IsModified ( )

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

Definition at line 339 of file kicad_plugin.cpp.

Referenced by PCB_IO::validateCache().

340 {
342 
343  return m_cache_dirty;
344 }
wxFileName m_lib_path
long long m_cache_timestamp
bool m_cache_dirty
static long long GetTimestamp(const wxString &aLibPath)
Function GetTimestamp Generate a timestamp representing all source files in the cache (including the ...
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 333 of file kicad_plugin.cpp.

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

334 {
335  return aPath == m_lib_raw_path;
336 }
wxString m_lib_raw_path
bool FP_CACHE::IsWritable ( ) const
inline

Definition at line 126 of file kicad_plugin.cpp.

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

126 { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
wxFileName m_lib_path
void FP_CACHE::Load ( )

Definition at line 255 of file kicad_plugin.cpp.

References Format(), FP_CACHE_ITEM::FP_CACHE_ITEM(), WX_FILENAME::GetFullPath(), WX_FILENAME::GetName(), WX_FILENAME::GetTimestamp(), KiCadFootprintFileExtension, MODULE::SetFPID(), WX_FILENAME::SetFullName(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by PCB_IO::validateCache().

256 {
257  m_cache_dirty = false;
258  m_cache_timestamp = 0;
259 
260  wxDir dir( m_lib_raw_path );
261 
262  if( !dir.IsOpened() )
263  {
264  wxString msg = wxString::Format( _( "Footprint library path \"%s\" does not exist" ),
265  m_lib_raw_path );
266  THROW_IO_ERROR( msg );
267  }
268 
269  wxString fullName;
270  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
271 
272  // wxFileName construction is egregiously slow. Construct it once and just swap out
273  // the filename thereafter.
274  WX_FILENAME fn( m_lib_raw_path, wxT( "dummyName" ) );
275 
276  if( dir.GetFirst( &fullName, fileSpec ) )
277  {
278  wxString cacheError;
279 
280  do
281  {
282  fn.SetFullName( fullName );
283 
284  // Queue I/O errors so only files that fail to parse don't get loaded.
285  try
286  {
287  FILE_LINE_READER reader( fn.GetFullPath() );
288 
289  m_owner->m_parser->SetLineReader( &reader );
290 
291  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
292  wxString fpName = fn.GetName();
293 
294  footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) );
295  m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );
296 
297  m_cache_timestamp += fn.GetTimestamp();
298  }
299  catch( const IO_ERROR& ioe )
300  {
301  if( !cacheError.IsEmpty() )
302  cacheError += "\n\n";
303 
304  cacheError += ioe.What();
305  }
306  } while( dir.GetNext( &fullName ) );
307 
308  if( !cacheError.IsEmpty() )
309  THROW_IO_ERROR( cacheError );
310  }
311 }
const std::string KiCadFootprintFileExtension
wxString m_lib_raw_path
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
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
PCB_IO * m_owner
#define THROW_IO_ERROR(msg)
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:293
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: common.h:391
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
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:194
void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 314 of file kicad_plugin.cpp.

References Format(), and THROW_IO_ERROR.

Referenced by PCB_IO::FootprintDelete().

315 {
316  MODULE_CITER it = m_modules.find( aFootprintName );
317 
318  if( it == m_modules.end() )
319  {
320  wxString msg = wxString::Format( _( "library \"%s\" has no footprint \"%s\" to delete" ),
322  aFootprintName );
323  THROW_IO_ERROR( msg );
324  }
325 
326  // Remove the module from the cache and delete the module file from the library.
327  wxString fullPath = it->second->GetFileName().GetFullPath();
328  m_modules.erase( aFootprintName );
329  wxRemoveFile( fullPath );
330 }
wxString m_lib_raw_path
MODULE_MAP m_modules
#define THROW_IO_ERROR(msg)
MODULE_MAP::const_iterator MODULE_CITER
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 ( 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 186 of file kicad_plugin.cpp.

References Format(), GetChars(), WX_FILENAME::GetFullPath(), WX_FILENAME::GetPath(), WX_FILENAME::GetTimestamp(), THROW_IO_ERROR, and traceKicadPcbPlugin.

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

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

Member Data Documentation

bool FP_CACHE::m_cache_dirty
private

Definition at line 117 of file kicad_plugin.cpp.

long long FP_CACHE::m_cache_timestamp
private

Definition at line 119 of file kicad_plugin.cpp.

wxFileName FP_CACHE::m_lib_path
private

Definition at line 113 of file kicad_plugin.cpp.

wxString FP_CACHE::m_lib_raw_path
private

Definition at line 114 of file kicad_plugin.cpp.

MODULE_MAP FP_CACHE::m_modules
private

Definition at line 115 of file kicad_plugin.cpp.

PCB_IO* FP_CACHE::m_owner
private

Definition at line 112 of file kicad_plugin.cpp.


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