KiCad PCB EDA Suite
Loading...
Searching...
No Matches
filename_resolver.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) 2015-2020 Cirilo Bernardo <[email protected]>
5 * Copyright (C) 2015-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <fstream>
26#include <mutex>
27#include <sstream>
28
29#include <wx/log.h>
30#include <wx/uri.h>
31#include <pgm_base.h>
32#include <trace_helpers.h>
33
34#include <common.h>
35#include <embedded_files.h>
36#include <env_vars.h>
37#include <filename_resolver.h>
38#include <confirm.h>
39#include <wx_filename.h>
40
41// configuration file version
42#define CFGFILE_VERSION 1
43
44// flag bits used to track different one-off messages to users
45#define ERRFLG_ALIAS (1)
46#define ERRFLG_RELPATH (2)
47#define ERRFLG_ENVPATH (4)
48
49#define MASK_3D_RESOLVER "3D_RESOLVER"
50
51static std::mutex mutex_resolver;
52
53
55 m_pgm( nullptr ),
56 m_project( nullptr )
57{
58 m_errflags = 0;
59}
60
61
62bool FILENAME_RESOLVER::Set3DConfigDir( const wxString& aConfigDir )
63{
64 if( aConfigDir.empty() )
65 return false;
66
67 wxFileName cfgdir( ExpandEnvVarSubstitutions( aConfigDir, m_project ), "" );
68
69 cfgdir.Normalize( FN_NORMALIZE_FLAGS );
70
71 if( !cfgdir.DirExists() )
72 return false;
73
74 m_configDir = cfgdir.GetPath();
76
77 return true;
78}
79
80
81bool FILENAME_RESOLVER::SetProject( PROJECT* aProject, bool* flgChanged )
82{
83 m_project = aProject;
84
85 if( !aProject )
86 return false;
87
88 wxFileName projdir( ExpandEnvVarSubstitutions( aProject->GetProjectPath(), aProject ), "" );
89
90 projdir.Normalize( FN_NORMALIZE_FLAGS );
91
92 if( !projdir.DirExists() )
93 return false;
94
95 m_curProjDir = projdir.GetPath();
96
97 if( flgChanged )
98 *flgChanged = false;
99
100 if( m_paths.empty() )
101 {
102 SEARCH_PATH al;
103 al.m_Alias = wxS( "${KIPRJMOD}" );
104 al.m_Pathvar = wxS( "${KIPRJMOD}" );
106 m_paths.push_back( al );
107
108 if( flgChanged )
109 *flgChanged = true;
110 }
111 else
112 {
113 if( m_paths.front().m_Pathexp != m_curProjDir )
114 {
115 m_paths.front().m_Pathexp = m_curProjDir;
116
117 if( flgChanged )
118 *flgChanged = true;
119 }
120 else
121 {
122 return true;
123 }
124 }
125
126#ifdef DEBUG
127 {
128 std::ostringstream ostr;
129 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
130 ostr << " * [INFO] changed project dir to ";
131 ostr << m_paths.front().m_Pathexp.ToUTF8();
132 wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
133 }
134#endif
135
136 return true;
137}
138
139
141{
142 return m_curProjDir;
143}
144
145
147{
148 m_pgm = aBase;
149
150 if( !m_pgm || m_paths.empty() )
151 return;
152
153 // recreate the path list
154 m_paths.clear();
156}
157
158
160{
161 if( !m_paths.empty() )
162 return true;
163
164 // add an entry for the default search path; at this point
165 // we cannot set a sensible default so we use an empty string.
166 // the user may change this later with a call to SetProjectDir()
167
168 SEARCH_PATH lpath;
169 lpath.m_Alias = wxS( "${KIPRJMOD}" );
170 lpath.m_Pathvar = wxS( "${KIPRJMOD}" );
171 lpath.m_Pathexp = m_curProjDir;
172 m_paths.push_back( lpath );
173 wxFileName fndummy;
174 wxUniChar psep = fndummy.GetPathSeparator();
175 std::list< wxString > epaths;
176
177 if( GetKicadPaths( epaths ) )
178 {
179 for( const wxString& currPath : epaths )
180 {
181 wxString currPathVarFormat = currPath;
182 currPathVarFormat.Prepend( wxS( "${" ) );
183 currPathVarFormat.Append( wxS( "}" ) );
184
185 wxString pathVal = ExpandEnvVarSubstitutions( currPathVarFormat, m_project );
186
187 if( pathVal.empty() )
188 {
189 lpath.m_Pathexp.clear();
190 }
191 else
192 {
193 fndummy.Assign( pathVal, "" );
194 fndummy.Normalize( FN_NORMALIZE_FLAGS );
195 lpath.m_Pathexp = fndummy.GetFullPath();
196 }
197
198 lpath.m_Alias = currPath;
199 lpath.m_Pathvar = currPath;
200
201 if( !lpath.m_Pathexp.empty() && psep == *lpath.m_Pathexp.rbegin() )
202 lpath.m_Pathexp.erase( --lpath.m_Pathexp.end() );
203
204 // we add it first with the alias set to the non-variable format
205 m_paths.push_back( lpath );
206
207 // now add it with the "new variable format ${VAR}"
208 lpath.m_Alias = currPathVarFormat;
209 m_paths.push_back( lpath );
210 }
211 }
212
213 if( m_paths.empty() )
214 return false;
215
216#ifdef DEBUG
217 wxLogTrace( MASK_3D_RESOLVER, wxS( " * [3D model] search paths:\n" ) );
218 std::list< SEARCH_PATH >::const_iterator sPL = m_paths.begin();
219
220 while( sPL != m_paths.end() )
221 {
222 wxLogTrace( MASK_3D_RESOLVER, wxS( " + %s : '%s'\n" ), (*sPL).m_Alias.GetData(),
223 (*sPL).m_Pathexp.GetData() );
224 ++sPL;
225 }
226#endif
227
228 return true;
229}
230
231
232bool FILENAME_RESOLVER::UpdatePathList( const std::vector< SEARCH_PATH >& aPathList )
233{
234 wxUniChar envMarker( '$' );
235
236 while( !m_paths.empty() && envMarker != *m_paths.back().m_Alias.rbegin() )
237 m_paths.pop_back();
238
239 for( const SEARCH_PATH& path : aPathList )
240 addPath( path );
241
242 return true;
243}
244
245
246wxString FILENAME_RESOLVER::ResolvePath( const wxString& aFileName, const wxString& aWorkingPath,
247 const EMBEDDED_FILES* aFiles )
248{
249 std::lock_guard<std::mutex> lock( mutex_resolver );
250
251 if( aFileName.empty() )
252 return wxEmptyString;
253
254 if( m_paths.empty() )
256
257 // first attempt to use the name as specified:
258 wxString tname = aFileName;
259
260 // Note: variable expansion must preferably be performed via a threadsafe wrapper for the
261 // getenv() system call. If we allow the wxFileName::Normalize() routine to perform expansion
262 // then we will have a race condition since wxWidgets does not assure a threadsafe wrapper
263 // for getenv().
264 tname = ExpandEnvVarSubstitutions( tname, m_project );
265
266 // Check to see if the file is a URI for an embedded file.
267 if( tname.StartsWith( FILEEXT::KiCadUriPrefix + "://" ) )
268 {
269 if( !aFiles )
270 {
271 wxLogTrace( wxT( "KICAD_EMBED" ),
272 wxT( "No EMBEDDED_FILES object provided for kicad_embed URI" ) );
273 return wxEmptyString;
274 }
275
276 wxString path = tname.Mid( 14 );
277 wxFileName temp_file = aFiles->GetTemporaryFileName( path );
278
279 if( !temp_file.IsOk() )
280 {
281 wxLogTrace( wxT( "KICAD_EMBED" ),
282 wxT( "Failed to get temp file '%s' for kicad_embed URI" ), path );
283 return wxEmptyString;
284 }
285
286 wxLogTrace( wxT( "KICAD_EMBED" ), wxT( "Opening embedded file '%s' as '%s'" ),
287 tname, temp_file.GetFullPath() );
288
289 return temp_file.GetFullPath();
290 }
291
292 wxFileName tmpFN( tname );
293
294 // this case covers full paths, leading expanded vars, and paths relative to the current
295 // working directory (which is not necessarily the current project directory)
296 if( tmpFN.FileExists() )
297 {
298 tmpFN.Normalize( FN_NORMALIZE_FLAGS );
299 tname = tmpFN.GetFullPath();
300
301 // special case: if a path begins with ${ENV_VAR} but is not in the resolver's path list
302 // then add it.
303 if( aFileName.StartsWith( wxS( "${" ) ) || aFileName.StartsWith( wxS( "$(" ) ) )
304 checkEnvVarPath( aFileName );
305
306 return tname;
307 }
308
309 // if a path begins with ${ENV_VAR}/$(ENV_VAR) and is not resolved then the file either does
310 // not exist or the ENV_VAR is not defined
311 if( aFileName.StartsWith( "${" ) || aFileName.StartsWith( "$(" ) )
312 {
313 if( !( m_errflags & ERRFLG_ENVPATH ) )
314 {
316 wxString errmsg = "[3D File Resolver] No such path; ensure the environment var is defined";
317 errmsg.append( "\n" );
318 errmsg.append( tname );
319 errmsg.append( "\n" );
320 wxLogTrace( tracePathsAndFiles, errmsg );
321 }
322
323 return wxEmptyString;
324 }
325
326 // at this point aFileName is:
327 // a. an aliased shortened name or
328 // b. cannot be determined
329
330 // check the path relative to the current project directory;
331 // NB: this is not necessarily the same as the current working directory, which has already
332 // been checked. This case accounts for partial paths which do not contain ${KIPRJMOD}.
333 // This check is performed before checking the path relative to ${KICAD7_3DMODEL_DIR} so that
334 // users can potentially override a model within ${KICAD7_3DMODEL_DIR}.
335 if( !m_paths.begin()->m_Pathexp.empty() && !tname.StartsWith( ":" ) )
336 {
337 tmpFN.Assign( m_paths.begin()->m_Pathexp, "" );
338 wxString fullPath = tmpFN.GetPathWithSep() + tname;
339
340 fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
341
342 if( wxFileName::FileExists( fullPath ) )
343 {
344 tmpFN.Assign( fullPath );
345 tmpFN.Normalize( FN_NORMALIZE_FLAGS );
346 tname = tmpFN.GetFullPath();
347 return tname;
348 }
349
350 }
351
352 // check path relative to search path
353 if( !aWorkingPath.IsEmpty() && !tname.StartsWith( ":" ) )
354 {
355 wxString tmp = aWorkingPath;
356 tmp.Append( tmpFN.GetPathSeparator() );
357 tmp.Append( tname );
358 tmpFN.Assign( tmp );
359
360 if( tmpFN.MakeAbsolute() && tmpFN.FileExists() )
361 {
362 tname = tmpFN.GetFullPath();
363 return tname;
364 }
365 }
366
367 // check the partial path relative to ${KICAD7_3DMODEL_DIR} (legacy behavior)
368 if( !tname.StartsWith( wxS( ":" ) ) )
369 {
370 wxFileName fpath;
371 wxString fullPath( wxString::Format( wxS( "${%s}" ),
372 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) ) );
373 fullPath.Append( fpath.GetPathSeparator() );
374 fullPath.Append( tname );
375 fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
376 fpath.Assign( fullPath );
377
378 if( fpath.Normalize( FN_NORMALIZE_FLAGS ) && fpath.FileExists() )
379 {
380 tname = fpath.GetFullPath();
381 return tname;
382 }
383
384 }
385
386 // at this point the filename must contain an alias or else it is invalid
387 wxString alias; // the alias portion of the short filename
388 wxString relpath; // the path relative to the alias
389
390 if( !SplitAlias( tname, alias, relpath ) )
391 {
392 if( !( m_errflags & ERRFLG_RELPATH ) )
393 {
394 // this can happen if the file was intended to be relative to ${KICAD7_3DMODEL_DIR}
395 // but ${KICAD7_3DMODEL_DIR} is not set or is incorrect.
397 wxString errmsg = "[3D File Resolver] No such path";
398 errmsg.append( wxS( "\n" ) );
399 errmsg.append( tname );
400 errmsg.append( wxS( "\n" ) );
401 wxLogTrace( tracePathsAndFiles, errmsg );
402 }
403
404 return wxEmptyString;
405 }
406
407 for( const SEARCH_PATH& path : m_paths )
408 {
409 // ${ENV_VAR} paths have already been checked; skip them
410 if( path.m_Alias.StartsWith( wxS( "${" ) ) || path.m_Alias.StartsWith( wxS( "$(" ) ) )
411 continue;
412
413 if( path.m_Alias == alias && !path.m_Pathexp.empty() )
414 {
415 wxFileName fpath( wxFileName::DirName( path.m_Pathexp ) );
416 wxString fullPath = fpath.GetPathWithSep() + relpath;
417
418 fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
419
420 if( wxFileName::FileExists( fullPath ) )
421 {
422 tname = fullPath;
423
424 wxFileName tmp( fullPath );
425
426 if( tmp.Normalize( FN_NORMALIZE_FLAGS ) )
427 tname = tmp.GetFullPath();
428
429 return tname;
430 }
431 }
432 }
433
434 if( !( m_errflags & ERRFLG_ALIAS ) )
435 {
437 wxString errmsg = "[3D File Resolver] No such path; ensure the path alias is defined";
438 errmsg.append( "\n" );
439 errmsg.append( tname.substr( 1 ) );
440 errmsg.append( "\n" );
441 wxLogTrace( tracePathsAndFiles, errmsg );
442 }
443
444 return wxEmptyString;
445}
446
447
449{
450 if( aPath.m_Alias.empty() || aPath.m_Pathvar.empty() )
451 return false;
452
453 std::lock_guard<std::mutex> lock( mutex_resolver );
454
455 SEARCH_PATH tpath = aPath;
456
457 #ifdef _WIN32
458 while( tpath.m_Pathvar.EndsWith( wxT( "\\" ) ) )
459 tpath.m_Pathvar.erase( tpath.m_Pathvar.length() - 1 );
460 #else
461 while( tpath.m_Pathvar.EndsWith( wxT( "/" ) ) && tpath.m_Pathvar.length() > 1 )
462 tpath.m_Pathvar.erase( tpath.m_Pathvar.length() - 1 );
463 #endif
464
465 wxFileName path( ExpandEnvVarSubstitutions( tpath.m_Pathvar, m_project ), "" );
466
467 path.Normalize( FN_NORMALIZE_FLAGS );
468
469 if( !path.DirExists() )
470 {
471 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
472 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
473
474 if( aPath.m_Pathvar == versionedPath
475 || aPath.m_Pathvar == wxS( "${KIPRJMOD}" ) || aPath.m_Pathvar == wxS( "$(KIPRJMOD)" )
476 || aPath.m_Pathvar == wxS( "${KISYS3DMOD}" ) || aPath.m_Pathvar == wxS( "$(KISYS3DMOD)" ) )
477 {
478 // suppress the message if the missing pathvar is a system variable
479 }
480 else
481 {
482 wxString msg = _( "The given path does not exist" );
483 msg.append( wxT( "\n" ) );
484 msg.append( tpath.m_Pathvar );
485 DisplayErrorMessage( nullptr, msg );
486 }
487
488 tpath.m_Pathexp.clear();
489 }
490 else
491 {
492 tpath.m_Pathexp = path.GetFullPath();
493
494#ifdef _WIN32
495 while( tpath.m_Pathexp.EndsWith( wxT( "\\" ) ) )
496 tpath.m_Pathexp.erase( tpath.m_Pathexp.length() - 1 );
497#else
498 while( tpath.m_Pathexp.EndsWith( wxT( "/" ) ) && tpath.m_Pathexp.length() > 1 )
499 tpath.m_Pathexp.erase( tpath.m_Pathexp.length() - 1 );
500#endif
501 }
502
503 std::list< SEARCH_PATH >::iterator sPL = m_paths.begin();
504 std::list< SEARCH_PATH >::iterator ePL = m_paths.end();
505
506 while( sPL != ePL )
507 {
508 if( tpath.m_Alias == sPL->m_Alias )
509 {
510 wxString msg = _( "Alias: " );
511 msg.append( tpath.m_Alias );
512 msg.append( wxT( "\n" ) );
513 msg.append( _( "This path:" ) + wxS( " " ) );
514 msg.append( tpath.m_Pathvar );
515 msg.append( wxT( "\n" ) );
516 msg.append( _( "Existing path:" ) + wxS( " " ) );
517 msg.append( sPL->m_Pathvar );
518 DisplayErrorMessage( nullptr, _( "Bad alias (duplicate name)" ), msg );
519 return false;
520 }
521
522 ++sPL;
523 }
524
525 m_paths.push_back( tpath );
526 return true;
527}
528
529
530void FILENAME_RESOLVER::checkEnvVarPath( const wxString& aPath )
531{
532 bool useParen = false;
533
534 if( aPath.StartsWith( wxS( "$(" ) ) )
535 useParen = true;
536 else if( !aPath.StartsWith( wxS( "${" ) ) )
537 return;
538
539 size_t pEnd;
540
541 if( useParen )
542 pEnd = aPath.find( wxS( ")" ) );
543 else
544 pEnd = aPath.find( wxS( "}" ) );
545
546 if( pEnd == wxString::npos )
547 return;
548
549 wxString envar = aPath.substr( 0, pEnd + 1 );
550
551 // check if the alias exists; if not then add it to the end of the
552 // env var section of the path list
553 auto sPL = m_paths.begin();
554 auto ePL = m_paths.end();
555
556 while( sPL != ePL )
557 {
558 if( sPL->m_Alias == envar )
559 return;
560
561 if( !sPL->m_Alias.StartsWith( wxS( "${" ) ) )
562 break;
563
564 ++sPL;
565 }
566
567 SEARCH_PATH lpath;
568 lpath.m_Alias = envar;
569 lpath.m_Pathvar = lpath.m_Alias;
570 wxFileName tmpFN( ExpandEnvVarSubstitutions( lpath.m_Alias, m_project ), "" );
571
572 wxUniChar psep = tmpFN.GetPathSeparator();
573 tmpFN.Normalize( FN_NORMALIZE_FLAGS );
574
575 if( !tmpFN.DirExists() )
576 return;
577
578 lpath.m_Pathexp = tmpFN.GetFullPath();
579
580 if( !lpath.m_Pathexp.empty() && psep == *lpath.m_Pathexp.rbegin() )
581 lpath.m_Pathexp.erase( --lpath.m_Pathexp.end() );
582
583 if( lpath.m_Pathexp.empty() )
584 return;
585
586 m_paths.insert( sPL, lpath );
587}
588
589
590wxString FILENAME_RESOLVER::ShortenPath( const wxString& aFullPathName )
591{
592 wxString fname = aFullPathName;
593
594 if( m_paths.empty() )
596
597 std::lock_guard<std::mutex> lock( mutex_resolver );
598
599 std::list< SEARCH_PATH >::const_iterator sL = m_paths.begin();
600 size_t idx;
601
602 while( sL != m_paths.end() )
603 {
604 // undefined paths do not participate in the
605 // file name shortening procedure
606 if( sL->m_Pathexp.empty() )
607 {
608 ++sL;
609 continue;
610 }
611
612 wxFileName fpath;
613
614 // in the case of aliases, ensure that we use the most recent definition
615 if( sL->m_Alias.StartsWith( wxS( "${" ) ) || sL->m_Alias.StartsWith( wxS( "$(" ) ) )
616 {
617 wxString tpath = ExpandEnvVarSubstitutions( sL->m_Alias, m_project );
618
619 if( tpath.empty() )
620 {
621 ++sL;
622 continue;
623 }
624
625 fpath.Assign( tpath, wxT( "" ) );
626 }
627 else
628 {
629 fpath.Assign( sL->m_Pathexp, wxT( "" ) );
630 }
631
632 wxString fps = fpath.GetPathWithSep();
633 wxString tname;
634
635 idx = fname.find( fps );
636
637 if( idx == 0 )
638 {
639 fname = fname.substr( fps.size() );
640
641 #ifdef _WIN32
642 // ensure only the '/' separator is used in the internal name
643 fname.Replace( wxT( "\\" ), wxT( "/" ) );
644 #endif
645
646 if( sL->m_Alias.StartsWith( wxS( "${" ) ) || sL->m_Alias.StartsWith( wxS( "$(" ) ) )
647 {
648 // old style ENV_VAR
649 tname = sL->m_Alias;
650 tname.Append( wxS( "/" ) );
651 tname.append( fname );
652 }
653 else
654 {
655 // new style alias
656 tname = "${";
657 tname.append( sL->m_Alias );
658 tname.append( wxS( "}/" ) );
659 tname.append( fname );
660 }
661
662 return tname;
663 }
664
665 ++sL;
666 }
667
668#ifdef _WIN32
669 // it is strange to convert an MSWin full path to use the
670 // UNIX separator but this is done for consistency and can
671 // be helpful even when transferring project files from
672 // MSWin to *NIX.
673 fname.Replace( wxT( "\\" ), wxT( "/" ) );
674#endif
675
676 return fname;
677}
678
679
680
681const std::list< SEARCH_PATH >* FILENAME_RESOLVER::GetPaths() const
682{
683 return &m_paths;
684}
685
686
687bool FILENAME_RESOLVER::SplitAlias( const wxString& aFileName,
688 wxString& anAlias, wxString& aRelPath ) const
689{
690 anAlias.clear();
691 aRelPath.clear();
692
693 size_t searchStart = 0;
694
695 if( aFileName.StartsWith( wxT( ":" ) ) )
696 searchStart = 1;
697
698 size_t tagpos = aFileName.find( wxT( ":" ), searchStart );
699
700 if( tagpos == wxString::npos || tagpos == searchStart )
701 return false;
702
703 if( tagpos + 1 >= aFileName.length() )
704 return false;
705
706 anAlias = aFileName.substr( searchStart, tagpos - searchStart );
707 aRelPath = aFileName.substr( tagpos + 1 );
708
709 return true;
710}
711
712
713bool FILENAME_RESOLVER::ValidateFileName( const wxString& aFileName, bool& hasAlias ) const
714{
715 // Rules:
716 // 1. The generic form of an aliased 3D relative path is:
717 // ALIAS:relative/path
718 // 2. ALIAS is a UTF string excluding wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" )
719 // 3. The relative path must be a valid relative path for the platform
720 // 4. We allow a URI for embedded files, but only if it has a name
721
722 hasAlias = false;
723
724 if( aFileName.empty() )
725 return false;
726
727 if( aFileName.StartsWith( wxT( "file://" ) )
728 || aFileName.StartsWith( FILEEXT::KiCadUriPrefix + "://" ) )
729 {
730 size_t prefixLength = aFileName.StartsWith( wxT( "file://" ) ) ? 7 : 14;
731 if( aFileName.length() > prefixLength && aFileName[prefixLength] != '/' )
732 return true;
733 else
734 return false;
735 }
736
737 wxString filename = aFileName;
738 wxString lpath;
739 size_t aliasStart = aFileName.StartsWith( ':' ) ? 1 : 0;
740 size_t aliasEnd = aFileName.find( ':', aliasStart );
741
742 // ensure that the file separators suit the current platform
743#ifdef __WINDOWS__
744 filename.Replace( wxT( "/" ), wxT( "\\" ) );
745
746 // if we see the :\ pattern then it must be a drive designator
747 if( aliasEnd != wxString::npos )
748 {
749 size_t pos1 = filename.find( wxT( ":\\" ) );
750
751 if( pos1 != wxString::npos && ( pos1 != aliasEnd || pos1 != 1 ) )
752 return false;
753
754 // if we have a drive designator then we have no alias
755 if( pos1 != wxString::npos )
756 aliasEnd = wxString::npos;
757 }
758#else
759 filename.Replace( wxT( "\\" ), wxT( "/" ) );
760#endif
761
762 // names may not end with ':'
763 if( aliasEnd == aFileName.length() -1 )
764 return false;
765
766 if( aliasEnd != wxString::npos )
767 {
768 // ensure the alias component is not empty
769 if( aliasEnd == aliasStart )
770 return false;
771
772 lpath = filename.substr( aliasStart, aliasEnd );
773
774 // check the alias for restricted characters
775 if( wxString::npos != lpath.find_first_of( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) ) )
776 return false;
777
778 hasAlias = true;
779 lpath = aFileName.substr( aliasEnd + 1 );
780 }
781 else
782 {
783 lpath = aFileName;
784
785 // in the case of ${ENV_VAR}|$(ENV_VAR)/path, strip the
786 // environment string before testing
787 aliasEnd = wxString::npos;
788
789 if( aFileName.StartsWith( wxS( "${" ) ) )
790 aliasEnd = aFileName.find( '}' );
791 else if( aFileName.StartsWith( wxS( "$(" ) ) )
792 aliasEnd = aFileName.find( ')' );
793
794 if( aliasEnd != wxString::npos )
795 lpath = aFileName.substr( aliasEnd + 1 );
796
797 }
798
799 // Test for forbidden chars in filenames. Should be wxFileName::GetForbiddenChars()
800 // On MSW, the list returned by wxFileName::GetForbiddenChars() contains separators
801 // '\'and '/' used here because lpath can be a full path.
802 // So remove separators
803 wxString lpath_no_sep = lpath;
804#ifdef __WINDOWS__
805 lpath_no_sep.Replace( "/", " " );
806 lpath_no_sep.Replace( "\\", " " );
807
808 // A disk identifier is allowed, and therefore remove its separator
809 if( lpath_no_sep.Length() > 1 && lpath_no_sep[1] == ':' )
810 lpath_no_sep[1] = ' ';
811#endif
812
813 if( wxString::npos != lpath_no_sep.find_first_of( wxFileName::GetForbiddenChars() ) )
814 return false;
815
816 return true;
817}
818
819
820bool FILENAME_RESOLVER::GetKicadPaths( std::list< wxString >& paths ) const
821{
822 paths.clear();
823
824 if( !m_pgm )
825 return false;
826
827 bool hasKisys3D = false;
828
829
830 // iterate over the list of internally defined ENV VARs
831 // and add them to the paths list
834
835 while( mS != mE )
836 {
837 // filter out URLs, template directories, and known system paths
838 if( mS->first == wxS( "KICAD_PTEMPLATES" )
839 || mS->first.Matches( wxS( "KICAD*_FOOTPRINT_DIR") ) )
840 {
841 ++mS;
842 continue;
843 }
844
845 if( wxString::npos != mS->second.GetValue().find( wxS( "://" ) ) )
846 {
847 ++mS;
848 continue;
849 }
850
851 //also add the path without the ${} to act as legacy alias support for older files
852 paths.push_back( mS->first );
853
854 if( mS->first.Matches( wxS("KICAD*_3DMODEL_DIR") ) )
855 hasKisys3D = true;
856
857 ++mS;
858 }
859
860 if( !hasKisys3D )
861 paths.emplace_back( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
862
863 return true;
864}
wxFileName GetTemporaryFileName(const wxString &aName) const
bool ValidateFileName(const wxString &aFileName, bool &hasAlias) const
Returns true if the given path is a valid aliased relative path.
bool createPathList(void)
Build the path list using available information such as KICAD7_3DMODEL_DIR and the 3d_path_list confi...
bool addPath(const SEARCH_PATH &aPath)
Check that a path is valid and adds it to the search list.
wxString GetProjectDir() const
bool GetKicadPaths(std::list< wxString > &paths) const
Return a list of path environment variables local to KiCad.
bool Set3DConfigDir(const wxString &aConfigDir)
Set the user's configuration directory for 3D models.
void checkEnvVarPath(const wxString &aPath)
Check the ${ENV_VAR} component of a path and adds it to the resolver's path list if it is not yet in ...
std::list< SEARCH_PATH > m_paths
bool SetProject(PROJECT *aProject, bool *flgChanged=nullptr)
Set the current KiCad project directory as the first entry in the model path list.
void SetProgramBase(PGM_BASE *aBase)
Set a pointer to the application's PGM_BASE instance used to extract the local env vars.
bool SplitAlias(const wxString &aFileName, wxString &anAlias, wxString &aRelPath) const
Return true if the given name contains an alias and populates the string anAlias with the alias and a...
wxString ResolvePath(const wxString &aFileName, const wxString &aWorkingPath, const EMBEDDED_FILES *aFiles)
Determines the full path of the given file name.
const std::list< SEARCH_PATH > * GetPaths() const
Return a pointer to the internal path list; the items in:load.
bool UpdatePathList(const std::vector< SEARCH_PATH > &aPathList)
Clear the current path list and substitutes the given path list and update the path configuration fil...
wxString ShortenPath(const wxString &aFullPathName)
Produce a relative path based on the existing search directories or returns the same path if the path...
Container for data for KiCad programs.
Definition: pgm_base.h:102
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.cpp:923
Container for project specific data.
Definition: project.h:62
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:135
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:343
The common library.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
#define _(s)
Functions related to environment variables, including help functions.
#define ERRFLG_RELPATH
static std::mutex mutex_resolver
#define ERRFLG_ENVPATH
#define MASK_3D_RESOLVER
#define ERRFLG_ALIAS
static const std::string KiCadUriPrefix
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
std::map< wxString, ENV_VAR_ITEM >::const_iterator ENV_VAR_MAP_CITER
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Constructs a versioned environment variable based on this KiCad major version.
Definition: env_vars.cpp:74
see class PGM_BASE
wxString m_Pathvar
wxString m_Pathexp
wxString m_Alias
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39