KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gestfich.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2004 Jean-Pierre Charras, [email protected]
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
30
31#include <wx/mimetype.h>
32#include <wx/dir.h>
33
34#include <pgm_base.h>
35#include <confirm.h>
36#include <core/arraydim.h>
37#include <gestfich.h>
38#include <string_utils.h>
39#include <launch_ext.h>
40#include "wx/tokenzr.h"
41#include <richio.h>
42#include <sexpr/sexpr.h>
43#include <sexpr/sexpr_parser.h>
44
45#include <wx/wfstream.h>
46#include <wx/fs_zip.h>
47#include <wx/zipstrm.h>
48
49#include <filesystem>
50#include <core/kicad_algo.h>
51
52void QuoteString( wxString& string )
53{
54 if( !string.StartsWith( wxT( "\"" ) ) )
55 {
56 string.Prepend ( wxT( "\"" ) );
57 string.Append ( wxT( "\"" ) );
58 }
59}
60
61
62wxString FindKicadFile( const wxString& shortname )
63{
64 // Test the presence of the file in the directory shortname of
65 // the KiCad binary path.
66#ifndef __WXMAC__
67 wxString fullFileName = Pgm().GetExecutablePath() + shortname;
68#else
69 wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
70#endif
71 if( wxFileExists( fullFileName ) )
72 return fullFileName;
73
74 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
75 {
76 wxFileName buildDir( Pgm().GetExecutablePath(), shortname );
77
78#ifdef __WXMAC__
79 if( !buildDir.GetDirs().IsEmpty()
80 && buildDir.GetDirs().Last().Lower().EndsWith( wxT( ".app" ) ) )
81 {
82 buildDir.RemoveLastDir();
83 }
84#endif
85 buildDir.RemoveLastDir();
86#ifndef __WXMSW__
87 buildDir.AppendDir( shortname );
88#else
89 buildDir.AppendDir( shortname.BeforeLast( '.' ) );
90#endif
91
92 if( buildDir.GetDirs().Last() == "pl_editor" )
93 {
94 buildDir.RemoveLastDir();
95 buildDir.AppendDir( "pagelayout_editor" );
96 }
97
98#ifdef __WXMAC__
99 buildDir.AppendDir( shortname + wxT( ".app" ) );
100 buildDir.AppendDir( wxT( "Contents" ) );
101 buildDir.AppendDir( wxT( "MacOS" ) );
102#endif
103
104 if( wxFileExists( buildDir.GetFullPath() ) )
105 return buildDir.GetFullPath();
106 }
107
108 // Test the presence of the file in the directory shortname
109 // defined by the environment variable KiCad.
110 if( Pgm().IsKicadEnvVariableDefined() )
111 {
112 fullFileName = Pgm().GetKicadEnvVariable() + shortname;
113
114 if( wxFileExists( fullFileName ) )
115 return fullFileName;
116 }
117
118#if defined( __WINDOWS__ )
119 // KiCad can be installed highly portably on Windows, anywhere and concurrently
120 // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
121 return shortname;
122#else
123
124 // Path list for KiCad binary files
125 const static wxChar* possibilities[] = {
126#if defined( __WXMAC__ )
127 // all internal paths are relative to main bundle kicad.app
128 wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
129 wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
130 wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
131 wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
132 wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
133 wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
134#else
135 wxT( "/usr/bin/" ),
136 wxT( "/usr/local/bin/" ),
137 wxT( "/usr/local/kicad/bin/" ),
138#endif
139 };
140
141 // find binary file from possibilities list:
142 for( unsigned i=0; i<arrayDim(possibilities); ++i )
143 {
144#ifndef __WXMAC__
145 fullFileName = possibilities[i] + shortname;
146#else
147 // make relative paths absolute
148 fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
149#endif
150
151 if( wxFileExists( fullFileName ) )
152 return fullFileName;
153 }
154
155 return shortname;
156
157#endif
158}
159
160
161int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess* aCallback,
162 bool aFileForKicad )
163{
164 wxString fullEditorName;
165 std::vector<wxString> params;
166
167#ifdef __UNIX__
168 wxString param;
169 bool inSingleQuotes = false;
170 bool inDoubleQuotes = false;
171
172 auto pushParam =
173 [&]()
174 {
175 if( !param.IsEmpty() )
176 {
177 params.push_back( param );
178 param.clear();
179 }
180 };
181
182 for( wxUniChar ch : aEditorName )
183 {
184 if( inSingleQuotes )
185 {
186 if( ch == '\'' )
187 {
188 pushParam();
189 inSingleQuotes = false;
190 continue;
191 }
192 else
193 {
194 param += ch;
195 }
196 }
197 else if( inDoubleQuotes )
198 {
199 if( ch == '"' )
200 {
201 pushParam();
202 inDoubleQuotes = false;
203 }
204 else
205 {
206 param += ch;
207 }
208 }
209 else if( ch == '\'' )
210 {
211 pushParam();
212 inSingleQuotes = true;
213 }
214 else if( ch == '"' )
215 {
216 pushParam();
217 inDoubleQuotes = true;
218 }
219 else if( ch == ' ' )
220 {
221 pushParam();
222 }
223 else
224 {
225 param += ch;
226 }
227 }
228
229 pushParam();
230
231 if( aFileForKicad )
232 fullEditorName = FindKicadFile( params[0] );
233 else
234 fullEditorName = params[0];
235
236 params.erase( params.begin() );
237#else
238
239 if( aFileForKicad )
240 fullEditorName = FindKicadFile( aEditorName );
241 else
242 fullEditorName = aEditorName;
243#endif
244
245 if( wxFileExists( fullEditorName ) )
246 {
247 std::vector<const wchar_t*> args;
248
249 args.emplace_back( fullEditorName.wc_str() );
250
251 if( !params.empty() )
252 {
253 for( const wxString& p : params )
254 args.emplace_back( p.wc_str() );
255 }
256
257 if( !aFileName.IsEmpty() )
258 args.emplace_back( aFileName.wc_str() );
259
260 args.emplace_back( nullptr );
261
262 return wxExecute( const_cast<wchar_t**>( args.data() ), wxEXEC_ASYNC, aCallback );
263 }
264
265 wxString msg;
266 msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
267 DisplayErrorMessage( nullptr, msg );
268 return -1;
269}
270
271
272bool OpenPDF( const wxString& file )
273{
274 wxString msg;
275 wxString filename = file;
276
278
279 if( Pgm().UseSystemPdfBrowser() )
280 {
281 if( !LaunchExternal( filename ) )
282 {
283 msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
284 DisplayErrorMessage( nullptr, msg );
285 return false;
286 }
287 }
288 else
289 {
290 const wchar_t* args[3];
291
292 args[0] = Pgm().GetPdfBrowserName().wc_str();
293 args[1] = filename.wc_str();
294 args[2] = nullptr;
295
296 if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
297 {
298 msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
299 DisplayErrorMessage( nullptr, msg );
300 return false;
301 }
302 }
303
304 return true;
305}
306
307
308void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
309{
310 if( !wxCopyFile( aSrcPath, aDestPath ) )
311 {
312 wxString msg;
313
314 if( !aErrors.IsEmpty() )
315 aErrors += "\n";
316
317 msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
318 aErrors += msg;
319 }
320}
321
322
323static void traverseSEXPR( SEXPR::SEXPR* aNode, const std::function<void( SEXPR::SEXPR* )>& aVisitor )
324{
325 aVisitor( aNode );
326
327 if( aNode->IsList() )
328 {
329 for( unsigned i = 0; i < aNode->GetNumberOfChildren(); i++ )
330 traverseSEXPR( aNode->GetChild( i ), aVisitor );
331 }
332}
333
334
335void CopySexprFile( const wxString& aSrcPath, const wxString& aDestPath,
336 std::function<bool( const std::string& token, wxString& value )> aCallback,
337 wxString& aErrors )
338{
339 bool success = false;
340
341 try
342 {
343 SEXPR::PARSER parser;
344 std::unique_ptr<SEXPR::SEXPR> sexpr( parser.ParseFromFile( TO_UTF8( aSrcPath ) ) );
345
346 traverseSEXPR( sexpr.get(),
347 [&]( SEXPR::SEXPR* node )
348 {
349 if( node->IsList() && node->GetNumberOfChildren() > 1 && node->GetChild( 0 )->IsSymbol() )
350 {
351 std::string token = node->GetChild( 0 )->GetSymbol();
352 SEXPR::SEXPR_STRING* pathNode = dynamic_cast<SEXPR::SEXPR_STRING*>( node->GetChild( 1 ) );
353 SEXPR::SEXPR_SYMBOL* symNode = dynamic_cast<SEXPR::SEXPR_SYMBOL*>( node->GetChild( 1 ) );
354 wxString path;
355
356 if( pathNode )
357 path = pathNode->m_value;
358 else if( symNode )
359 path = symNode->m_value;
360
361 if( aCallback( token, path ) )
362 {
363 if( pathNode )
364 pathNode->m_value = path;
365 else if( symNode )
366 symNode->m_value = path;
367 }
368 }
369 } );
370
371
372 // Pass through the pretifier to ensure format is the same as when a file is saved by a frame
373 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( aDestPath );
374
375 // Format as a string to prevent format string attacks
376 formatter.Print( "%s", sexpr->AsString( 0 ).c_str() );
377
378 success = formatter.Finish();
379 }
380 catch( ... )
381 {
382 success = false;
383 }
384
385 if( !success )
386 {
387 wxString msg;
388
389 if( !aErrors.empty() )
390 aErrors += wxS( "\n" );
391
392 msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
393 aErrors += msg;
394 }
395}
396
397
398wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
399{
400 return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
401}
402
403
404bool RmDirRecursive( const wxString& aFileName, wxString* aErrors )
405{
406 namespace fs = std::filesystem;
407
408 std::string rmDir = aFileName.ToStdString();
409
410 if( rmDir.length() < 3 )
411 {
412 if( aErrors )
413 *aErrors = _( "Invalid directory name, cannot remove root" );
414
415 return false;
416 }
417
418 if( !fs::exists( rmDir ) )
419 {
420 if( aErrors )
421 *aErrors = wxString::Format( _( "Directory '%s' does not exist" ), aFileName );
422
423 return false;
424 }
425
426 fs::path path( rmDir );
427
428 if( !fs::is_directory( path ) )
429 {
430 if( aErrors )
431 *aErrors = wxString::Format( _( "'%s' is not a directory" ), aFileName );
432
433 return false;
434 }
435
436 try
437 {
438 fs::remove_all( path );
439 }
440 catch( const fs::filesystem_error& e )
441 {
442 if( aErrors )
443 *aErrors = wxString::Format( _( "Error removing directory '%s': %s" ), aFileName, e.what() );
444
445 return false;
446 }
447
448 return true;
449}
450
451
452bool CopyDirectory( const wxString& aSourceDir, const wxString& aDestDir,
453 const std::vector<wxString>& aPathsWithOverwriteDisallowed, wxString& aErrors )
454{
455 wxDir dir( aSourceDir );
456
457 if( !dir.IsOpened() )
458 {
459 aErrors += wxString::Format( _( "Could not open source directory: %s" ), aSourceDir );
460 aErrors += wxT( "\n" );
461 return false;
462 }
463
464 if( !wxFileName::Mkdir( aDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
465 {
466 aErrors += wxString::Format( _( "Could not create destination directory: %s" ), aDestDir );
467 aErrors += wxT( "\n" );
468 return false;
469 }
470
471 wxString filename;
472 bool cont = dir.GetFirst( &filename );
473
474 while( cont )
475 {
476 wxString sourcePath = dir.GetNameWithSep() + filename;
477 wxString destPath = aDestDir + wxFileName::GetPathSeparator() + filename;
478
479 if( wxFileName::DirExists( sourcePath ) )
480 {
481 // Recursively copy subdirectories
482 if( !CopyDirectory( sourcePath, destPath, aPathsWithOverwriteDisallowed, aErrors ) )
483 return false;
484 }
485 else
486 {
487 // Copy files
488 if( alg::contains( aPathsWithOverwriteDisallowed, sourcePath ) && wxFileExists( destPath ) )
489 {
490 // Presumably user does not want an error on a no-overwrite condition....
491 }
492 else if( !wxCopyFile( sourcePath, destPath ) )
493 {
494 aErrors += wxString::Format( _( "Could not copy file: %s to %s" ), sourcePath, destPath );
495 return false;
496 }
497 }
498
499 cont = dir.GetNext( &filename );
500 }
501
502 return true;
503}
504
505
506bool CopyFilesOrDirectory( const wxString& aSourcePath, const wxString& aDestDir, bool aAllowOverwrites,
507 wxString& aErrors, std::vector<wxString>& aPathsWritten )
508{
509 // Parse source path and determine if it's a directory
510 wxFileName sourceFn( aSourcePath );
511 wxString sourcePath = sourceFn.GetFullPath();
512 bool isSourceDirectory = wxFileName::DirExists( sourcePath );
513 wxString baseDestDir = aDestDir;
514
515 auto performCopy =
516 [&]( const wxString& src, const wxString& dest ) -> bool
517 {
518 if( wxCopyFile( src, dest, aAllowOverwrites ) )
519 {
520 aPathsWritten.push_back( dest );
521 return true;
522 }
523
524 aErrors += wxString::Format( _( "Could not copy file: %s to %s" ), src, dest );
525 aErrors += wxT( "\n" );
526 return false;
527 };
528
529 auto processEntries =
530 [&]( const wxString& srcDir, const wxString& pattern, const wxString& destDir ) -> bool
531 {
532 wxDir dir( srcDir );
533
534 if( !dir.IsOpened() )
535 {
536 aErrors += wxString::Format( _( "Could not open source directory: %s" ), srcDir );
537 aErrors += wxT( "\n" );
538 return false;
539 }
540
541 wxString filename;
542 bool success = true;
543
544 // Find all entries matching pattern (files + directories + hidden items)
545 bool cont = dir.GetFirst( &filename, pattern, wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN );
546
547 while( cont )
548 {
549 const wxString entrySrc = srcDir + wxFileName::GetPathSeparator() + filename;
550 const wxString entryDest = destDir + wxFileName::GetPathSeparator() + filename;
551
552 if( !filename.Matches( wxT( "~*.lck" ) ) && !filename.Matches( wxT( "*.lck" ) ) )
553 {
554 if( wxFileName::DirExists( entrySrc ) )
555 {
556 // Recursively process subdirectories
557 if( !CopyFilesOrDirectory( entrySrc, destDir, aAllowOverwrites, aErrors, aPathsWritten ) )
558 {
559 aErrors += wxString::Format( _( "Could not copy directory: %s to %s" ),
560 entrySrc, entryDest );
561 aErrors += wxT( "\n" );
562
563 success = false;
564 }
565 }
566 else
567 {
568 // Copy individual files
569 if( !performCopy( entrySrc, entryDest ) )
570 {
571 success = false;
572 }
573 }
574 }
575
576 cont = dir.GetNext( &filename );
577 }
578
579 return success;
580 };
581
582 // If copying a directory, append its name to destination path
583 if( isSourceDirectory )
584 {
585 wxString sourceDirName = sourceFn.GetFullName();
586 baseDestDir = wxFileName( aDestDir, sourceDirName ).GetFullPath();
587 }
588
589 // Create destination directory hierarchy
590 if( !wxFileName::Mkdir( baseDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
591 {
592 aErrors += wxString::Format( _( "Could not create destination directory: %s" ), baseDestDir );
593 aErrors += wxT( "\n" );
594
595 return false;
596 }
597
598 // Execute appropriate copy operation based on source type
599 if( !isSourceDirectory )
600 {
601 const wxString fileName = sourceFn.GetFullName();
602
603 // Handle wildcard patterns in filenames
604 if( fileName.Contains( '*' ) || fileName.Contains( '?' ) )
605 {
606 const wxString dirPath = sourceFn.GetPath();
607
608 if( !wxFileName::DirExists( dirPath ) )
609 {
610 aErrors += wxString::Format( _( "Source directory does not exist: %s" ), dirPath );
611 aErrors += wxT( "\n" );
612
613 return false;
614 }
615
616 // Process all matching files in source directory
617 return processEntries( dirPath, fileName, baseDestDir );
618 }
619
620 // Single file copy operation
621 return performCopy( sourcePath, wxFileName( baseDestDir, fileName ).GetFullPath() );
622 }
623
624 // Full directory copy operation
625 return processEntries( sourcePath, wxEmptyString, baseDestDir );
626}
627
628
629bool AddDirectoryToZip( wxZipOutputStream& aZip, const wxString& aSourceDir, wxString& aErrors,
630 const wxString& aParentDir )
631{
632 wxDir dir( aSourceDir );
633
634 if( !dir.IsOpened() )
635 {
636 aErrors += wxString::Format( _( "Could not open source directory: %s" ), aSourceDir );
637 aErrors += "\n";
638 return false;
639 }
640
641 wxString filename;
642 bool cont = dir.GetFirst( &filename );
643
644 while( cont )
645 {
646 wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
647 wxString zipPath = aParentDir + filename;
648
649 if( wxFileName::DirExists( sourcePath ) )
650 {
651 // Add directory entry to the ZIP file
652 aZip.PutNextDirEntry( zipPath + "/" );
653
654 // Recursively add subdirectories
655 if( !AddDirectoryToZip( aZip, sourcePath, aErrors, zipPath + "/" ) )
656 return false;
657 }
658 else
659 {
660 // Add file entry to the ZIP file
661 aZip.PutNextEntry( zipPath );
662 wxFFileInputStream fileStream( sourcePath );
663
664 if( !fileStream.IsOk() )
665 {
666 aErrors += wxString::Format( _( "Could not read file: %s" ), sourcePath );
667 return false;
668 }
669
670 aZip.Write( fileStream );
671 }
672
673 cont = dir.GetNext( &filename );
674 }
675
676 return true;
677}
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition arraydim.h:31
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:426
virtual const wxString & GetKicadEnvVariable() const
Definition pgm_base.h:179
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition pgm_base.cpp:873
virtual const wxString & GetPdfBrowserName() const
Definition pgm_base.h:185
virtual const wxString & GetExecutablePath() const
Definition pgm_base.cpp:867
bool Finish() override
Runs prettification over the buffered bytes, writes them to the sibling temp file,...
Definition richio.cpp:700
std::unique_ptr< SEXPR > ParseFromFile(const std::string &aFilename)
size_t GetNumberOfChildren() const
Definition sexpr.cpp:72
bool IsList() const
Definition sexpr.h:49
SEXPR * GetChild(size_t aIndex) const
Definition sexpr.cpp:50
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:221
This file is part of the common library.
#define _(s)
wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
Definition gestfich.cpp:62
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition gestfich.cpp:398
void CopySexprFile(const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
Definition gestfich.cpp:335
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition gestfich.cpp:272
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:308
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
Definition gestfich.cpp:161
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Remove the directory aDirName and all its contents including subdirectories and their files.
Definition gestfich.cpp:404
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition gestfich.cpp:52
bool CopyFilesOrDirectory(const wxString &aSourcePath, const wxString &aDestDir, bool aAllowOverwrites, wxString &aErrors, std::vector< wxString > &aPathsWritten)
Definition gestfich.cpp:506
static void traverseSEXPR(SEXPR::SEXPR *aNode, const std::function< void(SEXPR::SEXPR *)> &aVisitor)
Definition gestfich.cpp:323
bool CopyDirectory(const wxString &aSourceDir, const wxString &aDestDir, const std::vector< wxString > &aPathsWithOverwriteDisallowed, wxString &aErrors)
Copy a directory and its contents to another directory.
Definition gestfich.cpp:452
bool AddDirectoryToZip(wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir)
Add a directory and its contents to a zip file.
Definition gestfich.cpp:629
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:100
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
std::string path