Move duplicated code of plugin.cpp and sch_plugin.cpp into a new plugin_utils located in commons

This commit is contained in:
Thomas Pointhuber 2023-10-01 13:58:38 +02:00
parent a475b01a90
commit 3b3e4fd34a
16 changed files with 176 additions and 121 deletions

View File

@ -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

View File

@ -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 <wx/wfstream.h>
#include <wx/txtstrm.h>
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<uint8_t>& aHeader )
{
wxFileInputStream input( aFilePath );
if( input.IsOk() && !input.Eof() )
{
if( static_cast<size_t>( input.GetLength() ) < aHeader.size() )
return false;
std::vector<uint8_t> parsedHeader( aHeader.size() );
if( !input.ReadAll( parsedHeader.data(), parsedHeader.size() ) )
return false;
return parsedHeader == aHeader;
}
return false;
}
}

View File

@ -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 <cstdint>
#include <vector>
#include <wx/string.h>
namespace PLUGIN_UTILS
{
static const std::vector<uint8_t> 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<uint8_t>& aHeader );
}
#endif // PLUGIN_UTILS_H

View File

@ -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<uint8_t>& aHeader );
};
#endif // _SCH_IO_MGR_H_

View File

@ -28,8 +28,6 @@
#include <wx/translation.h>
#include <wx/filename.h>
#include <wx/dir.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#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<uint8_t>& aHeader )
{
wxFFileInputStream input( aFilePath );
if( input.IsOk() && !input.Eof() )
{
if( input.GetLength() < aHeader.size() )
return false;
std::vector<uint8_t> parsedHeader( aHeader.size() );
if( !input.ReadAll( parsedHeader.data(), parsedHeader.size() ) )
return false;
return parsedHeader == aHeader;
}
return false;
}

View File

@ -26,6 +26,7 @@
#include "altium_parser_sch.h"
#include "sch_shape.h"
#include <plugins/plugin_utils.h>
#include <plugins/altium/altium_parser.h>
#include <plugins/altium/altium_parser_utils.h>
#include <sch_plugins/altium/sch_altium_plugin.h>
@ -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() )

View File

@ -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<wxString, long long> m_timestamps;
std::map<wxString, std::map<wxString, LIB_SYMBOL*>> m_libCache;

View File

@ -53,6 +53,7 @@
#include <sch_sheet.h>
#include <sch_sheet_pin.h>
#include <bus_alias.h>
#include <plugins/plugin_utils.h>
#include <sch_plugins/legacy/sch_legacy_lib_plugin_cache.h>
#include <sch_plugins/legacy/sch_legacy_plugin.h>
#include <sch_plugins/legacy/sch_legacy_plugin_helpers.h>
@ -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 );
}

View File

@ -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<uint8_t>& aHeader );
};
#endif // IO_MGR_H_

View File

@ -30,10 +30,7 @@
#include <wx/log.h>
#include <wx/filename.h>
#include <wx/translation.h>
#include <wx/filename.h>
#include <wx/dir.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#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<uint8_t>& aHeader )
{
wxFileInputStream input( aFilePath );
if( input.IsOk() && !input.Eof() )
{
if( input.GetLength() < aHeader.size() )
return false;
std::vector<uint8_t> parsedHeader(aHeader.size());
if (!input.ReadAll(parsedHeader.data(), parsedHeader.size()))
return false;
return parsedHeader == aHeader;
}
return false;
}

View File

@ -32,7 +32,7 @@
#include <altium_circuit_maker_plugin.h>
#include <altium_designer_plugin.h>
#include <altium_pcb.h>
#include "plugins/altium/altium_parser.h"
#include <plugins/altium/altium_parser.h>
#include <board.h>

View File

@ -32,7 +32,7 @@
#include <altium_circuit_studio_plugin.h>
#include <altium_designer_plugin.h>
#include <altium_pcb.h>
#include "plugins/altium/altium_parser.h"
#include <plugins/altium/altium_parser.h>
#include <board.h>

View File

@ -31,7 +31,8 @@
#include <altium_designer_plugin.h>
#include <altium_pcb.h>
#include "plugins/altium/altium_parser.h"
#include <plugins/plugin_utils.h>
#include <plugins/altium/altium_parser.h>
#include <board.h>
@ -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 );
}

View File

@ -22,7 +22,7 @@
#include <solidworks_pcb_plugin.h>
#include <altium_designer_plugin.h>
#include <altium_pcb.h>
#include "plugins/altium/altium_parser.h"
#include <plugins/altium/altium_parser.h>
#include <board.h>

View File

@ -29,6 +29,7 @@
#include <board.h>
#include <footprint.h>
#include <string_utf8_map.h>
#include <plugins/plugin_utils.h>
std::map<wxString, PCB_LAYER_ID> 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 );
}

View File

@ -30,6 +30,7 @@
#include <pcad/pcad_plugin.h>
#include <pcad/pcad_pcb.h>
#include <pcad/s_expr_loader.h>
#include <plugins/plugin_utils.h>
#include <board.h>
#include <locale_io.h>
@ -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 );
}