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
 
FOOTPRINT_MAP m_footprints
 
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 3035 of file legacy_plugin.cpp.

Constructor & Destructor Documentation

◆ LP_CACHE()

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

Definition at line 3066 of file legacy_plugin.cpp.

3066  :
3067  m_owner( aOwner ),
3068  m_lib_path( aLibraryPath ),
3069  m_writable( true ),
3070  m_cache_dirty( true ),
3071  m_cache_timestamp( 0 )
3072 {
3073 }
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 3084 of file legacy_plugin.cpp.

3085 {
3086  return wxFileName( aLibPath ).GetModificationTime().GetValue().GetValue();
3087 }

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

◆ IsModified()

bool LP_CACHE::IsModified ( )

Definition at line 3076 of file legacy_plugin.cpp.

3077 {
3079 
3080  return m_cache_dirty;
3081 }
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 3090 of file legacy_plugin.cpp.

3091 {
3092  m_cache_dirty = false;
3093 
3094  FILE_LINE_READER reader( m_lib_path );
3095 
3096  ReadAndVerifyHeader( &reader );
3097  SkipIndex( &reader );
3098  LoadModules( &reader );
3099 
3100  // Remember the file modification time of library file when the
3101  // cache snapshot was made, so that in a networked environment we will
3102  // reload the cache as needed.
3104 }
void SkipIndex(LINE_READER *aReader)
bool m_cache_dirty
A LINE_READER that reads from an open file.
Definition: richio.h:172
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 3163 of file legacy_plugin.cpp.

3164 {
3165  m_owner->SetReader( aReader );
3166 
3167  char* line = aReader->Line();
3168 
3169  do
3170  {
3171  // test first for the $MODULE, even before reading because of INDEX bug.
3172  if( TESTLINE( "$MODULE" ) )
3173  {
3174  std::unique_ptr<FOOTPRINT> fp_ptr = std::make_unique<FOOTPRINT>( m_owner->m_board );
3175 
3176  std::string footprintName = StrPurge( line + SZ( "$MODULE" ) );
3177 
3178  // The footprint names in legacy libraries can contain the '/' and ':'
3179  // characters which will cause the LIB_ID parser to choke.
3180  ReplaceIllegalFileNameChars( &footprintName );
3181 
3182  // set the footprint name first thing, so exceptions can use name.
3183  fp_ptr->SetFPID( LIB_ID( wxEmptyString, footprintName ) );
3184 
3185  m_owner->loadFOOTPRINT( fp_ptr.get());
3186 
3187  FOOTPRINT* fp = fp_ptr.release(); // exceptions after this are not expected.
3188 
3189  // Not sure why this is asserting on debug builds. The debugger shows the
3190  // strings are the same. If it's not really needed maybe it can be removed.
3191 // wxASSERT( footprintName == m->GetFPID().GetLibItemName() );
3192 
3193  /*
3194 
3195  There was a bug in old legacy library management code
3196  (pre-LEGACY_PLUGIN) which was introducing duplicate footprint names
3197  in legacy libraries without notification. To best recover from such
3198  bad libraries, and use them to their fullest, there are a few
3199  strategies that could be used. (Note: footprints must have unique
3200  names to be accepted into this cache.) The strategy used here is to
3201  append a differentiating version counter to the end of the name as:
3202  _v2, _v3, etc.
3203 
3204  */
3205 
3206  FOOTPRINT_MAP::const_iterator it = m_footprints.find( footprintName );
3207 
3208  if( it == m_footprints.end() ) // footprintName is not present in cache yet.
3209  {
3210  if( !m_footprints.insert( footprintName, fp ).second )
3211  {
3212  wxFAIL_MSG( "error doing cache insert using guaranteed unique name" );
3213  }
3214  }
3215 
3216  // Bad library has a duplicate of this footprintName, generate a
3217  // unique footprint name and load it anyway.
3218  else
3219  {
3220  bool nameOK = false;
3221  int version = 2;
3222  char buf[48];
3223 
3224  while( !nameOK )
3225  {
3226  std::string newName = footprintName;
3227 
3228  newName += "_v";
3229  sprintf( buf, "%d", version++ );
3230  newName += buf;
3231 
3232  it = m_footprints.find( newName );
3233 
3234  if( it == m_footprints.end() )
3235  {
3236  nameOK = true;
3237 
3238  fp->SetFPID( LIB_ID( wxEmptyString, newName ) );
3239 
3240  if( !m_footprints.insert( newName, fp ).second )
3241  {
3242  wxFAIL_MSG( "error doing cache insert using guaranteed unique name" );
3243  }
3244  }
3245  }
3246  }
3247  }
3248 
3249  } while( ( line = aReader->ReadLine() ) != NULL );
3250 }
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:376
LEGACY_PLUGIN * m_owner
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar)
Checks aName for illegal file name characters.
Definition: string.cpp:746
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void SetReader(LINE_READER *aReader)
Definition: legacy_plugin.h:97
#define NULL
BOARD * m_board
which BOARD, no ownership here
void loadFOOTPRINT(FOOTPRINT *aFootprint)
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:211
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
FOOTPRINT_MAP m_footprints
#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 LINE_READER::Line(), LEGACY_PLUGIN::loadFOOTPRINT(), LEGACY_PLUGIN::m_board, m_footprints, m_owner, NULL, LINE_READER::ReadLine(), ReplaceIllegalFileNameChars(), FOOTPRINT::SetFPID(), LEGACY_PLUGIN::SetReader(), StrPurge(), SZ, and TESTLINE.

Referenced by Load().

◆ ReadAndVerifyHeader()

void LP_CACHE::ReadAndVerifyHeader ( LINE_READER aReader)

Definition at line 3107 of file legacy_plugin.cpp.

3108 {
3109  char* line = aReader->ReadLine();
3110  char* data;
3111 
3112  if( !line )
3113  THROW_IO_ERROR( wxString::Format( _( "File '%s' is empty." ), m_lib_path ) );
3114 
3115  if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
3116  THROW_IO_ERROR( wxString::Format( _( "File '%s' is not a legacy library." ), m_lib_path ) );
3117 
3118  while( ( line = aReader->ReadLine() ) != NULL )
3119  {
3120  if( TESTLINE( "Units" ) )
3121  {
3122  const char* units = strtok_r( line + SZ( "Units" ), delims, &data );
3123 
3124  if( !strcmp( units, "mm" ) )
3126 
3127  }
3128  else if( TESTLINE( "$INDEX" ) )
3129  return;
3130  }
3131 }
static constexpr double IU_PER_MM
Mock up a conversion function.
LEGACY_PLUGIN * m_owner
static const char delims[]
#define NULL
double diskToBiu
convert from disk engineering units to BIUs with this scale factor
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
#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 THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
#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 3134 of file legacy_plugin.cpp.

3135 {
3136  // Some broken INDEX sections have more than one section, due to prior bugs.
3137  // So we must read the next line after $EndINDEX tag,
3138  // to see if this is not a new $INDEX tag.
3139  bool exit = false;
3140  char* line = aReader->Line();
3141 
3142  do
3143  {
3144  if( TESTLINE( "$INDEX" ) )
3145  {
3146  exit = false;
3147 
3148  while( ( line = aReader->ReadLine() ) != NULL )
3149  {
3150  if( TESTLINE( "$EndINDEX" ) )
3151  {
3152  exit = true;
3153  break;
3154  }
3155  }
3156  }
3157  else if( exit )
3158  break;
3159  } while( ( line = aReader->ReadLine() ) != NULL );
3160 }
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
#define NULL
virtual char * ReadLine()=0
Read 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 3042 of file legacy_plugin.cpp.

Referenced by IsModified(), and Load().

◆ m_cache_timestamp

long long LP_CACHE::m_cache_timestamp

Definition at line 3044 of file legacy_plugin.cpp.

Referenced by IsModified(), and Load().

◆ m_footprints

FOOTPRINT_MAP LP_CACHE::m_footprints

◆ m_lib_path

wxString LP_CACHE::m_lib_path

◆ m_owner

LEGACY_PLUGIN* LP_CACHE::m_owner

Definition at line 3037 of file legacy_plugin.cpp.

Referenced by LoadModules(), and ReadAndVerifyHeader().

◆ m_writable

bool LP_CACHE::m_writable

Definition at line 3040 of file legacy_plugin.cpp.

Referenced by LEGACY_PLUGIN::IsFootprintLibWritable().


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