GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
load-path.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2006-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_load_path_h)
27 #define octave_load_path_h 1
28 
29 #include "octave-config.h"
30 
31 #include <functional>
32 #include <iosfwd>
33 #include <list>
34 #include <map>
35 #include <set>
36 #include <string>
37 
38 #include "oct-time.h"
39 #include "pathsearch.h"
40 #include "str-vec.h"
41 
42 namespace octave
43 {
44  class
45  OCTINTERP_API
46  load_path
47  {
48  public:
49 
50  load_path (interpreter& interp);
51 
52  typedef void (*hook_fcn_ptr) (const std::string& dir);
53 
54  load_path (const load_path&) = delete;
55 
56  load_path& operator = (const load_path&) = delete;
57 
58  ~load_path (void) = default;
59 
60  void initialize (bool set_initial_path = false);
61 
62  void clear (void);
63 
64  void set (const std::string& p, bool warn = false, bool is_init = false);
65 
66  void append (const std::string& dir, bool warn = false);
67 
68  void prepend (const std::string& dir, bool warn = false);
69 
70  bool remove (const std::string& dir);
71 
72  void update (void) const;
73 
74  bool contains_canonical (const std::string& dir_name) const;
75 
76  bool contains_file_in_dir (const std::string& file_name,
77  const std::string& dir_name);
78 
79  std::string find_method (const std::string& class_name,
80  const std::string& meth,
81  std::string& dir_name,
82  const std::string& pack_name = "")
83  {
84  return get_package (pack_name).find_method (class_name, meth, dir_name);
85  }
86 
87  std::string find_method (const std::string& class_name,
88  const std::string& meth,
89  const std::string& pack_name = "")
90  {
91  std::string dir_name;
92  return find_method (class_name, meth, dir_name, pack_name);
93  }
94 
95  std::list<std::string> methods (const std::string& class_name,
96  const std::string& pack_name = "")
97  {
98  return get_package (pack_name).methods (class_name);
99  }
100 
101  std::list<std::string> overloads (const std::string& meth) const;
102 
103  bool find_package (const std::string& package_name) const
104  {
105  return (package_map.find (package_name) != package_map.end ());
106  }
107 
108  std::list<std::string>
109  get_all_package_names (bool only_top_level = true) const;
110 
111  std::string find_fcn (const std::string& fcn, std::string& dir_name,
112  const std::string& pack_name = "")
113  {
114  return get_package (pack_name).find_fcn (fcn, dir_name);
115  }
116 
117  std::string find_fcn (const std::string& fcn,
118  const std::string& pack_name = "")
119  {
120  std::string dir_name;
121  return find_fcn (fcn, dir_name, pack_name);
122  }
123 
124  std::string find_private_fcn (const std::string& dir,
125  const std::string& fcn,
126  const std::string& pack_name = "")
127  {
128  return get_package (pack_name).find_private_fcn (dir, fcn);
129  }
130 
131  std::string find_fcn_file (const std::string& fcn,
132  const std::string& pack_name = "")
133  {
134  std::string dir_name;
135  return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
136  }
137 
138  std::string find_oct_file (const std::string& fcn,
139  const std::string& pack_name = "")
140  {
141  std::string dir_name;
142  return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
143  }
144 
145  std::string find_mex_file (const std::string& fcn,
146  const std::string& pack_name = "")
147  {
148  std::string dir_name;
149  return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
150  }
151 
152  std::string find_file (const std::string& file) const;
153 
154  std::string find_dir (const std::string& dir) const;
155 
156  string_vector find_matching_dirs (const std::string& dir) const;
157 
158  std::string find_first_of (const string_vector& files) const;
159 
160  string_vector find_all_first_of (const string_vector& files) const;
161 
162  string_vector dirs (void) const;
163 
164  std::list<std::string> dir_list (void) const;
165 
166  string_vector files (const std::string& dir, bool omit_exts = false) const;
167 
168  string_vector fcn_names (void) const;
169 
170  std::string path (void) const;
171 
172  void display (std::ostream& os) const;
173 
174  std::function<void (const std::string&)> get_add_hook (void)
175  {
176  return add_hook;
177  }
178 
179  std::function<void (const std::string&)> get_remove_hook (void)
180  {
181  return remove_hook;
182  }
183 
184  void set_add_hook (const std::function<void (const std::string&)>& f)
185  {
186  add_hook = f;
187  }
188 
189  void set_remove_hook (const std::function<void (const std::string&)>& f)
190  {
191  remove_hook = f;
192  }
193 
194  void execute_pkg_add (const std::string& dir);
195  void execute_pkg_del (const std::string& dir);
196 
197  void set_command_line_path (const std::string& p)
198  {
199  if (m_command_line_path.empty ())
200  m_command_line_path = p;
201  else
202  m_command_line_path += directory_path::path_sep_str () + p;
203  }
204 
205  std::string get_command_line_path (void) const
206  {
207  return m_command_line_path;
208  }
209 
210  std::string system_path (void) const { return sys_path; }
211 
212  static const int M_FILE = 1;
213  static const int OCT_FILE = 2;
214  static const int MEX_FILE = 4;
215 
216  private:
217 
218  class dir_info
219  {
220  public:
221 
222  // <FCN_NAME, TYPE>
223  typedef std::map<std::string, int> fcn_file_map_type;
224 
225  typedef fcn_file_map_type::const_iterator const_fcn_file_map_iterator;
226  typedef fcn_file_map_type::iterator fcn_file_map_iterator;
227 
228  struct class_info
229  {
230  class_info (void) : method_file_map (), private_file_map () { }
231 
232  class_info (const class_info& ci)
233  : method_file_map (ci.method_file_map),
234  private_file_map (ci.private_file_map)
235  { }
236 
237  class_info& operator = (const class_info& ci)
238  {
239  if (this != &ci)
240  {
241  method_file_map = ci.method_file_map;
242  private_file_map = ci.private_file_map;
243  }
244  return *this;
245  }
246 
247  ~class_info (void) = default;
248 
251  };
252 
253  // <CLASS_NAME, CLASS_INFO>
254  typedef std::map<std::string, class_info> method_file_map_type;
255 
256  typedef method_file_map_type::const_iterator const_method_file_map_iterator;
257  typedef method_file_map_type::iterator method_file_map_iterator;
258 
259  // <PACKAGE_NAME, DIR_INFO>
260  typedef std::map<std::string, dir_info> package_dir_map_type;
261 
262  typedef package_dir_map_type::const_iterator const_package_dir_map_iterator;
263  typedef package_dir_map_type::iterator package_dir_map_iterator;
264 
265  // This default constructor is only provided so we can create a
266  // std::map of dir_info objects. You should not use this
267  // constructor for any other purpose.
268  dir_info (void) = default;
269 
270  dir_info (const std::string& d)
271  : dir_name (d), abs_dir_name (), is_relative (false),
272  dir_mtime (), dir_time_last_checked (), all_files (), fcn_files (),
273  private_file_map (), method_file_map (), package_dir_map ()
274  {
275  initialize ();
276  }
277 
278  dir_info (const dir_info&) = default;
279 
280  ~dir_info (void) = default;
281 
282  dir_info& operator = (const dir_info&) = default;
283 
284  bool update (void);
285 
286  std::string dir_name;
287  std::string abs_dir_name;
296 
297  bool is_package (const std::string& name) const;
298 
299  private:
300 
301  void initialize (void);
302 
303  void get_file_list (const std::string& d);
304 
305  void get_private_file_map (const std::string& d);
306 
307  void get_method_file_map (const std::string& d,
308  const std::string& class_name);
309 
310  void get_package_dir (const std::string& d,
311  const std::string& package_name);
312 
313  friend fcn_file_map_type get_fcn_files (const std::string& d);
314  };
315 
316  class file_info
317  {
318  public:
319 
320  file_info (const std::string& d, int t) : dir_name (d), types (t) { }
321 
323  : dir_name (fi.dir_name), types (fi.types) { }
324 
325  ~file_info (void) = default;
326 
328  {
329  if (&fi != this)
330  {
331  dir_name = fi.dir_name;
332  types = fi.types;
333  }
334 
335  return *this;
336  }
337 
338  std::string dir_name;
339  int types;
340  };
341 
342  // We maintain two ways of looking at the same information.
343  //
344  // First, a list of directories and the set of "public" files and
345  // private files (those found in the special "private" subdirectory)
346  // in each directory.
347  //
348  // Second, a map from filenames (the union of all "public" files for all
349  // directories, but without filename extensions) to a list of
350  // corresponding information (directory name and file types). This
351  // way, we can quickly find shadowed filenames and look up all
352  // overloaded functions (in the "@" directories used to implement
353  // classes).
354 
355  typedef std::list<dir_info> dir_info_list_type;
356 
357  typedef dir_info_list_type::const_iterator const_dir_info_list_iterator;
358  typedef dir_info_list_type::iterator dir_info_list_iterator;
359 
360  typedef std::map<std::string, dir_info> abs_dir_cache_type;
361 
362  typedef abs_dir_cache_type::const_iterator const_abs_dir_cache_iterator;
363  typedef abs_dir_cache_type::iterator abs_dir_cache_iterator;
364 
365  typedef std::list<file_info> file_info_list_type;
366 
367  typedef file_info_list_type::const_iterator const_file_info_list_iterator;
368  typedef file_info_list_type::iterator file_info_list_iterator;
369 
370  // <FCN_NAME, FILE_INFO_LIST>
371  typedef std::map<std::string, file_info_list_type> fcn_map_type;
372 
373  typedef fcn_map_type::const_iterator const_fcn_map_iterator;
374  typedef fcn_map_type::iterator fcn_map_iterator;
375 
376  // <DIR_NAME, <FCN_NAME, TYPES>>
377  typedef std::map<std::string, dir_info::fcn_file_map_type>
379 
380  typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator;
381  typedef private_fcn_map_type::iterator private_fcn_map_iterator;
382 
383  // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>>
384  typedef std::map<std::string, fcn_map_type> method_map_type;
385 
386  typedef method_map_type::const_iterator const_method_map_iterator;
387  typedef method_map_type::iterator method_map_iterator;
388 
390  {
391  public:
392 
393  package_info (const std::string& package_name = "")
394  : m_package_name (package_name), dir_list (), fcn_map (),
395  private_fcn_map (),
396  method_map ()
397  { }
398 
400  : m_package_name (l.m_package_name), dir_list (l.dir_list),
401  private_fcn_map (l.private_fcn_map), method_map (l.method_map)
402  { }
403 
404  ~package_info (void) = default;
405 
406  package_info& operator = (const package_info& l)
407  {
408  if (&l != this)
409  {
410  m_package_name = l.m_package_name;
411  dir_list = l.dir_list;
412  fcn_map = l.fcn_map;
413  private_fcn_map = l.private_fcn_map;
414  method_map = l.method_map;
415  }
416 
417  return *this;
418  }
419 
420  void add (const dir_info& di, bool at_end, bool updating)
421  {
422  if (at_end)
423  dir_list.push_back (di.dir_name);
424  else
425  dir_list.push_front (di.dir_name);
426 
427  add_to_fcn_map (di, at_end, updating);
428 
429  add_to_private_fcn_map (di);
430 
431  add_to_method_map (di, at_end);
432  }
433 
434  void move (const dir_info& di, bool at_end);
435 
436  void remove (const dir_info& di);
437 
438  void clear (void)
439  {
440  dir_list.clear ();
441 
442  fcn_map.clear ();
443 
444  private_fcn_map.clear ();
445 
446  method_map.clear ();
447  }
448 
449  void display (std::ostream& out) const;
450 
451  std::string
452  find_fcn (const std::string& fcn, std::string& dir_name,
453  int type = M_FILE | OCT_FILE | MEX_FILE) const;
454 
455  std::string
456  find_private_fcn (const std::string& dir, const std::string& fcn,
457  int type = M_FILE | OCT_FILE | MEX_FILE) const;
458 
459  std::string
460  find_method (const std::string& class_name, const std::string& meth,
461  std::string& dir_name,
462  int type = M_FILE | OCT_FILE | MEX_FILE) const;
463 
464  std::list<std::string> methods (const std::string& class_name) const;
465 
466  void overloads (const std::string& meth, std::list<std::string>& l) const;
467 
468  string_vector fcn_names (void) const;
469 
470  private:
471 
472  void add_to_fcn_map (const dir_info& di, bool at_end, bool updating);
473 
474  void add_to_private_fcn_map (const dir_info& di);
475 
476  void add_to_method_map (const dir_info& di, bool at_end);
477 
478  void move_fcn_map (const std::string& dir,
479  const string_vector& fcn_files, bool at_end);
480 
481  void move_method_map (const std::string& dir, bool at_end);
482 
483  void remove_fcn_map (const std::string& dir,
484  const string_vector& fcn_files);
485 
486  void remove_private_fcn_map (const std::string& dir);
487 
488  void remove_method_map (const std::string& dir);
489 
490  bool check_file_type (std::string& fname, int type, int possible_types,
491  const std::string& fcn, const char *who) const;
492 
493  void print_types (std::ostream& os, int types) const;
494 
495  void print_fcn_list (std::ostream& os,
496  const dir_info::fcn_file_map_type& lst) const;
497 
498  std::string m_package_name;
499 
500  std::list<std::string> dir_list;
501 
503 
505 
507  };
508 
509  // <PACKAGE_NAME, PACKAGE_INFO>
510  typedef std::map<std::string, package_info> package_map_type;
511 
512  typedef package_map_type::const_iterator const_package_map_iterator;
513  typedef package_map_type::iterator package_map_iterator;
514 
516 
518 
520 
522 
523  mutable std::set<std::string> init_dirs;
524 
525  std::string m_command_line_path;
526 
527  static std::string sys_path;
528 
530 
531  std::function<void (const std::string&)> add_hook;
532 
533  std::function<void (const std::string&)> remove_hook;
534 
535  void execute_pkg_add_or_del (const std::string& dir,
536  const std::string& script_file);
537 
538  const_dir_info_list_iterator find_dir_info (const std::string& dir) const;
539  dir_info_list_iterator find_dir_info (const std::string& dir);
540 
541  bool contains (const std::string& dir) const;
542 
543  void move (dir_info_list_iterator i, bool at_end);
544 
545  void move (const dir_info& di, bool at_end, const std::string& pname = "");
546 
547  void remove (const dir_info& di, const std::string& pname = "");
548 
549  void add (const std::string& dir, bool at_end, bool warn);
550 
551  void add (const dir_info& di, bool at_end, const std::string& pname = "",
552  bool updating = false) const;
553 
554  bool is_package (const std::string& name) const;
555 
556  package_info& get_package (const std::string& name) const
557  {
558  if (! name.empty () && is_package (name))
559  {
560  package_map_iterator l = package_map.find (name);
561 
562  if (l == package_map.end ())
563  l = package_map.insert (package_map.end (),
564  package_map_type::value_type (name, package_info (name)));
565 
566  return l->second;
567  }
568 
569  return top_level_package;
570  }
571 
572  string_vector get_file_list (const dir_info::fcn_file_map_type& lst) const;
573 
574  friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d);
575  };
576 
577  extern std::string
578  genpath (const std::string& dir, const string_vector& skip = "private");
579 }
580 
581 #endif
static std::string path_sep_str(void)
Definition: pathsearch.cc:127
file_info & operator=(const file_info &)=default
method_file_map_type::const_iterator const_method_file_map_iterator
Definition: load-path.h:256
package_dir_map_type package_dir_map
Definition: load-path.h:295
package_dir_map_type::iterator package_dir_map_iterator
Definition: load-path.h:263
method_file_map_type::iterator method_file_map_iterator
Definition: load-path.h:257
fcn_file_map_type private_file_map
Definition: load-path.h:293
dir_info(const std::string &d)
Definition: load-path.h:270
fcn_file_map_type::const_iterator const_fcn_file_map_iterator
Definition: load-path.h:225
std::map< std::string, class_info > method_file_map_type
Definition: load-path.h:254
package_dir_map_type::const_iterator const_package_dir_map_iterator
Definition: load-path.h:262
std::map< std::string, int > fcn_file_map_type
Definition: load-path.h:223
std::map< std::string, dir_info > package_dir_map_type
Definition: load-path.h:260
fcn_file_map_type::iterator fcn_file_map_iterator
Definition: load-path.h:226
method_file_map_type method_file_map
Definition: load-path.h:294
dir_info(const dir_info &)=default
file_info(const std::string &d, int t)
Definition: load-path.h:320
file_info(const file_info &fi)
Definition: load-path.h:322
package_info(const package_info &l)
Definition: load-path.h:399
private_fcn_map_type private_fcn_map
Definition: load-path.h:504
void add(const dir_info &di, bool at_end, bool updating)
Definition: load-path.h:420
package_info(const std::string &package_name="")
Definition: load-path.h:393
std::list< std::string > dir_list
Definition: load-path.h:500
abs_dir_cache_type::const_iterator const_abs_dir_cache_iterator
Definition: load-path.h:362
std::string find_private_fcn(const std::string &dir, const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:124
static std::string sys_path
Definition: load-path.h:527
std::string find_method(const std::string &class_name, const std::string &meth, std::string &dir_name, const std::string &pack_name="")
Definition: load-path.h:79
std::string m_command_line_path
Definition: load-path.h:525
std::set< std::string > init_dirs
Definition: load-path.h:523
file_info_list_type::const_iterator const_file_info_list_iterator
Definition: load-path.h:367
package_info & get_package(const std::string &name) const
Definition: load-path.h:556
load_path(const load_path &)=delete
std::string find_fcn_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:131
std::map< std::string, fcn_map_type > method_map_type
Definition: load-path.h:384
method_map_type::iterator method_map_iterator
Definition: load-path.h:387
package_map_type::const_iterator const_package_map_iterator
Definition: load-path.h:512
std::function< void(const std::string &)> get_add_hook(void)
Definition: load-path.h:174
std::string find_mex_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:145
bool find_package(const std::string &package_name) const
Definition: load-path.h:103
private_fcn_map_type::iterator private_fcn_map_iterator
Definition: load-path.h:381
std::string find_fcn(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:117
std::string find_oct_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:138
fcn_map_type::iterator fcn_map_iterator
Definition: load-path.h:374
void set_remove_hook(const std::function< void(const std::string &)> &f)
Definition: load-path.h:189
std::list< std::string > methods(const std::string &class_name, const std::string &pack_name="")
Definition: load-path.h:95
std::map< std::string, dir_info > abs_dir_cache_type
Definition: load-path.h:360
std::map< std::string, package_info > package_map_type
Definition: load-path.h:510
std::string find_fcn(const std::string &fcn, std::string &dir_name, const std::string &pack_name="")
Definition: load-path.h:111
std::map< std::string, dir_info::fcn_file_map_type > private_fcn_map_type
Definition: load-path.h:378
std::map< std::string, file_info_list_type > fcn_map_type
Definition: load-path.h:371
private_fcn_map_type::const_iterator const_private_fcn_map_iterator
Definition: load-path.h:380
std::string system_path(void) const
Definition: load-path.h:210
dir_info_list_type dir_info_list
Definition: load-path.h:521
std::string find_method(const std::string &class_name, const std::string &meth, const std::string &pack_name="")
Definition: load-path.h:87
~load_path(void)=default
file_info_list_type::iterator file_info_list_iterator
Definition: load-path.h:368
std::string get_command_line_path(void) const
Definition: load-path.h:205
abs_dir_cache_type::iterator abs_dir_cache_iterator
Definition: load-path.h:363
static abs_dir_cache_type abs_dir_cache
Definition: load-path.h:529
dir_info_list_type::const_iterator const_dir_info_list_iterator
Definition: load-path.h:357
std::function< void(const std::string &)> get_remove_hook(void)
Definition: load-path.h:179
dir_info_list_type::iterator dir_info_list_iterator
Definition: load-path.h:358
interpreter & m_interpreter
Definition: load-path.h:515
package_info top_level_package
Definition: load-path.h:519
method_map_type::const_iterator const_method_map_iterator
Definition: load-path.h:386
package_map_type::iterator package_map_iterator
Definition: load-path.h:513
std::function< void(const std::string &)> add_hook
Definition: load-path.h:531
std::list< dir_info > dir_info_list_type
Definition: load-path.h:355
void set_add_hook(const std::function< void(const std::string &)> &f)
Definition: load-path.h:184
package_map_type package_map
Definition: load-path.h:517
fcn_map_type::const_iterator const_fcn_map_iterator
Definition: load-path.h:373
std::function< void(const std::string &)> remove_hook
Definition: load-path.h:533
void set_command_line_path(const std::string &p)
Definition: load-path.h:197
std::list< file_info > file_info_list_type
Definition: load-path.h:365
QString path
QString name
static std::list< std::string > find_first_of(const std::string &path, const std::list< std::string > &names, bool all)
Definition: kpse.cc:639
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
static void initialize(void)
static double f(double k, double l_nu, double c_pm)
Definition: randpoisson.cc:118
load_path::dir_info::fcn_file_map_type get_fcn_files(const std::string &d)
Definition: load-path.cc:1201
static double fi[256]
Definition: randmtzig.cc:447
std::string genpath(const std::string &dirname, const string_vector &skip)
Definition: load-path.cc:2280
class_info(const class_info &ci)
Definition: load-path.h:232