5832
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2006, 2007 John W. Eaton |
5832
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
5832
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
5832
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_load_path_h) |
|
24 #define octave_load_path_h 1 |
|
25 |
|
26 #include <iostream> |
|
27 #include <list> |
|
28 #include <map> |
|
29 #include <string> |
|
30 |
5835
|
31 #include "pathsearch.h" |
5832
|
32 #include "str-vec.h" |
|
33 |
|
34 class |
6241
|
35 OCTINTERP_API |
5832
|
36 load_path |
|
37 { |
|
38 protected: |
|
39 |
7336
|
40 load_path (void) : dir_info_list (), fcn_map (), method_map () { } |
5832
|
41 |
|
42 public: |
|
43 |
7336
|
44 typedef void (*hook_fcn_ptr) (const std::string& dir); |
5832
|
45 |
|
46 ~load_path (void) { } |
|
47 |
6365
|
48 static void initialize (bool set_initial_path = false) |
5832
|
49 { |
|
50 if (instance_ok ()) |
6365
|
51 instance->do_initialize (set_initial_path); |
5832
|
52 } |
|
53 |
|
54 static void clear (void) |
|
55 { |
|
56 if (instance_ok ()) |
|
57 instance->do_clear (); |
|
58 } |
|
59 |
5867
|
60 static void set (const std::string& p, bool warn = false) |
5832
|
61 { |
|
62 if (instance_ok ()) |
5867
|
63 instance->do_set (p, warn); |
5832
|
64 } |
|
65 |
5867
|
66 static void append (const std::string& dir, bool warn = false) |
5832
|
67 { |
|
68 if (instance_ok ()) |
5867
|
69 instance->do_append (dir, warn); |
5832
|
70 } |
|
71 |
5867
|
72 static void prepend (const std::string& dir, bool warn = false) |
5832
|
73 { |
|
74 if (instance_ok ()) |
5867
|
75 instance->do_prepend (dir, warn); |
5832
|
76 } |
|
77 |
|
78 static bool remove (const std::string& dir) |
|
79 { |
|
80 return instance_ok () ? instance->do_remove (dir) : false; |
|
81 } |
|
82 |
|
83 static void update (void) |
|
84 { |
|
85 if (instance_ok ()) |
|
86 instance->do_update (); |
|
87 } |
|
88 |
7336
|
89 static std::string find_method (const std::string& class_name, |
|
90 const std::string& meth, |
|
91 std::string& dir_name) |
|
92 { |
|
93 return instance_ok () |
|
94 ? instance->do_find_method (class_name, meth, dir_name) : std::string (); |
|
95 } |
|
96 |
|
97 static std::string find_method (const std::string& class_name, |
|
98 const std::string& meth) |
|
99 { |
|
100 std::string dir_name; |
|
101 return find_method (class_name, meth, dir_name); |
|
102 } |
|
103 |
|
104 static std::list<std::string> methods (const std::string& class_name) |
|
105 { |
|
106 return instance_ok () |
|
107 ? instance->do_methods (class_name) : std::list<std::string> (); |
|
108 } |
|
109 |
|
110 static std::string find_fcn (const std::string& fcn, std::string& dir_name) |
|
111 { |
|
112 return instance_ok () |
|
113 ? instance->do_find_fcn (fcn, dir_name) : std::string (); |
|
114 } |
|
115 |
5832
|
116 static std::string find_fcn (const std::string& fcn) |
|
117 { |
7336
|
118 std::string dir_name; |
|
119 return find_fcn (fcn, dir_name); |
|
120 } |
|
121 |
|
122 static std::string find_private_fcn (const std::string& dir, |
|
123 const std::string& fcn) |
|
124 { |
5832
|
125 return instance_ok () |
7336
|
126 ? instance->do_find_private_fcn (dir, fcn) : std::string (); |
5832
|
127 } |
|
128 |
|
129 static std::string find_fcn_file (const std::string& fcn) |
|
130 { |
7336
|
131 std::string dir_name; |
|
132 |
5832
|
133 return instance_ok () ? |
7336
|
134 instance->do_find_fcn (fcn, dir_name, M_FILE) : std::string (); |
5832
|
135 } |
|
136 |
|
137 static std::string find_oct_file (const std::string& fcn) |
|
138 { |
7336
|
139 std::string dir_name; |
|
140 |
5832
|
141 return instance_ok () ? |
7336
|
142 instance->do_find_fcn (fcn, dir_name, OCT_FILE) : std::string (); |
5832
|
143 } |
|
144 |
5864
|
145 static std::string find_mex_file (const std::string& fcn) |
|
146 { |
7336
|
147 std::string dir_name; |
|
148 |
5864
|
149 return instance_ok () ? |
7336
|
150 instance->do_find_fcn (fcn, dir_name, MEX_FILE) : std::string (); |
5864
|
151 } |
|
152 |
5832
|
153 static std::string find_file (const std::string& file) |
|
154 { |
|
155 return instance_ok () |
|
156 ? instance->do_find_file (file) : std::string (); |
|
157 } |
|
158 |
|
159 static std::string find_first_of (const string_vector& files) |
|
160 { |
|
161 return instance_ok () ? |
|
162 instance->do_find_first_of (files) : std::string (); |
|
163 } |
|
164 |
|
165 static string_vector find_all_first_of (const string_vector& files) |
|
166 { |
|
167 return instance_ok () ? |
|
168 instance->do_find_all_first_of (files) : string_vector (); |
|
169 } |
|
170 |
|
171 static string_vector dirs (void) |
|
172 { |
|
173 return instance_ok () ? instance->do_dirs () : string_vector (); |
|
174 } |
|
175 |
|
176 static std::list<std::string> dir_list (void) |
|
177 { |
|
178 return instance_ok () |
|
179 ? instance->do_dir_list () : std::list<std::string> (); |
|
180 } |
|
181 |
|
182 static string_vector files (const std::string& dir) |
|
183 { |
|
184 return instance_ok () ? instance->do_files (dir) : string_vector (); |
|
185 } |
|
186 |
|
187 static string_vector fcn_names (void) |
|
188 { |
|
189 return instance_ok () ? instance->do_fcn_names () : string_vector (); |
|
190 } |
|
191 |
|
192 static std::string path (void) |
|
193 { |
|
194 return instance_ok () ? instance->do_path () : std::string (); |
|
195 } |
|
196 |
|
197 static void display (std::ostream& os) |
|
198 { |
|
199 if (instance_ok ()) |
|
200 instance->do_display (os); |
|
201 } |
|
202 |
7336
|
203 static void set_add_hook (hook_fcn_ptr f) { add_hook = f; } |
5832
|
204 |
7336
|
205 static void set_remove_hook (hook_fcn_ptr f) { remove_hook = f; } |
5832
|
206 |
|
207 static void set_command_line_path (const std::string& p) |
|
208 { |
5835
|
209 if (command_line_path.empty ()) |
|
210 command_line_path = p; |
|
211 else |
|
212 command_line_path += dir_path::path_sep_str + p; |
5832
|
213 } |
|
214 |
6626
|
215 static std::string system_path (void) |
|
216 { |
6630
|
217 return instance_ok () ? instance->do_system_path () : std::string (); |
6626
|
218 } |
|
219 |
5832
|
220 private: |
|
221 |
|
222 static const int M_FILE = 1; |
|
223 static const int OCT_FILE = 2; |
5864
|
224 static const int MEX_FILE = 4; |
5832
|
225 |
|
226 class dir_info |
|
227 { |
|
228 public: |
|
229 |
7336
|
230 // <FCN_NAME, TYPE> |
|
231 typedef std::map<std::string, int> fcn_file_map_type; |
|
232 |
|
233 typedef fcn_file_map_type::const_iterator const_fcn_file_map_iterator; |
|
234 typedef fcn_file_map_type::iterator fcn_file_map_iterator; |
|
235 |
|
236 // <CLASS_NAME, <FCN_NAME, TYPE>> |
|
237 typedef std::map<std::string, fcn_file_map_type> method_file_map_type; |
|
238 |
|
239 typedef method_file_map_type::const_iterator const_method_file_map_iterator; |
|
240 typedef method_file_map_type::iterator method_file_map_iterator; |
|
241 |
5832
|
242 dir_info (const std::string& d) : dir_name (d) { initialize (); } |
|
243 |
|
244 dir_info (const dir_info& di) |
|
245 : dir_name (di.dir_name), is_relative (di.is_relative), |
|
246 dir_mtime (di.dir_mtime), all_files (di.all_files), |
|
247 fcn_files (di.fcn_files), |
7336
|
248 private_file_map (di.private_file_map), |
|
249 method_file_map (di.method_file_map) { } |
5832
|
250 |
|
251 ~dir_info (void) { } |
|
252 |
|
253 dir_info& operator = (const dir_info& di) |
|
254 { |
|
255 if (&di != this) |
|
256 { |
|
257 dir_name = di.dir_name; |
|
258 is_relative = di.is_relative; |
|
259 dir_mtime = di.dir_mtime; |
|
260 all_files = di.all_files; |
|
261 fcn_files = di.fcn_files; |
7336
|
262 private_file_map = di.private_file_map; |
|
263 method_file_map = di.method_file_map; |
5832
|
264 } |
|
265 |
|
266 return *this; |
|
267 } |
|
268 |
|
269 void update (void); |
|
270 |
|
271 std::string dir_name; |
|
272 bool is_relative; |
|
273 octave_time dir_mtime; |
|
274 string_vector all_files; |
|
275 string_vector fcn_files; |
7336
|
276 fcn_file_map_type private_file_map; |
|
277 method_file_map_type method_file_map; |
5832
|
278 |
|
279 private: |
|
280 |
|
281 void initialize (void); |
|
282 |
7336
|
283 void get_file_list (const std::string& d); |
|
284 |
|
285 void get_private_file_map (const std::string& d); |
5832
|
286 |
7336
|
287 void get_method_file_map (const std::string& d, |
|
288 const std::string& class_name); |
|
289 |
|
290 friend fcn_file_map_type get_fcn_files (const std::string& d); |
5832
|
291 }; |
|
292 |
|
293 class file_info |
|
294 { |
|
295 public: |
|
296 |
|
297 file_info (const std::string& d, int t) : dir_name (d), types (t) { } |
|
298 |
|
299 file_info (const file_info& fi) |
|
300 : dir_name (fi.dir_name), types (fi.types) { } |
|
301 |
|
302 ~file_info (void) { } |
|
303 |
|
304 file_info& operator = (const file_info& fi) |
|
305 { |
|
306 if (&fi != this) |
|
307 { |
|
308 dir_name = fi.dir_name; |
|
309 types = fi.types; |
|
310 } |
|
311 |
|
312 return *this; |
|
313 } |
|
314 |
|
315 std::string dir_name; |
|
316 int types; |
|
317 }; |
|
318 |
|
319 // We maintain two ways of looking at the same information. |
|
320 // |
|
321 // First, a list of directories and the set of "public" files and |
|
322 // private files (those found in the special "private" subdirectory) |
|
323 // in each directory. |
|
324 // |
|
325 // Second, a map from file names (the union of all "public" files for all |
7336
|
326 // directories, but without filename extensions) to a list of |
5832
|
327 // corresponding information (directory name and file types). This |
|
328 // way, we can quickly find shadowed file names and look up all |
|
329 // overloaded functions (in the "@" directories used to implement |
|
330 // classes). |
|
331 |
7336
|
332 typedef std::list<dir_info> dir_info_list_type; |
|
333 |
|
334 typedef dir_info_list_type::const_iterator const_dir_info_list_iterator; |
|
335 typedef dir_info_list_type::iterator dir_info_list_iterator; |
|
336 |
|
337 typedef std::list<file_info> file_info_list_type; |
|
338 |
|
339 typedef file_info_list_type::const_iterator const_file_info_list_iterator; |
|
340 typedef file_info_list_type::iterator file_info_list_iterator; |
|
341 |
|
342 // <FCN_NAME, FILE_INFO_LIST> |
|
343 typedef std::map<std::string, file_info_list_type> fcn_map_type; |
|
344 |
|
345 typedef fcn_map_type::const_iterator const_fcn_map_iterator; |
|
346 typedef fcn_map_type::iterator fcn_map_iterator; |
5832
|
347 |
7336
|
348 // <DIR_NAME, <FCN_NAME, TYPE>> |
|
349 typedef std::map<std::string, dir_info::fcn_file_map_type> private_fcn_map_type; |
|
350 |
|
351 typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator; |
|
352 typedef private_fcn_map_type::iterator private_fcn_map_iterator; |
|
353 |
|
354 // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>> |
|
355 typedef std::map<std::string, fcn_map_type> method_map_type; |
|
356 |
|
357 typedef method_map_type::const_iterator const_method_map_iterator; |
|
358 typedef method_map_type::iterator method_map_iterator; |
|
359 |
|
360 mutable dir_info_list_type dir_info_list; |
|
361 |
|
362 mutable fcn_map_type fcn_map; |
|
363 |
|
364 mutable private_fcn_map_type private_fcn_map; |
|
365 |
|
366 mutable method_map_type method_map; |
5832
|
367 |
|
368 static load_path *instance; |
|
369 |
7336
|
370 static hook_fcn_ptr add_hook; |
5832
|
371 |
7336
|
372 static hook_fcn_ptr remove_hook; |
5832
|
373 |
|
374 static std::string command_line_path; |
|
375 |
6626
|
376 static std::string sys_path; |
|
377 |
5832
|
378 static bool instance_ok (void); |
|
379 |
|
380 const_dir_info_list_iterator find_dir_info (const std::string& dir) const; |
|
381 dir_info_list_iterator find_dir_info (const std::string& dir); |
|
382 |
|
383 bool contains (const std::string& dir) const; |
|
384 |
7336
|
385 void move_fcn_map (const std::string& dir, |
|
386 const string_vector& fcn_files, bool at_end); |
|
387 |
|
388 void move_method_map (const std::string& dir, bool at_end); |
|
389 |
5832
|
390 void move (std::list<dir_info>::iterator i, bool at_end); |
|
391 |
6365
|
392 void do_initialize (bool set_initial_path); |
5832
|
393 |
|
394 void do_clear (void); |
|
395 |
5867
|
396 void do_set (const std::string& p, bool warn); |
|
397 |
|
398 void do_append (const std::string& dir, bool warn); |
5832
|
399 |
5867
|
400 void do_prepend (const std::string& dir, bool warn); |
5832
|
401 |
5867
|
402 void do_add (const std::string& dir, bool at_end, bool warn); |
5832
|
403 |
7336
|
404 void remove_fcn_map (const std::string& dir, const string_vector& fcn_files); |
|
405 |
|
406 void remove_private_fcn_map (const std::string& dir); |
|
407 |
|
408 void remove_method_map (const std::string& dir); |
|
409 |
5832
|
410 bool do_remove (const std::string& dir); |
|
411 |
|
412 void do_update (void) const; |
|
413 |
7336
|
414 static bool |
|
415 check_file_type (std::string& fname, int type, int possible_types, |
|
416 const std::string& fcn, const char *who); |
|
417 |
5832
|
418 std::string do_find_fcn (const std::string& fcn, |
7336
|
419 std::string& dir_name, |
5864
|
420 int type = M_FILE | OCT_FILE | MEX_FILE) const; |
5832
|
421 |
7336
|
422 std::string do_find_private_fcn (const std::string& dir, |
|
423 const std::string& fcn, |
|
424 int type = M_FILE | OCT_FILE | MEX_FILE) const; |
|
425 |
|
426 std::string do_find_method (const std::string& class_name, |
|
427 const std::string& meth, |
|
428 std::string& dir_name, |
|
429 int type = M_FILE | OCT_FILE | MEX_FILE) const; |
|
430 |
|
431 std::list<std::string> do_methods (const std::string& class_name) const; |
|
432 |
5832
|
433 std::string do_find_file (const std::string& file) const; |
|
434 |
|
435 std::string do_find_first_of (const string_vector& files) const; |
|
436 |
|
437 string_vector do_find_all_first_of (const string_vector& files) const; |
|
438 |
|
439 string_vector do_dirs (void) const; |
|
440 |
|
441 std::list<std::string> do_dir_list (void) const; |
|
442 |
|
443 string_vector do_files (const std::string& dir) const; |
|
444 |
|
445 string_vector do_fcn_names (void) const; |
|
446 |
|
447 std::string do_path (void) const; |
|
448 |
7336
|
449 friend void print_types (std::ostream& os, int types); |
|
450 |
|
451 friend string_vector get_file_list (const dir_info::fcn_file_map_type& lst); |
|
452 |
|
453 friend void |
|
454 print_fcn_list (std::ostream& os, const dir_info::fcn_file_map_type& lst); |
|
455 |
5832
|
456 void do_display (std::ostream& os) const; |
|
457 |
6629
|
458 std::string do_system_path (void) const { return sys_path; } |
6626
|
459 |
5832
|
460 void add_to_fcn_map (const dir_info& di, bool at_end) const; |
7336
|
461 |
|
462 void add_to_private_fcn_map (const dir_info& di) const; |
|
463 |
|
464 void add_to_method_map (const dir_info& di, bool at_end) const; |
|
465 |
|
466 friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d); |
5832
|
467 }; |
|
468 |
|
469 extern std::string |
|
470 genpath (const std::string& dir, const string_vector& skip = "private"); |
|
471 |
|
472 extern void execute_pkg_add (const std::string& dir); |
|
473 extern void execute_pkg_del (const std::string& dir); |
|
474 |
|
475 #endif |
|
476 |
|
477 /* |
|
478 ;;; Local Variables: *** |
|
479 ;;; mode: C++ *** |
|
480 ;;; page-delimiter: "^/\\*" *** |
|
481 ;;; End: *** |
|
482 */ |