From 3b3e4fd34a760cc6bde4521d910d1489adcbba87 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sun, 1 Oct 2023 13:58:38 +0200 Subject: [PATCH] Move duplicated code of plugin.cpp and sch_plugin.cpp into a new plugin_utils located in commons --- common/CMakeLists.txt | 1 + common/plugins/plugin_utils.cpp | 76 +++++++++++++++++++ common/plugins/plugin_utils.h | 55 ++++++++++++++ eeschema/sch_io_mgr.h | 7 -- eeschema/sch_plugin.cpp | 48 ------------ .../sch_plugins/altium/sch_altium_plugin.cpp | 26 +++++++ .../sch_plugins/altium/sch_altium_plugin.h | 5 ++ .../sch_plugins/legacy/sch_legacy_plugin.cpp | 5 +- pcbnew/io_mgr.h | 7 -- pcbnew/plugin.cpp | 50 ------------ .../altium/altium_circuit_maker_plugin.cpp | 2 +- .../altium/altium_circuit_studio_plugin.cpp | 2 +- .../plugins/altium/altium_designer_plugin.cpp | 5 +- .../plugins/altium/solidworks_pcb_plugin.cpp | 2 +- .../cadstar/cadstar_pcb_archive_plugin.cpp | 3 +- pcbnew/plugins/pcad/pcad_plugin.cpp | 3 +- 16 files changed, 176 insertions(+), 121 deletions(-) create mode 100644 common/plugins/plugin_utils.cpp create mode 100644 common/plugins/plugin_utils.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index ad1856a2c5..f99c23b1e8 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -462,6 +462,7 @@ set( COMMON_SRCS origin_transforms.cpp page_info.cpp plugin_file_desc.cpp + plugins/plugin_utils.cpp printout.cpp project.cpp ptree.cpp diff --git a/common/plugins/plugin_utils.cpp b/common/plugins/plugin_utils.cpp new file mode 100644 index 0000000000..46f20e387d --- /dev/null +++ b/common/plugins/plugin_utils.cpp @@ -0,0 +1,76 @@ +/* +* This program source code file is part of KiCad, a free EDA CAD application. +* +* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, you may find one here: +* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html +* or you may search the http://www.gnu.org website for the version 2 license, +* or you may write to the Free Software Foundation, Inc., +* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#include "plugin_utils.h" + +#include +#include + +namespace PLUGIN_UTILS +{ + +bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix, bool aIgnoreWhitespace ) +{ + wxFileInputStream input( aFilePath ); + + if( input.IsOk() && !input.Eof() ) + { + // Find first non-empty line + wxTextInputStream text( input ); + wxString line = text.ReadLine(); + + if( aIgnoreWhitespace ) + { + while( line.IsEmpty() ) + line = text.ReadLine().Trim( false /*trim from left*/ ); + } + + if( line.StartsWith( aPrefix ) ) + return true; + } + + return false; +} + + +bool fileStartsWithBinaryHeader( const wxString& aFilePath, const std::vector& aHeader ) +{ + wxFileInputStream input( aFilePath ); + + if( input.IsOk() && !input.Eof() ) + { + if( static_cast( input.GetLength() ) < aHeader.size() ) + return false; + + std::vector parsedHeader( aHeader.size() ); + + if( !input.ReadAll( parsedHeader.data(), parsedHeader.size() ) ) + return false; + + return parsedHeader == aHeader; + } + + return false; +} + +} \ No newline at end of file diff --git a/common/plugins/plugin_utils.h b/common/plugins/plugin_utils.h new file mode 100644 index 0000000000..b3538a2692 --- /dev/null +++ b/common/plugins/plugin_utils.h @@ -0,0 +1,55 @@ +/* +* This program source code file is part of KiCad, a free EDA CAD application. +* +* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, you may find one here: +* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html +* or you may search the http://www.gnu.org website for the version 2 license, +* or you may write to the Free Software Foundation, Inc., +* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#ifndef PLUGIN_UTILS_H +#define PLUGIN_UTILS_H + +#include +#include + +#include + +namespace PLUGIN_UTILS +{ + +static const std::vector COMPOUND_FILE_HEADER{ 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1}; + +/** + * Check if a file starts with a defined string + * @param aFilePath path to the file where we want to check the prefix + * @param aPrefix prefix string which should match with the initial characters in the file + * @param aIgnoreWhitespace true if whitespace characters should be ignored before the prefix + */ +bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix, bool aIgnoreWhitespace ); + +/** + * Check if a file starts with a defined binary header + * @param aFilePath path to the file where we want to check the prefix + * @param aHeader vector of bytes which need to match with the start of the file + */ +bool fileStartsWithBinaryHeader( const wxString& aFilePath, const std::vector& aHeader ); + +} + + +#endif // PLUGIN_UTILS_H diff --git a/eeschema/sch_io_mgr.h b/eeschema/sch_io_mgr.h index 6a5fff1926..03891193e2 100644 --- a/eeschema/sch_io_mgr.h +++ b/eeschema/sch_io_mgr.h @@ -571,13 +571,6 @@ public: return plugin; } }; - - protected: - static bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix, - bool aIgnoreWhitespace ); - - static bool fileStartsWithBinaryHeader( const wxString& aFilePath, - const std::vector& aHeader ); }; #endif // _SCH_IO_MGR_H_ diff --git a/eeschema/sch_plugin.cpp b/eeschema/sch_plugin.cpp index ed37ae66a0..c29d86da16 100644 --- a/eeschema/sch_plugin.cpp +++ b/eeschema/sch_plugin.cpp @@ -28,8 +28,6 @@ #include #include #include -#include -#include #define FMT_UNIMPLEMENTED wxT( "Plugin \"%s\" does not implement the \"%s\" function." ) #define NOT_IMPLEMENTED( aCaller ) \ @@ -218,49 +216,3 @@ const wxString& SCH_PLUGIN::GetError() const // not pure virtual so that plugins only have to implement subset of the SCH_PLUGIN interface. NOT_IMPLEMENTED( __FUNCTION__ ); } - - -bool SCH_PLUGIN::fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix, - bool aIgnoreWhitespace ) -{ - wxFFileInputStream input( aFilePath ); - - if( input.IsOk() && !input.Eof() ) - { - // Find first non-empty line - wxTextInputStream text( input ); - wxString line = text.ReadLine(); - - if( aIgnoreWhitespace ) - { - while( line.IsEmpty() ) - line = text.ReadLine().Trim( false /*trim from left*/ ); - } - - if( line.StartsWith( aPrefix ) ) - return true; - } - - return false; -} - - -bool SCH_PLUGIN::fileStartsWithBinaryHeader( const wxString& aFilePath, - const std::vector& aHeader ) -{ - wxFFileInputStream input( aFilePath ); - - if( input.IsOk() && !input.Eof() ) - { - if( input.GetLength() < aHeader.size() ) - return false; - - std::vector parsedHeader( aHeader.size() ); - if( !input.ReadAll( parsedHeader.data(), parsedHeader.size() ) ) - return false; - - return parsedHeader == aHeader; - } - - return false; -} diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp index d9f0d75463..9f82b8afc6 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.cpp +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.cpp @@ -26,6 +26,7 @@ #include "altium_parser_sch.h" #include "sch_shape.h" +#include #include #include #include @@ -257,6 +258,31 @@ int SCH_ALTIUM_PLUGIN::GetModifyHash() const } +bool SCH_ALTIUM_PLUGIN::checkFileHeader( const wxString& aFileName ) +{ + // Compound File Binary Format header + return PLUGIN_UTILS::fileStartsWithBinaryHeader( aFileName, PLUGIN_UTILS::COMPOUND_FILE_HEADER ); +} + + +bool SCH_ALTIUM_PLUGIN::CanReadSchematicFile( const wxString& aFileName ) const +{ + if( !SCH_PLUGIN::CanReadSchematicFile( aFileName ) ) + return false; + + return checkFileHeader( aFileName ); +} + + +bool SCH_ALTIUM_PLUGIN::CanReadLibrary( const wxString& aFileName ) const +{ + if( !SCH_PLUGIN::CanReadLibrary( aFileName ) ) + return false; + + return checkFileHeader( aFileName ); +} + + wxString SCH_ALTIUM_PLUGIN::getLibName() { if( m_libName.IsEmpty() ) diff --git a/eeschema/sch_plugins/altium/sch_altium_plugin.h b/eeschema/sch_plugins/altium/sch_altium_plugin.h index 8695b4c417..586e19bbdc 100644 --- a/eeschema/sch_plugins/altium/sch_altium_plugin.h +++ b/eeschema/sch_plugins/altium/sch_altium_plugin.h @@ -69,6 +69,9 @@ public: return PLUGIN_FILE_DESC( _HKI( "Altium schematic library files" ), { "SchLib" } ); } + bool CanReadSchematicFile( const wxString& aFileName ) const override; + bool CanReadLibrary( const wxString& aFileName ) const override; + int GetModifyHash() const override; SCH_SHEET* LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic, @@ -218,6 +221,8 @@ private: void ensureLoadedLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties ); long long getLibraryTimestamp( const wxString& aLibraryPath ) const; + static bool checkFileHeader( const wxString& aFileName ); + std::map m_timestamps; std::map> m_libCache; diff --git a/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp b/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp index 1a35769a71..d9ff884048 100644 --- a/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp +++ b/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -2243,7 +2244,7 @@ bool SCH_LEGACY_PLUGIN::CanReadSchematicFile( const wxString& aFileName ) const if( !SCH_PLUGIN::CanReadSchematicFile( aFileName ) ) return false; - return fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true ); + return PLUGIN_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true ); } @@ -2252,7 +2253,7 @@ bool SCH_LEGACY_PLUGIN::CanReadLibrary( const wxString& aFileName ) const if( !SCH_PLUGIN::CanReadLibrary( aFileName ) ) return false; - return fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true ); + return PLUGIN_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true ); } diff --git a/pcbnew/io_mgr.h b/pcbnew/io_mgr.h index 5a90c734cb..52dc7a4a55 100644 --- a/pcbnew/io_mgr.h +++ b/pcbnew/io_mgr.h @@ -652,13 +652,6 @@ public: } }; #endif - -protected: - static bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix, - bool aIgnoreWhitespace ); - - static bool fileStartsWithBinaryHeader( const wxString& aFilePath, - const std::vector& aHeader ); }; #endif // IO_MGR_H_ diff --git a/pcbnew/plugin.cpp b/pcbnew/plugin.cpp index 8a14c237b1..1bdd4734ed 100644 --- a/pcbnew/plugin.cpp +++ b/pcbnew/plugin.cpp @@ -30,10 +30,7 @@ #include #include #include -#include #include -#include -#include #define FMT_UNIMPLEMENTED wxT( "Plugin \"%s\" does not implement the \"%s\" function." ) @@ -292,50 +289,3 @@ void PLUGIN::FootprintLibOptions( STRING_UTF8_MAP* aListToAppendTo ) const "functions." ) ); #endif } - - -bool PLUGIN::fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix, - bool aIgnoreWhitespace ) -{ - wxFileInputStream input( aFilePath ); - - if( input.IsOk() && !input.Eof() ) - { - // Find first non-empty line - wxTextInputStream text( input ); - wxString line = text.ReadLine(); - - if( aIgnoreWhitespace ) - { - while( line.IsEmpty() ) - line = text.ReadLine().Trim( false /*trim from left*/ ); - } - - if( line.StartsWith( aPrefix ) ) - return true; - } - - return false; -} - - -bool PLUGIN::fileStartsWithBinaryHeader( const wxString& aFilePath, const std::vector& aHeader ) -{ - wxFileInputStream input( aFilePath ); - - if( input.IsOk() && !input.Eof() ) - { - if( input.GetLength() < aHeader.size() ) - return false; - - std::vector parsedHeader(aHeader.size()); - - if (!input.ReadAll(parsedHeader.data(), parsedHeader.size())) - return false; - - return parsedHeader == aHeader; - } - - return false; -} - diff --git a/pcbnew/plugins/altium/altium_circuit_maker_plugin.cpp b/pcbnew/plugins/altium/altium_circuit_maker_plugin.cpp index c661eaa63a..3ed5e2e4c9 100644 --- a/pcbnew/plugins/altium/altium_circuit_maker_plugin.cpp +++ b/pcbnew/plugins/altium/altium_circuit_maker_plugin.cpp @@ -32,7 +32,7 @@ #include #include #include -#include "plugins/altium/altium_parser.h" +#include #include diff --git a/pcbnew/plugins/altium/altium_circuit_studio_plugin.cpp b/pcbnew/plugins/altium/altium_circuit_studio_plugin.cpp index fb78753b59..17443fe2b8 100644 --- a/pcbnew/plugins/altium/altium_circuit_studio_plugin.cpp +++ b/pcbnew/plugins/altium/altium_circuit_studio_plugin.cpp @@ -32,7 +32,7 @@ #include #include #include -#include "plugins/altium/altium_parser.h" +#include #include diff --git a/pcbnew/plugins/altium/altium_designer_plugin.cpp b/pcbnew/plugins/altium/altium_designer_plugin.cpp index c72968042e..43f0197bd0 100644 --- a/pcbnew/plugins/altium/altium_designer_plugin.cpp +++ b/pcbnew/plugins/altium/altium_designer_plugin.cpp @@ -31,7 +31,8 @@ #include #include -#include "plugins/altium/altium_parser.h" +#include +#include #include @@ -53,7 +54,7 @@ ALTIUM_DESIGNER_PLUGIN::~ALTIUM_DESIGNER_PLUGIN() bool ALTIUM_DESIGNER_PLUGIN::checkFileHeader( const wxString& aFileName ) { // Compound File Binary Format header - return fileStartsWithBinaryHeader( aFileName, { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1} ); + return PLUGIN_UTILS::fileStartsWithBinaryHeader( aFileName, PLUGIN_UTILS::COMPOUND_FILE_HEADER ); } diff --git a/pcbnew/plugins/altium/solidworks_pcb_plugin.cpp b/pcbnew/plugins/altium/solidworks_pcb_plugin.cpp index d2356d2c18..83324600f8 100644 --- a/pcbnew/plugins/altium/solidworks_pcb_plugin.cpp +++ b/pcbnew/plugins/altium/solidworks_pcb_plugin.cpp @@ -22,7 +22,7 @@ #include #include #include -#include "plugins/altium/altium_parser.h" +#include #include diff --git a/pcbnew/plugins/cadstar/cadstar_pcb_archive_plugin.cpp b/pcbnew/plugins/cadstar/cadstar_pcb_archive_plugin.cpp index 7c69464080..752dc6566d 100644 --- a/pcbnew/plugins/cadstar/cadstar_pcb_archive_plugin.cpp +++ b/pcbnew/plugins/cadstar/cadstar_pcb_archive_plugin.cpp @@ -29,6 +29,7 @@ #include #include #include +#include std::map CADSTAR_PCB_ARCHIVE_PLUGIN::DefaultLayerMappingCallback( @@ -139,7 +140,7 @@ BOARD* CADSTAR_PCB_ARCHIVE_PLUGIN::LoadBoard( const wxString& aFileName, BOARD* bool CADSTAR_PCB_ARCHIVE_PLUGIN::checkBoardHeader( const wxString& aFileName ) const { - return fileStartsWithPrefix( aFileName, wxT( "(CADSTARPCB" ), true ); + return PLUGIN_UTILS::fileStartsWithPrefix( aFileName, wxT( "(CADSTARPCB" ), true ); } diff --git a/pcbnew/plugins/pcad/pcad_plugin.cpp b/pcbnew/plugins/pcad/pcad_plugin.cpp index e4cdd70e7b..2f4550e01d 100644 --- a/pcbnew/plugins/pcad/pcad_plugin.cpp +++ b/pcbnew/plugins/pcad/pcad_plugin.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -59,7 +60,7 @@ bool PCAD_PLUGIN::CanReadBoard( const wxString& aFileName ) const if( !PLUGIN::CanReadBoard( aFileName ) ) return false; - return fileStartsWithPrefix( aFileName, wxT( "ACCEL_ASCII" ), false ); + return PLUGIN_UTILS::fileStartsWithPrefix( aFileName, wxT( "ACCEL_ASCII" ), false ); }