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 The 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( const 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 std::vector<const EMBEDDED_FILES*> aEmbeddedFilesStack )
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 // Swap separators for non-Windows, in case a Windows path is being passed
261#ifndef __WIN32__
262 tname.Replace( "\\", "/" );
263#endif
264
265 // Note: variable expansion must preferably be performed via a threadsafe wrapper for the
266 // getenv() system call. If we allow the wxFileName::Normalize() routine to perform expansion
267 // then we will have a race condition since wxWidgets does not assure a threadsafe wrapper
268 // for getenv().
269 tname = ExpandEnvVarSubstitutions( tname, m_project );
270
271 // Check to see if the file is a URI for an embedded file.
272 if( tname.StartsWith( FILEEXT::KiCadUriPrefix + "://" ) )
273 {
274 if( aEmbeddedFilesStack.empty() )
275 {
276 wxLogTrace( wxT( "KICAD_EMBED" ),
277 wxT( "No EMBEDDED_FILES object provided for kicad_embed URI" ) );
278 return wxEmptyString;
279 }
280
281 wxString path = tname.Mid( wxString( FILEEXT::KiCadUriPrefix + "://" ).length() );
282
283 wxFileName temp_file = aEmbeddedFilesStack[0]->GetTemporaryFileName( path );
284 int ii = 1;
285
286 while( !temp_file.IsOk() && ii < (int) aEmbeddedFilesStack.size() )
287 temp_file = aEmbeddedFilesStack[ii++]->GetTemporaryFileName( path );
288
289 if( !temp_file.IsOk() )
290 {
291 wxLogTrace( wxT( "KICAD_EMBED" ),
292 wxT( "Failed to get temp file '%s' for kicad_embed URI" ), path );
293 return wxEmptyString;
294 }
295
296 wxLogTrace( wxT( "KICAD_EMBED" ), wxT( "Opening embedded file '%s' as '%s'" ),
297 tname, temp_file.GetFullPath() );
298
299 return temp_file.GetFullPath();
300 }
301
302 wxFileName tmpFN( tname );
303
304 // this case covers full paths, leading expanded vars, and paths relative to the current
305 // working directory (which is not necessarily the current project directory)
306 if( tmpFN.FileExists() )
307 {
308 tmpFN.Normalize( FN_NORMALIZE_FLAGS );
309 tname = tmpFN.GetFullPath();
310
311 // special case: if a path begins with ${ENV_VAR} but is not in the resolver's path list
312 // then add it.
313 if( aFileName.StartsWith( wxS( "${" ) ) || aFileName.StartsWith( wxS( "$(" ) ) )
314 checkEnvVarPath( aFileName );
315
316 return tname;
317 }
318
319 // if a path begins with ${ENV_VAR}/$(ENV_VAR) and is not resolved then the file either does
320 // not exist or the ENV_VAR is not defined
321 if( aFileName.StartsWith( "${" ) || aFileName.StartsWith( "$(" ) )
322 {
323 if( !( m_errflags & ERRFLG_ENVPATH ) )
324 {
326 wxString errmsg = "[3D File Resolver] No such path; ensure the environment var is "
327 "defined";
328 errmsg.append( "\n" );
329 errmsg.append( tname );
330 errmsg.append( "\n" );
331 wxLogTrace( tracePathsAndFiles, errmsg );
332 }
333
334 return wxEmptyString;
335 }
336
337 // at this point aFileName is:
338 // a. an aliased shortened name or
339 // b. cannot be determined
340
341 // check the path relative to the current project directory;
342 // NB: this is not necessarily the same as the current working directory, which has already
343 // been checked. This case accounts for partial paths which do not contain ${KIPRJMOD}.
344 // This check is performed before checking the path relative to ${KICAD7_3DMODEL_DIR} so that
345 // users can potentially override a model within ${KICAD7_3DMODEL_DIR}.
346 if( !m_paths.begin()->m_Pathexp.empty() && !tname.StartsWith( ":" ) )
347 {
348 tmpFN.Assign( m_paths.begin()->m_Pathexp, "" );
349 wxString fullPath = tmpFN.GetPathWithSep() + tname;
350
351 fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
352
353 if( wxFileName::FileExists( fullPath ) )
354 {
355 tmpFN.Assign( fullPath );
356 tmpFN.Normalize( FN_NORMALIZE_FLAGS );
357 tname = tmpFN.GetFullPath();
358 return tname;
359 }
360
361 }
362
363 // check path relative to search path
364 if( !aWorkingPath.IsEmpty() && !tname.StartsWith( ":" ) )
365 {
366 wxString tmp = aWorkingPath;
367 tmp.Append( tmpFN.GetPathSeparator() );
368 tmp.Append( tname );
369 tmpFN.Assign( tmp );
370
371 if( tmpFN.MakeAbsolute() && tmpFN.FileExists() )
372 {
373 tname = tmpFN.GetFullPath();
374 return tname;
375 }
376 }
377
378 // check the partial path relative to ${KICAD7_3DMODEL_DIR} (legacy behavior)
379 if( !tname.StartsWith( wxS( ":" ) ) )
380 {
381 wxFileName fpath;
382 wxString fullPath( wxString::Format( wxS( "${%s}" ),
383 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) ) );
384 fullPath.Append( fpath.GetPathSeparator() );
385 fullPath.Append( tname );
386 fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
387 fpath.Assign( fullPath );
388
389 if( fpath.Normalize( FN_NORMALIZE_FLAGS ) && fpath.FileExists() )
390 {
391 tname = fpath.GetFullPath();
392 return tname;
393 }
394
395 }
396
397 // at this point the filename must contain an alias or else it is invalid
398 wxString alias; // the alias portion of the short filename
399 wxString relpath; // the path relative to the alias
400
401 if( !SplitAlias( tname, alias, relpath ) )
402 {
403 if( !( m_errflags & ERRFLG_RELPATH ) )
404 {
405 // this can happen if the file was intended to be relative to ${KICAD7_3DMODEL_DIR}
406 // but ${KICAD7_3DMODEL_DIR} is not set or is incorrect.
408 wxString errmsg = "[3D File Resolver] No such path";
409 errmsg.append( wxS( "\n" ) );
410 errmsg.append( tname );
411 errmsg.append( wxS( "\n" ) );
412 wxLogTrace( tracePathsAndFiles, errmsg );
413 }
414
415 return wxEmptyString;
416 }
417
418 for( const SEARCH_PATH& path : m_paths )
419 {
420 // ${ENV_VAR} paths have already been checked; skip them
421 if( path.m_Alias.StartsWith( wxS( "${" ) ) || path.m_Alias.StartsWith( wxS( "$(" ) ) )
422 continue;
423
424 if( path.m_Alias == alias && !path.m_Pathexp.empty() )
425 {
426 wxFileName fpath( wxFileName::DirName( path.m_Pathexp ) );
427 wxString fullPath = fpath.GetPathWithSep() + relpath;
428
429 fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
430
431 if( wxFileName::FileExists( fullPath ) )
432 {
433 tname = fullPath;
434
435 wxFileName tmp( fullPath );
436
437 if( tmp.Normalize( FN_NORMALIZE_FLAGS ) )
438 tname = tmp.GetFullPath();
439
440 return tname;
441 }
442 }
443 }
444
445 if( !( m_errflags & ERRFLG_ALIAS ) )
446 {
448 wxString errmsg = "[3D File Resolver] No such path; ensure the path alias is defined";
449 errmsg.append( "\n" );
450 errmsg.append( tname.substr( 1 ) );
451 errmsg.append( "\n" );
452 wxLogTrace( tracePathsAndFiles, errmsg );
453 }
454
455 return wxEmptyString;
456}
457
458
460{
461 if( aPath.m_Alias.empty() || aPath.m_Pathvar.empty() )
462 return false;
463
464 std::lock_guard<std::mutex> lock( mutex_resolver );
465
466 SEARCH_PATH tpath = aPath;
467
468 #ifdef _WIN32
469 while( tpath.m_Pathvar.EndsWith( wxT( "\\" ) ) )
470 tpath.m_Pathvar.erase( tpath.m_Pathvar.length() - 1 );
471 #else
472 while( tpath.m_Pathvar.EndsWith( wxT( "/" ) ) && tpath.m_Pathvar.length() > 1 )
473 tpath.m_Pathvar.erase( tpath.m_Pathvar.length() - 1 );
474 #endif
475
476 wxFileName path( ExpandEnvVarSubstitutions( tpath.m_Pathvar, m_project ), "" );
477
478 path.Normalize( FN_NORMALIZE_FLAGS );
479
480 if( !path.DirExists() )
481 {
482 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
483 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
484
485 if( aPath.m_Pathvar == versionedPath
486 || aPath.m_Pathvar == wxS( "${KIPRJMOD}" ) || aPath.m_Pathvar == wxS( "$(KIPRJMOD)" )
487 || aPath.m_Pathvar == wxS( "${KISYS3DMOD}" )
488 || aPath.m_Pathvar == wxS( "$(KISYS3DMOD)" ) )
489 {
490 // suppress the message if the missing pathvar is a system variable
491 }
492 else
493 {
494 wxString msg = _( "The given path does not exist" );
495 msg.append( wxT( "\n" ) );
496 msg.append( tpath.m_Pathvar );
497 DisplayErrorMessage( nullptr, msg );
498 }
499
500 tpath.m_Pathexp.clear();
501 }
502 else
503 {
504 tpath.m_Pathexp = path.GetFullPath();
505
506#ifdef _WIN32
507 while( tpath.m_Pathexp.EndsWith( wxT( "\\" ) ) )
508 tpath.m_Pathexp.erase( tpath.m_Pathexp.length() - 1 );
509#else
510 while( tpath.m_Pathexp.EndsWith( wxT( "/" ) ) && tpath.m_Pathexp.length() > 1 )
511 tpath.m_Pathexp.erase( tpath.m_Pathexp.length() - 1 );
512#endif
513 }
514
515 std::list< SEARCH_PATH >::iterator sPL = m_paths.begin();
516 std::list< SEARCH_PATH >::iterator ePL = m_paths.end();
517
518 while( sPL != ePL )
519 {
520 if( tpath.m_Alias == sPL->m_Alias )
521 {
522 wxString msg = _( "Alias: " );
523 msg.append( tpath.m_Alias );
524 msg.append( wxT( "\n" ) );
525 msg.append( _( "This path:" ) + wxS( " " ) );
526 msg.append( tpath.m_Pathvar );
527 msg.append( wxT( "\n" ) );
528 msg.append( _( "Existing path:" ) + wxS( " " ) );
529 msg.append( sPL->m_Pathvar );
530 DisplayErrorMessage( nullptr, _( "Bad alias (duplicate name)" ), msg );
531 return false;
532 }
533
534 ++sPL;
535 }
536
537 m_paths.push_back( tpath );
538 return true;
539}
540
541
542void FILENAME_RESOLVER::checkEnvVarPath( const wxString& aPath )
543{
544 bool useParen = false;
545
546 if( aPath.StartsWith( wxS( "$(" ) ) )
547 useParen = true;
548 else if( !aPath.StartsWith( wxS( "${" ) ) )
549 return;
550
551 size_t pEnd;
552
553 if( useParen )
554 pEnd = aPath.find( wxS( ")" ) );
555 else
556 pEnd = aPath.find( wxS( "}" ) );
557
558 if( pEnd == wxString::npos )
559 return;
560
561 wxString envar = aPath.substr( 0, pEnd + 1 );
562
563 // check if the alias exists; if not then add it to the end of the
564 // env var section of the path list
565 auto sPL = m_paths.begin();
566 auto ePL = m_paths.end();
567
568 while( sPL != ePL )
569 {
570 if( sPL->m_Alias == envar )
571 return;
572
573 if( !sPL->m_Alias.StartsWith( wxS( "${" ) ) )
574 break;
575
576 ++sPL;
577 }
578
579 SEARCH_PATH lpath;
580 lpath.m_Alias = envar;
581 lpath.m_Pathvar = lpath.m_Alias;
582 wxFileName tmpFN( ExpandEnvVarSubstitutions( lpath.m_Alias, m_project ), "" );
583
584 wxUniChar psep = tmpFN.GetPathSeparator();
585 tmpFN.Normalize( FN_NORMALIZE_FLAGS );
586
587 if( !tmpFN.DirExists() )
588 return;
589
590 lpath.m_Pathexp = tmpFN.GetFullPath();
591
592 if( !lpath.m_Pathexp.empty() && psep == *lpath.m_Pathexp.rbegin() )
593 lpath.m_Pathexp.erase( --lpath.m_Pathexp.end() );
594
595 if( lpath.m_Pathexp.empty() )
596 return;
597
598 m_paths.insert( sPL, lpath );
599}
600
601
602wxString FILENAME_RESOLVER::ShortenPath( const wxString& aFullPathName )
603{
604 wxString fname = aFullPathName;
605
606 if( m_paths.empty() )
608
609 std::lock_guard<std::mutex> lock( mutex_resolver );
610
611 std::list< SEARCH_PATH >::const_iterator sL = m_paths.begin();
612 size_t idx;
613
614 while( sL != m_paths.end() )
615 {
616 // undefined paths do not participate in the
617 // file name shortening procedure
618 if( sL->m_Pathexp.empty() )
619 {
620 ++sL;
621 continue;
622 }
623
624 wxFileName fpath;
625
626 // in the case of aliases, ensure that we use the most recent definition
627 if( sL->m_Alias.StartsWith( wxS( "${" ) ) || sL->m_Alias.StartsWith( wxS( "$(" ) ) )
628 {
629 wxString tpath = ExpandEnvVarSubstitutions( sL->m_Alias, m_project );
630
631 if( tpath.empty() )
632 {
633 ++sL;
634 continue;
635 }
636
637 fpath.Assign( tpath, wxT( "" ) );
638 }
639 else
640 {
641 fpath.Assign( sL->m_Pathexp, wxT( "" ) );
642 }
643
644 wxString fps = fpath.GetPathWithSep();
645 wxString tname;
646
647 idx = fname.find( fps );
648
649 if( idx == 0 )
650 {
651 fname = fname.substr( fps.size() );
652
653 #ifdef _WIN32
654 // ensure only the '/' separator is used in the internal name
655 fname.Replace( wxT( "\\" ), wxT( "/" ) );
656 #endif
657
658 if( sL->m_Alias.StartsWith( wxS( "${" ) ) || sL->m_Alias.StartsWith( wxS( "$(" ) ) )
659 {
660 // old style ENV_VAR
661 tname = sL->m_Alias;
662 tname.Append( wxS( "/" ) );
663 tname.append( fname );
664 }
665 else
666 {
667 // new style alias
668 tname = "${";
669 tname.append( sL->m_Alias );
670 tname.append( wxS( "}/" ) );
671 tname.append( fname );
672 }
673
674 return tname;
675 }
676
677 ++sL;
678 }
679
680#ifdef _WIN32
681 // it is strange to convert an MSWin full path to use the
682 // UNIX separator but this is done for consistency and can
683 // be helpful even when transferring project files from
684 // MSWin to *NIX.
685 fname.Replace( wxT( "\\" ), wxT( "/" ) );
686#endif
687
688 return fname;
689}
690
691
692const std::list< SEARCH_PATH >* FILENAME_RESOLVER::GetPaths() const
693{
694 return &m_paths;
695}
696
697
698bool FILENAME_RESOLVER::SplitAlias( const wxString& aFileName,
699 wxString& anAlias, wxString& aRelPath ) const
700{
701 anAlias.clear();
702 aRelPath.clear();
703
704 size_t searchStart = 0;
705
706 if( aFileName.StartsWith( wxT( ":" ) ) )
707 searchStart = 1;
708
709 size_t tagpos = aFileName.find( wxT( ":" ), searchStart );
710
711 if( tagpos == wxString::npos || tagpos == searchStart )
712 return false;
713
714 if( tagpos + 1 >= aFileName.length() )
715 return false;
716
717 anAlias = aFileName.substr( searchStart, tagpos - searchStart );
718 aRelPath = aFileName.substr( tagpos + 1 );
719
720 return true;
721}
722
723
724bool FILENAME_RESOLVER::ValidateFileName( const wxString& aFileName, bool& hasAlias ) const
725{
726 // Rules:
727 // 1. The generic form of an aliased 3D relative path is:
728 // ALIAS:relative/path
729 // 2. ALIAS is a UTF string excluding wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" )
730 // 3. The relative path must be a valid relative path for the platform
731 // 4. We allow a URI for embedded files, but only if it has a name
732
733 hasAlias = false;
734
735 if( aFileName.empty() )
736 return false;
737
738 if( aFileName.StartsWith( wxT( "file://" ) )
739 || aFileName.StartsWith( FILEEXT::KiCadUriPrefix + "://" ) )
740 {
741 size_t prefixLength = aFileName.StartsWith( wxT( "file://" ) ) ? 7 : 14;
742 if( aFileName.length() > prefixLength && aFileName[prefixLength] != '/' )
743 return true;
744 else
745 return false;
746 }
747
748 wxString filename = aFileName;
749 wxString lpath;
750 size_t aliasStart = aFileName.StartsWith( ':' ) ? 1 : 0;
751 size_t aliasEnd = aFileName.find( ':', aliasStart );
752
753 // ensure that the file separators suit the current platform
754#ifdef __WINDOWS__
755 filename.Replace( wxT( "/" ), wxT( "\\" ) );
756
757 // if we see the :\ pattern then it must be a drive designator
758 if( aliasEnd != wxString::npos )
759 {
760 size_t pos1 = filename.find( wxT( ":\\" ) );
761
762 if( pos1 != wxString::npos && ( pos1 != aliasEnd || pos1 != 1 ) )
763 return false;
764
765 // if we have a drive designator then we have no alias
766 if( pos1 != wxString::npos )
767 aliasEnd = wxString::npos;
768 }
769#else
770 filename.Replace( wxT( "\\" ), wxT( "/" ) );
771#endif
772
773 // names may not end with ':'
774 if( aliasEnd == aFileName.length() -1 )
775 return false;
776
777 if( aliasEnd != wxString::npos )
778 {
779 // ensure the alias component is not empty
780 if( aliasEnd == aliasStart )
781 return false;
782
783 lpath = filename.substr( aliasStart, aliasEnd );
784
785 // check the alias for restricted characters
786 if( wxString::npos != lpath.find_first_of( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) ) )
787 return false;
788
789 hasAlias = true;
790 lpath = aFileName.substr( aliasEnd + 1 );
791 }
792 else
793 {
794 lpath = aFileName;
795
796 // in the case of ${ENV_VAR}|$(ENV_VAR)/path, strip the
797 // environment string before testing
798 aliasEnd = wxString::npos;
799
800 if( aFileName.StartsWith( wxS( "${" ) ) )
801 aliasEnd = aFileName.find( '}' );
802 else if( aFileName.StartsWith( wxS( "$(" ) ) )
803 aliasEnd = aFileName.find( ')' );
804
805 if( aliasEnd != wxString::npos )
806 lpath = aFileName.substr( aliasEnd + 1 );
807
808 }
809
810 // Test for forbidden chars in filenames. Should be wxFileName::GetForbiddenChars()
811 // On MSW, the list returned by wxFileName::GetForbiddenChars() contains separators
812 // '\'and '/' used here because lpath can be a full path.
813 // So remove separators
814 wxString lpath_no_sep = lpath;
815
816#ifdef __WINDOWS__
817 lpath_no_sep.Replace( "/", " " );
818 lpath_no_sep.Replace( "\\", " " );
819
820 // A disk identifier is allowed, and therefore remove its separator
821 if( lpath_no_sep.Length() > 1 && lpath_no_sep[1] == ':' )
822 lpath_no_sep[1] = ' ';
823#endif
824
825 if( wxString::npos != lpath_no_sep.find_first_of( wxFileName::GetForbiddenChars() ) )
826 return false;
827
828 return true;
829}
830
831
832bool FILENAME_RESOLVER::GetKicadPaths( std::list< wxString >& paths ) const
833{
834 paths.clear();
835
836 if( !m_pgm )
837 return false;
838
839 bool hasKisys3D = false;
840
841
842 // iterate over the list of internally defined ENV VARs
843 // and add them to the paths list
846
847 while( mS != mE )
848 {
849 // filter out URLs, template directories, and known system paths
850 if( mS->first == wxS( "KICAD_PTEMPLATES" )
851 || mS->first.Matches( wxS( "KICAD*_FOOTPRINT_DIR") ) )
852 {
853 ++mS;
854 continue;
855 }
856
857 if( wxString::npos != mS->second.GetValue().find( wxS( "://" ) ) )
858 {
859 ++mS;
860 continue;
861 }
862
863 //also add the path without the ${} to act as legacy alias support for older files
864 paths.push_back( mS->first );
865
866 if( mS->first.Matches( wxS("KICAD*_3DMODEL_DIR") ) )
867 hasKisys3D = true;
868
869 ++mS;
870 }
871
872 if( !hasKisys3D )
873 paths.emplace_back( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
874
875 return true;
876}
bool ValidateFileName(const wxString &aFileName, bool &hasAlias) const
Return 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...
wxString ResolvePath(const wxString &aFileName, const wxString &aWorkingPath, std::vector< const EMBEDDED_FILES * > aEmbeddedFilesStack)
Determine the full path of the given file name.
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
List of base paths to search from.
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 m_configDir
3D configuration directory.
const std::list< SEARCH_PATH > * GetPaths() const
Return a pointer to the internal path list; the items in:load.
bool SetProject(const PROJECT *aProject, bool *flgChanged=nullptr)
Set the current KiCad project directory as the first entry in the model path list.
const PROJECT * m_project
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:103
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.cpp:802
Container for project specific data.
Definition: project.h:65
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:148
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:355
The common library.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
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)
Construct a versioned environment variable based on this KiCad major version.
Definition: env_vars.cpp:83
see class PGM_BASE
wxString m_Pathvar
Base path as stored in the configuration file.
wxString m_Pathexp
Expanded base path.
wxString m_Alias
Alias to the base path.
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39