KiCad PCB EDA Suite
LP_CACHE Struct Reference

Class LP_CACHE assists only for the footprint portion of the PLUGIN API, and only for the LEGACY_PLUGIN, so therefore is private to this implementation file, i.e. More...

Public Member Functions

 LP_CACHE (LEGACY_PLUGIN *aOwner, const wxString &aLibraryPath)
 
void Load ()
 
void ReadAndVerifyHeader (LINE_READER *aReader)
 
void SkipIndex (LINE_READER *aReader)
 
void LoadModules (LINE_READER *aReader)
 
bool IsModified ()
 

Static Public Member Functions

static long long GetTimestamp (const wxString &aLibPath)
 

Public Attributes

LEGACY_PLUGINm_owner
 
wxString m_lib_path
 
MODULE_MAP m_modules
 
bool m_writable
 
bool m_cache_dirty
 
long long m_cache_timestamp
 

Detailed Description

Class LP_CACHE assists only for the footprint portion of the PLUGIN API, and only for the LEGACY_PLUGIN, so therefore is private to this implementation file, i.e.

not placed into a header.

Definition at line 3164 of file legacy_plugin.cpp.

Constructor & Destructor Documentation

◆ LP_CACHE()

LP_CACHE::LP_CACHE ( LEGACY_PLUGIN aOwner,
const wxString &  aLibraryPath 
)

Definition at line 3195 of file legacy_plugin.cpp.

3195  :
3196  m_owner( aOwner ),
3197  m_lib_path( aLibraryPath ),
3198  m_writable( true ),
3199  m_cache_dirty( true ),
3200  m_cache_timestamp( 0 )
3201 {
3202 }
LEGACY_PLUGIN * m_owner
bool m_cache_dirty
long long m_cache_timestamp
wxString m_lib_path

Member Function Documentation

◆ GetTimestamp()

long long LP_CACHE::GetTimestamp ( const wxString &  aLibPath)
static

Definition at line 3213 of file legacy_plugin.cpp.

3214 {
3215  return wxFileName( aLibPath ).GetModificationTime().GetValue().GetValue();
3216 }

Referenced by LEGACY_PLUGIN::GetLibraryTimestamp(), IsModified(), and Load().

◆ IsModified()

bool LP_CACHE::IsModified ( )

Definition at line 3205 of file legacy_plugin.cpp.

3206 {
3208 
3209  return m_cache_dirty;
3210 }
bool m_cache_dirty
long long m_cache_timestamp
wxString m_lib_path
static long long GetTimestamp(const wxString &aLibPath)

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

Referenced by LEGACY_PLUGIN::cacheLib().

◆ Load()

void LP_CACHE::Load ( )

Definition at line 3219 of file legacy_plugin.cpp.

3220 {
3221  m_cache_dirty = false;
3222 
3223  FILE_LINE_READER reader( m_lib_path );
3224 
3225  ReadAndVerifyHeader( &reader );
3226  SkipIndex( &reader );
3227  LoadModules( &reader );
3228 
3229  // Remember the file modification time of library file when the
3230  // cache snapshot was made, so that in a networked environment we will
3231  // reload the cache as needed.
3233 }
void SkipIndex(LINE_READER *aReader)
bool m_cache_dirty
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
void LoadModules(LINE_READER *aReader)
long long m_cache_timestamp
wxString m_lib_path
static long long GetTimestamp(const wxString &aLibPath)
void ReadAndVerifyHeader(LINE_READER *aReader)

References GetTimestamp(), LoadModules(), m_cache_dirty, m_cache_timestamp, m_lib_path, ReadAndVerifyHeader(), and SkipIndex().

Referenced by LEGACY_PLUGIN::cacheLib().

◆ LoadModules()

void LP_CACHE::LoadModules ( LINE_READER aReader)

Definition at line 3298 of file legacy_plugin.cpp.

3299 {
3300  m_owner->SetReader( aReader );
3301 
3302  char* line = aReader->Line();
3303 
3304  do
3305  {
3306  // test first for the $MODULE, even before reading because of INDEX bug.
3307  if( TESTLINE( "$MODULE" ) )
3308  {
3309  unique_ptr<MODULE> module( new MODULE( m_owner->m_board ) );
3310 
3311  std::string footprintName = StrPurge( line + SZ( "$MODULE" ) );
3312 
3313  // The footprint names in legacy libraries can contain the '/' and ':'
3314  // characters which will cause the LIB_ID parser to choke.
3315  ReplaceIllegalFileNameChars( &footprintName );
3316 
3317  // set the footprint name first thing, so exceptions can use name.
3318  module->SetFPID( LIB_ID( wxEmptyString, footprintName ) );
3319 
3320  m_owner->loadMODULE( module.get() );
3321 
3322  MODULE* m = module.release(); // exceptions after this are not expected.
3323 
3324  // Not sure why this is asserting on debug builds. The debugger shows the
3325  // strings are the same. If it's not really needed maybe it can be removed.
3326 // wxASSERT( footprintName == m->GetFPID().GetLibItemName() );
3327 
3328  /*
3329 
3330  There was a bug in old legacy library management code
3331  (pre-LEGACY_PLUGIN) which was introducing duplicate footprint names
3332  in legacy libraries without notification. To best recover from such
3333  bad libraries, and use them to their fullest, there are a few
3334  strategies that could be used. (Note: footprints must have unique
3335  names to be accepted into this cache.) The strategy used here is to
3336  append a differentiating version counter to the end of the name as:
3337  _v2, _v3, etc.
3338 
3339  */
3340 
3341  MODULE_CITER it = m_modules.find( footprintName );
3342 
3343  if( it == m_modules.end() ) // footprintName is not present in cache yet.
3344  {
3345  std::pair<MODULE_ITER, bool> r = m_modules.insert( footprintName, m );
3346 
3347  wxASSERT_MSG( r.second, wxT( "error doing cache insert using guaranteed unique name" ) );
3348  (void) r;
3349  }
3350 
3351  // Bad library has a duplicate of this footprintName, generate a
3352  // unique footprint name and load it anyway.
3353  else
3354  {
3355  bool nameOK = false;
3356  int version = 2;
3357  char buf[48];
3358 
3359  while( !nameOK )
3360  {
3361  std::string newName = footprintName;
3362 
3363  newName += "_v";
3364  sprintf( buf, "%d", version++ );
3365  newName += buf;
3366 
3367  it = m_modules.find( newName );
3368 
3369  if( it == m_modules.end() )
3370  {
3371  nameOK = true;
3372 
3373  m->SetFPID( LIB_ID( wxEmptyString, newName ) );
3374  std::pair<MODULE_ITER, bool> r = m_modules.insert( newName, m );
3375 
3376  wxASSERT_MSG( r.second, wxT( "error doing cache insert using guaranteed unique name" ) );
3377  (void) r;
3378  }
3379  }
3380  }
3381  }
3382 
3383  } while( ( line = aReader->ReadLine() ) != NULL );
3384 }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:298
LEGACY_PLUGIN * m_owner
void loadMODULE(MODULE *aModule)
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar)
Checks aName for illegal file name characters.
Definition: string.cpp:668
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void SetReader(LINE_READER *aReader)
BOARD * m_board
which BOARD, no ownership here
MODULE_MAP::const_iterator MODULE_CITER
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter.
#define SZ(x)
Get the length of a string constant, at compile time.
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:198
#define TESTLINE(x)
C string compare test for a specific length of characters.
MODULE_MAP m_modules

References LINE_READER::Line(), LEGACY_PLUGIN::loadMODULE(), LEGACY_PLUGIN::m_board, m_modules, m_owner, LINE_READER::ReadLine(), ReplaceIllegalFileNameChars(), MODULE::SetFPID(), LEGACY_PLUGIN::SetReader(), StrPurge(), SZ, and TESTLINE.

Referenced by Load().

◆ ReadAndVerifyHeader()

void LP_CACHE::ReadAndVerifyHeader ( LINE_READER aReader)

Definition at line 3236 of file legacy_plugin.cpp.

3237 {
3238  char* line = aReader->ReadLine();
3239  char* saveptr;
3240 
3241  if( !line )
3242  goto L_bad_library;
3243 
3244  if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
3245  goto L_bad_library;
3246 
3247  while( ( line = aReader->ReadLine() ) != NULL )
3248  {
3249  if( TESTLINE( "Units" ) )
3250  {
3251  const char* units = strtok_r( line + SZ( "Units" ), delims, &saveptr );
3252 
3253  if( !strcmp( units, "mm" ) )
3254  {
3255  m_owner->diskToBiu = IU_PER_MM;
3256  }
3257 
3258  }
3259  else if( TESTLINE( "$INDEX" ) )
3260  return;
3261  }
3262 
3263 L_bad_library:
3264  THROW_IO_ERROR( wxString::Format( _( "File \"%s\" is empty or is not a legacy library" ),
3265  m_lib_path.GetData() ) );
3266 }
LEGACY_PLUGIN * m_owner
static const char delims[]
#define THROW_IO_ERROR(msg)
double diskToBiu
convert from disk engineering units to BIUs with this scale factor
#define _(s)
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter.
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 m_lib_path
char * strtok_r(char *str, const char *delim, char **nextp)
#define SZ(x)
Get the length of a string constant, at compile time.
#define TESTLINE(x)
C string compare test for a specific length of characters.

References _, delims, LEGACY_PLUGIN::diskToBiu, Format(), m_lib_path, m_owner, LINE_READER::ReadLine(), strtok_r(), SZ, TESTLINE, and THROW_IO_ERROR.

Referenced by Load().

◆ SkipIndex()

void LP_CACHE::SkipIndex ( LINE_READER aReader)

Definition at line 3269 of file legacy_plugin.cpp.

3270 {
3271  // Some broken INDEX sections have more than one section, due to prior bugs.
3272  // So we must read the next line after $EndINDEX tag,
3273  // to see if this is not a new $INDEX tag.
3274  bool exit = false;
3275  char* line = aReader->Line();
3276 
3277  do
3278  {
3279  if( TESTLINE( "$INDEX" ) )
3280  {
3281  exit = false;
3282 
3283  while( ( line = aReader->ReadLine() ) != NULL )
3284  {
3285  if( TESTLINE( "$EndINDEX" ) )
3286  {
3287  exit = true;
3288  break;
3289  }
3290  }
3291  }
3292  else if( exit )
3293  break;
3294  } while( ( line = aReader->ReadLine() ) != NULL );
3295 }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter.
#define TESTLINE(x)
C string compare test for a specific length of characters.

References LINE_READER::Line(), LINE_READER::ReadLine(), and TESTLINE.

Referenced by Load().

Member Data Documentation

◆ m_cache_dirty

bool LP_CACHE::m_cache_dirty

Definition at line 3171 of file legacy_plugin.cpp.

Referenced by IsModified(), and Load().

◆ m_cache_timestamp

long long LP_CACHE::m_cache_timestamp

Definition at line 3173 of file legacy_plugin.cpp.

Referenced by IsModified(), and Load().

◆ m_lib_path

wxString LP_CACHE::m_lib_path

◆ m_modules

MODULE_MAP LP_CACHE::m_modules

◆ m_owner

LEGACY_PLUGIN* LP_CACHE::m_owner

Definition at line 3166 of file legacy_plugin.cpp.

Referenced by LoadModules(), and ReadAndVerifyHeader().

◆ m_writable

bool LP_CACHE::m_writable

Definition at line 3169 of file legacy_plugin.cpp.

Referenced by LEGACY_PLUGIN::IsFootprintLibWritable().


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