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::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

References m_cache_dirty, m_cache_timestamp, m_lib_path, m_lib_raw_path, and m_owner.

Member Function Documentation

◆ Exists()

bool FP_CACHE::Exists ( ) const
inline

Definition at line 127 of file kicad_plugin.cpp.

127 { return m_lib_path.IsOk() && m_lib_path.DirExists(); }
wxFileName m_lib_path

References m_lib_path.

Referenced by PCB_IO::FootprintSave().

◆ GetModules()

MODULE_MAP& FP_CACHE::GetModules ( )
inline

Definition at line 128 of file kicad_plugin.cpp.

128 { return m_modules; }
MODULE_MAP m_modules

References m_modules.

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

◆ GetPath()

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

References m_lib_raw_path.

◆ GetTimestamp()

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

349 {
350  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
351 
352  return TimestampDir( aLibPath, fileSpec );
353 }
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:916

References KiCadFootprintFileExtension, and TimestampDir().

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

◆ IsModified()

bool FP_CACHE::IsModified ( )

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

Definition at line 340 of file kicad_plugin.cpp.

341 {
343 
344  return m_cache_dirty;
345 }
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 ...

References GetTimestamp(), m_cache_dirty, m_cache_timestamp, and m_lib_path.

Referenced by PCB_IO::validateCache().

◆ IsPath()

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

335 {
336  return aPath == m_lib_raw_path;
337 }
wxString m_lib_raw_path

References m_lib_raw_path.

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

◆ IsWritable()

bool FP_CACHE::IsWritable ( ) const
inline

Definition at line 126 of file kicad_plugin.cpp.

126 { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
wxFileName m_lib_path

References m_lib_path.

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

◆ Load()

void FP_CACHE::Load ( )

Definition at line 255 of file kicad_plugin.cpp.

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  "(or is not a directory)." ),
266  m_lib_raw_path );
267  THROW_IO_ERROR( msg );
268  }
269 
270  wxString fullName;
271  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
272 
273  // wxFileName construction is egregiously slow. Construct it once and just swap out
274  // the filename thereafter.
275  WX_FILENAME fn( m_lib_raw_path, wxT( "dummyName" ) );
276 
277  if( dir.GetFirst( &fullName, fileSpec ) )
278  {
279  wxString cacheError;
280 
281  do
282  {
283  fn.SetFullName( fullName );
284 
285  // Queue I/O errors so only files that fail to parse don't get loaded.
286  try
287  {
288  FILE_LINE_READER reader( fn.GetFullPath() );
289 
290  m_owner->m_parser->SetLineReader( &reader );
291 
292  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
293  wxString fpName = fn.GetName();
294 
295  footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) );
296  m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );
297 
298  m_cache_timestamp += fn.GetTimestamp();
299  }
300  catch( const IO_ERROR& ioe )
301  {
302  if( !cacheError.IsEmpty() )
303  cacheError += "\n\n";
304 
305  cacheError += ioe.What();
306  }
307  } while( dir.GetNext( &fullName ) );
308 
309  if( !cacheError.IsEmpty() )
310  THROW_IO_ERROR( cacheError );
311  }
312 }
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
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
PCB_IO * m_owner
#define THROW_IO_ERROR(msg)
PCB_PARSER * m_parser
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:467
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:335
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
#define _(s)
Definition: 3d_actions.cpp:33
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:220

References _, Format(), WX_FILENAME::GetFullPath(), WX_FILENAME::GetName(), WX_FILENAME::GetTimestamp(), KiCadFootprintFileExtension, m_cache_dirty, m_cache_timestamp, m_lib_raw_path, m_modules, m_owner, PCB_IO::m_parser, PCB_PARSER::Parse(), MODULE::SetFPID(), WX_FILENAME::SetFullName(), PCB_PARSER::SetLineReader(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by PCB_IO::validateCache().

◆ Remove()

void FP_CACHE::Remove ( const wxString &  aFootprintName)

Definition at line 315 of file kicad_plugin.cpp.

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

References _, Format(), m_lib_raw_path, m_modules, and THROW_IO_ERROR.

Referenced by PCB_IO::FootprintDelete().

◆ Save()

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.

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
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
MODULE_MAP m_modules
long long GetTimestamp()
Definition: common.cpp:757
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.
PCB_IO * m_owner
#define THROW_IO_ERROR(msg)
wxString GetPath() const
Definition: common.cpp:736
wxString GetFullPath() const
Definition: common.cpp:742
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:101
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
#define _(s)
Definition: 3d_actions.cpp:33
FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
bool m_cache_dirty
void Format(BOARD_ITEM *aItem, int aNestLevel=0) const
Function Format outputs aItem to aFormatter in s-expression format.

References _, PCB_IO::Format(), Format(), GetChars(), WX_FILENAME::GetFullPath(), WX_FILENAME::GetPath(), WX_FILENAME::GetTimestamp(), m_cache_dirty, m_cache_timestamp, m_lib_path, m_lib_raw_path, m_modules, m_owner, PCB_IO::SetOutputFormatter(), THROW_IO_ERROR, and traceKicadPcbPlugin.

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

Member Data Documentation

◆ m_cache_dirty

bool FP_CACHE::m_cache_dirty
private

Definition at line 117 of file kicad_plugin.cpp.

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

◆ m_cache_timestamp

long long FP_CACHE::m_cache_timestamp
private

Definition at line 119 of file kicad_plugin.cpp.

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

◆ m_lib_path

wxFileName FP_CACHE::m_lib_path
private

Definition at line 113 of file kicad_plugin.cpp.

Referenced by Exists(), FP_CACHE(), IsModified(), IsWritable(), and Save().

◆ m_lib_raw_path

wxString FP_CACHE::m_lib_raw_path
private

Definition at line 114 of file kicad_plugin.cpp.

Referenced by FP_CACHE(), GetPath(), IsPath(), Load(), Remove(), and Save().

◆ m_modules

MODULE_MAP FP_CACHE::m_modules
private

Definition at line 115 of file kicad_plugin.cpp.

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

◆ m_owner

PCB_IO* FP_CACHE::m_owner
private

Definition at line 112 of file kicad_plugin.cpp.

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


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