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 Save ()
 save the entire legacy library to m_lib_path; More...
 
void SaveHeader (FILE *aFile)
 
void SaveIndex (FILE *aFile)
 
void SaveModules (FILE *aFile)
 
void SaveEndOfFile (FILE *aFile)
 
void Load ()
 
void ReadAndVerifyHeader (LINE_READER *aReader)
 
void SkipIndex (LINE_READER *aReader)
 
void LoadModules (LINE_READER *aReader)
 
wxDateTime GetLibModificationTime ()
 

Public Attributes

LEGACY_PLUGINm_owner
 
wxString m_lib_path
 
wxDateTime m_mod_time
 
MODULE_MAP m_modules
 
bool m_writable
 

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

Constructor & Destructor Documentation

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

Definition at line 3200 of file legacy_plugin.cpp.

Referenced by LEGACY_PLUGIN::cacheLib().

3200  :
3201  m_owner( aOwner ),
3202  m_lib_path( aLibraryPath ),
3203  m_writable( true )
3204 {
3205 }
LEGACY_PLUGIN * m_owner
wxString m_lib_path

Member Function Documentation

wxDateTime LP_CACHE::GetLibModificationTime ( )

Definition at line 3208 of file legacy_plugin.cpp.

References m_lib_path, and m_writable.

Referenced by Load().

3209 {
3210  wxFileName fn( m_lib_path );
3211 
3212  // update the writable flag while we have a wxFileName, in a network this
3213  // is possibly quite dynamic anyway.
3214  m_writable = fn.IsFileWritable();
3215 
3216  return fn.GetModificationTime();
3217 }
wxString m_lib_path
void LP_CACHE::Load ( )

Definition at line 3220 of file legacy_plugin.cpp.

References GetLibModificationTime(), LoadModules(), m_lib_path, m_mod_time, ReadAndVerifyHeader(), and SkipIndex().

3221 {
3222  FILE_LINE_READER reader( m_lib_path );
3223 
3224  ReadAndVerifyHeader( &reader );
3225  SkipIndex( &reader );
3226  LoadModules( &reader );
3227 
3228  // Remember the file modification time of library file when the
3229  // cache snapshot was made, so that in a networked environment we will
3230  // reload the cache as needed.
3232 }
wxDateTime m_mod_time
void SkipIndex(LINE_READER *aReader)
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
void LoadModules(LINE_READER *aReader)
wxDateTime GetLibModificationTime()
wxString m_lib_path
void ReadAndVerifyHeader(LINE_READER *aReader)
void LP_CACHE::LoadModules ( LINE_READER aReader)

Definition at line 3297 of file legacy_plugin.cpp.

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

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

Definition at line 3235 of file legacy_plugin.cpp.

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

3236 {
3237  char* line = aReader->ReadLine();
3238  char* saveptr;
3239 
3240  if( !line )
3241  goto L_bad_library;
3242 
3243  if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
3244  goto L_bad_library;
3245 
3246  while( ( line = aReader->ReadLine() ) != NULL )
3247  {
3248  if( TESTLINE( "Units" ) )
3249  {
3250  const char* units = strtok_r( line + SZ( "Units" ), delims, &saveptr );
3251 
3252  if( !strcmp( units, "mm" ) )
3253  {
3254  m_owner->diskToBiu = IU_PER_MM;
3255  }
3256 
3257  }
3258  else if( TESTLINE( "$INDEX" ) )
3259  return;
3260  }
3261 
3262 L_bad_library:
3263  THROW_IO_ERROR( wxString::Format( _( "File \"%s\" is empty or is not a legacy library" ),
3264  m_lib_path.GetData() ) );
3265 }
LEGACY_PLUGIN * m_owner
static const char delims[]
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
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 THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
#define TESTLINE(x)
C string compare test for a specific length of characters.
void LP_CACHE::Save ( )

save the entire legacy library to m_lib_path;

void LP_CACHE::SaveEndOfFile ( FILE *  aFile)
inline

Definition at line 3183 of file legacy_plugin.cpp.

References PLUGIN::GetLibModificationTime(), and LEGACY_PLUGIN::Load().

3184  {
3185  fprintf( aFile, "$EndLIBRARY\n" );
3186  }
void LP_CACHE::SaveHeader ( FILE *  aFile)
void LP_CACHE::SaveIndex ( FILE *  aFile)
void LP_CACHE::SaveModules ( FILE *  aFile)
void LP_CACHE::SkipIndex ( LINE_READER aReader)

Definition at line 3268 of file legacy_plugin.cpp.

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

Referenced by Load().

3269 {
3270  // Some broken INDEX sections have more than one section, due to prior bugs.
3271  // So we must read the next line after $EndINDEX tag,
3272  // to see if this is not a new $INDEX tag.
3273  bool exit = false;
3274  char* line = aReader->Line();
3275 
3276  do
3277  {
3278  if( TESTLINE( "$INDEX" ) )
3279  {
3280  exit = false;
3281 
3282  while( ( line = aReader->ReadLine() ) != NULL )
3283  {
3284  if( TESTLINE( "$EndINDEX" ) )
3285  {
3286  exit = true;
3287  break;
3288  }
3289  }
3290  }
3291  else if( exit )
3292  break;
3293  } while( ( line = aReader->ReadLine() ) != NULL );
3294 }
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.

Member Data Documentation

wxString LP_CACHE::m_lib_path

Definition at line 3163 of file legacy_plugin.cpp.

Referenced by GetLibModificationTime(), Load(), and ReadAndVerifyHeader().

wxDateTime LP_CACHE::m_mod_time

Definition at line 3164 of file legacy_plugin.cpp.

Referenced by Load().

MODULE_MAP LP_CACHE::m_modules

Definition at line 3165 of file legacy_plugin.cpp.

Referenced by LoadModules().

LEGACY_PLUGIN* LP_CACHE::m_owner

Definition at line 3162 of file legacy_plugin.cpp.

Referenced by LoadModules(), and ReadAndVerifyHeader().

bool LP_CACHE::m_writable

Definition at line 3166 of file legacy_plugin.cpp.

Referenced by GetLibModificationTime().


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