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