KiCad PCB EDA Suite
LP_CACHE Struct Reference

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

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

Constructor & Destructor Documentation

◆ LP_CACHE()

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

Definition at line 2949 of file legacy_plugin.cpp.

2949  :
2950  m_owner( aOwner ),
2951  m_lib_path( aLibraryPath ),
2952  m_writable( true ),
2953  m_cache_dirty( true ),
2954  m_cache_timestamp( 0 )
2955 {
2956 }
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 2967 of file legacy_plugin.cpp.

2968 {
2969  return wxFileName( aLibPath ).GetModificationTime().GetValue().GetValue();
2970 }

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

◆ IsModified()

bool LP_CACHE::IsModified ( )

Definition at line 2959 of file legacy_plugin.cpp.

2960 {
2962 
2963  return m_cache_dirty;
2964 }
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 2973 of file legacy_plugin.cpp.

2974 {
2975  m_cache_dirty = false;
2976 
2977  FILE_LINE_READER reader( m_lib_path );
2978 
2979  ReadAndVerifyHeader( &reader );
2980  SkipIndex( &reader );
2981  LoadModules( &reader );
2982 
2983  // Remember the file modification time of library file when the
2984  // cache snapshot was made, so that in a networked environment we will
2985  // reload the cache as needed.
2987 }
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 3050 of file legacy_plugin.cpp.

3051 {
3052  m_owner->SetReader( aReader );
3053 
3054  char* line = aReader->Line();
3055 
3056  do
3057  {
3058  // test first for the $MODULE, even before reading because of INDEX bug.
3059  if( TESTLINE( "$MODULE" ) )
3060  {
3061  std::unique_ptr<FOOTPRINT> fp_ptr = std::make_unique<FOOTPRINT>( m_owner->m_board );
3062 
3063  std::string footprintName = StrPurge( line + SZ( "$MODULE" ) );
3064 
3065  // The footprint names in legacy libraries can contain the '/' and ':'
3066  // characters which will cause the LIB_ID parser to choke.
3067  ReplaceIllegalFileNameChars( &footprintName );
3068 
3069  // set the footprint name first thing, so exceptions can use name.
3070  fp_ptr->SetFPID( LIB_ID( wxEmptyString, footprintName ) );
3071 
3072  m_owner->loadFOOTPRINT( fp_ptr.get());
3073 
3074  FOOTPRINT* fp = fp_ptr.release(); // exceptions after this are not expected.
3075 
3076  // Not sure why this is asserting on debug builds. The debugger shows the
3077  // strings are the same. If it's not really needed maybe it can be removed.
3078 
3079  /*
3080 
3081  There was a bug in old legacy library management code
3082  (pre-LEGACY_PLUGIN) which was introducing duplicate footprint names
3083  in legacy libraries without notification. To best recover from such
3084  bad libraries, and use them to their fullest, there are a few
3085  strategies that could be used. (Note: footprints must have unique
3086  names to be accepted into this cache.) The strategy used here is to
3087  append a differentiating version counter to the end of the name as:
3088  _v2, _v3, etc.
3089 
3090  */
3091 
3092  FOOTPRINT_MAP::const_iterator it = m_footprints.find( footprintName );
3093 
3094  if( it == m_footprints.end() ) // footprintName is not present in cache yet.
3095  {
3096  if( !m_footprints.insert( footprintName, fp ).second )
3097  {
3098  wxFAIL_MSG( "error doing cache insert using guaranteed unique name" );
3099  }
3100  }
3101  else
3102  {
3103  // Bad library has a duplicate of this footprintName, generate a
3104  // unique footprint name and load it anyway.
3105  bool nameOK = false;
3106  int version = 2;
3107  char buf[48];
3108 
3109  while( !nameOK )
3110  {
3111  std::string newName = footprintName;
3112 
3113  newName += "_v";
3114  sprintf( buf, "%d", version++ );
3115  newName += buf;
3116 
3117  it = m_footprints.find( newName );
3118 
3119  if( it == m_footprints.end() )
3120  {
3121  nameOK = true;
3122 
3123  fp->SetFPID( LIB_ID( wxEmptyString, newName ) );
3124 
3125  if( !m_footprints.insert( newName, fp ).second )
3126  {
3127  wxFAIL_MSG( "error doing cache insert using guaranteed unique name" );
3128  }
3129  }
3130  }
3131  }
3132  }
3133 
3134  } while( ( line = aReader->ReadLine() ) != nullptr );
3135 }
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
LEGACY_PLUGIN * m_owner
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
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar)
Checks aName for illegal file name characters.
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
BOARD * m_board
which BOARD, no ownership here
void loadFOOTPRINT(FOOTPRINT *aFootprint)
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:195
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, 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 2990 of file legacy_plugin.cpp.

2991 {
2992  char* line = aReader->ReadLine();
2993  char* data;
2994 
2995  if( !line )
2996  THROW_IO_ERROR( wxString::Format( _( "File '%s' is empty." ), m_lib_path ) );
2997 
2998  if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
2999  THROW_IO_ERROR( wxString::Format( _( "File '%s' is not a legacy library." ), m_lib_path ) );
3000 
3001  while( ( line = aReader->ReadLine() ) != nullptr )
3002  {
3003  if( TESTLINE( "Units" ) )
3004  {
3005  const char* units = strtok_r( line + SZ( "Units" ), delims, &data );
3006 
3007  if( !strcmp( units, "mm" ) )
3009 
3010  }
3011  else if( TESTLINE( "$INDEX" ) )
3012  {
3013  return;
3014  }
3015  }
3016 }
char * strtok_r(char *str, const char *delim, char **nextp)
static constexpr double IU_PER_MM
Mock up a conversion function.
LEGACY_PLUGIN * m_owner
static const char delims[]
#define _(s)
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.
wxString m_lib_path
#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, 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 3019 of file legacy_plugin.cpp.

3020 {
3021  // Some broken INDEX sections have more than one section, due to prior bugs.
3022  // So we must read the next line after $EndINDEX tag,
3023  // to see if this is not a new $INDEX tag.
3024  bool exit = false;
3025  char* line = aReader->Line();
3026 
3027  do
3028  {
3029  if( TESTLINE( "$INDEX" ) )
3030  {
3031  exit = false;
3032 
3033  while( ( line = aReader->ReadLine() ) != nullptr )
3034  {
3035  if( TESTLINE( "$EndINDEX" ) )
3036  {
3037  exit = true;
3038  break;
3039  }
3040  }
3041  }
3042  else if( exit )
3043  {
3044  break;
3045  }
3046  } while( ( line = aReader->ReadLine() ) != nullptr );
3047 }
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
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 exit, 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 2942 of file legacy_plugin.cpp.

Referenced by IsModified(), and Load().

◆ m_cache_timestamp

long long LP_CACHE::m_cache_timestamp

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

Referenced by LoadModules(), and ReadAndVerifyHeader().

◆ m_writable

bool LP_CACHE::m_writable

Definition at line 2940 of file legacy_plugin.cpp.

Referenced by LEGACY_PLUGIN::IsFootprintLibWritable().


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