GNU Octave  8.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-2023 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  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);
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 (m_package_map.find (package_name) != m_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 read_dir_config (const std::string& dir) const;
195 
196  void execute_pkg_add (const std::string& dir);
197  void execute_pkg_del (const std::string& dir);
198 
199  void set_command_line_path (const std::string& p)
200  {
201  if (m_command_line_path.empty ())
202  m_command_line_path = p;
203  else
204  m_command_line_path += directory_path::path_sep_str () + p;
205  }
206 
207  std::string get_command_line_path (void) const
208  {
209  return m_command_line_path;
210  }
211 
212  std::string system_path (void) const { return s_sys_path; }
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 (void) : 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 (void) = default;
251 
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 (void) = 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 (void) = default;
284 
285  dir_info& operator = (const dir_info&) = default;
286 
287  bool update (void);
288 
289  std::string dir_name;
290  std::string abs_dir_name;
292  sys::time dir_mtime;
299 
300  bool is_package (const std::string& name) const;
301 
302  private:
303 
304  void initialize (void);
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 (const std::string& d, int t) : dir_name (d), types (t) { }
324 
326  : dir_name (fi.dir_name), types (fi.types) { }
327 
328  ~file_info (void) = default;
329 
331  {
332  if (&fi != this)
333  {
334  dir_name = fi.dir_name;
335  types = fi.types;
336  }
337 
338  return *this;
339  }
340 
341  std::string dir_name;
342  int types;
343  };
344 
345  // We maintain two ways of looking at the same information.
346  //
347  // First, a list of directories and the set of "public" files and
348  // private files (those found in the special "private" subdirectory)
349  // in each directory.
350  //
351  // Second, a map from filenames (the union of all "public" files for all
352  // directories, but without filename extensions) to a list of
353  // corresponding information (directory name and file types). This
354  // way, we can quickly find shadowed filenames and look up all
355  // overloaded functions (in the "@" directories used to implement
356  // classes).
357 
358  typedef std::list<dir_info> dir_info_list_type;
359 
360  typedef dir_info_list_type::const_iterator const_dir_info_list_iterator;
361  typedef dir_info_list_type::iterator dir_info_list_iterator;
362 
363  typedef std::map<std::string, dir_info> abs_dir_cache_type;
364 
365  typedef abs_dir_cache_type::const_iterator const_abs_dir_cache_iterator;
366  typedef abs_dir_cache_type::iterator abs_dir_cache_iterator;
367 
368  typedef std::list<file_info> file_info_list_type;
369 
370  typedef file_info_list_type::const_iterator const_file_info_list_iterator;
371  typedef file_info_list_type::iterator file_info_list_iterator;
372 
373  // <FCN_NAME, FILE_INFO_LIST>
374  typedef std::map<std::string, file_info_list_type> fcn_map_type;
375 
376  typedef fcn_map_type::const_iterator const_fcn_map_iterator;
377  typedef fcn_map_type::iterator fcn_map_iterator;
378 
379  // <DIR_NAME, <FCN_NAME, TYPES>>
380  typedef std::map<std::string, dir_info::fcn_file_map_type>
382 
383  typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator;
384  typedef private_fcn_map_type::iterator private_fcn_map_iterator;
385 
386  // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>>
387  typedef std::map<std::string, fcn_map_type> method_map_type;
388 
389  typedef method_map_type::const_iterator const_method_map_iterator;
390  typedef method_map_type::iterator method_map_iterator;
391 
393  {
394  public:
395 
396  package_info (const std::string& package_name = "")
397  : m_package_name (package_name), m_dir_list (), m_fcn_map (),
398  m_private_fcn_map (),
399  m_method_map ()
400  { }
401 
403  : m_package_name (l.m_package_name), m_dir_list (l.m_dir_list),
404  m_private_fcn_map (l.m_private_fcn_map), m_method_map (l.m_method_map)
405  { }
406 
407  ~package_info (void) = default;
408 
409  package_info& operator = (const package_info& l)
410  {
411  if (&l != this)
412  {
413  m_package_name = l.m_package_name;
414  m_dir_list = l.m_dir_list;
415  m_fcn_map = l.m_fcn_map;
416  m_private_fcn_map = l.m_private_fcn_map;
417  m_method_map = l.m_method_map;
418  }
419 
420  return *this;
421  }
422 
423  void add (const dir_info& di, bool at_end, bool updating)
424  {
425  if (at_end)
426  m_dir_list.push_back (di.dir_name);
427  else
428  m_dir_list.push_front (di.dir_name);
429 
430  add_to_fcn_map (di, at_end, updating);
431 
432  add_to_private_fcn_map (di);
433 
434  add_to_method_map (di, at_end);
435  }
436 
437  void move (const dir_info& di, bool at_end);
438 
439  void remove (const dir_info& di);
440 
441  void clear (void)
442  {
443  m_dir_list.clear ();
444 
445  m_fcn_map.clear ();
446 
447  m_private_fcn_map.clear ();
448 
449  m_method_map.clear ();
450  }
451 
452  void display (std::ostream& out) const;
453 
454  std::string
455  find_fcn (const std::string& fcn, std::string& dir_name,
456  int type = M_FILE | OCT_FILE | MEX_FILE) const;
457 
458  std::string
459  find_private_fcn (const std::string& dir, const std::string& fcn,
460  int type = M_FILE | OCT_FILE | MEX_FILE) const;
461 
462  std::string
463  find_method (const std::string& class_name, const std::string& meth,
464  std::string& dir_name,
465  int type = M_FILE | OCT_FILE | MEX_FILE) const;
466 
467  std::list<std::string> methods (const std::string& class_name) const;
468 
469  void overloads (const std::string& meth, std::list<std::string>& l) const;
470 
471  string_vector fcn_names (void) const;
472 
473  private:
474 
475  void add_to_fcn_map (const dir_info& di, bool at_end, bool updating);
476 
477  void add_to_private_fcn_map (const dir_info& di);
478 
479  void add_to_method_map (const dir_info& di, bool at_end);
480 
481  void move_fcn_map (const std::string& dir,
482  const string_vector& fcn_files, bool at_end);
483 
484  void move_method_map (const std::string& dir, bool at_end);
485 
486  void remove_fcn_map (const std::string& dir,
487  const string_vector& fcn_files);
488 
489  void remove_private_fcn_map (const std::string& dir);
490 
491  void remove_method_map (const std::string& dir);
492 
493  bool check_file_type (std::string& fname, int type, int possible_types,
494  const std::string& fcn, const char *who) const;
495 
496  void print_types (std::ostream& os, int types) const;
497 
498  void print_fcn_list (std::ostream& os,
499  const dir_info::fcn_file_map_type& lst) const;
500 
501  std::string m_package_name;
502 
503  std::list<std::string> m_dir_list;
504 
506 
508 
510  };
511 
512  // <PACKAGE_NAME, PACKAGE_INFO>
513  typedef std::map<std::string, package_info> package_map_type;
514 
515  typedef package_map_type::const_iterator const_package_map_iterator;
516  typedef package_map_type::iterator package_map_iterator;
517 
518  std::function<void (const std::string&)> add_hook;
519 
520  std::function<void (const std::string&)> remove_hook;
521 
522  void execute_pkg_add_or_del (const std::string& dir,
523  const std::string& script_file);
524 
525  const_dir_info_list_iterator find_dir_info (const std::string& dir) const;
526  dir_info_list_iterator find_dir_info (const std::string& dir);
527 
528  bool contains (const std::string& dir) const;
529 
530  void move (dir_info_list_iterator i, bool at_end);
531 
532  void move (const dir_info& di, bool at_end, const std::string& pname = "");
533 
534  void remove (const dir_info& di, const std::string& pname = "");
535 
536  void add (const std::string& dir, bool at_end, bool warn);
537 
538  void add (const dir_info& di, bool at_end, const std::string& pname = "",
539  bool updating = false);
540 
541  bool is_package (const std::string& name) const;
542 
543  package_info& get_package (const std::string& name)
544  {
545  if (! name.empty () && is_package (name))
546  {
547  package_map_iterator l = m_package_map.find (name);
548 
549  if (l == m_package_map.end ())
550  l = m_package_map.insert (m_package_map.end (),
551  package_map_type::value_type (name, package_info (name)));
552 
553  return l->second;
554  }
555 
556  return m_top_level_package;
557  }
558 
559  string_vector get_file_list (const dir_info::fcn_file_map_type& lst) const;
560 
561  friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d);
562 
563  //--------
564 
565  static std::string s_sys_path;
566 
568 
570 
572 
574 
576 
577  std::set<std::string> m_init_dirs;
578 
579  std::string m_command_line_path;
580 
581 };
582 
583 extern std::string
584 genpath (const std::string& dir, const string_vector& skip = "private");
585 
587 
588 #endif
OCTAVE_END_NAMESPACE(octave)
static std::string path_sep_str(void)
Definition: pathsearch.cc:127
file_info & operator=(const file_info &)=default
package_dir_map_type package_dir_map
Definition: load-path.h:298
~dir_info(void)=default
dir_info(const std::string &d)
Definition: load-path.h:273
dir_info(void)=default
package_dir_map_type::iterator package_dir_map_iterator
Definition: load-path.h:266
sys::time dir_time_last_checked
Definition: load-path.h:293
method_file_map_type::iterator method_file_map_iterator
Definition: load-path.h:260
std::map< std::string, dir_info > package_dir_map_type
Definition: load-path.h:263
fcn_file_map_type::iterator fcn_file_map_iterator
Definition: load-path.h:228
std::string abs_dir_name
Definition: load-path.h:290
dir_info(const dir_info &)=default
std::string dir_name
Definition: load-path.h:289
std::map< std::string, int > fcn_file_map_type
Definition: load-path.h:225
string_vector fcn_files
Definition: load-path.h:295
fcn_file_map_type::const_iterator const_fcn_file_map_iterator
Definition: load-path.h:227
method_file_map_type::const_iterator const_method_file_map_iterator
Definition: load-path.h:259
std::map< std::string, class_info > method_file_map_type
Definition: load-path.h:257
fcn_file_map_type private_file_map
Definition: load-path.h:296
sys::time dir_mtime
Definition: load-path.h:292
package_dir_map_type::const_iterator const_package_dir_map_iterator
Definition: load-path.h:265
method_file_map_type method_file_map
Definition: load-path.h:297
string_vector all_files
Definition: load-path.h:294
file_info(const file_info &fi)
Definition: load-path.h:325
file_info(const std::string &d, int t)
Definition: load-path.h:323
std::string dir_name
Definition: load-path.h:341
~file_info(void)=default
method_map_type m_method_map
Definition: load-path.h:509
void add(const dir_info &di, bool at_end, bool updating)
Definition: load-path.h:423
package_info(const std::string &package_name="")
Definition: load-path.h:396
std::string m_package_name
Definition: load-path.h:501
private_fcn_map_type m_private_fcn_map
Definition: load-path.h:507
~package_info(void)=default
std::list< std::string > m_dir_list
Definition: load-path.h:503
package_info(const package_info &l)
Definition: load-path.h:402
fcn_map_type m_fcn_map
Definition: load-path.h:505
std::string find_mex_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:145
private_fcn_map_type::iterator private_fcn_map_iterator
Definition: load-path.h:384
~load_path(void)=default
load_path(const load_path &)=delete
abs_dir_cache_type::iterator abs_dir_cache_iterator
Definition: load-path.h:366
void set_add_hook(const std::function< void(const std::string &)> &f)
Definition: load-path.h:184
static std::string s_sys_path
Definition: load-path.h:565
std::list< file_info > file_info_list_type
Definition: load-path.h:368
method_map_type::const_iterator const_method_map_iterator
Definition: load-path.h:389
std::function< void(const std::string &)> get_add_hook(void)
Definition: load-path.h:174
dir_info_list_type::const_iterator const_dir_info_list_iterator
Definition: load-path.h:360
std::map< std::string, dir_info::fcn_file_map_type > private_fcn_map_type
Definition: load-path.h:381
std::string find_oct_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:138
std::list< dir_info > dir_info_list_type
Definition: load-path.h:358
std::map< std::string, fcn_map_type > method_map_type
Definition: load-path.h:387
void set_remove_hook(const std::function< void(const std::string &)> &f)
Definition: load-path.h:189
bool find_package(const std::string &package_name) const
Definition: load-path.h:103
std::function< void(const std::string &)> get_remove_hook(void)
Definition: load-path.h:179
std::string find_method(const std::string &class_name, const std::string &meth, const std::string &pack_name="")
Definition: load-path.h:87
dir_info_list_type m_dir_info_list
Definition: load-path.h:575
std::map< std::string, dir_info > abs_dir_cache_type
Definition: load-path.h:363
std::set< std::string > m_init_dirs
Definition: load-path.h:577
static abs_dir_cache_type s_abs_dir_cache
Definition: load-path.h:567
dir_info_list_type::iterator dir_info_list_iterator
Definition: load-path.h:361
file_info_list_type::const_iterator const_file_info_list_iterator
Definition: load-path.h:370
std::function< void(const std::string &)> add_hook
Definition: load-path.h:518
std::string find_fcn(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:117
interpreter & m_interpreter
Definition: load-path.h:569
std::map< std::string, package_info > package_map_type
Definition: load-path.h:513
package_map_type m_package_map
Definition: load-path.h:571
std::string system_path(void) const
Definition: load-path.h:212
package_info m_top_level_package
Definition: load-path.h:573
std::string find_fcn(const std::string &fcn, std::string &dir_name, const std::string &pack_name="")
Definition: load-path.h:111
std::string find_private_fcn(const std::string &dir, const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:124
std::string get_command_line_path(void) const
Definition: load-path.h:207
fcn_map_type::const_iterator const_fcn_map_iterator
Definition: load-path.h:376
abs_dir_cache_type::const_iterator const_abs_dir_cache_iterator
Definition: load-path.h:365
std::string find_fcn_file(const std::string &fcn, const std::string &pack_name="")
Definition: load-path.h:131
std::list< std::string > methods(const std::string &class_name, const std::string &pack_name="")
Definition: load-path.h:95
package_info & get_package(const std::string &name)
Definition: load-path.h:543
package_map_type::iterator package_map_iterator
Definition: load-path.h:516
std::string m_command_line_path
Definition: load-path.h:579
private_fcn_map_type::const_iterator const_private_fcn_map_iterator
Definition: load-path.h:383
void set_command_line_path(const std::string &p)
Definition: load-path.h:199
package_map_type::const_iterator const_package_map_iterator
Definition: load-path.h:515
method_map_type::iterator method_map_iterator
Definition: load-path.h:390
fcn_map_type::iterator fcn_map_iterator
Definition: load-path.h:377
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::function< void(const std::string &)> remove_hook
Definition: load-path.h:520
file_info_list_type::iterator file_info_list_iterator
Definition: load-path.h:371
std::map< std::string, file_info_list_type > fcn_map_type
Definition: load-path.h:374
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
static std::list< std::string > find_first_of(const std::string &path, const std::list< std::string > &names, bool all)
Definition: kpse.cc:651
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE const F77_DBLE * f
load_path::dir_info::fcn_file_map_type get_fcn_files(const std::string &d)
Definition: load-path.cc:1338
std::string genpath(const std::string &dir, const string_vector &skip="private")
Definition: load-path.cc:2413
static void initialize(void)
static double fi[256]
Definition: randmtzig.cc:464
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