KiCad PCB EDA Suite
SEARCH_STACK Class Reference

Class 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

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

Class 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

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 117 of file search_stack.cpp.

References Split().

Referenced by add_search_paths(), PGM_KICAD::OnPgmInit(), SearchHelpFileFullPath(), setSearchPaths(), and SystemDirsAppend().

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

References base_dir().

Referenced by DIALOG_EDIT_COMPONENT_IN_LIBRARY::BrowseAndSelectDocFile().

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

Definition at line 71 of file search_stack.h.

Referenced by CVPCB_MAINFRAME::buildEquivalenceList(), copy_pro_file_template(), NETLIST_EXPORTER_PSPICE::Format(), PART_LIBS::LoadAllLibraries(), FP_LIB_TABLE::LoadGlobalTable(), SYMBOL_LIB_TABLE::LoadGlobalTable(), WORKSHEET_LAYOUT::MakeFullFileName(), and WORKSHEET_LAYOUT::MakeShortFileName().

72  {
73 #if 1 // might not be needed
74 
75  if( wxFileName::FileExists( aFileName ) )
76  return aFileName;
77  else
78 #endif
79  return wxPathList::FindValidPath( aFileName );
80  }
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 159 of file search_stack.cpp.

Referenced by DIALOG_EDIT_COMPONENT_IN_LIBRARY::BrowseAndSelectDocFile(), LIB_EDIT_FRAME::LoadOneSymbol(), LIB_EDIT_FRAME::SaveActiveLibrary(), and LIB_EDIT_FRAME::SaveOneSymbol().

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

References Split().

99 {
100  bool isCS = wxFileName::IsCaseSensitive();
101  wxArrayString paths;
102 
103  Split( &paths, aPaths );
104 
105  for( unsigned i=0; i<paths.GetCount(); ++i )
106  {
107  wxString path = paths[i];
108 
109  if( Index( path, isCS ) != wxNOT_FOUND )
110  {
111  Remove( path );
112  }
113  }
114 }
static int Split(wxArrayString *aResult, const wxString aPathString)
Function Split separates aPathString into individual paths.
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 37 of file search_stack.cpp.

References PATH_SEPS.

Referenced by AddPaths(), DIALOG_EESCHEMA_CONFIG::DIALOG_EESCHEMA_CONFIG(), and RemovePaths().

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

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