GNU Octave  9.1.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-2024 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 
43 
44 class
45 OCTINTERP_API
47 {
48 public:
49 
50  load_path (interpreter& interp);
51 
52  typedef void (*hook_fcn_ptr) (const std::string& dir);
53 
54  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (load_path)
55 
56  ~load_path () = default;
57 
58  void initialize (bool set_initial_path = false);
59 
60  void clear ();
61 
62  void set (const std::string& p, bool warn = false, bool is_init = false);
63 
64  void append (const std::string& dir, bool warn = false);
65 
66  void prepend (const std::string& dir, bool warn = false);
67 
68  bool remove (const std::string& dir);
69 
70  void update ();
71 
72  bool contains_canonical (const std::string& dir_name) const;
73 
74  bool contains_file_in_dir (const std::string& file_name,
75  const std::string& dir_name);
76 
77  std::string find_method (const std::string& class_name,
78  const std::string& meth,
79  std::string& dir_name,
80  const std::string& pack_name = "")
81  {
82  return get_package (pack_name).find_method (class_name, meth, dir_name);
83  }
84 
85  std::string find_method (const std::string& class_name,
86  const std::string& meth,
87  const std::string& pack_name = "")
88  {
89  std::string dir_name;
90  return find_method (class_name, meth, dir_name, pack_name);
91  }
92 
93  std::list<std::string> methods (const std::string& class_name,
94  const std::string& pack_name = "")
95  {
96  return get_package (pack_name).methods (class_name);
97  }
98 
99  std::list<std::string> overloads (const std::string& meth) const;
100 
101  bool find_package (const std::string& package_name) const
102  {
103  return (m_package_map.find (package_name) != m_package_map.end ());
104  }
105 
106  std::list<std::string>
107  get_all_package_names (bool only_top_level = true) const;
108 
109  std::string find_fcn (const std::string& fcn, std::string& dir_name,
110  const std::string& pack_name = "")
111  {
112  return get_package (pack_name).find_fcn (fcn, dir_name);
113  }
114 
115  std::string find_fcn (const std::string& fcn,
116  const std::string& pack_name = "")
117  {
118  std::string dir_name;
119  return find_fcn (fcn, dir_name, pack_name);
120  }
121 
122  std::string find_private_fcn (const std::string& dir,
123  const std::string& fcn,
124  const std::string& pack_name = "")
125  {
126  return get_package (pack_name).find_private_fcn (dir, fcn);
127  }
128 
129  std::string find_fcn_file (const std::string& fcn,
130  const std::string& pack_name = "")
131  {
132  std::string dir_name;
133  return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
134  }
135 
136  std::string find_oct_file (const std::string& fcn,
137  const std::string& pack_name = "")
138  {
139  std::string dir_name;
140  return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
141  }
142 
143  std::string find_mex_file (const std::string& fcn,
144  const std::string& pack_name = "")
145  {
146  std::string dir_name;
147  return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
148  }
149 
150  std::string find_file (const std::string& file) const;
151 
152  std::string find_dir (const std::string& dir) const;
153 
154  string_vector find_matching_dirs (const std::string& dir) const;
155 
156  std::string find_first_of (const string_vector& files) const;
157 
158  string_vector find_all_first_of (const string_vector& files) const;
159 
160  string_vector dirs () const;
161 
162  std::list<std::string> dir_list () const;
163 
164  string_vector files (const std::string& dir, bool omit_exts = false) const;
165 
166  string_vector fcn_names () const;
167 
168  std::string path () const;
169 
170  void display (std::ostream& os) const;
171 
172  std::function<void (const std::string&)> get_add_hook ()
173  {
174  return m_add_hook;
175  }
176 
177  std::function<void (const std::string&)> get_remove_hook ()
178  {
179  return m_remove_hook;
180  }
181 
182  void set_add_hook (const std::function<void (const std::string&)>& f)
183  {
184  m_add_hook = f;
185  }
186 
187  void set_remove_hook (const std::function<void (const std::string&)>& f)
188  {
189  m_remove_hook = f;
190  }
191 
192  void read_dir_config (const std::string& dir) const;
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 () const
206  {
207  return m_command_line_path;
208  }
209 
210  std::string system_path () const { return s_sys_path; }
211 
212  void rehash ();
213 
214  static const int M_FILE = 1;
215  static const int OCT_FILE = 2;
216  static const int MEX_FILE = 4;
217 
218 private:
219 
220  class dir_info
221  {
222  public:
223 
224  // <FCN_NAME, TYPE>
225  typedef std::map<std::string, int> fcn_file_map_type;
226 
227  typedef fcn_file_map_type::const_iterator const_fcn_file_map_iterator;
228  typedef fcn_file_map_type::iterator fcn_file_map_iterator;
229 
230  struct class_info
231  {
232  public:
233  class_info () : method_file_map (), private_file_map () { }
234 
235  class_info (const class_info& ci)
236  : method_file_map (ci.method_file_map),
237  private_file_map (ci.private_file_map)
238  { }
239 
240  class_info& operator = (const class_info& ci)
241  {
242  if (this != &ci)
243  {
244  method_file_map = ci.method_file_map;
245  private_file_map = ci.private_file_map;
246  }
247  return *this;
248  }
249 
250  ~class_info () = default;
251 
252  fcn_file_map_type method_file_map;
253  fcn_file_map_type private_file_map;
254  };
255 
256  // <CLASS_NAME, CLASS_INFO>
257  typedef std::map<std::string, class_info> method_file_map_type;
258 
259  typedef method_file_map_type::const_iterator const_method_file_map_iterator;
260  typedef method_file_map_type::iterator method_file_map_iterator;
261 
262  // <PACKAGE_NAME, DIR_INFO>
263  typedef std::map<std::string, dir_info> package_dir_map_type;
264 
265  typedef package_dir_map_type::const_iterator const_package_dir_map_iterator;
266  typedef package_dir_map_type::iterator package_dir_map_iterator;
267 
268  // This default constructor is only provided so we can create a
269  // std::map of dir_info objects. You should not use this
270  // constructor for any other purpose.
271  dir_info () = default;
272 
273  dir_info (const std::string& d)
274  : dir_name (d), abs_dir_name (), is_relative (false),
275  dir_mtime (), dir_time_last_checked (), all_files (), fcn_files (),
276  private_file_map (), method_file_map (), package_dir_map ()
277  {
278  initialize ();
279  }
280 
281  dir_info (const dir_info&) = default;
282 
283  ~dir_info () = default;
284 
285  dir_info& operator = (const dir_info&) = default;
286 
287  bool update ();
288 
289  std::string dir_name;
290  std::string abs_dir_name;
291  bool is_relative;
292  sys::file_time dir_mtime;
293  sys::file_time dir_time_last_checked;
294  string_vector all_files;
295  string_vector fcn_files;
296  fcn_file_map_type private_file_map;
297  method_file_map_type method_file_map;
298  package_dir_map_type package_dir_map;
299 
300  bool is_package (const std::string& name) const;
301 
302  private:
303 
304  void initialize ();
305 
306  void get_file_list (const std::string& d);
307 
308  void get_private_file_map (const std::string& d);
309 
310  void get_method_file_map (const std::string& d,
311  const std::string& class_name);
312 
313  void get_package_dir (const std::string& d,
314  const std::string& package_name);
315 
316  friend fcn_file_map_type get_fcn_files (const std::string& d);
317  };
318 
319  class file_info
320  {
321  public:
322 
323  file_info () = delete;
324 
325  file_info (const std::string& d, int t) : dir_name (d), types (t) { }
326 
327  file_info (const file_info& fi)
328  : dir_name (fi.dir_name), types (fi.types) { }
329 
330  ~file_info () = default;
331 
332  file_info& operator = (const file_info& fi)
333  {
334  if (&fi != this)
335  {
336  dir_name = fi.dir_name;
337  types = fi.types;
338  }
339 
340  return *this;
341  }
342 
343  std::string dir_name;
344  int types;
345  };
346 
347  // We maintain two ways of looking at the same information.
348  //
349  // First, a list of directories and the set of "public" files and
350  // private files (those found in the special "private" subdirectory)
351  // in each directory.
352  //
353  // Second, a map from filenames (the union of all "public" files for all
354  // directories, but without filename extensions) to a list of
355  // corresponding information (directory name and file types). This
356  // way, we can quickly find shadowed filenames and look up all
357  // overloaded functions (in the "@" directories used to implement
358  // classes).
359 
360  typedef std::list<dir_info> dir_info_list_type;
361 
362  typedef dir_info_list_type::const_iterator const_dir_info_list_iterator;
363  typedef dir_info_list_type::iterator dir_info_list_iterator;
364 
365  typedef std::map<std::string, dir_info> abs_dir_cache_type;
366 
367  typedef abs_dir_cache_type::const_iterator const_abs_dir_cache_iterator;
368  typedef abs_dir_cache_type::iterator abs_dir_cache_iterator;
369 
370  typedef std::list<file_info> file_info_list_type;
371 
372  typedef file_info_list_type::const_iterator const_file_info_list_iterator;
373  typedef file_info_list_type::iterator file_info_list_iterator;
374 
375  // <FCN_NAME, FILE_INFO_LIST>
376  typedef std::map<std::string, file_info_list_type> fcn_map_type;
377 
378  typedef fcn_map_type::const_iterator const_fcn_map_iterator;
379  typedef fcn_map_type::iterator fcn_map_iterator;
380 
381  // <DIR_NAME, <FCN_NAME, TYPES>>
382  typedef std::map<std::string, dir_info::fcn_file_map_type>
383  private_fcn_map_type;
384 
385  typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator;
386  typedef private_fcn_map_type::iterator private_fcn_map_iterator;
387 
388  // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>>
389  typedef std::map<std::string, fcn_map_type> method_map_type;
390 
391  typedef method_map_type::const_iterator const_method_map_iterator;
392  typedef method_map_type::iterator method_map_iterator;
393 
394  class package_info
395  {
396  public:
397 
398  package_info (const std::string& package_name = "")
399  : m_package_name (package_name), m_dir_list (), m_fcn_map (),
400  m_private_fcn_map (),
401  m_method_map ()
402  { }
403 
404  package_info (const package_info& l)
405  : m_package_name (l.m_package_name), m_dir_list (l.m_dir_list),
406  m_private_fcn_map (l.m_private_fcn_map), m_method_map (l.m_method_map)
407  { }
408 
409  ~package_info () = default;
410 
411  package_info& operator = (const package_info& l)
412  {
413  if (&l != this)
414  {
415  m_package_name = l.m_package_name;
416  m_dir_list = l.m_dir_list;
417  m_fcn_map = l.m_fcn_map;
418  m_private_fcn_map = l.m_private_fcn_map;
419  m_method_map = l.m_method_map;
420  }
421 
422  return *this;
423  }
424 
425  void add (const dir_info& di, bool at_end, bool updating)
426  {
427  if (at_end)
428  m_dir_list.push_back (di.dir_name);
429  else
430  m_dir_list.push_front (di.dir_name);
431 
432  add_to_fcn_map (di, at_end, updating);
433 
434  add_to_private_fcn_map (di);
435 
436  add_to_method_map (di, at_end);
437  }
438 
439  void move (const dir_info& di, bool at_end);
440 
441  void remove (const dir_info& di);
442 
443  void clear ()
444  {
445  m_dir_list.clear ();
446 
447  m_fcn_map.clear ();
448 
449  m_private_fcn_map.clear ();
450 
451  m_method_map.clear ();
452  }
453 
454  void display (std::ostream& out) const;
455 
456  std::string
457  find_fcn (const std::string& fcn, std::string& dir_name,
458  int type = M_FILE | OCT_FILE | MEX_FILE) const;
459 
460  std::string
461  find_private_fcn (const std::string& dir, const std::string& fcn,
462  int type = M_FILE | OCT_FILE | MEX_FILE) const;
463 
464  std::string
465  find_method (const std::string& class_name, const std::string& meth,
466  std::string& dir_name,
467  int type = M_FILE | OCT_FILE | MEX_FILE) const;
468 
469  std::list<std::string> methods (const std::string& class_name) const;
470 
471  void overloads (const std::string& meth, std::list<std::string>& l) const;
472 
473  string_vector fcn_names () const;
474 
475  private:
476 
477  void add_to_fcn_map (const dir_info& di, bool at_end, bool updating);
478 
479  void add_to_private_fcn_map (const dir_info& di);
480 
481  void add_to_method_map (const dir_info& di, bool at_end);
482 
483  void move_fcn_map (const std::string& dir,
484  const string_vector& fcn_files, bool at_end);
485 
486  void move_method_map (const std::string& dir, bool at_end);
487 
488  void remove_fcn_map (const std::string& dir,
489  const string_vector& fcn_files);
490 
491  void remove_private_fcn_map (const std::string& dir);
492 
493  void remove_method_map (const std::string& dir);
494 
495  bool check_file_type (std::string& fname, int type, int possible_types,
496  const std::string& fcn, const char *who) const;
497 
498  void print_types (std::ostream& os, int types) const;
499 
500  void print_fcn_list (std::ostream& os,
501  const dir_info::fcn_file_map_type& lst) const;
502 
503  std::string m_package_name;
504 
505  std::list<std::string> m_dir_list;
506 
507  fcn_map_type m_fcn_map;
508 
509  private_fcn_map_type m_private_fcn_map;
510 
511  method_map_type m_method_map;
512  };
513 
514  // <PACKAGE_NAME, PACKAGE_INFO>
515  typedef std::map<std::string, package_info> package_map_type;
516 
517  typedef package_map_type::const_iterator const_package_map_iterator;
518  typedef package_map_type::iterator package_map_iterator;
519 
520  std::function<void (const std::string&)> m_add_hook;
521 
522  std::function<void (const std::string&)> m_remove_hook;
523 
524  void execute_pkg_add_or_del (const std::string& dir,
525  const std::string& script_file);
526 
527  const_dir_info_list_iterator find_dir_info (const std::string& dir) const;
528  dir_info_list_iterator find_dir_info (const std::string& dir);
529 
530  bool contains (const std::string& dir) const;
531 
532  void move (dir_info_list_iterator i, bool at_end);
533 
534  void move (const dir_info& di, bool at_end, const std::string& pname = "");
535 
536  void remove (const dir_info& di, const std::string& pname = "");
537 
538  void add (const std::string& dir, bool at_end, bool warn);
539 
540  void add (const dir_info& di, bool at_end, const std::string& pname = "",
541  bool updating = false);
542 
543  bool is_package (const std::string& name) const;
544 
545  package_info& get_package (const std::string& name)
546  {
547  if (! name.empty () && is_package (name))
548  {
549  package_map_iterator l = m_package_map.find (name);
550 
551  if (l == m_package_map.end ())
552  l = m_package_map.insert (m_package_map.end (),
553  package_map_type::value_type (name, package_info (name)));
554 
555  return l->second;
556  }
557 
558  return m_top_level_package;
559  }
560 
561  string_vector get_file_list (const dir_info::fcn_file_map_type& lst) const;
562 
563  std::string find_private_file (const std::string& fname) const;
564 
565  friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d);
566 
567  //--------
568 
569  static std::string s_sys_path;
570 
571  static abs_dir_cache_type s_abs_dir_cache;
572 
573  interpreter& m_interpreter;
574 
575  package_map_type m_package_map;
576 
577  package_info m_top_level_package;
578 
579  dir_info_list_type m_dir_info_list;
580 
581  std::set<std::string> m_init_dirs;
582 
583  std::string m_command_line_path;
584 
585 };
586 
587 extern std::string
588 genpath (const std::string& dir, const string_vector& skip = "private");
589 
590 OCTAVE_END_NAMESPACE(octave)
591 
592 #endif
static std::string path_sep_str()
Definition: pathsearch.cc:134
file_info & operator=(const file_info &)=default
~file_info()=default
file_info()
Definition: file-info.h:44
std::string find_mex_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:143
std::function< void(const std::string &)> get_remove_hook()
Definition: load-path.h:177
void set_add_hook(const std::function< void(const std::string &)> &f)
Definition: load-path.h:182
std::string find_oct_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:136
void set_remove_hook(const std::function< void(const std::string &)> &f)
Definition: load-path.h:187
bool find_package(const std::string &package_name) const
Definition: load-path.h:101
std::string find_method(const std::string &class_name, const std::string &meth, const std::string &pack_name="")
Definition: load-path.h:85
~load_path()=default
std::string find_fcn(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:115
std::string system_path() const
Definition: load-path.h:210
std::string find_fcn(const std::string &fcn, std::string &dir_name, const std::string &pack_name="")
Definition: load-path.h:109
std::function< void(const std::string &)> get_add_hook()
Definition: load-path.h:172
std::string find_private_fcn(const std::string &dir, const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:122
std::string find_fcn_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:129
std::list< std::string > methods(const std::string &class_name, const std::string &pack_name="")
Definition: load-path.h:93
void set_command_line_path(const std::string &p)
Definition: load-path.h:197
std::string get_command_line_path() const
Definition: load-path.h:205
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:77
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE const F77_DBLE * f
std::string genpath(const std::string &dir, const string_vector &skip="private")
load_path::dir_info::fcn_file_map_type get_fcn_files(const std::string &d)
Definition: load-path.cc:1332
class_info(const class_info &ci)
Definition: load-path.h:235
fcn_file_map_type method_file_map
Definition: load-path.h:252
fcn_file_map_type private_file_map
Definition: load-path.h:253