KiCad PCB EDA Suite
SEARCH_STACK Class Reference

SEARCH_STACK looks for files in a number of places. More...

#include <search_stack.h>

Inheritance diagram for SEARCH_STACK:
PROJECT::_ELEM

Public Member Functions

KICAD_T Type () override
 
wxString FilenameWithRelativePathInSearchList (const wxString &aFullFilename, const wxString &aBaseDir)
 Function FilenameWithRelativePathInSearchList returns the shortest possible path which can be use later to find a full path from this SEARCH_STACK. More...
 
wxString FindValidPath (const wxString &aFileName) const
 
void AddPaths (const wxString &aPaths, int aIndex=-1)
 Function AddPaths insert or append path(s) More...
 
void RemovePaths (const wxString &aPaths)
 Function RemovePaths removes the given path(s) from the library path list. More...
 
const wxString LastVisitedPath (const wxString &aSubPathToSearch=wxEmptyString)
 Function LastVisitedPath is a quirky function inherited from old code that seems to serve particular needs in the UI. More...
 

Static Public Member Functions

static int Split (wxArrayString *aResult, const wxString &aPathString)
 Function Split separates aPathString into individual paths. More...
 

Detailed Description

SEARCH_STACK looks for files in a number of places.

Augments wxPathList. I chose the name because it sounded like a stack of work, as a reminder that anything you put in here means searching work at some point in time. (An alternative is to simply know where something is.)

Definition at line 41 of file search_stack.h.

Member Function Documentation

◆ AddPaths()

void SEARCH_STACK::AddPaths ( const wxString &  aPaths,
int  aIndex = -1 
)

Function AddPaths insert or append path(s)

Parameters
aPaths= path or path list to add. paths must be separated by ";" on windows, or ":" | ";" on unix.
aIndex= insertion point, -1 for append.

Definition at line 119 of file search_stack.cpp.

120 {
121  bool isCS = wxFileName::IsCaseSensitive();
122  wxArrayString paths;
123 
124  Split( &paths, aPaths );
125 
126  // appending all of them, on large or negative aIndex
127  if( unsigned( aIndex ) >= GetCount() )
128  {
129  for( unsigned i=0; i<paths.GetCount(); ++i )
130  {
131  wxString path = paths[i];
132 
133  if( wxFileName::IsDirReadable( path )
134  && Index( path, isCS ) == wxNOT_FOUND )
135  {
136  Add( path );
137  }
138  }
139  }
140 
141  // inserting all of them:
142  else
143  {
144  for( unsigned i=0; i<paths.GetCount(); ++i )
145  {
146  wxString path = paths[i];
147 
148  if( wxFileName::IsDirReadable( path )
149  && Index( path, isCS ) == wxNOT_FOUND )
150  {
151  Insert( path, aIndex );
152  aIndex++;
153  }
154  }
155  }
156 }
static int Split(wxArrayString *aResult, const wxString &aPathString)
Function Split separates aPathString into individual paths.

References Split().

Referenced by add_search_paths(), PGM_BASE::InitPgm(), SYMBOL_LIB_TABLE::LoadGlobalTable(), FP_LIB_TABLE::LoadGlobalTable(), PGM_KICAD::OnPgmInit(), SearchHelpFileFullPath(), setSearchPaths(), SystemDirsAppend(), and DIALOG_GLOBAL_LIB_TABLE_CONFIG::TransferDataToWindow().

◆ FilenameWithRelativePathInSearchList()

wxString SEARCH_STACK::FilenameWithRelativePathInSearchList ( const wxString &  aFullFilename,
const wxString &  aBaseDir 
)

Function FilenameWithRelativePathInSearchList returns the shortest possible path which can be use later to find a full path from this SEARCH_STACK.

If the library path is already in the library search paths list, just add the library name to the list. Otherwise, add the library name with the full or relative path. The relative path is preferable because it preserves use of default libraries paths, when the path is a sub path of these default paths. Note we accept only sub paths not relative paths starting by ../ that are not subpaths and are outside kicad libs paths

Parameters
aFullFilenameThe filename with path and extension.
aBaseDirThe absolute path on which relative paths in this SEARCH_STACK are based.
Returns
a short filename (with extension) with only a relative path if this filename can be found in library paths

Definition at line 69 of file search_stack.cpp.

71 {
72  wxFileName fn = aFullFilename;
73  wxString filename = aFullFilename;
74 
75  unsigned pathlen = fn.GetPath().Len(); // path len, used to find the better (shortest)
76  // subpath within defaults paths
77 
78  for( unsigned kk = 0; kk < GetCount(); kk++ )
79  {
80  fn = aFullFilename;
81 
82  // Search for the shortest subpath within 'this':
83  if( fn.MakeRelativeTo( base_dir( (*this)[kk], aBaseDir ) ) )
84  {
85  if( fn.GetPathWithSep().StartsWith( wxT("..") ) ) // Path outside kicad libs paths
86  continue;
87 
88  if( pathlen > fn.GetPath().Len() ) // A better (shortest) subpath is found
89  {
90  filename = fn.GetPathWithSep() + fn.GetFullName();
91  pathlen = fn.GetPath().Len();
92  }
93  }
94  }
95 
96  return filename;
97 }
static wxString base_dir(const wxString &aRelativePath, const wxString &aBaseDir)

References base_dir().

◆ FindValidPath()

wxString SEARCH_STACK::FindValidPath ( const wxString &  aFileName) const
inline

Definition at line 73 of file search_stack.h.

74  {
75 #if 1 // might not be needed
76 
77  if( wxFileName::FileExists( aFileName ) )
78  return aFileName;
79  else
80 #endif
81  return wxPathList::FindValidPath( aFileName );
82  }

Referenced by CVPCB_MAINFRAME::buildEquivalenceList(), KICAD_MANAGER_FRAME::CreateNewProject(), GetAssociatedDocument(), PART_LIBS::LoadAllLibraries(), SYMBOL_LIB_TABLE::LoadGlobalTable(), FP_LIB_TABLE::LoadGlobalTable(), WS_DATA_MODEL::MakeFullFileName(), WS_DATA_MODEL::MakeShortFileName(), and DIALOG_GLOBAL_LIB_TABLE_CONFIG::TransferDataToWindow().

◆ LastVisitedPath()

const wxString SEARCH_STACK::LastVisitedPath ( const wxString &  aSubPathToSearch = wxEmptyString)

Function LastVisitedPath is a quirky function inherited from old code that seems to serve particular needs in the UI.

It returns what is called the last visited directory, or if aSubPathToSearch is empty, the first path in this SEARCH_STACK ( but not the CWD ).

Todo:
add more here if you can figure it out.
Parameters
aSubPathToSearchis the preferred sub path to search in path list

Definition at line 161 of file search_stack.cpp.

162 {
163  wxString path;
164 
165  // Initialize default path to the main default lib path
166  // this is the second path in list (the first is the project path).
167  unsigned pcount = GetCount();
168 
169  if( pcount )
170  {
171  unsigned ipath = 0;
172 
173  if( (*this)[0] == wxGetCwd() )
174  ipath = 1;
175 
176  // First choice of path:
177  if( ipath < pcount )
178  path = (*this)[ipath];
179 
180  // Search a sub path matching this SEARCH_PATH
181  if( !IsEmpty() )
182  {
183  for( ; ipath < pcount; ipath++ )
184  {
185  if( (*this)[ipath].Contains( aSubPathToSearch ) )
186  {
187  path = (*this)[ipath];
188  break;
189  }
190  }
191  }
192  }
193 
194  if( path.IsEmpty() )
195  path = wxGetCwd();
196 
197  return path;
198 }

Referenced by SYMBOL_EDIT_FRAME::LoadOneSymbol(), SYMBOL_EDIT_FRAME::saveLibrary(), and SYMBOL_EDIT_FRAME::SaveOneSymbol().

◆ RemovePaths()

void SEARCH_STACK::RemovePaths ( const wxString &  aPaths)

Function RemovePaths removes the given path(s) from the library path list.

Parameters
aPaths= path or list of paths to remove. If list, paths must be separated by ";" on windows, or ":" | ";" on unix.

Definition at line 100 of file search_stack.cpp.

101 {
102  bool isCS = wxFileName::IsCaseSensitive();
103  wxArrayString paths;
104 
105  Split( &paths, aPaths );
106 
107  for( unsigned i=0; i<paths.GetCount(); ++i )
108  {
109  wxString path = paths[i];
110 
111  if( Index( path, isCS ) != wxNOT_FOUND )
112  {
113  Remove( path );
114  }
115  }
116 }
static int Split(wxArrayString *aResult, const wxString &aPathString)
Function Split separates aPathString into individual paths.

References Split().

◆ Split()

int SEARCH_STACK::Split ( wxArrayString *  aResult,
const wxString &  aPathString 
)
static

Function Split separates aPathString into individual paths.

Parameters
aResultis where to put the paths, it should be empty upon entry.
aPathStringis concatonated string with interposing ';' or ':' separators.
Returns
int - the count of paths found in aPathString

Definition at line 39 of file search_stack.cpp.

40 {
41  wxStringTokenizer tokenizer( aPathString, PATH_SEPS, wxTOKEN_STRTOK );
42 
43  while( tokenizer.HasMoreTokens() )
44  {
45  wxString path = tokenizer.GetNextToken();
46 
47  aResult->Add( path );
48  }
49 
50  return aResult->GetCount();
51 }
#define PATH_SEPS

References PATH_SEPS.

Referenced by AddPaths(), and RemovePaths().

◆ Type()

KICAD_T SEARCH_STACK::Type ( )
inlineoverridevirtual

Implements PROJECT::_ELEM.

Definition at line 45 of file search_stack.h.

45 { return SEARCH_STACK_T; }

References SEARCH_STACK_T.


The documentation for this class was generated from the following files: