GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
load-path.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2006-2025 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
44class OCTINTERP_API load_path
45{
46public:
47
48 load_path (interpreter& interp);
49
50 typedef void (*hook_fcn_ptr) (const std::string& dir);
51
52 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (load_path)
53
54 ~load_path () = default;
55
56 void initialize (bool set_initial_path = false);
57
58 void clear ();
59
60 void set (const std::string& p, bool warn = false, bool is_init = false);
61
62 void append (const std::string& dir, bool warn = false);
63
64 void prepend (const std::string& dir, bool warn = false);
65
66 bool remove (const std::string& dir);
67
68 void update ();
69
70 bool contains_canonical (const std::string& dir_name) const;
71
72 bool contains_file_in_dir (const std::string& file_name,
73 const std::string& dir_name);
74
75 std::string find_method (const std::string& class_name,
76 const std::string& meth,
77 std::string& dir_name,
78 const std::string& pack_name = "")
79 {
80 return get_package (pack_name).find_method (class_name, meth, dir_name);
81 }
82
83 std::string find_method (const std::string& class_name,
84 const std::string& meth,
85 const std::string& pack_name = "")
86 {
87 std::string dir_name;
88 return find_method (class_name, meth, dir_name, pack_name);
89 }
90
91 std::list<std::string> methods (const std::string& class_name,
92 const std::string& pack_name = "")
93 {
94 return get_package (pack_name).methods (class_name);
95 }
96
97 std::list<std::string> overloads (const std::string& meth) const;
98
99 bool find_package (const std::string& package_name) const
100 {
101 return (m_package_map.find (package_name) != m_package_map.end ());
102 }
103
104 std::list<std::string>
105 get_all_package_names (bool only_top_level = true) const;
106
107 std::string find_fcn (const std::string& fcn, std::string& dir_name,
108 const std::string& pack_name = "")
109 {
110 return get_package (pack_name).find_fcn (fcn, dir_name);
111 }
112
113 std::string find_fcn (const std::string& fcn,
114 const std::string& pack_name = "")
115 {
116 std::string dir_name;
117 return find_fcn (fcn, dir_name, pack_name);
118 }
119
120 std::string find_private_fcn (const std::string& dir,
121 const std::string& fcn,
122 const std::string& pack_name = "")
123 {
124 return get_package (pack_name).find_private_fcn (dir, fcn);
125 }
126
127 std::string find_fcn_file (const std::string& fcn,
128 const std::string& pack_name = "")
129 {
130 std::string dir_name;
131 return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
132 }
133
134 std::string find_oct_file (const std::string& fcn,
135 const std::string& pack_name = "")
136 {
137 std::string dir_name;
138 return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
139 }
140
141 std::string find_mex_file (const std::string& fcn,
142 const std::string& pack_name = "")
143 {
144 std::string dir_name;
145 return get_package (pack_name).find_fcn (fcn, dir_name, M_FILE);
146 }
147
148 std::string find_file (const std::string& file) const;
149
150 std::string find_dir (const std::string& dir) const;
151
152 string_vector find_matching_dirs (const std::string& dir) const;
153
154 std::string find_first_of (const string_vector& files) const;
155
156 string_vector find_all_first_of (const string_vector& files) const;
157
158 string_vector dirs () const;
159
160 std::list<std::string> dir_list () const;
161
162 string_vector files (const std::string& dir, bool omit_exts = false) const;
163
164 string_vector fcn_names () const;
165
166 std::string path () const;
167
168 void display (std::ostream& os) const;
169
170 std::function<void (const std::string&)> get_add_hook ()
171 {
172 return m_add_hook;
173 }
174
175 std::function<void (const std::string&)> get_remove_hook ()
176 {
177 return m_remove_hook;
178 }
179
180 void set_add_hook (const std::function<void (const std::string&)>& f)
181 {
182 m_add_hook = f;
183 }
184
185 void set_remove_hook (const std::function<void (const std::string&)>& f)
186 {
187 m_remove_hook = f;
188 }
189
190 void read_dir_config (const std::string& dir) const;
191
192 void execute_pkg_add (const std::string& dir);
193 void execute_pkg_del (const std::string& dir);
194
195 void set_command_line_path (const std::string& p)
196 {
197 if (m_command_line_path.empty ())
198 m_command_line_path = p;
199 else
200 m_command_line_path += directory_path::path_sep_str () + p;
201 }
202
203 std::string get_command_line_path () const
204 {
205 return m_command_line_path;
206 }
207
208 std::string system_path () const { return s_sys_path; }
209
210 void rehash ();
211
212 static const int M_FILE = 1;
213 static const int OCT_FILE = 2;
214 static const int MEX_FILE = 4;
215
216private:
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
229 {
230 public:
231 class_info () : method_file_map (), private_file_map () { }
232
234 : method_file_map (ci.method_file_map),
235 private_file_map (ci.private_file_map)
236 { }
237
238 class_info& operator = (const class_info& ci)
239 {
240 if (this != &ci)
241 {
242 method_file_map = ci.method_file_map;
243 private_file_map = ci.private_file_map;
244 }
245 return *this;
246 }
247
248 ~class_info () = default;
249
250 fcn_file_map_type method_file_map;
251 fcn_file_map_type private_file_map;
252 };
253
254 // <CLASS_NAME, CLASS_INFO>
255 typedef std::map<std::string, class_info> method_file_map_type;
256
257 typedef method_file_map_type::const_iterator const_method_file_map_iterator;
258 typedef method_file_map_type::iterator method_file_map_iterator;
259
260 // <PACKAGE_NAME, DIR_INFO>
261 typedef std::map<std::string, dir_info> package_dir_map_type;
262
263 typedef package_dir_map_type::const_iterator const_package_dir_map_iterator;
264 typedef package_dir_map_type::iterator package_dir_map_iterator;
265
266 // This default constructor is only provided so we can create a
267 // std::map of dir_info objects. You should not use this
268 // constructor for any other purpose.
269 dir_info () = default;
270
271 dir_info (const std::string& d)
272 : dir_name (d), abs_dir_name (), is_relative (false),
273 dir_mtime (), dir_time_last_checked (), all_files (), fcn_files (),
274 private_file_map (), method_file_map (), package_dir_map ()
275 {
276 initialize ();
277 }
278
279 dir_info (const dir_info&) = default;
280
281 ~dir_info () = default;
282
283 dir_info& operator = (const dir_info&) = default;
284
285 bool update ();
286
287 std::string dir_name;
288 std::string abs_dir_name;
289 bool is_relative;
290 sys::file_time dir_mtime;
291 sys::file_time dir_time_last_checked;
292 string_vector all_files;
293 string_vector fcn_files;
294 fcn_file_map_type private_file_map;
295 method_file_map_type method_file_map;
296 package_dir_map_type package_dir_map;
297
298 bool is_package (const std::string& name) const;
299
300 private:
301
302 void initialize ();
303
304 void get_file_list (const std::string& d);
305
306 void get_private_file_map (const std::string& d);
307
308 void get_method_file_map (const std::string& d,
309 const std::string& class_name);
310
311 void get_package_dir (const std::string& d,
312 const std::string& package_name);
313
314 friend fcn_file_map_type get_fcn_files (const std::string& d);
315 };
316
317 class file_info
318 {
319 public:
320
321 file_info () = delete;
322
323 file_info (const std::string& d, int t) : dir_name (d), types (t) { }
324
325 file_info (const file_info& fi)
326 : dir_name (fi.dir_name), types (fi.types) { }
327
328 ~file_info () = 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>
381 private_fcn_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
392 class package_info
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
402 package_info (const package_info& l)
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 () = 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 ()
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 () 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
505 fcn_map_type m_fcn_map;
506
507 private_fcn_map_type m_private_fcn_map;
508
509 method_map_type m_method_map;
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&)> m_add_hook;
519
520 std::function<void (const std::string&)> m_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 std::string find_private_file (const std::string& fname) const;
562
563 friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d);
564
565 //--------
566
567 static std::string s_sys_path;
568
569 static abs_dir_cache_type s_abs_dir_cache;
570
571 interpreter& m_interpreter;
572
573 package_map_type m_package_map;
574
575 package_info m_top_level_package;
576
577 dir_info_list_type m_dir_info_list;
578
579 std::set<std::string> m_init_dirs;
580
581 std::string m_command_line_path;
582
583};
584
585extern std::string
586genpath (const std::string& dir, const string_vector& skip = "private");
587
588OCTAVE_END_NAMESPACE(octave)
589
590#endif
static std::string path_sep_str()
file_info & operator=(const file_info &)=default
~file_info()=default
std::string find_mex_file(const std::string &fcn, const std::string &pack_name="")
Definition load-path.h:141
void set_add_hook(const std::function< void(const std::string &)> &f)
Definition load-path.h:180
std::string find_oct_file(const std::string &fcn, const std::string &pack_name="")
Definition load-path.h:134
std::function< void(const std::string &)> get_remove_hook()
Definition load-path.h:175
void set_remove_hook(const std::function< void(const std::string &)> &f)
Definition load-path.h:185
bool find_package(const std::string &package_name) const
Definition load-path.h:99
std::string find_method(const std::string &class_name, const std::string &meth, const std::string &pack_name="")
Definition load-path.h:83
~load_path()=default
std::list< std::string > methods(const std::string &class_name, const std::string &pack_name="")
Definition load-path.h:91
std::string find_fcn(const std::string &fcn, const std::string &pack_name="")
Definition load-path.h:113
std::string system_path() const
Definition load-path.h:208
std::string find_fcn(const std::string &fcn, std::string &dir_name, const std::string &pack_name="")
Definition load-path.h:107
std::function< void(const std::string &)> get_add_hook()
Definition load-path.h:170
std::string find_private_fcn(const std::string &dir, const std::string &fcn, const std::string &pack_name="")
Definition load-path.h:120
std::string find_fcn_file(const std::string &fcn, const std::string &pack_name="")
Definition load-path.h:127
void set_command_line_path(const std::string &p)
Definition load-path.h:195
std::string get_command_line_path() const
Definition load-path.h:203
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:75
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
load_path::dir_info::fcn_file_map_type get_fcn_files(const std::string &d)
std::string genpath(const std::string &dir, const string_vector &skip="private")
class_info(const class_info &ci)
Definition load-path.h:233
fcn_file_map_type method_file_map
Definition load-path.h:250
fcn_file_map_type private_file_map
Definition load-path.h:251