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

Constructor & Destructor Documentation

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

Definition at line 173 of file kicad_plugin.cpp.

174 {
175  m_owner = aOwner;
176  m_lib_raw_path = aLibraryPath;
177  m_lib_path.SetPath( aLibraryPath );
178  m_cache_timestamp = 0;
179  m_cache_dirty = true;
180 }
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 124 of file kicad_plugin.cpp.

Referenced by PCB_IO::FootprintSave().

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

Definition at line 125 of file kicad_plugin.cpp.

References Load(), and Save.

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

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

Definition at line 122 of file kicad_plugin.cpp.

122 { 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 344 of file kicad_plugin.cpp.

References KiCadFootprintFileExtension, and TimestampDir().

Referenced by PCB_IO::GetLibraryTimestamp().

345 {
346  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
347 
348  return TimestampDir( aLibPath, fileSpec );
349 }
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:803
bool FP_CACHE::IsModified ( )

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

Definition at line 336 of file kicad_plugin.cpp.

Referenced by PCB_IO::validateCache().

337 {
339 
340  return m_cache_dirty;
341 }
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 330 of file kicad_plugin.cpp.

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

331 {
332  return aPath == m_lib_raw_path;
333 }
wxString m_lib_raw_path
bool FP_CACHE::IsWritable ( ) const
inline

Definition at line 123 of file kicad_plugin.cpp.

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

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

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

253 {
254  m_cache_dirty = false;
255  m_cache_timestamp = 0;
256 
257  wxDir dir( m_lib_raw_path );
258 
259  if( !dir.IsOpened() )
260  {
261  wxString msg = wxString::Format( _( "Footprint library path \"%s\" does not exist" ),
262  m_lib_raw_path );
263  THROW_IO_ERROR( msg );
264  }
265 
266  wxString fullName;
267  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
268 
269  // wxFileName construction is egregiously slow. Construct it once and just swap out
270  // the filename thereafter.
271  WX_FILENAME fn( m_lib_raw_path, wxT( "dummyName" ) );
272 
273  if( dir.GetFirst( &fullName, fileSpec ) )
274  {
275  wxString cacheError;
276 
277  do
278  {
279  fn.SetFullName( fullName );
280 
281  // Queue I/O errors so only files that fail to parse don't get loaded.
282  try
283  {
284  FILE_LINE_READER reader( fn.GetFullPath() );
285 
286  m_owner->m_parser->SetLineReader( &reader );
287 
288  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
289  wxString fpName = fn.GetName();
290 
291  footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) );
292  m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );
293 
294  m_cache_timestamp += fn.GetTimestamp();
295  }
296  catch( const IO_ERROR& ioe )
297  {
298  if( !cacheError.IsEmpty() )
299  cacheError += "\n\n";
300 
301  cacheError += ioe.What();
302  }
303  } while( dir.GetNext( &fullName ) );
304 
305  if( !cacheError.IsEmpty() )
306  THROW_IO_ERROR( cacheError );
307  }
308 }
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:294
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:392
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:193
void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 311 of file kicad_plugin.cpp.

References Format(), and THROW_IO_ERROR.

Referenced by PCB_IO::FootprintDelete().

312 {
313  MODULE_CITER it = m_modules.find( aFootprintName );
314 
315  if( it == m_modules.end() )
316  {
317  wxString msg = wxString::Format( _( "library \"%s\" has no footprint \"%s\" to delete" ),
319  aFootprintName );
320  THROW_IO_ERROR( msg );
321  }
322 
323  // Remove the module from the cache and delete the module file from the library.
324  wxString fullPath = it->second->GetFileName().GetFullPath();
325  m_modules.erase( aFootprintName );
326  wxRemoveFile( fullPath );
327 }
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 183 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().

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

long long FP_CACHE::m_cache_timestamp
private

Definition at line 116 of file kicad_plugin.cpp.

wxFileName FP_CACHE::m_lib_path
private

Definition at line 110 of file kicad_plugin.cpp.

wxString FP_CACHE::m_lib_raw_path
private

Definition at line 111 of file kicad_plugin.cpp.

MODULE_MAP FP_CACHE::m_modules
private

Definition at line 112 of file kicad_plugin.cpp.

PCB_IO* FP_CACHE::m_owner
private

Definition at line 109 of file kicad_plugin.cpp.


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