KiCad PCB EDA Suite
searchhelpfilefullpath.cpp File Reference
#include <pgm_base.h>
#include <common.h>
#include <config.h>
#include <macros.h>

Go to the source code of this file.

Functions

wxString FindFileInSearchPaths (const SEARCH_STACK &aStack, const wxString &aFilename, const wxArrayString *aSubdirs)
 Function FindFileInSearchPaths looks in "this" for aFilename, but first modifies every search path by appending a list of path fragments from aSubdirs. More...
 
wxString SearchHelpFileFullPath (const SEARCH_STACK &aSStack, const wxString &aBaseName)
 Function SearchHelpFileFullPath returns the help file's full path. More...
 

Function Documentation

wxString FindFileInSearchPaths ( const SEARCH_STACK aStack,
const wxString &  aFilename,
const wxArrayString *  aSubdirs 
)

Function FindFileInSearchPaths looks in "this" for aFilename, but first modifies every search path by appending a list of path fragments from aSubdirs.

That modification is not relative.

Definition at line 38 of file searchhelpfilefullpath.cpp.

References GetChars().

Referenced by SearchHelpFileFullPath().

40 {
41  wxPathList paths;
42 
43  for( unsigned i = 0; i < aStack.GetCount(); ++i )
44  {
45  wxFileName fn( aStack[i], wxEmptyString );
46 
47  if( aSubdirs )
48  {
49  for( unsigned j = 0; j < aSubdirs->GetCount(); j++ )
50  fn.AppendDir( (*aSubdirs)[j] );
51  }
52 
53  wxLogDebug( wxT( " %s" ), GetChars( fn.GetFullPath() ) );
54 
55  if( fn.DirExists() )
56  {
57  paths.Add( fn.GetPath() );
58  }
59  }
60 
61  return paths.FindValidPath( aFilename );
62 }
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
wxString SearchHelpFileFullPath ( const SEARCH_STACK aSearchStack,
const wxString &  aBaseName 
)

Function SearchHelpFileFullPath returns the help file's full path.

Return the KiCad help file with path and extension. Help files can be html (.html ext) or pdf (.pdf ext) files. A <BaseName>.html file is searched and if not found, <BaseName>.pdf file is searched in the same path. If the help file for the current locale is not found, an attempt to find the English version of the help file is made. Help file is searched in directories in this order: help/<canonical name> like help/en_GB help/<short name> like help/en help/en

Parameters
aSearchStackcontains some possible base dirs that may be above the the one actually holding aBaseName. These are starting points for nested searches.
aBaseNameis the name of the help file to search for,

without extension

.

Returns
wxEmptyString is returned if aBaseName is not found, else the full path & filename.

Definition at line 66 of file searchhelpfilefullpath.cpp.

References SEARCH_STACK::AddPaths(), FindFileInSearchPaths(), GetChars(), PGM_BASE::GetLocale(), and Pgm().

Referenced by EDA_BASE_FRAME::GetKicadHelp().

67 {
68  wxArrayString subdirs;
69  wxArrayString altsubdirs;
70  SEARCH_STACK ss = aSStack;
71 
72  // It might already be in aSStack, but why depend on other code
73  // far away when it's so easy to add it again (to our copy) as the first place to look.
74 
75  // This is CMAKE_INSTALL_PREFIX unless DEFAULT_INSTALL_PATH was defined during
76  // build configuration:
77  ss.AddPaths( wxT( DEFAULT_INSTALL_PATH ), 0 );
78 
79 #if defined(__WXMAC__)
80  ss.AddPaths( GetOSXKicadMachineDataDir() );
81  ss.AddPaths( Pgm().GetExecutablePath(), 0 );
82 
83  // OS X packages can have the help files in
84  // /Library/Application\ Support/kicad/help,
85  // and in Contents/SharedSupport/help inside the
86  // bundle.
87  // Below we account for an international subdirectory.
88  subdirs.Add( wxT( "help" ) );
89  altsubdirs.Add( wxT( "Contents" ) );
90  altsubdirs.Add( wxT( "SharedSupport" ) );
91  altsubdirs.Add( wxT( "help" ) );
92 #endif
93 
94 #if ! defined(__WXMAC__) // && defined(__linux__)
95  // This is the executable path minus the trailing bin directory used on Windows and Linux.
96  wxFileName tmp( Pgm().GetExecutablePath(), wxEmptyString );
97  wxArrayString binDirs = tmp.GetDirs();
98 
99  if( !binDirs.IsEmpty() && binDirs[ binDirs.GetCount() - 1 ].CmpNoCase( wxT( "bin" ) ) == 0 )
100  tmp.RemoveLastDir();
101 
102  ss.AddPaths( tmp.GetPath(), 0 );
103 
104  // Based on kicad-doc.bzr/CMakeLists.txt, line 20, the help files are
105  // installed into "<CMAKE_INSTALL_PREFIX>/share/doc/kicad/help" for linux.
106  // This is ${KICAD_HELP} var in that CMakeLists.txt file.
107  // Below we account for an international subdirectory.
108  subdirs.Add( wxT( "share" ) );
109  subdirs.Add( wxT( "doc" ) );
110  subdirs.Add( wxT( "kicad" ) );
111  subdirs.Add( wxT( "help" ) );
112 
113  // Based on kicad-doc.bzr/CMakeLists.txt, line 35, the help files are
114  // installed into "<CMAKE_INSTALL_PREFIX>/doc/help" for Windows.
115  // This is ${KICAD_HELP} var in that CMakeLists.txt file.
116  // Below we account for an international subdirectory.
117  altsubdirs.Add( wxT( "doc" ) );
118  altsubdirs.Add( wxT( "help" ) );
119 #endif
120 
121  // If there's a KICAD environment variable set, always use that guy's path first.
122  if( !Pgm().GetKicadEnvVariable().IsEmpty() )
123  ss.AddPaths( Pgm().GetKicadEnvVariable(), 0 );
124 
125  /* Search for a help file.
126  * we *must* find a help file.
127  * so help is searched in directories in this order:
128  * help/<canonical name> like help/en_GB
129  * help/<short name> like help/en
130  * help/en
131  */
132 
133  wxLocale* i18n = Pgm().GetLocale();
134 
135  // We try to find help file in help/<canonical name>
136  // If fails, try to find help file in help/<short canonical name>
137  // If fails, try to find help file in help/en
138  wxArrayString locale_name_dirs;
139  locale_name_dirs.Add( i18n->GetCanonicalName() ); // canonical name like fr_FR
140 
141  // wxLocale::GetName() does not return always the short name
142  locale_name_dirs.Add( i18n->GetName().BeforeLast( '_' ) ); // short canonical name like fr
143  locale_name_dirs.Add( wxT( "en" ) ); // default (en)
144 
145 #if defined(DEBUG) && 1
146  ss.Show( wxString( __func__ ) );
147  wxLogDebug( wxT( "%s: m_help_file:'%s'" ), __func__, GetChars( aBaseName ) );
148 #endif
149 
150  wxLogDebug( wxT( "Checking SEARCH_STACK for file %s" ), GetChars( aBaseName ) );
151 
152  // Help files can be html (.html ext) or pdf (.pdf ext) files.
153  // Therefore, <BaseName>.html file is searched and if not found,
154  // <BaseName>.pdf file is searched in the same paths
155  wxString fn;
156 
157  for( unsigned ii = 0; ii < locale_name_dirs.GetCount(); ii++ )
158  {
159  subdirs.Add( locale_name_dirs[ii] );
160  altsubdirs.Add( locale_name_dirs[ii] );
161 
162  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &altsubdirs );
163 
164  if( !fn.IsEmpty() )
165  break;
166 
167  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &altsubdirs );
168 
169  if( !fn.IsEmpty() )
170  break;
171 
172  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &subdirs );
173 
174  if( !fn.IsEmpty() )
175  break;
176 
177  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &subdirs );
178 
179  if( !fn.IsEmpty() )
180  break;
181 
182  subdirs.RemoveAt( subdirs.GetCount() - 1 );
183  altsubdirs.RemoveAt( altsubdirs.GetCount() - 1 );
184  }
185 
186  return fn;
187 }
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
VTBL_ENTRY wxLocale * GetLocale()
Definition: pgm_base.h:175
wxString FindFileInSearchPaths(const SEARCH_STACK &aStack, const wxString &aFilename, const wxArrayString *aSubdirs)
Function FindFileInSearchPaths looks in "this" for aFilename, but first modifies every search path by...
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void AddPaths(const wxString &aPaths, int aIndex=-1)
Function AddPaths insert or append path(s)