You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

660 lines
19 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
19 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
19 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
  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. /**
  26. * @file gestfich.cpp
  27. * @brief Functions for file management
  28. */
  29. #include <wx/mimetype.h>
  30. #include <wx/dir.h>
  31. #include <pgm_base.h>
  32. #include <confirm.h>
  33. #include <core/arraydim.h>
  34. #include <gestfich.h>
  35. #include <string_utils.h>
  36. #include <launch_ext.h>
  37. #include "wx/tokenzr.h"
  38. #include <sexpr/sexpr.h>
  39. #include <sexpr/sexpr_parser.h>
  40. #include <wx/wfstream.h>
  41. #include <wx/fs_zip.h>
  42. #include <wx/zipstrm.h>
  43. #include <filesystem>
  44. #include <core/kicad_algo.h>
  45. void QuoteString( wxString& string )
  46. {
  47. if( !string.StartsWith( wxT( "\"" ) ) )
  48. {
  49. string.Prepend ( wxT( "\"" ) );
  50. string.Append ( wxT( "\"" ) );
  51. }
  52. }
  53. wxString FindKicadFile( const wxString& shortname )
  54. {
  55. // Test the presence of the file in the directory shortname of
  56. // the KiCad binary path.
  57. #ifndef __WXMAC__
  58. wxString fullFileName = Pgm().GetExecutablePath() + shortname;
  59. #else
  60. wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
  61. #endif
  62. if( wxFileExists( fullFileName ) )
  63. return fullFileName;
  64. if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
  65. {
  66. wxFileName buildDir( Pgm().GetExecutablePath(), shortname );
  67. buildDir.RemoveLastDir();
  68. #ifndef __WXMSW__
  69. buildDir.AppendDir( shortname );
  70. #else
  71. buildDir.AppendDir( shortname.BeforeLast( '.' ) );
  72. #endif
  73. if( buildDir.GetDirs().Last() == "pl_editor" )
  74. {
  75. buildDir.RemoveLastDir();
  76. buildDir.AppendDir( "pagelayout_editor" );
  77. }
  78. if( wxFileExists( buildDir.GetFullPath() ) )
  79. return buildDir.GetFullPath();
  80. }
  81. // Test the presence of the file in the directory shortname
  82. // defined by the environment variable KiCad.
  83. if( Pgm().IsKicadEnvVariableDefined() )
  84. {
  85. fullFileName = Pgm().GetKicadEnvVariable() + shortname;
  86. if( wxFileExists( fullFileName ) )
  87. return fullFileName;
  88. }
  89. #if defined( __WINDOWS__ )
  90. // KiCad can be installed highly portably on Windows, anywhere and concurrently
  91. // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
  92. return shortname;
  93. #else
  94. // Path list for KiCad binary files
  95. const static wxChar* possibilities[] = {
  96. #if defined( __WXMAC__ )
  97. // all internal paths are relative to main bundle kicad.app
  98. wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
  99. wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
  100. wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
  101. wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
  102. wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
  103. wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
  104. #else
  105. wxT( "/usr/bin/" ),
  106. wxT( "/usr/local/bin/" ),
  107. wxT( "/usr/local/kicad/bin/" ),
  108. #endif
  109. };
  110. // find binary file from possibilities list:
  111. for( unsigned i=0; i<arrayDim(possibilities); ++i )
  112. {
  113. #ifndef __WXMAC__
  114. fullFileName = possibilities[i] + shortname;
  115. #else
  116. // make relative paths absolute
  117. fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
  118. #endif
  119. if( wxFileExists( fullFileName ) )
  120. return fullFileName;
  121. }
  122. return shortname;
  123. #endif
  124. }
  125. int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess* aCallback,
  126. bool aFileForKicad )
  127. {
  128. wxString fullEditorName;
  129. std::vector<wxString> params;
  130. #ifdef __UNIX__
  131. wxString param;
  132. bool inSingleQuotes = false;
  133. bool inDoubleQuotes = false;
  134. auto pushParam =
  135. [&]()
  136. {
  137. if( !param.IsEmpty() )
  138. {
  139. params.push_back( param );
  140. param.clear();
  141. }
  142. };
  143. for( wxUniChar ch : aEditorName )
  144. {
  145. if( inSingleQuotes )
  146. {
  147. if( ch == '\'' )
  148. {
  149. pushParam();
  150. inSingleQuotes = false;
  151. continue;
  152. }
  153. else
  154. {
  155. param += ch;
  156. }
  157. }
  158. else if( inDoubleQuotes )
  159. {
  160. if( ch == '"' )
  161. {
  162. pushParam();
  163. inDoubleQuotes = false;
  164. }
  165. else
  166. {
  167. param += ch;
  168. }
  169. }
  170. else if( ch == '\'' )
  171. {
  172. pushParam();
  173. inSingleQuotes = true;
  174. }
  175. else if( ch == '"' )
  176. {
  177. pushParam();
  178. inDoubleQuotes = true;
  179. }
  180. else if( ch == ' ' )
  181. {
  182. pushParam();
  183. }
  184. else
  185. {
  186. param += ch;
  187. }
  188. }
  189. pushParam();
  190. if( aFileForKicad )
  191. fullEditorName = FindKicadFile( params[0] );
  192. else
  193. fullEditorName = params[0];
  194. params.erase( params.begin() );
  195. #else
  196. if( aFileForKicad )
  197. fullEditorName = FindKicadFile( aEditorName );
  198. else
  199. fullEditorName = aEditorName;
  200. #endif
  201. if( wxFileExists( fullEditorName ) )
  202. {
  203. std::vector<const wchar_t*> args;
  204. args.emplace_back( fullEditorName.wc_str() );
  205. if( !params.empty() )
  206. {
  207. for( const wxString& p : params )
  208. args.emplace_back( p.wc_str() );
  209. }
  210. if( !aFileName.IsEmpty() )
  211. args.emplace_back( aFileName.wc_str() );
  212. args.emplace_back( nullptr );
  213. return wxExecute( const_cast<wchar_t**>( args.data() ), wxEXEC_ASYNC, aCallback );
  214. }
  215. wxString msg;
  216. msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
  217. DisplayErrorMessage( nullptr, msg );
  218. return -1;
  219. }
  220. bool OpenPDF( const wxString& file )
  221. {
  222. wxString msg;
  223. wxString filename = file;
  224. Pgm().ReadPdfBrowserInfos();
  225. if( Pgm().UseSystemPdfBrowser() )
  226. {
  227. if( !LaunchExternal( filename ) )
  228. {
  229. msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
  230. DisplayErrorMessage( nullptr, msg );
  231. return false;
  232. }
  233. }
  234. else
  235. {
  236. const wchar_t* args[3];
  237. args[0] = Pgm().GetPdfBrowserName().wc_str();
  238. args[1] = filename.wc_str();
  239. args[2] = nullptr;
  240. if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
  241. {
  242. msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
  243. DisplayErrorMessage( nullptr, msg );
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
  250. {
  251. if( !wxCopyFile( aSrcPath, aDestPath ) )
  252. {
  253. wxString msg;
  254. if( !aErrors.IsEmpty() )
  255. aErrors += "\n";
  256. msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
  257. aErrors += msg;
  258. }
  259. }
  260. static void traverseSEXPR( SEXPR::SEXPR* aNode, const std::function<void( SEXPR::SEXPR* )>& aVisitor )
  261. {
  262. aVisitor( aNode );
  263. if( aNode->IsList() )
  264. {
  265. for( unsigned i = 0; i < aNode->GetNumberOfChildren(); i++ )
  266. traverseSEXPR( aNode->GetChild( i ), aVisitor );
  267. }
  268. }
  269. void CopySexprFile( const wxString& aSrcPath, const wxString& aDestPath,
  270. std::function<bool( const std::string& token, wxString& value )> aCallback,
  271. wxString& aErrors )
  272. {
  273. bool success = false;
  274. try
  275. {
  276. SEXPR::PARSER parser;
  277. std::unique_ptr<SEXPR::SEXPR> sexpr( parser.ParseFromFile( TO_UTF8( aSrcPath ) ) );
  278. traverseSEXPR( sexpr.get(),
  279. [&]( SEXPR::SEXPR* node )
  280. {
  281. if( node->IsList() && node->GetNumberOfChildren() > 1 && node->GetChild( 0 )->IsSymbol() )
  282. {
  283. std::string token = node->GetChild( 0 )->GetSymbol();
  284. SEXPR::SEXPR_STRING* pathNode = dynamic_cast<SEXPR::SEXPR_STRING*>( node->GetChild( 1 ) );
  285. SEXPR::SEXPR_SYMBOL* symNode = dynamic_cast<SEXPR::SEXPR_SYMBOL*>( node->GetChild( 1 ) );
  286. wxString path;
  287. if( pathNode )
  288. path = pathNode->m_value;
  289. else if( symNode )
  290. path = symNode->m_value;
  291. if( aCallback( token, path ) )
  292. {
  293. if( pathNode )
  294. pathNode->m_value = path;
  295. else if( symNode )
  296. symNode->m_value = path;
  297. }
  298. }
  299. } );
  300. wxFFile destFile( aDestPath, "wb" );
  301. if( destFile.IsOpened() )
  302. success = destFile.Write( sexpr->AsString( 0 ) );
  303. // wxFFile dtor will close the file
  304. }
  305. catch( ... )
  306. {
  307. success = false;
  308. }
  309. if( !success )
  310. {
  311. wxString msg;
  312. if( !aErrors.empty() )
  313. aErrors += wxS( "\n" );
  314. msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
  315. aErrors += msg;
  316. }
  317. }
  318. wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
  319. {
  320. return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
  321. }
  322. bool RmDirRecursive( const wxString& aFileName, wxString* aErrors )
  323. {
  324. namespace fs = std::filesystem;
  325. std::string rmDir = aFileName.ToStdString();
  326. if( rmDir.length() < 3 )
  327. {
  328. if( aErrors )
  329. *aErrors = _( "Invalid directory name, cannot remove root" );
  330. return false;
  331. }
  332. if( !fs::exists( rmDir ) )
  333. {
  334. if( aErrors )
  335. *aErrors = wxString::Format( _( "Directory '%s' does not exist" ), aFileName );
  336. return false;
  337. }
  338. fs::path path( rmDir );
  339. if( !fs::is_directory( path ) )
  340. {
  341. if( aErrors )
  342. *aErrors = wxString::Format( _( "'%s' is not a directory" ), aFileName );
  343. return false;
  344. }
  345. try
  346. {
  347. fs::remove_all( path );
  348. }
  349. catch( const fs::filesystem_error& e )
  350. {
  351. if( aErrors )
  352. *aErrors = wxString::Format( _( "Error removing directory '%s': %s" ), aFileName, e.what() );
  353. return false;
  354. }
  355. return true;
  356. }
  357. bool CopyDirectory( const wxString& aSourceDir, const wxString& aDestDir,
  358. const std::vector<wxString>& aPathsWithOverwriteDisallowed, wxString& aErrors )
  359. {
  360. wxDir dir( aSourceDir );
  361. if( !dir.IsOpened() )
  362. {
  363. aErrors += wxString::Format( _( "Could not open source directory: %s" ), aSourceDir );
  364. aErrors += wxT( "\n" );
  365. return false;
  366. }
  367. if( !wxFileName::Mkdir( aDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
  368. {
  369. aErrors += wxString::Format( _( "Could not create destination directory: %s" ), aDestDir );
  370. aErrors += wxT( "\n" );
  371. return false;
  372. }
  373. wxString filename;
  374. bool cont = dir.GetFirst( &filename );
  375. while( cont )
  376. {
  377. wxString sourcePath = dir.GetNameWithSep() + filename;
  378. wxString destPath = aDestDir + wxFileName::GetPathSeparator() + filename;
  379. if( wxFileName::DirExists( sourcePath ) )
  380. {
  381. // Recursively copy subdirectories
  382. if( !CopyDirectory( sourcePath, destPath, aPathsWithOverwriteDisallowed, aErrors ) )
  383. return false;
  384. }
  385. else
  386. {
  387. // Copy files
  388. if( alg::contains( aPathsWithOverwriteDisallowed, sourcePath ) && wxFileExists( destPath ) )
  389. {
  390. // Presumably user does not want an error on a no-overwrite condition....
  391. }
  392. else if( !wxCopyFile( sourcePath, destPath ) )
  393. {
  394. aErrors += wxString::Format( _( "Could not copy file: %s to %s" ), sourcePath, destPath );
  395. return false;
  396. }
  397. }
  398. cont = dir.GetNext( &filename );
  399. }
  400. return true;
  401. }
  402. bool CopyFilesOrDirectory( const wxString& aSourcePath, const wxString& aDestDir, bool aAllowOverwrites,
  403. wxString& aErrors, std::vector<wxString>& aPathsWritten )
  404. {
  405. // Parse source path and determine if it's a directory
  406. wxFileName sourceFn( aSourcePath );
  407. wxString sourcePath = sourceFn.GetFullPath();
  408. bool isSourceDirectory = wxFileName::DirExists( sourcePath );
  409. wxString baseDestDir = aDestDir;
  410. auto performCopy =
  411. [&]( const wxString& src, const wxString& dest ) -> bool
  412. {
  413. if( wxCopyFile( src, dest, aAllowOverwrites ) )
  414. {
  415. aPathsWritten.push_back( dest );
  416. return true;
  417. }
  418. aErrors += wxString::Format( _( "Could not copy file: %s to %s" ), src, dest );
  419. aErrors += wxT( "\n" );
  420. return false;
  421. };
  422. auto processEntries =
  423. [&]( const wxString& srcDir, const wxString& pattern, const wxString& destDir ) -> bool
  424. {
  425. wxDir dir( srcDir );
  426. if( !dir.IsOpened() )
  427. {
  428. aErrors += wxString::Format( _( "Could not open source directory: %s" ), srcDir );
  429. aErrors += wxT( "\n" );
  430. return false;
  431. }
  432. wxString filename;
  433. bool success = true;
  434. // Find all entries matching pattern (files + directories + hidden items)
  435. bool cont = dir.GetFirst( &filename, pattern, wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN );
  436. while( cont )
  437. {
  438. const wxString entrySrc = srcDir + wxFileName::GetPathSeparator() + filename;
  439. const wxString entryDest = destDir + wxFileName::GetPathSeparator() + filename;
  440. if( !filename.Matches( wxT( "~*.lck" ) ) && !filename.Matches( wxT( "*.lck" ) ) )
  441. {
  442. if( wxFileName::DirExists( entrySrc ) )
  443. {
  444. // Recursively process subdirectories
  445. if( !CopyFilesOrDirectory( entrySrc, destDir, aAllowOverwrites, aErrors, aPathsWritten ) )
  446. {
  447. aErrors += wxString::Format( _( "Could not copy directory: %s to %s" ),
  448. entrySrc, entryDest );
  449. aErrors += wxT( "\n" );
  450. success = false;
  451. }
  452. }
  453. else
  454. {
  455. // Copy individual files
  456. if( !performCopy( entrySrc, entryDest ) )
  457. {
  458. success = false;
  459. }
  460. }
  461. }
  462. cont = dir.GetNext( &filename );
  463. }
  464. return success;
  465. };
  466. // If copying a directory, append its name to destination path
  467. if( isSourceDirectory )
  468. {
  469. wxString sourceDirName = sourceFn.GetFullName();
  470. baseDestDir = wxFileName( aDestDir, sourceDirName ).GetFullPath();
  471. }
  472. // Create destination directory hierarchy
  473. if( !wxFileName::Mkdir( baseDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
  474. {
  475. aErrors += wxString::Format( _( "Could not create destination directory: %s" ), baseDestDir );
  476. aErrors += wxT( "\n" );
  477. return false;
  478. }
  479. // Execute appropriate copy operation based on source type
  480. if( !isSourceDirectory )
  481. {
  482. const wxString fileName = sourceFn.GetFullName();
  483. // Handle wildcard patterns in filenames
  484. if( fileName.Contains( '*' ) || fileName.Contains( '?' ) )
  485. {
  486. const wxString dirPath = sourceFn.GetPath();
  487. if( !wxFileName::DirExists( dirPath ) )
  488. {
  489. aErrors += wxString::Format( _( "Source directory does not exist: %s" ), dirPath );
  490. aErrors += wxT( "\n" );
  491. return false;
  492. }
  493. // Process all matching files in source directory
  494. return processEntries( dirPath, fileName, baseDestDir );
  495. }
  496. // Single file copy operation
  497. return performCopy( sourcePath, wxFileName( baseDestDir, fileName ).GetFullPath() );
  498. }
  499. // Full directory copy operation
  500. return processEntries( sourcePath, wxEmptyString, baseDestDir );
  501. }
  502. bool AddDirectoryToZip( wxZipOutputStream& aZip, const wxString& aSourceDir, wxString& aErrors,
  503. const wxString& aParentDir )
  504. {
  505. wxDir dir( aSourceDir );
  506. if( !dir.IsOpened() )
  507. {
  508. aErrors += wxString::Format( _( "Could not open source directory: %s" ), aSourceDir );
  509. aErrors += "\n";
  510. return false;
  511. }
  512. wxString filename;
  513. bool cont = dir.GetFirst( &filename );
  514. while( cont )
  515. {
  516. wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
  517. wxString zipPath = aParentDir + filename;
  518. if( wxFileName::DirExists( sourcePath ) )
  519. {
  520. // Add directory entry to the ZIP file
  521. aZip.PutNextDirEntry( zipPath + "/" );
  522. // Recursively add subdirectories
  523. if( !AddDirectoryToZip( aZip, sourcePath, aErrors, zipPath + "/" ) )
  524. return false;
  525. }
  526. else
  527. {
  528. // Add file entry to the ZIP file
  529. aZip.PutNextEntry( zipPath );
  530. wxFFileInputStream fileStream( sourcePath );
  531. if( !fileStream.IsOk() )
  532. {
  533. aErrors += wxString::Format( _( "Could not read file: %s" ), sourcePath );
  534. return false;
  535. }
  536. aZip.Write( fileStream );
  537. }
  538. cont = dir.GetNext( &filename );
  539. }
  540. return true;
  541. }