2011-11-10 15:55:05 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-02-11 15:01:55 +00:00
|
|
|
* Copyright (C) 2014-2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2018-05-07 15:47:16 +00:00
|
|
|
* Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
|
2020-02-11 15:01:55 +00:00
|
|
|
* Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2011-11-10 15:55:05 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
2018-01-29 15:39:40 +00:00
|
|
|
#include <eda_base_frame.h>
|
2020-03-26 11:02:59 +00:00
|
|
|
#include <project.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <common.h>
|
2014-10-26 13:59:01 +00:00
|
|
|
#include <reporter.h>
|
2020-04-24 23:44:09 +00:00
|
|
|
#include <macros.h>
|
2019-05-03 17:28:11 +00:00
|
|
|
#include <mutex>
|
2008-04-24 16:55:35 +00:00
|
|
|
#include <wx/process.h>
|
2014-09-05 21:12:38 +00:00
|
|
|
#include <wx/config.h>
|
|
|
|
#include <wx/utils.h>
|
|
|
|
#include <wx/stdpaths.h>
|
2018-03-08 22:57:31 +00:00
|
|
|
#include <wx/url.h>
|
2020-02-20 12:11:04 +00:00
|
|
|
#include <boost/uuid/uuid_generators.hpp>
|
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
|
|
|
#include <boost/functional/hash.hpp>
|
2016-05-10 07:11:09 +00:00
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
using KIGFX::COLOR4D;
|
|
|
|
|
2013-06-28 16:29:39 +00:00
|
|
|
|
2020-02-20 12:11:04 +00:00
|
|
|
// Create only once, as seeding is *very* expensive
|
|
|
|
static boost::uuids::random_generator randomGenerator;
|
|
|
|
|
|
|
|
// These don't have the same performance penalty, but might as well be consistent
|
|
|
|
static boost::uuids::string_generator stringGenerator;
|
|
|
|
static boost::uuids::nil_generator nilGenerator;
|
|
|
|
|
|
|
|
// Global nil reference
|
2020-02-21 22:20:42 +00:00
|
|
|
KIID niluuid( 0 );
|
2020-02-20 12:11:04 +00:00
|
|
|
|
|
|
|
|
2020-06-20 14:36:16 +00:00
|
|
|
// For static initialization
|
|
|
|
KIID& NilUuid()
|
|
|
|
{
|
|
|
|
static KIID nil( 0 );
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
KIID::KIID() :
|
2020-02-20 12:11:04 +00:00
|
|
|
m_uuid( randomGenerator() ),
|
|
|
|
m_cached_timestamp( 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
KIID::KIID( int null ) :
|
2020-02-20 12:11:04 +00:00
|
|
|
m_uuid( nilGenerator() ),
|
|
|
|
m_cached_timestamp( 0 )
|
|
|
|
{
|
|
|
|
wxASSERT( null == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
KIID::KIID( const wxString& aString ) :
|
2020-02-20 12:11:04 +00:00
|
|
|
m_uuid(),
|
|
|
|
m_cached_timestamp( 0 )
|
|
|
|
{
|
|
|
|
if( aString.length() == 8 )
|
|
|
|
{
|
|
|
|
// A legacy-timestamp-based UUID has only the last 4 octets filled in.
|
|
|
|
// Convert them individually to avoid stepping in the little-endian/big-endian
|
|
|
|
// doo-doo.
|
|
|
|
for( int i = 0; i < 4; ++i )
|
|
|
|
{
|
|
|
|
wxString octet = aString.substr( i * 2, 2 );
|
|
|
|
m_uuid.data[ i + 12 ] = strtol( octet.data(), NULL, 16 );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cached_timestamp = strtol( aString.c_str(), NULL, 16 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_uuid = stringGenerator( aString.wc_str() );
|
|
|
|
|
|
|
|
if( IsLegacyTimestamp() )
|
|
|
|
m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), NULL, 16 );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
// Failed to parse string representation; best we can do is assign a new
|
|
|
|
// random one.
|
|
|
|
m_uuid = randomGenerator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
KIID::KIID( timestamp_t aTimestamp )
|
2020-02-20 12:11:04 +00:00
|
|
|
{
|
|
|
|
m_cached_timestamp = aTimestamp;
|
|
|
|
|
|
|
|
// A legacy-timestamp-based UUID has only the last 4 octets filled in.
|
|
|
|
// Convert them individually to avoid stepping in the little-endian/big-endian
|
|
|
|
// doo-doo.
|
|
|
|
wxString str = AsLegacyTimestampString();
|
|
|
|
|
|
|
|
for( int i = 0; i < 4; ++i )
|
|
|
|
{
|
|
|
|
wxString octet = str.substr( i * 2, 2 );
|
|
|
|
m_uuid.data[ i + 12 ] = strtol( octet.data(), NULL, 16 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
bool KIID::IsLegacyTimestamp() const
|
2020-02-20 12:11:04 +00:00
|
|
|
{
|
|
|
|
return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
timestamp_t KIID::AsLegacyTimestamp() const
|
2020-02-20 12:11:04 +00:00
|
|
|
{
|
|
|
|
return m_cached_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
size_t KIID::Hash() const
|
2020-02-20 12:11:04 +00:00
|
|
|
{
|
|
|
|
size_t hash = 0;
|
|
|
|
|
|
|
|
// Note: this is NOT little-endian/big-endian safe, but as long as it's just used
|
|
|
|
// at runtime it won't matter.
|
|
|
|
|
|
|
|
for( int i = 0; i < 4; ++i )
|
|
|
|
boost::hash_combine( hash, reinterpret_cast<const uint32_t*>( m_uuid.data )[i] );
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
void KIID::Clone( const KIID& aUUID )
|
2020-02-20 12:11:04 +00:00
|
|
|
{
|
|
|
|
m_uuid = aUUID.m_uuid;
|
|
|
|
m_cached_timestamp = aUUID.m_cached_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
wxString KIID::AsString() const
|
2020-02-20 12:11:04 +00:00
|
|
|
{
|
|
|
|
return boost::uuids::to_string( m_uuid );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
wxString KIID::AsLegacyTimestampString() const
|
2020-02-20 12:11:04 +00:00
|
|
|
{
|
|
|
|
return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-26 20:53:29 +00:00
|
|
|
void KIID::ConvertTimestampToUuid()
|
|
|
|
{
|
|
|
|
if( !IsLegacyTimestamp() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_cached_timestamp = 0;
|
|
|
|
m_uuid = randomGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
/**
|
|
|
|
* Global variables definitions.
|
|
|
|
*
|
2013-03-19 01:25:19 +00:00
|
|
|
* TODO: All of these variables should be moved into the class were they
|
2009-04-05 20:49:15 +00:00
|
|
|
* are defined and used. Most of them probably belong in the
|
|
|
|
* application class.
|
|
|
|
*/
|
|
|
|
|
2020-02-11 15:01:55 +00:00
|
|
|
// When reading/writing files, we need to swtich to setlocale( LC_NUMERIC, "C" ).
|
|
|
|
// Works fine to read/write files with floating point numbers.
|
|
|
|
// We can call setlocale( LC_NUMERIC, "C" ) of wxLocale( "C", "C", "C", false )
|
|
|
|
// wxWidgets discourage a direct call to setlocale
|
2020-02-14 12:07:13 +00:00
|
|
|
// However, for us, calling wxLocale( "C", "C", "C", false ) has a unwanted effect:
|
|
|
|
// The I18N translations are no longer active, because the English dixtionary is selected.
|
|
|
|
// To read files, this is not a major issues, but the resul can differ
|
|
|
|
// from using setlocale(xx, "C").
|
2020-02-11 15:01:55 +00:00
|
|
|
// Previouly, we called setlocale( LC_NUMERIC, "C" )
|
2020-02-14 12:07:13 +00:00
|
|
|
// The old code will be removed when calling wxLocale( "C", "C", "C", false )
|
|
|
|
// is fully tested, and all issues fixed
|
2020-02-11 15:01:55 +00:00
|
|
|
#define USE_WXLOCALE 1 /* 0 to call setlocale, 1 to call wxLocale */
|
2019-01-15 14:29:33 +00:00
|
|
|
|
2020-02-11 15:01:55 +00:00
|
|
|
// On Windows, when using setlocale, a wx alert is generated
|
|
|
|
// in some cases (reading a bitmap for instance)
|
|
|
|
// So we disable alerts during the time a file is read or written
|
|
|
|
#if !USE_WXLOCALE
|
2019-01-15 14:29:33 +00:00
|
|
|
#if defined( _WIN32 ) && defined( DEBUG )
|
2018-10-27 17:53:52 +00:00
|
|
|
// a wxAssertHandler_t function to filter wxWidgets alert messages when reading/writing a file
|
|
|
|
// when switching the locale to LC_NUMERIC, "C"
|
|
|
|
// It is used in class LOCALE_IO to hide a useless (in kicad) wxWidgets alert message
|
|
|
|
void KiAssertFilter( const wxString &file, int line,
|
2020-02-11 15:01:55 +00:00
|
|
|
const wxString &func, const wxString &cond,
|
|
|
|
const wxString &msg)
|
2018-10-27 17:53:52 +00:00
|
|
|
{
|
|
|
|
if( !msg.Contains( "Decimal separator mismatch" ) )
|
2020-02-05 14:23:55 +00:00
|
|
|
wxTheApp->OnAssertFailure( file.c_str(), line, func.c_str(), cond.c_str(), msg.c_str() );
|
2018-10-27 17:53:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-02-11 15:01:55 +00:00
|
|
|
#endif
|
2012-03-26 21:45:05 +00:00
|
|
|
|
2019-01-15 14:29:33 +00:00
|
|
|
std::atomic<unsigned int> LOCALE_IO::m_c_count( 0 );
|
2020-02-11 15:01:55 +00:00
|
|
|
LOCALE_IO::LOCALE_IO() : m_wxLocale( nullptr )
|
2008-06-06 16:39:45 +00:00
|
|
|
{
|
2016-05-10 07:11:09 +00:00
|
|
|
// use thread safe, atomic operation
|
2016-05-28 16:46:22 +00:00
|
|
|
if( m_c_count++ == 0 )
|
2016-05-10 07:11:09 +00:00
|
|
|
{
|
2020-02-11 15:01:55 +00:00
|
|
|
#if USE_WXLOCALE
|
|
|
|
m_wxLocale = new wxLocale( "C", "C", "C", false );
|
|
|
|
#else
|
2016-05-10 07:11:09 +00:00
|
|
|
// Store the user locale name, to restore this locale later, in dtor
|
2018-10-07 14:07:10 +00:00
|
|
|
m_user_locale = setlocale( LC_NUMERIC, nullptr );
|
2019-01-15 14:29:33 +00:00
|
|
|
#if defined( _WIN32 ) && defined( DEBUG )
|
2018-10-27 17:53:52 +00:00
|
|
|
// Disable wxWidgets alerts
|
|
|
|
wxSetAssertHandler( KiAssertFilter );
|
|
|
|
#endif
|
2016-05-10 07:11:09 +00:00
|
|
|
// Switch the locale to C locale, to read/write files with fp numbers
|
2018-10-07 14:07:10 +00:00
|
|
|
setlocale( LC_NUMERIC, "C" );
|
2020-02-11 15:01:55 +00:00
|
|
|
#endif
|
2016-05-10 07:11:09 +00:00
|
|
|
}
|
2008-06-06 16:39:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 15:47:16 +00:00
|
|
|
|
2016-05-10 07:11:09 +00:00
|
|
|
LOCALE_IO::~LOCALE_IO()
|
2008-06-06 16:39:45 +00:00
|
|
|
{
|
2016-05-10 07:11:09 +00:00
|
|
|
// use thread safe, atomic operation
|
2016-05-28 16:46:22 +00:00
|
|
|
if( --m_c_count == 0 )
|
2016-05-10 07:11:09 +00:00
|
|
|
{
|
|
|
|
// revert to the user locale
|
2020-02-11 15:01:55 +00:00
|
|
|
#if USE_WXLOCALE
|
|
|
|
delete m_wxLocale; // Deleting m_wxLocale restored previous locale
|
|
|
|
m_wxLocale = nullptr;
|
|
|
|
#else
|
2018-10-07 14:07:10 +00:00
|
|
|
setlocale( LC_NUMERIC, m_user_locale.c_str() );
|
2019-01-15 14:29:33 +00:00
|
|
|
#if defined( _WIN32 ) && defined( DEBUG )
|
2019-01-15 17:23:26 +00:00
|
|
|
// Enable wxWidgets alerts
|
2018-10-27 17:53:52 +00:00
|
|
|
wxSetDefaultAssertHandler();
|
2020-02-11 15:01:55 +00:00
|
|
|
#endif
|
2018-10-27 17:53:52 +00:00
|
|
|
#endif
|
2016-05-10 07:11:09 +00:00
|
|
|
}
|
2008-06-06 16:39:45 +00:00
|
|
|
}
|
|
|
|
|
2008-09-09 11:32:21 +00:00
|
|
|
|
2012-05-06 23:32:01 +00:00
|
|
|
wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
|
|
|
|
{
|
|
|
|
wxCoord width;
|
|
|
|
wxCoord height;
|
|
|
|
|
|
|
|
{
|
|
|
|
wxClientDC dc( aWindow );
|
|
|
|
dc.SetFont( aWindow->GetFont() );
|
|
|
|
dc.GetTextExtent( aSingleLine, &width, &height );
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxSize( width, height );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-10 15:55:05 +00:00
|
|
|
bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
|
2009-01-29 17:30:38 +00:00
|
|
|
{
|
2009-06-13 17:06:07 +00:00
|
|
|
wxWindow* window = aCtrl->GetParent();
|
|
|
|
|
2009-01-29 17:30:38 +00:00
|
|
|
if( !window )
|
|
|
|
window = aCtrl;
|
|
|
|
|
2009-06-13 17:06:07 +00:00
|
|
|
wxString ctrlText;
|
2009-01-29 17:30:38 +00:00
|
|
|
|
|
|
|
if( !aString )
|
|
|
|
{
|
|
|
|
ctrlText = aCtrl->GetValue();
|
2009-06-13 17:06:07 +00:00
|
|
|
aString = &ctrlText;
|
2009-01-29 17:30:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 23:32:01 +00:00
|
|
|
wxSize textz = GetTextSize( *aString, window );
|
|
|
|
wxSize ctrlz = aCtrl->GetSize();
|
2011-11-10 15:55:05 +00:00
|
|
|
|
2012-05-06 23:32:01 +00:00
|
|
|
if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
|
2009-01-29 17:30:38 +00:00
|
|
|
{
|
2012-05-06 23:32:01 +00:00
|
|
|
ctrlz.SetWidth( textz.GetWidth() + 10 );
|
|
|
|
aCtrl->SetSizeHints( ctrlz );
|
2009-01-29 17:30:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-10 15:55:05 +00:00
|
|
|
|
2009-01-29 17:30:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-13 23:06:41 +00:00
|
|
|
void SelectReferenceNumber( wxTextEntry* aTextEntry )
|
|
|
|
{
|
|
|
|
wxString ref = aTextEntry->GetValue();
|
|
|
|
|
|
|
|
if( ref.find_first_of( '?' ) != ref.npos )
|
|
|
|
{
|
|
|
|
aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString num = ref;
|
|
|
|
|
2019-03-14 15:34:05 +00:00
|
|
|
while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
|
2018-10-13 23:06:41 +00:00
|
|
|
{
|
2019-03-14 15:34:05 +00:00
|
|
|
if( !isdigit( num.Last() ) )
|
2018-10-13 23:06:41 +00:00
|
|
|
num.RemoveLast();
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2019-03-14 15:34:05 +00:00
|
|
|
if( !isdigit( num.GetChar ( 0 ) ) )
|
2018-10-13 23:06:41 +00:00
|
|
|
num = num.Right( num.Length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
|
|
|
|
|
|
|
|
if( num.IsEmpty() )
|
|
|
|
aTextEntry->SetSelection( -1, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-15 20:01:53 +00:00
|
|
|
void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
|
2009-04-28 19:34:42 +00:00
|
|
|
{
|
2015-01-15 20:01:53 +00:00
|
|
|
wxString tmp;
|
2009-06-13 17:06:07 +00:00
|
|
|
|
2015-01-15 20:01:53 +00:00
|
|
|
for( unsigned ii = 0; ii < aText.Length(); ii++ )
|
2009-04-28 19:34:42 +00:00
|
|
|
{
|
2015-01-15 20:01:53 +00:00
|
|
|
if( aText[ii] == aSplitter )
|
|
|
|
{
|
|
|
|
aStrings.Add( tmp );
|
|
|
|
tmp.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
tmp << aText[ii];
|
2009-04-28 19:34:42 +00:00
|
|
|
}
|
2009-05-02 07:35:04 +00:00
|
|
|
|
2015-01-15 20:01:53 +00:00
|
|
|
if( !tmp.IsEmpty() )
|
2009-04-28 19:34:42 +00:00
|
|
|
{
|
2015-01-15 20:01:53 +00:00
|
|
|
aStrings.Add( tmp );
|
2009-04-28 19:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-22 05:11:01 +00:00
|
|
|
|
2013-01-18 10:42:23 +00:00
|
|
|
int ProcessExecute( const wxString& aCommandLine, int aFlags, wxProcess *callback )
|
2008-04-24 16:55:35 +00:00
|
|
|
{
|
2020-03-26 11:02:59 +00:00
|
|
|
return (int) wxExecute( aCommandLine, aFlags, callback );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-22 05:11:01 +00:00
|
|
|
|
2018-07-30 13:18:37 +00:00
|
|
|
enum Bracket
|
|
|
|
{
|
|
|
|
Bracket_None,
|
|
|
|
Bracket_Normal = ')',
|
|
|
|
Bracket_Curly = '}',
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
|
|
|
|
#endif
|
|
|
|
Bracket_Max
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-03-26 11:02:59 +00:00
|
|
|
wxString ExpandTextVars( const wxString& aSource,
|
2020-04-05 19:51:48 +00:00
|
|
|
const std::function<bool( wxString* )>* aLocalResolver,
|
2020-08-19 18:21:24 +00:00
|
|
|
const PROJECT* aProject,
|
|
|
|
const std::function<bool( wxString* )>* aFallbackResolver )
|
2020-03-26 11:02:59 +00:00
|
|
|
{
|
|
|
|
wxString newbuf;
|
|
|
|
size_t sourceLen = aSource.length();
|
|
|
|
|
2020-04-24 20:33:59 +00:00
|
|
|
newbuf.Alloc( sourceLen ); // best guess (improves performance)
|
|
|
|
|
2020-03-26 11:02:59 +00:00
|
|
|
for( size_t i = 0; i < sourceLen; ++i )
|
|
|
|
{
|
|
|
|
if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
|
|
|
|
{
|
|
|
|
wxString token;
|
|
|
|
|
|
|
|
for( i = i + 2; i < sourceLen; ++i )
|
|
|
|
{
|
|
|
|
if( aSource[i] == '}' )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
token.append( aSource[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( token.IsEmpty() )
|
|
|
|
continue;
|
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
if( aLocalResolver && (*aLocalResolver)( &token ) )
|
|
|
|
{
|
|
|
|
newbuf.append( token );
|
|
|
|
}
|
|
|
|
else if( aProject && aProject->TextVarResolver( &token ) )
|
2020-03-26 11:02:59 +00:00
|
|
|
{
|
|
|
|
newbuf.append( token );
|
|
|
|
}
|
2020-08-19 18:21:24 +00:00
|
|
|
else if( aFallbackResolver && (*aFallbackResolver)( &token ) )
|
|
|
|
{
|
|
|
|
newbuf.append( token );
|
|
|
|
}
|
2020-03-26 11:02:59 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Token not resolved: leave the reference unchanged
|
|
|
|
newbuf.append( "${" + token + "}" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newbuf.append( aSource[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 13:18:37 +00:00
|
|
|
//
|
|
|
|
// Stolen from wxExpandEnvVars and then heavily optimized
|
|
|
|
//
|
2020-04-05 19:51:48 +00:00
|
|
|
wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject )
|
2018-07-30 13:18:37 +00:00
|
|
|
{
|
|
|
|
size_t strlen = str.length();
|
|
|
|
|
|
|
|
wxString strResult;
|
2020-04-24 20:33:59 +00:00
|
|
|
strResult.Alloc( strlen ); // best guess (improves performance)
|
2018-07-30 13:18:37 +00:00
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
for( size_t n = 0; n < strlen; n++ )
|
|
|
|
{
|
2018-07-30 13:18:37 +00:00
|
|
|
wxUniChar str_n = str[n];
|
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
switch( str_n.GetValue() )
|
|
|
|
{
|
2018-07-30 13:18:37 +00:00
|
|
|
#ifdef __WINDOWS__
|
2019-01-15 17:23:26 +00:00
|
|
|
case wxT( '%' ):
|
2018-07-30 13:18:37 +00:00
|
|
|
#endif // __WINDOWS__
|
2019-01-15 17:23:26 +00:00
|
|
|
case wxT( '$' ):
|
2018-07-30 13:18:37 +00:00
|
|
|
{
|
|
|
|
Bracket bracket;
|
|
|
|
#ifdef __WINDOWS__
|
2019-01-15 17:23:26 +00:00
|
|
|
if( str_n == wxT( '%' ) )
|
2018-07-30 13:18:37 +00:00
|
|
|
bracket = Bracket_Windows;
|
|
|
|
else
|
|
|
|
#endif // __WINDOWS__
|
2019-01-15 17:23:26 +00:00
|
|
|
if( n == strlen - 1 )
|
|
|
|
{
|
2018-07-30 13:18:37 +00:00
|
|
|
bracket = Bracket_None;
|
|
|
|
}
|
2019-01-15 17:23:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
switch( str[n + 1].GetValue() )
|
|
|
|
{
|
|
|
|
case wxT( '(' ):
|
2018-07-30 13:18:37 +00:00
|
|
|
bracket = Bracket_Normal;
|
2018-08-04 10:28:46 +00:00
|
|
|
str_n = str[++n]; // skip the bracket
|
2018-07-30 13:18:37 +00:00
|
|
|
break;
|
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
case wxT( '{' ):
|
2018-07-30 13:18:37 +00:00
|
|
|
bracket = Bracket_Curly;
|
2018-08-04 10:28:46 +00:00
|
|
|
str_n = str[++n]; // skip the bracket
|
2018-07-30 13:18:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bracket = Bracket_None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t m = n + 1;
|
|
|
|
wxUniChar str_m = str[m];
|
|
|
|
|
2020-07-31 22:56:42 +00:00
|
|
|
while( m < strlen && ( wxIsalnum( str_m ) || str_m == wxT( '_' ) || str_m == wxT( ':' ) ) )
|
2018-07-30 13:18:37 +00:00
|
|
|
str_m = str[++m];
|
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
wxString strVarName( str.c_str() + n + 1, m - n - 1 );
|
2018-07-30 13:18:37 +00:00
|
|
|
|
|
|
|
// NB: use wxGetEnv instead of wxGetenv as otherwise variables
|
|
|
|
// set through wxSetEnv may not be read correctly!
|
|
|
|
bool expanded = false;
|
2020-04-05 19:51:48 +00:00
|
|
|
wxString tmp = strVarName;
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
if( aProject && aProject->TextVarResolver( &tmp ) )
|
|
|
|
{
|
|
|
|
strResult += tmp;
|
|
|
|
expanded = true;
|
|
|
|
}
|
|
|
|
else if( wxGetEnv( strVarName, &tmp ) )
|
2018-07-30 13:18:37 +00:00
|
|
|
{
|
|
|
|
strResult += tmp;
|
|
|
|
expanded = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// variable doesn't exist => don't change anything
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
if ( bracket != Bracket_Windows )
|
|
|
|
#endif
|
|
|
|
if ( bracket != Bracket_None )
|
|
|
|
strResult << str[n - 1];
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2018-07-30 13:18:37 +00:00
|
|
|
strResult << str_n << strVarName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the closing bracket
|
2019-01-15 17:23:26 +00:00
|
|
|
if( bracket != Bracket_None )
|
|
|
|
{
|
|
|
|
if( m == strlen || str_m != (wxChar)bracket )
|
|
|
|
{
|
2018-07-30 13:18:37 +00:00
|
|
|
// under MSW it's common to have '%' characters in the registry
|
|
|
|
// and it's annoying to have warnings about them each time, so
|
2019-01-15 17:23:26 +00:00
|
|
|
// ignore them silently if they are not used for env vars
|
2018-07-30 13:18:37 +00:00
|
|
|
//
|
|
|
|
// under Unix, OTOH, this warning could be useful for the user to
|
|
|
|
// understand why isn't the variable expanded as intended
|
|
|
|
#ifndef __WINDOWS__
|
2019-01-15 17:23:26 +00:00
|
|
|
wxLogWarning( _( "Environment variables expansion failed: missing '%c' "
|
|
|
|
"at position %u in '%s'." ),
|
|
|
|
(char)bracket, (unsigned int) (m + 1), str.c_str() );
|
2018-07-30 13:18:37 +00:00
|
|
|
#endif // __WINDOWS__
|
|
|
|
}
|
2019-01-15 17:23:26 +00:00
|
|
|
else
|
|
|
|
{
|
2018-07-30 13:18:37 +00:00
|
|
|
// skip closing bracket unless the variables wasn't expanded
|
2019-01-15 17:23:26 +00:00
|
|
|
if( !expanded )
|
2018-07-30 13:18:37 +00:00
|
|
|
strResult << (wxChar)bracket;
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2019-12-14 21:35:26 +00:00
|
|
|
m++;
|
2018-07-30 13:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = m - 1; // skip variable name
|
2018-08-04 10:28:46 +00:00
|
|
|
str_n = str[n];
|
2018-07-30 13:18:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
case wxT( '\\' ):
|
2018-07-30 13:18:37 +00:00
|
|
|
// backslash can be used to suppress special meaning of % and $
|
2019-01-15 17:23:26 +00:00
|
|
|
if( n != strlen - 1 && (str[n + 1] == wxT( '%' ) || str[n + 1] == wxT( '$' )) )
|
|
|
|
{
|
2018-08-04 10:28:46 +00:00
|
|
|
str_n = str[++n];
|
|
|
|
strResult += str_n;
|
2018-07-30 13:18:37 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2020-04-24 23:44:09 +00:00
|
|
|
KI_FALLTHROUGH;
|
2018-07-30 13:18:37 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
strResult += str_n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 22:45:04 +00:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
if( strResult.StartsWith( "~" ) )
|
|
|
|
strResult.Replace( "~", wxGetHomeDir(), false );
|
|
|
|
#endif // __WINDOWS__
|
|
|
|
|
2018-07-30 13:18:37 +00:00
|
|
|
return strResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
const wxString ExpandEnvVarSubstitutions( const wxString& aString, PROJECT* aProject )
|
2014-12-23 13:01:59 +00:00
|
|
|
{
|
|
|
|
// wxGetenv( wchar_t* ) is not re-entrant on linux.
|
|
|
|
// Put a lock on multithreaded use of wxGetenv( wchar_t* ), called from wxEpandEnvVars(),
|
2019-05-03 17:28:11 +00:00
|
|
|
static std::mutex getenv_mutex;
|
2014-12-23 13:01:59 +00:00
|
|
|
|
2019-05-03 17:28:11 +00:00
|
|
|
std::lock_guard<std::mutex> lock( getenv_mutex );
|
2014-12-23 13:01:59 +00:00
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
// We reserve the right to do this another way, by providing our own member function.
|
2020-04-05 19:51:48 +00:00
|
|
|
return KIwxExpandEnvVars( aString, aProject );
|
2014-12-23 13:01:59 +00:00
|
|
|
}
|
2014-10-26 13:59:01 +00:00
|
|
|
|
2018-03-08 22:57:31 +00:00
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
const wxString ResolveUriByEnvVars( const wxString& aUri, PROJECT* aProject )
|
2018-03-08 22:57:31 +00:00
|
|
|
{
|
2020-04-05 19:51:48 +00:00
|
|
|
wxString uri = ExpandTextVars( aUri, nullptr, aProject );
|
|
|
|
|
2018-03-08 22:57:31 +00:00
|
|
|
// URL-like URI: return as is.
|
2020-04-05 19:51:48 +00:00
|
|
|
wxURL url( uri );
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2018-03-08 22:57:31 +00:00
|
|
|
if( url.GetError() == wxURL_NOERR )
|
2020-04-05 19:51:48 +00:00
|
|
|
return uri;
|
2018-03-08 22:57:31 +00:00
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
// Otherwise, the path points to a local file. Resolve environment variables if any.
|
|
|
|
return ExpandEnvVarSubstitutions( aUri, aProject );
|
2018-03-08 22:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-26 13:59:01 +00:00
|
|
|
bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
|
|
|
|
const wxString& aBaseFilename,
|
|
|
|
REPORTER* aReporter )
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
|
|
|
|
|
|
|
|
// make aTargetFullFileName path, which is relative to aBaseFilename path (if it is not
|
|
|
|
// already an absolute path) absolute:
|
|
|
|
if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
|
|
|
|
{
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _( "Cannot make path \"%s\" absolute with respect to \"%s\"." ),
|
2020-02-28 00:05:40 +00:00
|
|
|
aTargetFullFileName->GetPath(),
|
|
|
|
baseFilePath );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2014-10-26 13:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the path of aTargetFullFileName exists, and create it if needed:
|
|
|
|
wxString outputPath( aTargetFullFileName->GetPath() );
|
|
|
|
|
|
|
|
if( !wxFileName::DirExists( outputPath ) )
|
|
|
|
{
|
|
|
|
if( wxMkdir( outputPath ) )
|
|
|
|
{
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2020-02-28 00:05:40 +00:00
|
|
|
msg.Printf( _( "Output directory \"%s\" created.\n" ), outputPath );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_INFO );
|
2014-10-26 13:59:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2020-02-28 00:05:40 +00:00
|
|
|
msg.Printf( _( "Cannot create output directory \"%s\".\n" ), outputPath );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2014-10-26 13:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 22:13:01 +00:00
|
|
|
#ifdef __WXMAC__
|
|
|
|
wxString GetOSXKicadUserDataDir()
|
|
|
|
{
|
|
|
|
// According to wxWidgets documentation for GetUserDataDir:
|
|
|
|
// Mac: ~/Library/Application Support/appname
|
|
|
|
wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
|
|
|
|
|
|
|
|
// Since appname is different if started via launcher or standalone binary
|
|
|
|
// map all to "kicad" here
|
|
|
|
udir.RemoveLastDir();
|
2019-01-15 17:23:26 +00:00
|
|
|
udir.AppendDir( "kicad" );
|
2014-10-16 22:13:01 +00:00
|
|
|
|
|
|
|
return udir.GetPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString GetOSXKicadMachineDataDir()
|
|
|
|
{
|
|
|
|
return wxT( "/Library/Application Support/kicad" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString GetOSXKicadDataDir()
|
|
|
|
{
|
|
|
|
// According to wxWidgets documentation for GetDataDir:
|
|
|
|
// Mac: appname.app/Contents/SharedSupport bundle subdirectory
|
|
|
|
wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
|
|
|
|
|
|
|
|
// This must be mapped to main bundle for everything but kicad.app
|
|
|
|
const wxArrayString dirs = ddir.GetDirs();
|
|
|
|
if( dirs[dirs.GetCount() - 3] != wxT( "kicad.app" ) )
|
|
|
|
{
|
|
|
|
// Bundle structure resp. current path is
|
|
|
|
// kicad.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
|
|
|
|
// and will be mapped to
|
|
|
|
// kicad.app/Contents/SharedSupprt
|
|
|
|
ddir.RemoveLastDir();
|
|
|
|
ddir.RemoveLastDir();
|
|
|
|
ddir.RemoveLastDir();
|
|
|
|
ddir.RemoveLastDir();
|
|
|
|
ddir.AppendDir( wxT( "SharedSupport" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ddir.GetPath();
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-12 16:45:45 +00:00
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2017-12-18 08:53:14 +00:00
|
|
|
// add this only if it is not in wxWidgets (for instance before 3.1.0)
|
|
|
|
#ifdef USE_KICAD_WXSTRING_HASH
|
2017-12-12 16:45:45 +00:00
|
|
|
size_t std::hash<wxString>::operator()( const wxString& s ) const
|
|
|
|
{
|
|
|
|
return std::hash<std::wstring>{}( s.ToStdWstring() );
|
|
|
|
}
|
2017-12-18 08:53:14 +00:00
|
|
|
#endif
|
2018-04-12 16:09:18 +00:00
|
|
|
|
2019-03-14 15:34:05 +00:00
|
|
|
#ifdef USE_KICAD_WXPOINT_LESS_AND_HASH
|
|
|
|
size_t std::hash<wxPoint>::operator() ( const wxPoint& k ) const
|
|
|
|
{
|
2019-04-10 03:40:04 +00:00
|
|
|
auto xhash = std::hash<int>()( k.x );
|
|
|
|
|
|
|
|
// 0x9e3779b9 is 2^33 / ( 1 + sqrt(5) )
|
|
|
|
// Adding this value ensures that consecutive bits of y will not be close to each other
|
|
|
|
// decreasing the likelihood of hash collision in similar values of x and y
|
|
|
|
return xhash ^ ( std::hash<int>()( k.y ) + 0x9e3779b9 + ( xhash << 6 ) + ( xhash >> 2 ) );
|
2019-03-14 15:34:05 +00:00
|
|
|
}
|
2018-04-12 16:09:18 +00:00
|
|
|
|
|
|
|
bool std::less<wxPoint>::operator()( const wxPoint& aA, const wxPoint& aB ) const
|
|
|
|
{
|
|
|
|
if( aA.x == aB.x )
|
|
|
|
return aA.y < aB.y;
|
|
|
|
|
|
|
|
return aA.x < aB.x;
|
|
|
|
}
|
2018-04-13 09:06:46 +00:00
|
|
|
#endif
|
2018-08-03 16:53:38 +00:00
|
|
|
|
|
|
|
|
2019-05-28 10:57:30 +00:00
|
|
|
std::ostream& operator<<( std::ostream& out, const wxSize& size )
|
|
|
|
{
|
|
|
|
out << " width=\"" << size.GetWidth() << "\" height=\"" << size.GetHeight() << "\"";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream& operator<<( std::ostream& out, const wxPoint& pt )
|
|
|
|
{
|
|
|
|
out << " x=\"" << pt.x << "\" y=\"" << pt.y << "\"";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-08 18:07:26 +00:00
|
|
|
/**
|
|
|
|
* Performance enhancements to file and directory operations.
|
|
|
|
*
|
|
|
|
* Note: while it's annoying to have to make copies of wxWidgets stuff and then
|
2019-01-15 17:23:26 +00:00
|
|
|
* add platform-specific performance optimizations, the following routines offer
|
2018-08-08 18:07:26 +00:00
|
|
|
* SIGNIFICANT performance benefits.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WX_FILENAME
|
|
|
|
*
|
|
|
|
* A wrapper around a wxFileName which avoids expensive calls to wxFileName::SplitPath()
|
|
|
|
* and string concatenations by caching the path and filename locally and only resolving
|
|
|
|
* the wxFileName when it has to.
|
|
|
|
*/
|
2018-08-05 11:56:02 +00:00
|
|
|
WX_FILENAME::WX_FILENAME( const wxString& aPath, const wxString& aFilename ) :
|
|
|
|
m_fn( aPath, aFilename ),
|
|
|
|
m_path( aPath ),
|
|
|
|
m_fullName( aFilename )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2018-08-03 16:53:38 +00:00
|
|
|
void WX_FILENAME::SetFullName( const wxString& aFileNameAndExtension )
|
|
|
|
{
|
|
|
|
m_fullName = aFileNameAndExtension;
|
2018-08-05 11:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString WX_FILENAME::GetName() const
|
|
|
|
{
|
|
|
|
size_t dot = m_fullName.find_last_of( wxT( '.' ) );
|
|
|
|
return m_fullName.substr( 0, dot );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString WX_FILENAME::GetFullName() const
|
|
|
|
{
|
|
|
|
return m_fullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString WX_FILENAME::GetPath() const
|
|
|
|
{
|
|
|
|
return m_path;
|
|
|
|
}
|
|
|
|
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
wxString WX_FILENAME::GetFullPath() const
|
|
|
|
{
|
|
|
|
return m_path + wxT( '/' ) + m_fullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Write locally-cached values to the wxFileName. MUST be called before using m_fn.
|
|
|
|
void WX_FILENAME::resolve()
|
|
|
|
{
|
2018-08-03 16:53:38 +00:00
|
|
|
size_t dot = m_fullName.find_last_of( wxT( '.' ) );
|
|
|
|
m_fn.SetName( m_fullName.substr( 0, dot ) );
|
|
|
|
m_fn.SetExt( m_fullName.substr( dot + 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long long WX_FILENAME::GetTimestamp()
|
|
|
|
{
|
2018-08-05 11:56:02 +00:00
|
|
|
resolve();
|
|
|
|
|
2018-08-03 16:53:38 +00:00
|
|
|
if( m_fn.FileExists() )
|
|
|
|
return m_fn.GetModificationTime().GetValue().GetValue();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-08 18:07:26 +00:00
|
|
|
/**
|
|
|
|
* A copy of wxMatchWild(), which wxWidgets attributes to Douglas A. Lewis
|
|
|
|
* <dalewis@cs.Buffalo.EDU> and ircII's reg.c.
|
|
|
|
*
|
|
|
|
* This version is modified to skip any encoding conversions (for performance).
|
|
|
|
*/
|
2018-08-05 11:56:02 +00:00
|
|
|
bool matchWild( const char* pat, const char* text, bool dot_special )
|
2018-08-03 16:53:38 +00:00
|
|
|
{
|
2019-01-15 17:23:26 +00:00
|
|
|
if( !*text )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
/* Match if both are empty. */
|
|
|
|
return !*pat;
|
|
|
|
}
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
const char *m = pat,
|
|
|
|
*n = text,
|
|
|
|
*ma = NULL,
|
|
|
|
*na = NULL;
|
|
|
|
int just = 0,
|
|
|
|
acount = 0,
|
|
|
|
count = 0;
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
if( dot_special && (*n == '.') )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
/* Never match so that hidden Unix files
|
|
|
|
* are never found. */
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
for(;;)
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
2019-01-15 17:23:26 +00:00
|
|
|
if( *m == '*' )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
ma = ++m;
|
|
|
|
na = n;
|
|
|
|
just = 1;
|
|
|
|
acount = count;
|
|
|
|
}
|
2019-01-15 17:23:26 +00:00
|
|
|
else if( *m == '?' )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
m++;
|
2019-01-15 17:23:26 +00:00
|
|
|
|
|
|
|
if( !*n++ )
|
2018-08-05 11:56:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-15 17:23:26 +00:00
|
|
|
if( *m == '\\' )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
m++;
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
/* Quoting "nothing" is a bad thing */
|
2019-01-15 17:23:26 +00:00
|
|
|
if( !*m )
|
2018-08-05 11:56:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 17:23:26 +00:00
|
|
|
if( !*m )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we are out of both strings or we just
|
|
|
|
* saw a wildcard, then we can say we have a
|
|
|
|
* match
|
|
|
|
*/
|
2019-01-15 17:23:26 +00:00
|
|
|
if( !*n )
|
2018-08-05 11:56:02 +00:00
|
|
|
return true;
|
2019-01-15 17:23:26 +00:00
|
|
|
|
|
|
|
if( just )
|
2018-08-05 11:56:02 +00:00
|
|
|
return true;
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
just = 0;
|
|
|
|
goto not_matched;
|
|
|
|
}
|
2019-01-15 17:23:26 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
/*
|
|
|
|
* We could check for *n == NULL at this point, but
|
|
|
|
* since it's more common to have a character there,
|
|
|
|
* check to see if they match first (m and n) and
|
|
|
|
* then if they don't match, THEN we can check for
|
|
|
|
* the NULL of n
|
|
|
|
*/
|
|
|
|
just = 0;
|
2019-01-15 17:23:26 +00:00
|
|
|
|
|
|
|
if( *m == *n )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
m++;
|
|
|
|
count++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
not_matched:
|
|
|
|
|
|
|
|
/*
|
2019-01-15 17:23:26 +00:00
|
|
|
* If there are no more characters in the
|
|
|
|
* string, but we still need to find another
|
|
|
|
* character (*m != NULL), then it will be
|
|
|
|
* impossible to match it
|
|
|
|
*/
|
|
|
|
if( !*n )
|
2018-08-05 11:56:02 +00:00
|
|
|
return false;
|
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
if( ma )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
|
|
|
m = ma;
|
|
|
|
n = ++na;
|
|
|
|
count = acount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 16:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-08 18:07:26 +00:00
|
|
|
/**
|
|
|
|
* A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function
|
|
|
|
* private to src/common/filename.cpp.
|
|
|
|
*/
|
|
|
|
#if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
|
|
|
|
|
|
|
|
// Convert between wxDateTime and FILETIME which is a 64-bit value representing
|
|
|
|
// the number of 100-nanosecond intervals since January 1, 1601 UTC.
|
|
|
|
//
|
|
|
|
// This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch.
|
|
|
|
static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000);
|
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
|
|
|
|
static void ConvertFileTimeToWx( wxDateTime *dt, const FILETIME &ft )
|
2018-08-08 18:07:26 +00:00
|
|
|
{
|
2019-01-15 17:23:26 +00:00
|
|
|
wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
|
2018-08-08 18:07:26 +00:00
|
|
|
t /= 10000; // Convert hundreds of nanoseconds to milliseconds.
|
|
|
|
t -= EPOCH_OFFSET_IN_MSEC;
|
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
*dt = wxDateTime( t );
|
2018-08-08 18:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // wxUSE_DATETIME && __WIN32__
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TimestampDir
|
|
|
|
*
|
|
|
|
* This routine offers SIGNIFICANT performance benefits over using wxWidgets to gather
|
|
|
|
* timestamps from matching files in a directory.
|
|
|
|
* @param aDirPath the directory to search
|
|
|
|
* @param aFilespec a (wildcarded) file spec to match against
|
|
|
|
* @return a hash of the last-mod-dates of all matching files in the directory
|
|
|
|
*/
|
2018-08-05 11:56:02 +00:00
|
|
|
long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
|
2018-08-03 16:53:38 +00:00
|
|
|
{
|
2018-08-05 11:56:02 +00:00
|
|
|
long long timestamp = 0;
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2019-01-15 17:23:26 +00:00
|
|
|
#if defined( __WIN32__ )
|
2018-08-08 18:07:26 +00:00
|
|
|
// Win32 version.
|
|
|
|
// Save time by not searching for each path twice: once in wxDir.GetNext() and once in
|
|
|
|
// wxFileName.GetModificationTime(). Also cuts out wxWidgets' string-matching and case
|
|
|
|
// conversion by staying on the MSW side of things.
|
|
|
|
std::wstring filespec( aDirPath.t_str() );
|
|
|
|
filespec += '\\';
|
|
|
|
filespec += aFilespec.t_str();
|
|
|
|
|
|
|
|
WIN32_FIND_DATA findData;
|
|
|
|
wxDateTime lastModDate;
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2018-08-08 18:07:26 +00:00
|
|
|
HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
|
|
|
|
|
|
|
|
if( fileHandle != INVALID_HANDLE_VALUE )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
2018-08-08 18:07:26 +00:00
|
|
|
do
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
2018-08-08 18:07:26 +00:00
|
|
|
ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
|
|
|
|
timestamp += lastModDate.GetValue().GetValue();
|
2018-08-05 11:56:02 +00:00
|
|
|
}
|
2019-01-15 17:23:26 +00:00
|
|
|
while ( FindNextFile( fileHandle, &findData ) != 0 );
|
2018-08-05 11:56:02 +00:00
|
|
|
}
|
2018-08-08 18:07:26 +00:00
|
|
|
|
|
|
|
FindClose( fileHandle );
|
2018-08-05 11:56:02 +00:00
|
|
|
#else
|
2018-08-08 18:07:26 +00:00
|
|
|
// POSIX version.
|
|
|
|
// Save time by not converting between encodings -- do everything on the file-system side.
|
2018-08-05 11:56:02 +00:00
|
|
|
std::string filespec( aFilespec.fn_str() );
|
|
|
|
std::string dir_path( aDirPath.fn_str() );
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
DIR* dir = opendir( dir_path.c_str() );
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
if( dir )
|
|
|
|
{
|
|
|
|
for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
|
|
|
|
{
|
|
|
|
if( !matchWild( filespec.c_str(), dir_entry->d_name, true ) )
|
|
|
|
continue;
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
std::string entry_path = dir_path + '/' + dir_entry->d_name;
|
|
|
|
struct stat entry_stat;
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2020-01-13 20:03:53 +00:00
|
|
|
if( wxCRT_Lstat( entry_path.c_str(), &entry_stat ) == 0 )
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
2020-01-13 20:03:53 +00:00
|
|
|
// Timestamp the source file, not the symlink
|
|
|
|
if( S_ISLNK( entry_stat.st_mode ) ) // wxFILE_EXISTS_SYMLINK
|
2018-08-05 11:56:02 +00:00
|
|
|
{
|
2020-01-13 20:03:53 +00:00
|
|
|
char buffer[ PATH_MAX + 1 ];
|
|
|
|
ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
|
|
|
|
|
|
|
|
if( pathLen > 0 )
|
|
|
|
{
|
|
|
|
struct stat linked_stat;
|
|
|
|
buffer[ pathLen ] = '\0';
|
|
|
|
entry_path = dir_path + buffer;
|
|
|
|
|
|
|
|
if( wxCRT_Lstat( entry_path.c_str(), &linked_stat ) == 0 )
|
|
|
|
{
|
|
|
|
entry_stat = linked_stat;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if we couldn't lstat the linked file we'll have to just use
|
|
|
|
// the symbolic link info
|
|
|
|
}
|
|
|
|
}
|
2018-08-05 11:56:02 +00:00
|
|
|
}
|
2018-08-03 16:53:38 +00:00
|
|
|
|
2020-01-13 20:03:53 +00:00
|
|
|
if( S_ISREG( entry_stat.st_mode ) ) // wxFileExists()
|
|
|
|
timestamp += entry_stat.st_mtime * 1000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if we couldn't lstat the file itself all we can do is use the name
|
|
|
|
timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
|
|
|
|
}
|
2018-08-05 11:56:02 +00:00
|
|
|
}
|
2018-08-21 17:50:55 +00:00
|
|
|
|
|
|
|
closedir( dir );
|
2018-08-05 11:56:02 +00:00
|
|
|
}
|
2018-08-03 16:53:38 +00:00
|
|
|
#endif
|
|
|
|
|
2018-08-05 11:56:02 +00:00
|
|
|
return timestamp;
|
|
|
|
}
|