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

Constructor & Destructor Documentation

◆ LP_CACHE()

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

Definition at line 2965 of file legacy_plugin.cpp.

2965  :
2966  m_owner( aOwner ),
2967  m_lib_path( aLibraryPath ),
2968  m_writable( true ),
2969  m_cache_dirty( true ),
2970  m_cache_timestamp( 0 )
2971 {
2972 }
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 2983 of file legacy_plugin.cpp.

2984 {
2985  return wxFileName( aLibPath ).GetModificationTime().GetValue().GetValue();
2986 }

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

◆ IsModified()

bool LP_CACHE::IsModified ( )

Definition at line 2975 of file legacy_plugin.cpp.

2976 {
2978 
2979  return m_cache_dirty;
2980 }
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 2989 of file legacy_plugin.cpp.

2990 {
2991  m_cache_dirty = false;
2992 
2993  FILE_LINE_READER reader( m_lib_path );
2994 
2995  ReadAndVerifyHeader( &reader );
2996  SkipIndex( &reader );
2997  LoadModules( &reader );
2998 
2999  // Remember the file modification time of library file when the
3000  // cache snapshot was made, so that in a networked environment we will
3001  // reload the cache as needed.
3003 }
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 3066 of file legacy_plugin.cpp.

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

3007 {
3008  char* line = aReader->ReadLine();
3009  char* data;
3010 
3011  if( !line )
3012  THROW_IO_ERROR( wxString::Format( _( "File '%s' is empty." ), m_lib_path ) );
3013 
3014  if( !TESTLINE( "PCBNEW-LibModule-V1" ) )
3015  THROW_IO_ERROR( wxString::Format( _( "File '%s' is not a legacy library." ), m_lib_path ) );
3016 
3017  while( ( line = aReader->ReadLine() ) != nullptr )
3018  {
3019  if( TESTLINE( "Units" ) )
3020  {
3021  const char* units = strtok_r( line + SZ( "Units" ), delims, &data );
3022 
3023  if( !strcmp( units, "mm" ) )
3025 
3026  }
3027  else if( TESTLINE( "$INDEX" ) )
3028  {
3029  return;
3030  }
3031  }
3032 }
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 3035 of file legacy_plugin.cpp.

3036 {
3037  // Some broken INDEX sections have more than one section, due to prior bugs.
3038  // So we must read the next line after $EndINDEX tag,
3039  // to see if this is not a new $INDEX tag.
3040  bool exit = false;
3041  char* line = aReader->Line();
3042 
3043  do
3044  {
3045  if( TESTLINE( "$INDEX" ) )
3046  {
3047  exit = false;
3048 
3049  while( ( line = aReader->ReadLine() ) != nullptr )
3050  {
3051  if( TESTLINE( "$EndINDEX" ) )
3052  {
3053  exit = true;
3054  break;
3055  }
3056  }
3057  }
3058  else if( exit )
3059  {
3060  break;
3061  }
3062  } while( ( line = aReader->ReadLine() ) != nullptr );
3063 }
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 2958 of file legacy_plugin.cpp.

Referenced by IsModified(), and Load().

◆ m_cache_timestamp

long long LP_CACHE::m_cache_timestamp

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

Referenced by LoadModules(), and ReadAndVerifyHeader().

◆ m_writable

bool LP_CACHE::m_writable

Definition at line 2956 of file legacy_plugin.cpp.

Referenced by LEGACY_PLUGIN::IsFootprintLibWritable().


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