From f8cb239f405e3b2681a03a18c3fd02f84ccb1378 Mon Sep 17 00:00:00 2001 From: Wayne Stambaugh Date: Sun, 3 Sep 2023 08:45:56 -0400 Subject: [PATCH] Fix symbol library save issue. Fixes https://gitlab.com/kicad/code/kicad/-/issues/15561 --- .../kicad/sch_sexpr_lib_plugin_cache.cpp | 49 ++++++------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp index 7b3c3f4a60..a889c46caf 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_lib_plugin_cache.cpp @@ -35,39 +35,6 @@ #include -/** - * Symbol library file sorting algorithm. - * - * The sort order of symbol library files is a twofold process based on the inheritance - * depth of a given symbol. The following criteria is used to sort the symbols in the - * library file: - * - * 1. The inheritance depth from 0 to N. - * 2. The alphabetical order using our #ValueStringCompare natural sorting. - * - * @note It is imperative that symbols with an inheritance depth of 0 are stored in the - * file before symbols with an inheritance depth of 1 and so on and so forth. This - * is necessary because the symbol library file parser expects parent symbols to be - * defined before child symbols to ensure they can be parented on load. - * - * @param aLhs is the left hand side to compare. - * @parem aRhs is the right hand side to compare. - * @return true if @a aLhs is less than @a aRhs otherwise false. - */ -struct LibSymbolFileSort -{ - bool operator() ( const LIB_SYMBOL* aLhs, const LIB_SYMBOL* aRhs ) const - { - wxCHECK( aLhs && aRhs, false ); - - if( aLhs->GetInheritanceDepth() < aRhs->GetInheritanceDepth() ) - return true; - - return ( ValueStringCompare( aLhs->GetName(), aRhs->GetName() ) < 0 ); - } -}; - - SCH_SEXPR_PLUGIN_CACHE::SCH_SEXPR_PLUGIN_CACHE( const wxString& aFullPathAndFileName ) : SCH_LIB_PLUGIN_CACHE( aFullPathAndFileName ) { @@ -128,10 +95,22 @@ void SCH_SEXPR_PLUGIN_CACHE::Save( const std::optional& aOpt ) formatter->Print( 0, "(kicad_symbol_lib (version %d) (generator kicad_symbol_editor)\n", SEXPR_SYMBOL_LIB_FILE_VERSION ); - std::set orderedSymbols; + std::vector orderedSymbols; for( const std::pair& parent : m_symbols ) - orderedSymbols.emplace( parent.second ); + orderedSymbols.push_back( parent.second ); + + // Library must be ordered by inheritance depth. + std::sort( orderedSymbols.begin(), orderedSymbols.end(), + []( const LIB_SYMBOL* aLhs, const LIB_SYMBOL* aRhs ) + { + wxCHECK( aLhs && aRhs, false ); + + if( aLhs->GetInheritanceDepth() < aRhs->GetInheritanceDepth() ) + return true; + + return aLhs->GetName() < aRhs->GetName(); + } ); for( LIB_SYMBOL* symbol : orderedSymbols ) SaveSymbol( symbol, *formatter.get(), 1 );