KiCad PCB EDA Suite
LP_CACHE Struct Reference

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

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 3115 of file legacy_plugin.cpp.

Constructor & Destructor Documentation

◆ LP_CACHE()

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

Definition at line 3146 of file legacy_plugin.cpp.

3146  :
3147  m_owner( aOwner ),
3148  m_lib_path( aLibraryPath ),
3149  m_writable( true ),
3150  m_cache_dirty( true ),
3151  m_cache_timestamp( 0 )
3152 {
3153 }
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 3164 of file legacy_plugin.cpp.

3165 {
3166  return wxFileName( aLibPath ).GetModificationTime().GetValue().GetValue();
3167 }

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

◆ IsModified()

bool LP_CACHE::IsModified ( )

Definition at line 3156 of file legacy_plugin.cpp.

3157 {
3159 
3160  return m_cache_dirty;
3161 }
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 3170 of file legacy_plugin.cpp.

3171 {
3172  m_cache_dirty = false;
3173 
3174  FILE_LINE_READER reader( m_lib_path );
3175 
3176  ReadAndVerifyHeader( &reader );
3177  SkipIndex( &reader );
3178  LoadModules( &reader );
3179 
3180  // Remember the file modification time of library file when the
3181  // cache snapshot was made, so that in a networked environment we will
3182  // reload the cache as needed.
3184 }
void SkipIndex(LINE_READER *aReader)
bool m_cache_dirty
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 3243 of file legacy_plugin.cpp.

3244 {
3245  m_owner->SetReader( aReader );
3246 
3247  char* line = aReader->Line();
3248 
3249  do
3250  {
3251  // test first for the $MODULE, even before reading because of INDEX bug.
3252  if( TESTLINE( "$MODULE" ) )
3253  {
3254  unique_ptr<MODULE> module( new MODULE( m_owner->m_board ) );
3255 
3256  std::string footprintName = StrPurge( line + SZ( "$MODULE" ) );
3257 
3258  // The footprint names in legacy libraries can contain the '/' and ':'
3259  // characters which will cause the LIB_ID parser to choke.
3260  ReplaceIllegalFileNameChars( &footprintName );
3261 
3262  // set the footprint name first thing, so exceptions can use name.
3263  module->SetFPID( LIB_ID( wxEmptyString, footprintName ) );
3264 
3265  m_owner->loadMODULE( module.get() );
3266 
3267  MODULE* m = module.release(); // exceptions after this are not expected.
3268 
3269  // Not sure why this is asserting on debug builds. The debugger shows the
3270  // strings are the same. If it's not really needed maybe it can be removed.
3271 // wxASSERT( footprintName == m->GetFPID().GetLibItemName() );
3272 
3273  /*
3274 
3275  There was a bug in old legacy library management code
3276  (pre-LEGACY_PLUGIN) which was introducing duplicate footprint names
3277  in legacy libraries without notification. To best recover from such
3278  bad libraries, and use them to their fullest, there are a few
3279  strategies that could be used. (Note: footprints must have unique
3280  names to be accepted into this cache.) The strategy used here is to
3281  append a differentiating version counter to the end of the name as:
3282  _v2, _v3, etc.
3283 
3284  */
3285 
3286  MODULE_CITER it = m_modules.find( footprintName );
3287 
3288  if( it == m_modules.end() ) // footprintName is not present in cache yet.
3289  {
3290  std::pair<MODULE_ITER, bool> r = m_modules.insert( footprintName, m );
3291 
3292  wxASSERT_MSG( r.second, wxT( "error doing cache insert using guaranteed unique name" ) );
3293  (void) r;
3294  }
3295 
3296  // Bad library has a duplicate of this footprintName, generate a
3297  // unique footprint name and load it anyway.
3298  else
3299  {
3300  bool nameOK = false;
3301  int version = 2;
3302  char buf[48];
3303 
3304  while( !nameOK )
3305  {
3306  std::string newName = footprintName;
3307 
3308  newName += "_v";
3309  sprintf( buf, "%d", version++ );
3310  newName += buf;
3311 
3312  it = m_modules.find( newName );
3313 
3314  if( it == m_modules.end() )
3315  {
3316  nameOK = true;
3317 
3318  m->SetFPID( LIB_ID( wxEmptyString, newName ) );
3319  std::pair<MODULE_ITER, bool> r = m_modules.insert( newName, m );
3320 
3321  wxASSERT_MSG( r.second, wxT( "error doing cache insert using guaranteed unique name" ) );
3322  (void) r;
3323  }
3324  }
3325  }
3326  }
3327 
3328  } while( ( line = aReader->ReadLine() ) != NULL );
3329 }
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:338
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:708
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void SetReader(LINE_READER *aReader)
#define NULL
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:226
#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, NULL, 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 3187 of file legacy_plugin.cpp.

3188 {
3189  char* line = aReader->ReadLine();
3190  char* data;
3191 
3192  if( !line )
3193  THROW_IO_ERROR( wxString::Format( _( "File '%s' is empty." ), m_lib_path ) );
3194 
3195  if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
3196  THROW_IO_ERROR( wxString::Format( _( "File '%s' is not a legacy library." ), m_lib_path ) );
3197 
3198  while( ( line = aReader->ReadLine() ) != NULL )
3199  {
3200  if( TESTLINE( "Units" ) )
3201  {
3202  const char* units = strtok_r( line + SZ( "Units" ), delims, &data );
3203 
3204  if( !strcmp( units, "mm" ) )
3206 
3207  }
3208  else if( TESTLINE( "$INDEX" ) )
3209  return;
3210  }
3211 }
static constexpr double IU_PER_MM
Mock up a conversion function.
LEGACY_PLUGIN * m_owner
static const char delims[]
#define NULL
#define THROW_IO_ERROR(msg)
double diskToBiu
convert from disk engineering units to BIUs with this scale factor
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
#define _(s)
Definition: 3d_actions.cpp:33
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(), IU_PER_MM, m_lib_path, m_owner, NULL, 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 3214 of file legacy_plugin.cpp.

3215 {
3216  // Some broken INDEX sections have more than one section, due to prior bugs.
3217  // So we must read the next line after $EndINDEX tag,
3218  // to see if this is not a new $INDEX tag.
3219  bool exit = false;
3220  char* line = aReader->Line();
3221 
3222  do
3223  {
3224  if( TESTLINE( "$INDEX" ) )
3225  {
3226  exit = false;
3227 
3228  while( ( line = aReader->ReadLine() ) != NULL )
3229  {
3230  if( TESTLINE( "$EndINDEX" ) )
3231  {
3232  exit = true;
3233  break;
3234  }
3235  }
3236  }
3237  else if( exit )
3238  break;
3239  } while( ( line = aReader->ReadLine() ) != NULL );
3240 }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
#define NULL
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(), NULL, LINE_READER::ReadLine(), and TESTLINE.

Referenced by Load().

Member Data Documentation

◆ m_cache_dirty

bool LP_CACHE::m_cache_dirty

Definition at line 3122 of file legacy_plugin.cpp.

Referenced by IsModified(), and Load().

◆ m_cache_timestamp

long long LP_CACHE::m_cache_timestamp

Definition at line 3124 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 3117 of file legacy_plugin.cpp.

Referenced by LoadModules(), and ReadAndVerifyHeader().

◆ m_writable

bool LP_CACHE::m_writable

Definition at line 3120 of file legacy_plugin.cpp.

Referenced by LEGACY_PLUGIN::IsFootprintLibWritable().


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