From 442ee52905dfe4057dafd04e474776aa8b3842a6 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 23 Jan 2023 09:42:27 -0800 Subject: [PATCH] Add versioning to lib tables Sets lib table version to allow easier migration between versions --- common/fp_lib_table.cpp | 14 +++++++++++++- common/lib_table.keywords | 1 + common/lib_table_base.cpp | 7 +++++-- eeschema/symbol_lib_table.cpp | 14 +++++++++++++- include/lib_table_base.h | 13 +++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/common/fp_lib_table.cpp b/common/fp_lib_table.cpp index 758c5b59c5..2d34981546 100644 --- a/common/fp_lib_table.cpp +++ b/common/fp_lib_table.cpp @@ -99,8 +99,19 @@ void FP_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) // in case there is a "row integrity" error, tell where later. int lineNum = in->CurLineNumber(); + tok = in->NextTok(); - if( ( tok = in->NextTok() ) != T_lib ) + // Optionally parse the current version number + if( tok == T_version ) + { + in->NeedNUMBER( "version" ); + m_version = std::stoi( in->CurText() ); + in->NeedRIGHT(); + in->NeedLEFT(); + tok = in->NextTok(); + } + + if( tok != T_lib ) in->Expecting( T_lib ); // (name NICKNAME) @@ -236,6 +247,7 @@ bool FP_LIB_TABLE::operator==( const FP_LIB_TABLE& aFpTable ) const void FP_LIB_TABLE::Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const { aOutput->Print( aIndentLevel, "(fp_lib_table\n" ); + aOutput->Print( aIndentLevel + 1, "(version %d)\n", m_version ); for( LIB_TABLE_ROWS_CITER it = m_rows.begin(); it != m_rows.end(); ++it ) it->Format( aOutput, aIndentLevel+1 ); diff --git a/common/lib_table.keywords b/common/lib_table.keywords index 8de5aef791..af323dca77 100644 --- a/common/lib_table.keywords +++ b/common/lib_table.keywords @@ -1,5 +1,6 @@ fp_lib_table sym_lib_table +version lib name type diff --git a/common/lib_table_base.cpp b/common/lib_table_base.cpp index 8cdd1c39b4..c2bd8d650f 100644 --- a/common/lib_table_base.cpp +++ b/common/lib_table_base.cpp @@ -114,7 +114,7 @@ void LIB_TABLE_ROW::SetOptions( const wxString& aOptions ) LIB_TABLE::LIB_TABLE( LIB_TABLE* aFallBackTable ) : - m_fallBack( aFallBackTable ) + m_fallBack( aFallBackTable ), m_version( 0 ) { // not copying fall back, simply search aFallBackTable separately // if "nickName not found". @@ -361,7 +361,7 @@ void LIB_TABLE::Load( const wxString& aFileName ) Parse( &lexer ); - if( migrate() && wxFileName::IsFileWritable( aFileName ) ) + if( m_version != 7 && migrate() && wxFileName::IsFileWritable( aFileName ) ) Save( aFileName ); } } @@ -370,6 +370,9 @@ void LIB_TABLE::Load( const wxString& aFileName ) void LIB_TABLE::Save( const wxString& aFileName ) const { FILE_OUTPUTFORMATTER sf( aFileName ); + + // Force the lib table version to 7 before saving + m_version = 7; Format( &sf, 0 ); } diff --git a/eeschema/symbol_lib_table.cpp b/eeschema/symbol_lib_table.cpp index 3a1875567f..65d64d2ea6 100644 --- a/eeschema/symbol_lib_table.cpp +++ b/eeschema/symbol_lib_table.cpp @@ -141,8 +141,19 @@ void SYMBOL_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) // in case there is a "row integrity" error, tell where later. int lineNum = in->CurLineNumber(); + tok = in->NextTok(); - if( ( tok = in->NextTok() ) != T_lib ) + // Optionally parse the current version number + if( tok == T_version ) + { + in->NeedNUMBER( "version" ); + m_version = std::stoi( in->CurText() ); + in->NeedRIGHT(); + in->NeedLEFT(); + tok = in->NextTok(); + } + + if( tok != T_lib ) in->Expecting( T_lib ); // (name NICKNAME) @@ -269,6 +280,7 @@ void SYMBOL_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) void SYMBOL_LIB_TABLE::Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const { aOutput->Print( aIndentLevel, "(sym_lib_table\n" ); + aOutput->Print( aIndentLevel + 1, "(version %d)\n", m_version ); for( LIB_TABLE_ROWS_CITER it = m_rows.begin(); it != m_rows.end(); ++it ) { diff --git a/include/lib_table_base.h b/include/lib_table_base.h index 21a813b270..04769543ea 100644 --- a/include/lib_table_base.h +++ b/include/lib_table_base.h @@ -510,6 +510,16 @@ public: */ static UTF8 FormatOptions( const STRING_UTF8_MAP* aProperties ); + /** + * Returns the version number (0 if unset) + * + * @return integer version number read from table + */ + int GetVersion() const + { + return m_version; + } + protected: /** * Return a #LIB_TABLE_ROW if \a aNickname is found in this table or in any chained @@ -566,6 +576,9 @@ protected: LIB_TABLE* m_fallBack; + /// Versioning to handle importing old tables + mutable int m_version; + /// Mutex to protect access to the nickIndex variable mutable std::mutex m_nickIndexMutex; };