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 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <algorithm> |
|
28 |
|
29 #include "dir-ops.h" |
|
30 #include "file-ops.h" |
|
31 #include "file-stat.h" |
|
32 #include "oct-env.h" |
|
33 #include "pathsearch.h" |
|
34 |
|
35 #include "defaults.h" |
|
36 #include "defun.h" |
|
37 #include "input.h" |
|
38 #include "load-path.h" |
|
39 #include "pager.h" |
|
40 #include "parse.h" |
|
41 #include "toplev.h" |
|
42 #include "unwind-prot.h" |
|
43 #include "utils.h" |
|
44 |
|
45 load_path *load_path::instance = 0; |
7336
|
46 load_path::hook_fcn_ptr load_path::add_hook = execute_pkg_add; |
|
47 load_path::hook_fcn_ptr load_path::remove_hook = execute_pkg_del; |
5832
|
48 std::string load_path::command_line_path; |
6630
|
49 std::string load_path::sys_path; |
5832
|
50 |
|
51 void |
|
52 load_path::dir_info::update (void) |
|
53 { |
|
54 if (is_relative) |
|
55 initialize (); |
|
56 else |
|
57 { |
|
58 file_stat fs (dir_name); |
|
59 |
|
60 if (fs) |
|
61 { |
|
62 if (fs.mtime () != dir_mtime) |
|
63 initialize (); |
|
64 } |
|
65 else |
|
66 { |
|
67 std::string msg = fs.error (); |
|
68 warning ("load_path: %s: %s", dir_name.c_str (), msg.c_str ()); |
|
69 } |
|
70 } |
|
71 } |
|
72 |
|
73 void |
|
74 load_path::dir_info::initialize (void) |
|
75 { |
|
76 is_relative = ! octave_env::absolute_pathname (dir_name); |
|
77 |
|
78 file_stat fs (dir_name); |
|
79 |
|
80 if (fs) |
|
81 { |
|
82 dir_mtime = fs.mtime (); |
|
83 |
7336
|
84 get_file_list (dir_name); |
5832
|
85 } |
|
86 else |
|
87 { |
|
88 std::string msg = fs.error (); |
|
89 warning ("load_path: %s: %s", dir_name.c_str (), msg.c_str ()); |
|
90 } |
|
91 } |
|
92 |
7336
|
93 void |
5832
|
94 load_path::dir_info::get_file_list (const std::string& d) |
|
95 { |
|
96 dir_entry dir (d); |
|
97 |
|
98 if (dir) |
|
99 { |
|
100 string_vector flist = dir.read (); |
|
101 |
|
102 octave_idx_type len = flist.length (); |
|
103 |
|
104 all_files.resize (len); |
|
105 fcn_files.resize (len); |
|
106 |
|
107 octave_idx_type all_files_count = 0; |
|
108 octave_idx_type fcn_files_count = 0; |
|
109 |
|
110 for (octave_idx_type i = 0; i < len; i++) |
|
111 { |
|
112 std::string fname = flist[i]; |
|
113 |
7272
|
114 std::string full_name = file_ops::concat (d, fname); |
5832
|
115 |
|
116 file_stat fs (full_name); |
|
117 |
|
118 if (fs) |
|
119 { |
|
120 if (fs.is_dir ()) |
|
121 { |
7336
|
122 if (fname == "private") |
|
123 get_private_file_map (full_name); |
|
124 else if (fname[0] == '@') |
|
125 get_method_file_map (full_name, fname.substr (1)); |
5832
|
126 } |
|
127 else |
|
128 { |
|
129 all_files[all_files_count++] = fname; |
|
130 |
|
131 size_t pos = fname.rfind ('.'); |
|
132 |
|
133 if (pos != NPOS) |
|
134 { |
|
135 std::string ext = fname.substr (pos); |
|
136 |
5864
|
137 if (ext == ".m" || ext == ".oct" || ext == ".mex") |
5832
|
138 { |
|
139 std::string base = fname.substr (0, pos); |
|
140 |
|
141 if (valid_identifier (base)) |
|
142 fcn_files[fcn_files_count++] = fname; |
|
143 } |
|
144 } |
|
145 } |
|
146 } |
|
147 } |
|
148 |
|
149 all_files.resize (all_files_count); |
|
150 fcn_files.resize (fcn_files_count); |
|
151 } |
|
152 else |
|
153 { |
|
154 std::string msg = dir.error (); |
|
155 warning ("load_path: %s: %s", d.c_str (), msg.c_str ()); |
|
156 } |
|
157 } |
|
158 |
7336
|
159 load_path::dir_info::fcn_file_map_type |
|
160 get_fcn_files (const std::string& d) |
5832
|
161 { |
7336
|
162 load_path::dir_info::fcn_file_map_type retval; |
|
163 |
5832
|
164 dir_entry dir (d); |
|
165 |
|
166 if (dir) |
|
167 { |
|
168 string_vector flist = dir.read (); |
|
169 |
|
170 octave_idx_type len = flist.length (); |
|
171 |
|
172 for (octave_idx_type i = 0; i < len; i++) |
|
173 { |
|
174 std::string fname = flist[i]; |
|
175 |
|
176 std::string ext; |
|
177 std::string base = fname; |
|
178 |
|
179 size_t pos = fname.rfind ('.'); |
|
180 |
|
181 if (pos != NPOS) |
|
182 { |
|
183 base = fname.substr (0, pos); |
|
184 ext = fname.substr (pos); |
|
185 |
|
186 if (valid_identifier (base)) |
|
187 { |
|
188 int t = 0; |
|
189 |
|
190 if (ext == ".m") |
|
191 t = load_path::M_FILE; |
|
192 else if (ext == ".oct") |
|
193 t = load_path::OCT_FILE; |
5864
|
194 else if (ext == ".mex") |
|
195 t = load_path::MEX_FILE; |
5832
|
196 |
7336
|
197 retval[base] |= t; |
5832
|
198 } |
|
199 } |
|
200 } |
|
201 } |
|
202 else |
|
203 { |
|
204 std::string msg = dir.error (); |
|
205 warning ("load_path: %s: %s", d.c_str (), msg.c_str ()); |
|
206 } |
7336
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
|
211 void |
|
212 load_path::dir_info::get_private_file_map (const std::string& d) |
|
213 { |
|
214 private_file_map = get_fcn_files (d); |
|
215 } |
|
216 |
|
217 void |
|
218 load_path::dir_info::get_method_file_map (const std::string& d, |
|
219 const std::string& class_name) |
|
220 { |
|
221 method_file_map[class_name] = get_fcn_files (d); |
5832
|
222 } |
|
223 |
|
224 bool |
|
225 load_path::instance_ok (void) |
|
226 { |
|
227 bool retval = true; |
|
228 |
|
229 if (! instance) |
|
230 instance = new load_path (); |
|
231 |
|
232 if (! instance) |
|
233 { |
|
234 ::error ("unable to create load path object!"); |
|
235 |
|
236 retval = false; |
|
237 } |
|
238 |
|
239 return retval; |
|
240 } |
|
241 |
7336
|
242 // FIXME -- maybe we should also maintain a map to speed up this |
|
243 // method of access. |
|
244 |
5832
|
245 load_path::const_dir_info_list_iterator |
5919
|
246 load_path::find_dir_info (const std::string& dir_arg) const |
5832
|
247 { |
5919
|
248 std::string dir = file_ops::tilde_expand (dir_arg); |
|
249 |
5832
|
250 const_dir_info_list_iterator retval = dir_info_list.begin (); |
|
251 |
|
252 while (retval != dir_info_list.end ()) |
|
253 { |
|
254 if (retval->dir_name == dir) |
|
255 break; |
|
256 |
|
257 retval++; |
|
258 } |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
|
263 load_path::dir_info_list_iterator |
5919
|
264 load_path::find_dir_info (const std::string& dir_arg) |
5832
|
265 { |
5919
|
266 std::string dir = file_ops::tilde_expand (dir_arg); |
|
267 |
5832
|
268 dir_info_list_iterator retval = dir_info_list.begin (); |
|
269 |
|
270 while (retval != dir_info_list.end ()) |
|
271 { |
|
272 if (retval->dir_name == dir) |
|
273 break; |
|
274 |
|
275 retval++; |
|
276 } |
|
277 |
|
278 return retval; |
|
279 } |
|
280 |
|
281 bool |
|
282 load_path::contains (const std::string& dir) const |
|
283 { |
|
284 return find_dir_info (dir) != dir_info_list.end (); |
|
285 } |
|
286 |
|
287 void |
7336
|
288 load_path::move_fcn_map (const std::string& dir_name, |
|
289 const string_vector& fcn_files, bool at_end) |
5832
|
290 { |
7336
|
291 octave_idx_type len = fcn_files.length (); |
|
292 |
|
293 for (octave_idx_type k = 0; k < len; k++) |
5832
|
294 { |
7336
|
295 std::string fname = fcn_files[k]; |
5832
|
296 |
7336
|
297 std::string ext; |
|
298 std::string base = fname; |
|
299 |
|
300 size_t pos = fname.rfind ('.'); |
5832
|
301 |
7336
|
302 if (pos != NPOS) |
|
303 { |
|
304 base = fname.substr (0, pos); |
|
305 ext = fname.substr (pos); |
|
306 } |
5832
|
307 |
7336
|
308 file_info_list_type& file_info_list = fcn_map[base]; |
5832
|
309 |
7336
|
310 if (file_info_list.size () == 1) |
|
311 continue; |
|
312 else |
5832
|
313 { |
7336
|
314 for (file_info_list_iterator p = file_info_list.begin (); |
|
315 p != file_info_list.end (); |
|
316 p++) |
|
317 { |
|
318 if (p->dir_name == dir_name) |
|
319 { |
|
320 file_info fi = *p; |
5832
|
321 |
7336
|
322 file_info_list.erase (p); |
|
323 |
|
324 if (at_end) |
|
325 file_info_list.push_back (fi); |
|
326 else |
|
327 file_info_list.push_front (fi); |
5832
|
328 |
7336
|
329 break; |
|
330 } |
|
331 } |
|
332 } |
|
333 } |
|
334 } |
5832
|
335 |
7336
|
336 void |
|
337 load_path::move_method_map (const std::string& dir_name, bool at_end) |
|
338 { |
|
339 for (method_map_iterator i = method_map.begin (); |
|
340 i != method_map.end (); |
|
341 i++) |
|
342 { |
|
343 std::string class_name = i->first; |
5832
|
344 |
7336
|
345 fcn_map_type& fm = i->second; |
|
346 |
|
347 std::string full_dir_name |
|
348 = file_ops::concat (dir_name, "@" + class_name); |
|
349 |
|
350 for (fcn_map_iterator q = fm.begin (); q != fm.end (); q++) |
|
351 { |
|
352 file_info_list_type& file_info_list = q->second; |
5832
|
353 |
|
354 if (file_info_list.size () == 1) |
|
355 continue; |
|
356 else |
|
357 { |
7336
|
358 for (file_info_list_iterator p = file_info_list.begin (); |
|
359 p != file_info_list.end (); |
|
360 p++) |
5832
|
361 { |
7336
|
362 if (p->dir_name == full_dir_name) |
5832
|
363 { |
6149
|
364 file_info fi = *p; |
5832
|
365 |
|
366 file_info_list.erase (p); |
|
367 |
|
368 if (at_end) |
|
369 file_info_list.push_back (fi); |
|
370 else |
|
371 file_info_list.push_front (fi); |
|
372 |
|
373 break; |
|
374 } |
|
375 } |
|
376 } |
|
377 } |
|
378 } |
|
379 } |
|
380 |
7336
|
381 void |
|
382 load_path::move (dir_info_list_iterator i, bool at_end) |
|
383 { |
|
384 if (dir_info_list.size () > 1) |
|
385 { |
|
386 dir_info di = *i; |
|
387 |
|
388 dir_info_list.erase (i); |
|
389 |
|
390 if (at_end) |
|
391 dir_info_list.push_back (di); |
|
392 else |
|
393 dir_info_list.push_front (di); |
|
394 |
|
395 std::string dir_name = di.dir_name; |
|
396 |
|
397 move_fcn_map (dir_name, di.fcn_files, at_end); |
|
398 |
|
399 // No need to move elements of private function map. |
|
400 |
|
401 move_method_map (dir_name, at_end); |
|
402 } |
|
403 } |
|
404 |
5832
|
405 static void |
|
406 maybe_add_path_elts (std::string& path, const std::string& dir) |
|
407 { |
|
408 std::string tpath = genpath (dir); |
|
409 |
|
410 if (! tpath.empty ()) |
|
411 path += dir_path::path_sep_str + tpath; |
|
412 } |
|
413 |
|
414 void |
6365
|
415 load_path::do_initialize (bool set_initial_path) |
5832
|
416 { |
6626
|
417 sys_path = dir_path::path_sep_str; |
5832
|
418 |
6365
|
419 if (set_initial_path) |
|
420 { |
6626
|
421 maybe_add_path_elts (sys_path, Vlocal_ver_oct_file_dir); |
|
422 maybe_add_path_elts (sys_path, Vlocal_api_oct_file_dir); |
|
423 maybe_add_path_elts (sys_path, Vlocal_oct_file_dir); |
|
424 maybe_add_path_elts (sys_path, Vlocal_ver_fcn_file_dir); |
|
425 maybe_add_path_elts (sys_path, Vlocal_api_fcn_file_dir); |
|
426 maybe_add_path_elts (sys_path, Vlocal_fcn_file_dir); |
|
427 maybe_add_path_elts (sys_path, Voct_file_dir); |
|
428 maybe_add_path_elts (sys_path, Vfcn_file_dir); |
6365
|
429 } |
5832
|
430 |
|
431 std::string tpath = load_path::command_line_path; |
|
432 |
|
433 if (tpath.empty ()) |
|
434 tpath = octave_env::getenv ("OCTAVE_LOADPATH"); |
|
435 |
|
436 std::string xpath = "."; |
|
437 |
|
438 if (! tpath.empty ()) |
|
439 xpath += dir_path::path_sep_str + tpath; |
|
440 |
6626
|
441 if (sys_path != dir_path::path_sep_str) |
|
442 xpath += sys_path; |
5832
|
443 |
6119
|
444 do_set (xpath, false); |
5832
|
445 } |
|
446 |
|
447 void |
|
448 load_path::do_clear (void) |
|
449 { |
|
450 dir_info_list.clear (); |
|
451 fcn_map.clear (); |
7336
|
452 private_fcn_map.clear (); |
|
453 method_map.clear (); |
5867
|
454 |
|
455 do_append (".", false); |
5832
|
456 } |
|
457 |
|
458 static std::list<std::string> |
|
459 split_path (const std::string& p) |
|
460 { |
|
461 std::list<std::string> retval; |
|
462 |
|
463 size_t beg = 0; |
|
464 size_t end = p.find (dir_path::path_sep_char); |
|
465 |
|
466 size_t len = p.length (); |
|
467 |
|
468 while (end != NPOS) |
|
469 { |
|
470 std::string elt = p.substr (beg, end-beg); |
|
471 |
|
472 if (! elt.empty ()) |
|
473 retval.push_back (elt); |
|
474 |
|
475 beg = end + 1; |
|
476 |
|
477 if (beg == len) |
|
478 break; |
|
479 |
|
480 end = p.find (dir_path::path_sep_char, beg); |
|
481 } |
|
482 |
|
483 std::string elt = p.substr (beg); |
|
484 |
|
485 if (! elt.empty ()) |
|
486 retval.push_back (elt); |
|
487 |
|
488 return retval; |
|
489 } |
|
490 |
|
491 void |
5867
|
492 load_path::do_set (const std::string& p, bool warn) |
5832
|
493 { |
|
494 do_clear (); |
|
495 |
|
496 std::list<std::string> elts = split_path (p); |
|
497 |
|
498 // Temporarily disable add hook. |
|
499 |
5854
|
500 unwind_protect_fptr (add_hook); |
5832
|
501 |
|
502 add_hook = 0; |
|
503 |
|
504 for (std::list<std::string>::const_iterator i = elts.begin (); |
|
505 i != elts.end (); |
|
506 i++) |
5867
|
507 do_append (*i, warn); |
5832
|
508 |
|
509 // Restore add hook and execute for all newly added directories. |
|
510 |
|
511 unwind_protect::run (); |
|
512 |
|
513 for (dir_info_list_iterator i = dir_info_list.begin (); |
|
514 i != dir_info_list.end (); |
|
515 i++) |
|
516 { |
|
517 if (add_hook) |
|
518 add_hook (i->dir_name); |
|
519 } |
|
520 } |
|
521 |
|
522 void |
5867
|
523 load_path::do_append (const std::string& dir, bool warn) |
5832
|
524 { |
|
525 if (! dir.empty ()) |
5867
|
526 do_add (dir, true, warn); |
|
527 } |
5832
|
528 |
5867
|
529 void |
|
530 load_path::do_prepend (const std::string& dir, bool warn) |
|
531 { |
|
532 if (! dir.empty ()) |
|
533 do_add (dir, false, warn); |
5832
|
534 } |
|
535 |
|
536 void |
5919
|
537 load_path::do_add (const std::string& dir_arg, bool at_end, bool warn) |
5832
|
538 { |
5919
|
539 size_t len = dir_arg.length (); |
5911
|
540 |
5919
|
541 if (len > 1 && dir_arg.substr (len-2) == "//") |
5911
|
542 warning_with_id ("Octave:recursive-path-search", |
|
543 "trailing `//' is no longer special in search path elements"); |
|
544 |
5919
|
545 std::string dir = file_ops::tilde_expand (dir_arg); |
|
546 |
5867
|
547 dir_info_list_iterator i = find_dir_info (dir); |
|
548 |
|
549 if (i != dir_info_list.end ()) |
|
550 move (i, at_end); |
|
551 else |
5832
|
552 { |
5867
|
553 file_stat fs (dir); |
5832
|
554 |
5867
|
555 if (fs) |
5832
|
556 { |
5867
|
557 if (fs.is_dir ()) |
|
558 { |
|
559 dir_info di (dir); |
5832
|
560 |
5867
|
561 if (! error_state) |
|
562 { |
|
563 if (at_end) |
|
564 dir_info_list.push_back (di); |
|
565 else |
|
566 dir_info_list.push_front (di); |
|
567 |
|
568 add_to_fcn_map (di, true); |
5832
|
569 |
7336
|
570 add_to_private_fcn_map (di); |
|
571 |
|
572 add_to_method_map (di, true); |
|
573 |
5867
|
574 if (add_hook) |
|
575 add_hook (dir); |
|
576 } |
5832
|
577 } |
5867
|
578 else if (warn) |
5919
|
579 warning ("addpath: %s: not a directory", dir_arg.c_str ()); |
5832
|
580 } |
5867
|
581 else if (warn) |
|
582 { |
|
583 std::string msg = fs.error (); |
5919
|
584 warning ("addpath: %s: %s", dir_arg.c_str (), msg.c_str ()); |
5867
|
585 } |
|
586 } |
5832
|
587 |
5867
|
588 // FIXME -- is there a better way to do this? |
5832
|
589 |
5867
|
590 i = find_dir_info ("."); |
5832
|
591 |
5867
|
592 if (i != dir_info_list.end ()) |
5871
|
593 move (i, false); |
5867
|
594 else |
|
595 panic_impossible (); |
5832
|
596 } |
|
597 |
7336
|
598 void |
|
599 load_path::remove_fcn_map (const std::string& dir, |
|
600 const string_vector& fcn_files) |
|
601 { |
|
602 octave_idx_type len = fcn_files.length (); |
|
603 |
|
604 for (octave_idx_type k = 0; k < len; k++) |
|
605 { |
|
606 std::string fname = fcn_files[k]; |
|
607 |
|
608 std::string ext; |
|
609 std::string base = fname; |
|
610 |
|
611 size_t pos = fname.rfind ('.'); |
|
612 |
|
613 if (pos != NPOS) |
|
614 { |
|
615 base = fname.substr (0, pos); |
|
616 ext = fname.substr (pos); |
|
617 } |
|
618 |
|
619 file_info_list_type& file_info_list = fcn_map[base]; |
|
620 |
|
621 for (file_info_list_iterator p = file_info_list.begin (); |
|
622 p != file_info_list.end (); |
|
623 p++) |
|
624 { |
|
625 if (p->dir_name == dir) |
|
626 { |
|
627 file_info_list.erase (p); |
|
628 |
|
629 if (file_info_list.empty ()) |
|
630 fcn_map.erase (fname); |
|
631 |
|
632 break; |
|
633 } |
|
634 } |
|
635 } |
|
636 } |
|
637 |
|
638 void |
|
639 load_path::remove_private_fcn_map (const std::string& dir) |
|
640 { |
|
641 private_fcn_map_iterator p = private_fcn_map.find (dir); |
|
642 |
|
643 if (p != private_fcn_map.end ()) |
|
644 private_fcn_map.erase (p); |
|
645 } |
|
646 |
|
647 void |
|
648 load_path::remove_method_map (const std::string& dir) |
|
649 { |
|
650 for (method_map_iterator i = method_map.begin (); |
|
651 i != method_map.end (); |
|
652 i++) |
|
653 { |
|
654 std::string class_name = i->first; |
|
655 |
|
656 fcn_map_type& fm = i->second; |
|
657 |
|
658 std::string full_dir_name = file_ops::concat (dir, "@" + class_name); |
|
659 |
|
660 for (fcn_map_iterator q = fm.begin (); q != fm.end (); q++) |
|
661 { |
|
662 file_info_list_type& file_info_list = q->second; |
|
663 |
|
664 if (file_info_list.size () == 1) |
|
665 continue; |
|
666 else |
|
667 { |
|
668 for (file_info_list_iterator p = file_info_list.begin (); |
|
669 p != file_info_list.end (); |
|
670 p++) |
|
671 { |
|
672 if (p->dir_name == full_dir_name) |
|
673 { |
|
674 file_info_list.erase (p); |
|
675 |
|
676 // FIXME -- if there are no other elements, we |
|
677 // should remove this element of fm but calling |
|
678 // erase here would invalidate the iterator q. |
|
679 |
|
680 break; |
|
681 } |
|
682 } |
|
683 } |
|
684 } |
|
685 } |
|
686 } |
|
687 |
5832
|
688 bool |
5919
|
689 load_path::do_remove (const std::string& dir_arg) |
5832
|
690 { |
|
691 bool retval = false; |
|
692 |
5919
|
693 if (! dir_arg.empty ()) |
5832
|
694 { |
5919
|
695 if (dir_arg == ".") |
5867
|
696 { |
|
697 warning ("rmpath: can't remove \".\" from path"); |
5832
|
698 |
5867
|
699 // Avoid additional warnings. |
5832
|
700 retval = true; |
5867
|
701 } |
|
702 else |
|
703 { |
5919
|
704 std::string dir = file_ops::tilde_expand (dir_arg); |
|
705 |
5867
|
706 dir_info_list_iterator i = find_dir_info (dir); |
5832
|
707 |
5867
|
708 if (i != dir_info_list.end ()) |
|
709 { |
|
710 retval = true; |
5832
|
711 |
6825
|
712 if (remove_hook) |
|
713 remove_hook (dir); |
|
714 |
5867
|
715 string_vector fcn_files = i->fcn_files; |
|
716 |
|
717 dir_info_list.erase (i); |
5832
|
718 |
7336
|
719 remove_fcn_map (dir, fcn_files); |
5867
|
720 |
7336
|
721 remove_private_fcn_map (dir); |
5832
|
722 |
7336
|
723 remove_method_map (dir); |
5832
|
724 } |
|
725 } |
|
726 } |
|
727 |
|
728 return retval; |
|
729 } |
|
730 |
|
731 void |
|
732 load_path::do_update (void) const |
|
733 { |
|
734 // I don't see a better way to do this because we need to |
|
735 // preserve the correct directory ordering for new files that |
|
736 // have appeared. |
|
737 |
|
738 fcn_map.clear (); |
|
739 |
7336
|
740 private_fcn_map.clear (); |
|
741 |
|
742 method_map.clear (); |
|
743 |
5832
|
744 for (dir_info_list_iterator p = dir_info_list.begin (); |
|
745 p != dir_info_list.end (); |
|
746 p++) |
|
747 { |
|
748 dir_info& di = *p; |
|
749 |
|
750 di.update (); |
|
751 |
|
752 add_to_fcn_map (di, true); |
7336
|
753 |
|
754 add_to_private_fcn_map (di); |
|
755 |
|
756 add_to_method_map (di, true); |
5832
|
757 } |
|
758 } |
|
759 |
7336
|
760 bool |
|
761 load_path::check_file_type (std::string& fname, int type, int possible_types, |
|
762 const std::string& fcn, const char *who) |
|
763 { |
|
764 bool retval = false; |
|
765 |
|
766 if (type == load_path::OCT_FILE) |
|
767 { |
|
768 if ((type & possible_types) == load_path::OCT_FILE) |
|
769 { |
|
770 fname += ".oct"; |
|
771 retval = true; |
|
772 } |
|
773 } |
|
774 else if (type == load_path::M_FILE) |
|
775 { |
|
776 if ((type & possible_types) == load_path::M_FILE) |
|
777 { |
|
778 fname += ".m"; |
|
779 retval = true; |
|
780 } |
|
781 } |
|
782 else if (type == load_path::MEX_FILE) |
|
783 { |
|
784 if ((type & possible_types) == load_path::MEX_FILE) |
|
785 { |
|
786 fname += ".mex"; |
|
787 retval = true; |
|
788 } |
|
789 } |
|
790 else if (type == (load_path::M_FILE | load_path::OCT_FILE)) |
|
791 { |
|
792 if (possible_types & load_path::OCT_FILE) |
|
793 { |
|
794 fname += ".oct"; |
|
795 retval = true; |
|
796 } |
|
797 else if (possible_types & load_path::M_FILE) |
|
798 { |
|
799 fname += ".m"; |
|
800 retval = true; |
|
801 } |
|
802 } |
|
803 else if (type == (load_path::M_FILE | load_path::MEX_FILE)) |
|
804 { |
|
805 if (possible_types & load_path::MEX_FILE) |
|
806 { |
|
807 fname += ".mex"; |
|
808 retval = true; |
|
809 } |
|
810 else if (possible_types & load_path::M_FILE) |
|
811 { |
|
812 fname += ".m"; |
|
813 retval = true; |
|
814 } |
|
815 } |
|
816 else if (type == (load_path::OCT_FILE | load_path::MEX_FILE)) |
|
817 { |
|
818 if (possible_types & load_path::OCT_FILE) |
|
819 { |
|
820 fname += ".oct"; |
|
821 retval = true; |
|
822 } |
|
823 else if (possible_types & load_path::MEX_FILE) |
|
824 { |
|
825 fname += ".mex"; |
|
826 retval = true; |
|
827 } |
|
828 } |
|
829 else if (type == (load_path::M_FILE | load_path::OCT_FILE |
|
830 | load_path::MEX_FILE)) |
|
831 { |
|
832 if (possible_types & load_path::OCT_FILE) |
|
833 { |
|
834 fname += ".oct"; |
|
835 retval = true; |
|
836 } |
|
837 else if (possible_types & load_path::MEX_FILE) |
|
838 { |
|
839 fname += ".mex"; |
|
840 retval = true; |
|
841 } |
|
842 else if (possible_types & load_path::M_FILE) |
|
843 { |
|
844 fname += ".m"; |
|
845 retval = true; |
|
846 } |
|
847 } |
|
848 else |
|
849 error ("%s: %s: invalid type code = %d", who, fcn.c_str (), type); |
|
850 |
|
851 return retval; |
|
852 } |
|
853 |
5832
|
854 std::string |
7336
|
855 load_path::do_find_fcn (const std::string& fcn, std::string& dir_name, |
|
856 int type) const |
5832
|
857 { |
|
858 std::string retval; |
7336
|
859 |
|
860 // update (); |
5832
|
861 |
7336
|
862 dir_name = std::string (); |
5832
|
863 |
|
864 const_fcn_map_iterator p = fcn_map.find (fcn); |
|
865 |
|
866 if (p != fcn_map.end ()) |
|
867 { |
7336
|
868 const file_info_list_type& file_info_list = p->second; |
5832
|
869 |
|
870 for (const_file_info_list_iterator i = file_info_list.begin (); |
|
871 i != file_info_list.end (); |
|
872 i++) |
|
873 { |
|
874 const file_info& fi = *i; |
|
875 |
7272
|
876 retval = file_ops::concat (fi.dir_name, fcn); |
5832
|
877 |
7336
|
878 if (check_file_type (retval, type, fi.types, |
|
879 fcn, "load_path::do_find_fcn")) |
5832
|
880 { |
7336
|
881 dir_name = fi.dir_name; |
|
882 break; |
5832
|
883 } |
7336
|
884 else |
|
885 retval = std::string (); |
|
886 } |
|
887 } |
|
888 |
|
889 return retval; |
|
890 } |
|
891 |
|
892 std::string |
|
893 load_path::do_find_private_fcn (const std::string& dir, |
|
894 const std::string& fcn, int type) const |
|
895 { |
|
896 std::string retval; |
|
897 |
|
898 // update (); |
|
899 |
|
900 const_private_fcn_map_iterator q = private_fcn_map.find (dir); |
|
901 |
|
902 if (q != private_fcn_map.end ()) |
|
903 { |
|
904 const dir_info::fcn_file_map_type& m = q->second; |
|
905 |
|
906 dir_info::const_fcn_file_map_iterator p = m.find (fcn); |
|
907 |
|
908 if (p != m.end ()) |
|
909 { |
|
910 std::string fname |
|
911 = file_ops::concat (file_ops::concat (dir, "private"), fcn); |
|
912 |
|
913 if (check_file_type (fname, type, p->second, fcn, |
|
914 "load_path::find_private_fcn")) |
|
915 retval = fname; |
|
916 } |
|
917 } |
|
918 |
|
919 return retval; |
|
920 } |
|
921 |
|
922 std::string |
|
923 load_path::do_find_method (const std::string& class_name, |
|
924 const std::string& meth, |
|
925 std::string& dir_name, int type) const |
|
926 { |
|
927 std::string retval; |
|
928 |
|
929 // update (); |
|
930 |
|
931 dir_name = std::string (); |
|
932 |
|
933 const_method_map_iterator q = method_map.find (class_name); |
|
934 |
|
935 if (q != method_map.end ()) |
|
936 { |
|
937 const fcn_map_type& m = q->second; |
|
938 |
|
939 const_fcn_map_iterator p = m.find (meth); |
|
940 |
|
941 if (p != m.end ()) |
|
942 { |
|
943 const file_info_list_type& file_info_list = p->second; |
|
944 |
|
945 for (const_file_info_list_iterator i = file_info_list.begin (); |
|
946 i != file_info_list.end (); |
|
947 i++) |
5864
|
948 { |
7336
|
949 const file_info& fi = *i; |
|
950 |
|
951 retval = file_ops::concat (fi.dir_name, meth); |
|
952 |
|
953 bool found = check_file_type (retval, type, fi.types, |
|
954 meth, "load_path::do_find_method"); |
|
955 |
|
956 if (found) |
5864
|
957 { |
7336
|
958 dir_name = fi.dir_name; |
5832
|
959 break; |
|
960 } |
7336
|
961 else |
|
962 retval = std::string (); |
5864
|
963 } |
5832
|
964 } |
|
965 } |
|
966 |
|
967 return retval; |
|
968 } |
|
969 |
7336
|
970 std::list<std::string> |
|
971 load_path::do_methods (const std::string& class_name) const |
|
972 { |
|
973 std::list<std::string> retval; |
|
974 |
|
975 // update (); |
|
976 |
|
977 const_method_map_iterator q = method_map.find (class_name); |
|
978 |
|
979 if (q != method_map.end ()) |
|
980 { |
|
981 const fcn_map_type& m = q->second; |
|
982 |
|
983 for (const_fcn_map_iterator p = m.begin (); p != m.end (); p++) |
|
984 retval.push_back (p->first); |
|
985 } |
|
986 |
|
987 if (! retval.empty ()) |
|
988 retval.sort (); |
|
989 |
|
990 return retval; |
|
991 } |
|
992 |
5832
|
993 std::string |
|
994 load_path::do_find_file (const std::string& file) const |
|
995 { |
|
996 std::string retval; |
|
997 |
6850
|
998 if (file.find_first_of (file_ops::dir_sep_chars) != NPOS) |
5832
|
999 { |
6838
|
1000 if (octave_env::absolute_pathname (file) |
|
1001 || octave_env::rooted_relative_pathname (file)) |
|
1002 { |
|
1003 file_stat fs (file); |
5832
|
1004 |
6838
|
1005 if (fs.exists ()) |
|
1006 return file; |
|
1007 } |
|
1008 else |
|
1009 { |
|
1010 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
|
1011 p != dir_info_list.end (); |
|
1012 p++) |
|
1013 { |
7272
|
1014 std::string tfile = file_ops::concat (p->dir_name, file); |
5832
|
1015 |
6838
|
1016 file_stat fs (tfile); |
6159
|
1017 |
6838
|
1018 if (fs.exists ()) |
|
1019 return tfile; |
5832
|
1020 } |
|
1021 } |
|
1022 } |
6838
|
1023 else |
|
1024 { |
|
1025 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
|
1026 p != dir_info_list.end (); |
|
1027 p++) |
|
1028 { |
|
1029 string_vector all_files = p->all_files; |
6159
|
1030 |
6838
|
1031 octave_idx_type len = all_files.length (); |
|
1032 |
|
1033 for (octave_idx_type i = 0; i < len; i++) |
|
1034 { |
|
1035 if (all_files[i] == file) |
7272
|
1036 return file_ops::concat (p->dir_name, file); |
6838
|
1037 } |
|
1038 } |
|
1039 } |
5832
|
1040 |
|
1041 return retval; |
|
1042 } |
|
1043 |
|
1044 std::string |
|
1045 load_path::do_find_first_of (const string_vector& flist) const |
|
1046 { |
|
1047 std::string retval; |
|
1048 |
|
1049 std::string dir_name; |
|
1050 std::string file_name; |
|
1051 |
|
1052 octave_idx_type flen = flist.length (); |
|
1053 octave_idx_type rel_flen = 0; |
|
1054 |
|
1055 string_vector rel_flist (flen); |
|
1056 |
|
1057 for (octave_idx_type i = 0; i < flen; i++) |
|
1058 { |
|
1059 if (octave_env::absolute_pathname (flist[i])) |
|
1060 { |
|
1061 file_stat fs (flist[i]); |
|
1062 |
|
1063 if (fs.exists ()) |
|
1064 return flist[i]; |
|
1065 } |
|
1066 else |
|
1067 rel_flist[rel_flen++] = flist[i]; |
|
1068 } |
|
1069 |
|
1070 rel_flist.resize (rel_flen); |
|
1071 |
|
1072 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
|
1073 p != dir_info_list.end (); |
|
1074 p++) |
|
1075 { |
|
1076 string_vector all_files = p->all_files; |
|
1077 |
|
1078 octave_idx_type len = all_files.length (); |
|
1079 |
|
1080 for (octave_idx_type i = 0; i < len; i++) |
|
1081 { |
|
1082 for (octave_idx_type j = 0; j < rel_flen; j++) |
|
1083 { |
|
1084 if (all_files[i] == rel_flist[j]) |
|
1085 { |
|
1086 dir_name = p->dir_name; |
|
1087 file_name = rel_flist[j]; |
6159
|
1088 |
|
1089 goto done; |
5832
|
1090 } |
|
1091 } |
|
1092 } |
|
1093 } |
|
1094 |
6159
|
1095 done: |
|
1096 |
5832
|
1097 if (! dir_name.empty ()) |
7272
|
1098 retval = file_ops::concat (dir_name, file_name); |
5832
|
1099 |
|
1100 return retval; |
|
1101 } |
|
1102 |
|
1103 string_vector |
|
1104 load_path::do_find_all_first_of (const string_vector& flist) const |
|
1105 { |
|
1106 std::list<std::string> retlist; |
|
1107 |
|
1108 std::string dir_name; |
|
1109 std::string file_name; |
|
1110 |
|
1111 octave_idx_type flen = flist.length (); |
|
1112 octave_idx_type rel_flen = 0; |
|
1113 |
|
1114 string_vector rel_flist (flen); |
|
1115 |
|
1116 for (octave_idx_type i = 0; i < flen; i++) |
|
1117 { |
|
1118 if (octave_env::absolute_pathname (flist[i])) |
|
1119 { |
|
1120 file_stat fs (flist[i]); |
|
1121 |
|
1122 if (fs.exists ()) |
|
1123 retlist.push_back (flist[i]); |
|
1124 } |
|
1125 else |
|
1126 rel_flist[rel_flen++] = flist[i]; |
|
1127 } |
|
1128 |
|
1129 rel_flist.resize (rel_flen); |
|
1130 |
|
1131 for (const_dir_info_list_iterator p = dir_info_list.begin (); |
|
1132 p != dir_info_list.end (); |
|
1133 p++) |
|
1134 { |
|
1135 string_vector all_files = p->all_files; |
|
1136 |
|
1137 octave_idx_type len = all_files.length (); |
|
1138 |
|
1139 for (octave_idx_type i = 0; i < len; i++) |
|
1140 { |
|
1141 for (octave_idx_type j = 0; j < rel_flen; j++) |
|
1142 { |
|
1143 if (all_files[i] == rel_flist[j]) |
|
1144 retlist.push_back |
7272
|
1145 (file_ops::concat (p->dir_name, rel_flist[j])); |
5832
|
1146 } |
|
1147 } |
|
1148 } |
|
1149 |
|
1150 size_t retsize = retlist.size (); |
|
1151 |
|
1152 string_vector retval (retsize); |
|
1153 |
|
1154 for (size_t i = 0; i < retsize; i++) |
|
1155 { |
|
1156 retval[i] = retlist.front (); |
|
1157 |
|
1158 retlist.pop_front (); |
|
1159 } |
|
1160 |
|
1161 return retval; |
|
1162 } |
|
1163 |
|
1164 string_vector |
|
1165 load_path::do_dirs (void) const |
|
1166 { |
|
1167 size_t len = dir_info_list.size (); |
|
1168 |
|
1169 string_vector retval (len); |
|
1170 |
|
1171 octave_idx_type k = 0; |
|
1172 |
|
1173 for (const_dir_info_list_iterator i = dir_info_list.begin (); |
|
1174 i != dir_info_list.end (); |
|
1175 i++) |
|
1176 retval[k++] = i->dir_name; |
|
1177 |
|
1178 return retval; |
|
1179 } |
|
1180 |
|
1181 std::list<std::string> |
|
1182 load_path::do_dir_list (void) const |
|
1183 { |
|
1184 std::list<std::string> retval; |
|
1185 |
|
1186 for (const_dir_info_list_iterator i = dir_info_list.begin (); |
|
1187 i != dir_info_list.end (); |
|
1188 i++) |
|
1189 retval.push_back (i->dir_name); |
|
1190 |
|
1191 return retval; |
|
1192 } |
|
1193 |
|
1194 string_vector |
|
1195 load_path::do_files (const std::string& dir) const |
|
1196 { |
|
1197 string_vector retval; |
|
1198 |
|
1199 const_dir_info_list_iterator i = find_dir_info (dir); |
|
1200 |
|
1201 if (i != dir_info_list.end ()) |
|
1202 retval = i->fcn_files; |
|
1203 |
|
1204 return retval; |
|
1205 } |
|
1206 |
|
1207 string_vector |
|
1208 load_path::do_fcn_names (void) const |
|
1209 { |
|
1210 size_t len = fcn_map.size (); |
|
1211 |
|
1212 string_vector retval (len); |
|
1213 |
|
1214 octave_idx_type count = 0; |
|
1215 |
|
1216 for (const_fcn_map_iterator p = fcn_map.begin (); |
|
1217 p != fcn_map.end (); |
|
1218 p++) |
|
1219 retval[count++] = p->first; |
|
1220 |
|
1221 return retval; |
|
1222 } |
|
1223 |
|
1224 std::string |
|
1225 load_path::do_path (void) const |
|
1226 { |
|
1227 std::string xpath; |
|
1228 |
|
1229 string_vector xdirs = load_path::dirs (); |
|
1230 |
|
1231 octave_idx_type len = xdirs.length (); |
|
1232 |
|
1233 if (len > 0) |
|
1234 xpath = xdirs[0]; |
|
1235 |
|
1236 for (octave_idx_type i = 1; i < len; i++) |
|
1237 xpath += dir_path::path_sep_str + xdirs[i]; |
|
1238 |
|
1239 return xpath; |
|
1240 } |
|
1241 |
|
1242 void |
7336
|
1243 print_types (std::ostream& os, int types) |
|
1244 { |
|
1245 bool printed_type = false; |
|
1246 |
|
1247 if (types & load_path::OCT_FILE) |
|
1248 { |
|
1249 os << "oct"; |
|
1250 printed_type = true; |
|
1251 } |
|
1252 |
|
1253 if (types & load_path::MEX_FILE) |
|
1254 { |
|
1255 if (printed_type) |
|
1256 os << "|"; |
|
1257 os << "mex"; |
|
1258 printed_type = true; |
|
1259 } |
|
1260 |
|
1261 if (types & load_path::M_FILE) |
|
1262 { |
|
1263 if (printed_type) |
|
1264 os << "|"; |
|
1265 os << "m"; |
|
1266 printed_type = true; |
|
1267 } |
|
1268 } |
|
1269 |
|
1270 void |
|
1271 print_fcn_list (std::ostream& os, |
|
1272 const load_path::dir_info::fcn_file_map_type& lst) |
|
1273 { |
|
1274 for (load_path::dir_info::const_fcn_file_map_iterator p = lst.begin (); |
|
1275 p != lst.end (); |
|
1276 p++) |
|
1277 { |
|
1278 os << " " << p->first << " ("; |
|
1279 |
|
1280 print_types (os, p->second); |
|
1281 |
|
1282 os << ")\n"; |
|
1283 } |
|
1284 } |
|
1285 |
|
1286 string_vector |
|
1287 get_file_list (const load_path::dir_info::fcn_file_map_type& lst) |
|
1288 { |
|
1289 octave_idx_type n = lst.size (); |
|
1290 |
|
1291 string_vector retval (n); |
|
1292 |
|
1293 octave_idx_type count = 0; |
|
1294 |
|
1295 for (load_path::dir_info::const_fcn_file_map_iterator p = lst.begin (); |
|
1296 p != lst.end (); |
|
1297 p++) |
|
1298 { |
|
1299 std::string nm = p->first; |
|
1300 |
|
1301 int types = p->second; |
|
1302 |
|
1303 if (types & load_path::OCT_FILE) |
|
1304 nm += ".oct"; |
|
1305 else if (types & load_path::MEX_FILE) |
|
1306 nm += ".mex"; |
|
1307 else |
|
1308 nm += ".m"; |
|
1309 |
|
1310 retval[count++] = nm; |
|
1311 } |
|
1312 |
|
1313 return retval; |
|
1314 } |
|
1315 |
|
1316 void |
5832
|
1317 load_path::do_display (std::ostream& os) const |
|
1318 { |
|
1319 for (const_dir_info_list_iterator i = dir_info_list.begin (); |
|
1320 i != dir_info_list.end (); |
|
1321 i++) |
|
1322 { |
|
1323 string_vector fcn_files = i->fcn_files; |
|
1324 |
|
1325 if (! fcn_files.empty ()) |
|
1326 { |
|
1327 os << "\n*** function files in " << i->dir_name << ":\n\n"; |
|
1328 |
|
1329 fcn_files.list_in_columns (os); |
|
1330 } |
|
1331 |
7336
|
1332 const dir_info::method_file_map_type& method_file_map |
|
1333 = i->method_file_map; |
5832
|
1334 |
7336
|
1335 if (! method_file_map.empty ()) |
5832
|
1336 { |
7336
|
1337 for (dir_info::const_method_file_map_iterator p = method_file_map.begin (); |
|
1338 p != method_file_map.end (); |
5832
|
1339 p++) |
|
1340 { |
7336
|
1341 os << "\n*** methods in " << i->dir_name |
|
1342 << "/@" << p->first << ":\n\n"; |
5832
|
1343 |
7336
|
1344 string_vector method_files = get_file_list (p->second); |
5832
|
1345 |
7336
|
1346 method_files.list_in_columns (os); |
|
1347 } |
|
1348 } |
|
1349 } |
5864
|
1350 |
7336
|
1351 for (const_private_fcn_map_iterator i = private_fcn_map.begin (); |
|
1352 i != private_fcn_map.end (); i++) |
|
1353 { |
|
1354 os << "\n*** private functions in " |
|
1355 << file_ops::concat (i->first, "private") << ":\n\n"; |
5832
|
1356 |
7336
|
1357 print_fcn_list (os, i->second); |
5832
|
1358 } |
|
1359 |
|
1360 #if defined (DEBUG_LOAD_PATH) |
|
1361 |
|
1362 for (const_fcn_map_iterator i = fcn_map.begin (); |
|
1363 i != fcn_map.end (); |
|
1364 i++) |
|
1365 { |
|
1366 os << i->first << ":\n"; |
|
1367 |
7336
|
1368 const file_info_list_type& file_info_list = i->second; |
5832
|
1369 |
|
1370 for (const_file_info_list_iterator p = file_info_list.begin (); |
|
1371 p != file_info_list.end (); |
|
1372 p++) |
|
1373 { |
|
1374 os << " " << p->dir_name << " ("; |
|
1375 |
7336
|
1376 print_types (os, p->types); |
5832
|
1377 |
|
1378 os << ")\n"; |
|
1379 } |
|
1380 } |
|
1381 |
7336
|
1382 for (const_method_map_iterator i = method_map.begin (); |
|
1383 i != method_map.end (); |
|
1384 i++) |
|
1385 { |
|
1386 os << "CLASS " << i->first << ":\n"; |
|
1387 |
|
1388 const fcn_map_type& fm = i->second; |
|
1389 |
|
1390 for (const_fcn_map_iterator q = fm.begin (); |
|
1391 q != fm.end (); |
|
1392 q++) |
|
1393 { |
|
1394 os << " " << q->first << ":\n"; |
|
1395 |
|
1396 const file_info_list_type& file_info_list = q->second; |
|
1397 |
|
1398 for (const_file_info_list_iterator p = file_info_list.begin (); |
|
1399 p != file_info_list.end (); |
|
1400 p++) |
|
1401 { |
|
1402 os << " " << p->dir_name << " ("; |
|
1403 |
|
1404 print_types (os, p->types); |
|
1405 |
|
1406 os << ")\n"; |
|
1407 } |
|
1408 } |
|
1409 } |
|
1410 |
5832
|
1411 os << "\n"; |
|
1412 |
|
1413 #endif |
|
1414 } |
|
1415 |
|
1416 void |
|
1417 load_path::add_to_fcn_map (const dir_info& di, bool at_end) const |
|
1418 { |
|
1419 std::string dir_name = di.dir_name; |
|
1420 |
|
1421 string_vector fcn_files = di.fcn_files; |
|
1422 |
|
1423 octave_idx_type len = fcn_files.length (); |
|
1424 |
|
1425 for (octave_idx_type i = 0; i < len; i++) |
|
1426 { |
|
1427 std::string fname = fcn_files[i]; |
|
1428 |
|
1429 std::string ext; |
|
1430 std::string base = fname; |
|
1431 |
|
1432 size_t pos = fname.rfind ('.'); |
|
1433 |
|
1434 if (pos != NPOS) |
|
1435 { |
|
1436 base = fname.substr (0, pos); |
|
1437 ext = fname.substr (pos); |
|
1438 } |
|
1439 |
7336
|
1440 file_info_list_type& file_info_list = fcn_map[base]; |
5832
|
1441 |
|
1442 file_info_list_iterator p = file_info_list.begin (); |
|
1443 |
|
1444 while (p != file_info_list.end ()) |
|
1445 { |
|
1446 if (p->dir_name == dir_name) |
|
1447 break; |
|
1448 |
|
1449 p++; |
|
1450 } |
|
1451 |
|
1452 int t = 0; |
|
1453 if (ext == ".m") |
|
1454 t = load_path::M_FILE; |
|
1455 else if (ext == ".oct") |
|
1456 t = load_path::OCT_FILE; |
5864
|
1457 else if (ext == ".mex") |
|
1458 t = load_path::MEX_FILE; |
5832
|
1459 |
|
1460 if (p == file_info_list.end ()) |
|
1461 { |
|
1462 file_info fi (dir_name, t); |
|
1463 |
|
1464 if (at_end) |
|
1465 file_info_list.push_back (fi); |
|
1466 else |
|
1467 file_info_list.push_front (fi); |
|
1468 } |
|
1469 else |
|
1470 { |
|
1471 file_info& fi = *p; |
|
1472 |
|
1473 fi.types |= t; |
|
1474 } |
|
1475 } |
|
1476 } |
|
1477 |
7336
|
1478 void |
|
1479 load_path::add_to_private_fcn_map (const dir_info& di) const |
|
1480 { |
|
1481 dir_info::fcn_file_map_type private_file_map = di.private_file_map; |
|
1482 |
|
1483 if (! private_file_map.empty ()) |
|
1484 private_fcn_map[di.dir_name] = private_file_map; |
|
1485 } |
|
1486 |
|
1487 void |
|
1488 load_path::add_to_method_map (const dir_info& di, bool at_end) const |
|
1489 { |
|
1490 std::string dir_name = di.dir_name; |
|
1491 |
|
1492 // <CLASS_NAME, <FCN_NAME, TYPES>> |
|
1493 dir_info::method_file_map_type method_file_map = di.method_file_map; |
|
1494 |
|
1495 for (dir_info::const_method_file_map_iterator q = method_file_map.begin (); |
|
1496 q != method_file_map.end (); |
|
1497 q++) |
|
1498 { |
|
1499 std::string class_name = q->first; |
|
1500 |
|
1501 fcn_map_type& fm = method_map[class_name]; |
|
1502 |
|
1503 std::string full_dir_name |
|
1504 = file_ops::concat (dir_name, "@" + class_name); |
|
1505 |
|
1506 // <FCN_NAME, TYPES> |
|
1507 const dir_info::fcn_file_map_type& m = q->second; |
|
1508 |
|
1509 for (dir_info::const_fcn_file_map_iterator p = m.begin (); |
|
1510 p != m.end (); |
|
1511 p++) |
|
1512 { |
|
1513 std::string base = p->first; |
|
1514 |
|
1515 int types = p->second; |
|
1516 |
|
1517 file_info_list_type& file_info_list = fm[base]; |
|
1518 |
|
1519 file_info_list_iterator p2 = file_info_list.begin (); |
|
1520 |
|
1521 while (p2 != file_info_list.end ()) |
|
1522 { |
|
1523 if (p2->dir_name == full_dir_name) |
|
1524 break; |
|
1525 |
|
1526 p2++; |
|
1527 } |
|
1528 |
|
1529 if (p2 == file_info_list.end ()) |
|
1530 { |
|
1531 file_info fi (full_dir_name, types); |
|
1532 |
|
1533 if (at_end) |
|
1534 file_info_list.push_back (fi); |
|
1535 else |
|
1536 file_info_list.push_front (fi); |
|
1537 } |
|
1538 else |
|
1539 { |
|
1540 // FIXME -- is this possible? |
|
1541 |
|
1542 file_info& fi = *p2; |
|
1543 |
|
1544 fi.types = types; |
|
1545 } |
|
1546 } |
|
1547 } |
|
1548 } |
|
1549 |
5832
|
1550 std::string |
|
1551 genpath (const std::string& dirname, const string_vector& skip) |
|
1552 { |
|
1553 std::string retval; |
|
1554 |
5871
|
1555 dir_entry dir (dirname); |
5832
|
1556 |
|
1557 if (dir) |
|
1558 { |
|
1559 retval = dirname; |
|
1560 |
|
1561 string_vector dirlist = dir.read (); |
|
1562 |
|
1563 octave_idx_type len = dirlist.length (); |
|
1564 |
|
1565 for (octave_idx_type i = 0; i < len; i++) |
|
1566 { |
|
1567 std::string elt = dirlist[i]; |
|
1568 |
|
1569 // FIXME -- the caller should be able to specify the list of |
7336
|
1570 // directories to skip in addition to ".", "..", and |
|
1571 // directories beginning with "@". |
5832
|
1572 |
7336
|
1573 bool skip_p = (elt == "." || elt == ".." || elt[0] == '@'); |
5832
|
1574 |
|
1575 if (! skip_p) |
|
1576 { |
|
1577 for (octave_idx_type j = 0; j < skip.length (); j++) |
|
1578 { |
|
1579 skip_p = (elt == skip[j]); |
|
1580 if (skip_p) |
|
1581 break; |
|
1582 } |
|
1583 |
|
1584 if (! skip_p) |
|
1585 { |
7272
|
1586 std::string nm = file_ops::concat (dirname, elt); |
5832
|
1587 |
|
1588 file_stat fs (nm); |
|
1589 |
|
1590 if (fs && fs.is_dir ()) |
|
1591 retval += dir_path::path_sep_str + genpath (nm); |
|
1592 } |
|
1593 } |
|
1594 } |
|
1595 } |
|
1596 |
|
1597 return retval; |
|
1598 } |
|
1599 |
|
1600 static void |
|
1601 execute_pkg_add_or_del (const std::string& dir, |
|
1602 const std::string& script_file) |
|
1603 { |
|
1604 if (! octave_interpreter_ready) |
|
1605 return; |
|
1606 |
|
1607 unwind_protect::begin_frame ("execute_pkg_add_or_del"); |
|
1608 |
|
1609 unwind_protect_bool (input_from_startup_file); |
|
1610 |
|
1611 input_from_startup_file = true; |
|
1612 |
7272
|
1613 std::string file = file_ops::concat (dir, script_file); |
5832
|
1614 |
5978
|
1615 file_stat fs (file); |
5832
|
1616 |
|
1617 if (fs.exists ()) |
5975
|
1618 source_file (file, "base"); |
5832
|
1619 |
|
1620 unwind_protect::run_frame ("execute_pkg_add_or_del"); |
|
1621 } |
|
1622 |
|
1623 void |
|
1624 execute_pkg_add (const std::string& dir) |
|
1625 { |
|
1626 execute_pkg_add_or_del (dir, "PKG_ADD"); |
|
1627 } |
|
1628 |
|
1629 void |
|
1630 execute_pkg_del (const std::string& dir) |
|
1631 { |
|
1632 execute_pkg_add_or_del (dir, "PKG_DEL"); |
|
1633 } |
|
1634 |
|
1635 DEFUN (genpath, args, , |
|
1636 "-*- texinfo -*-\n\ |
|
1637 @deftypefn {Built-in Function} {} genpath (@var{dir})\n\ |
|
1638 Return a path constructed from @var{dir} and all its subdiretories.\n\ |
|
1639 @end deftypefn") |
|
1640 { |
|
1641 octave_value retval; |
|
1642 |
|
1643 if (args.length () == 1) |
|
1644 { |
|
1645 std::string dirname = args(0).string_value (); |
|
1646 |
|
1647 if (! error_state) |
|
1648 retval = genpath (dirname); |
|
1649 else |
|
1650 error ("genpath: expecting argument to be a character string"); |
|
1651 } |
|
1652 else |
|
1653 print_usage (); |
|
1654 |
|
1655 return retval; |
|
1656 } |
|
1657 |
|
1658 DEFUN (rehash, , , |
|
1659 "-*- texinfo -*-\n\ |
|
1660 @deftypefn {Built-in Function} {} rehash ()\n\ |
6644
|
1661 Reinitialize Octave's load path directory cache.\n\ |
5832
|
1662 @end deftypefn") |
|
1663 { |
|
1664 octave_value_list retval; |
|
1665 |
|
1666 load_path::update (); |
|
1667 |
|
1668 // FIXME -- maybe we should rename this variable since it is being |
|
1669 // used for more than keeping track of the prompt time. |
|
1670 |
|
1671 // This will force updated functions to be found. |
|
1672 Vlast_prompt_time.stamp (); |
|
1673 |
|
1674 return retval; |
|
1675 } |
|
1676 |
|
1677 DEFUN (pathdef, , , |
|
1678 "-*- texinfo -*-\n\ |
|
1679 @deftypefn {Built-in Function} {@var{val} =} pathdef ()\n\ |
|
1680 Return the default list of directories in which to search for function\n\ |
|
1681 files.\n\ |
|
1682 @seealso{path, addpath, rmpath, genpath, savepath, pathsep}\n\ |
|
1683 @end deftypefn") |
|
1684 { |
6630
|
1685 return octave_value (load_path::system_path ()); |
5832
|
1686 } |
|
1687 |
|
1688 DEFUN (path, args, nargout, |
|
1689 "-*- texinfo -*-\n\ |
6678
|
1690 @deftypefn {Built-in Function} {} path (@dots{})\n\ |
6644
|
1691 Modify or display Octave's load path.\n\ |
5832
|
1692 \n\ |
|
1693 If @var{nargin} and @var{nargout} are zero, display the elements of\n\ |
6644
|
1694 Octave's load path in an easy to read format.\n\ |
5832
|
1695 \n\ |
|
1696 If @var{nargin} is zero and nargout is greater than zero, return the\n\ |
6644
|
1697 current load path.\n\ |
5832
|
1698 \n\ |
|
1699 If @var{nargin} is greater than zero, concatenate the arguments,\n\ |
|
1700 separating them with @code{pathsep()}. Set the internal search path\n\ |
|
1701 to the result and return it.\n\ |
|
1702 \n\ |
|
1703 No checks are made for duplicate elements.\n\ |
|
1704 @seealso{addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ |
|
1705 @end deftypefn") |
|
1706 { |
|
1707 octave_value retval; |
|
1708 |
|
1709 int argc = args.length () + 1; |
|
1710 |
|
1711 string_vector argv = args.make_argv ("path"); |
|
1712 |
|
1713 if (! error_state) |
|
1714 { |
|
1715 if (argc > 1) |
|
1716 { |
|
1717 std::string path = argv[1]; |
|
1718 |
|
1719 for (int i = 2; i < argc; i++) |
5867
|
1720 path += dir_path::path_sep_str + argv[i]; |
5832
|
1721 |
5867
|
1722 load_path::set (path, true); |
5832
|
1723 } |
|
1724 |
|
1725 if (nargout > 0) |
|
1726 retval = load_path::path (); |
|
1727 else if (argc == 1 && nargout == 0) |
|
1728 { |
|
1729 octave_stdout << "\nOctave's search path contains the following directories:\n\n"; |
|
1730 |
|
1731 string_vector dirs = load_path::dirs (); |
|
1732 |
|
1733 dirs.list_in_columns (octave_stdout); |
|
1734 |
|
1735 octave_stdout << "\n"; |
|
1736 } |
|
1737 } |
|
1738 |
|
1739 return retval; |
|
1740 } |
|
1741 |
|
1742 DEFCMD (addpath, args, nargout, |
|
1743 "-*- texinfo -*-\n\ |
6678
|
1744 @deftypefn {Built-in Function} {} addpath (@var{dir1}, @dots{})\n\ |
|
1745 @deftypefnx {Built-in Function} {} addpath (@var{dir1}, @dots{}, @var{option})\n\ |
5832
|
1746 Add @var{dir1}, @dots{} to the current function search path. If\n\ |
|
1747 @var{option} is @samp{\"-begin\"} or 0 (the default), prepend the\n\ |
|
1748 directory name to the current path. If @var{option} is @samp{\"-end\"}\n\ |
|
1749 or 1, append the directory name to the current path.\n\ |
|
1750 Directories added to the path must exist.\n\ |
|
1751 @seealso{path, rmpath, genpath, pathdef, savepath, pathsep}\n\ |
|
1752 @end deftypefn") |
|
1753 { |
|
1754 octave_value retval; |
|
1755 |
|
1756 // Originally written by Bill Denney and Etienne Grossman. Heavily |
|
1757 // modified and translated to C++ by jwe. |
|
1758 |
|
1759 if (nargout > 0) |
|
1760 retval = load_path::path (); |
|
1761 |
|
1762 int nargin = args.length (); |
|
1763 |
|
1764 if (nargin > 0) |
|
1765 { |
|
1766 bool append = false; |
|
1767 |
|
1768 octave_value option_arg = args(nargin-1); |
|
1769 |
|
1770 if (option_arg.is_string ()) |
|
1771 { |
|
1772 std::string option = option_arg.string_value (); |
|
1773 |
|
1774 if (option == "-end") |
|
1775 { |
|
1776 append = true; |
|
1777 nargin--; |
|
1778 } |
|
1779 else if (option == "-begin") |
|
1780 nargin--; |
|
1781 } |
|
1782 else if (option_arg.is_numeric_type ()) |
|
1783 { |
|
1784 int val = option_arg.int_value (); |
|
1785 |
|
1786 if (! error_state) |
|
1787 { |
|
1788 if (val == 0) |
|
1789 append = false; |
|
1790 else if (val == 1) |
|
1791 append = true; |
|
1792 else |
|
1793 { |
|
1794 error ("addpath: expecting final argument to be 1 or 0"); |
|
1795 return retval; |
|
1796 } |
|
1797 } |
|
1798 else |
|
1799 { |
|
1800 error ("addpath: expecting final argument to be 1 or 0"); |
|
1801 return retval; |
|
1802 } |
|
1803 } |
|
1804 |
|
1805 for (int i = 0; i < nargin; i++) |
|
1806 { |
|
1807 std::string arg = args(i).string_value (); |
|
1808 |
|
1809 if (! error_state) |
|
1810 { |
|
1811 std::list<std::string> dir_elts = split_path (arg); |
|
1812 |
|
1813 for (std::list<std::string>::const_iterator p = dir_elts.begin (); |
|
1814 p != dir_elts.end (); |
|
1815 p++) |
|
1816 { |
|
1817 std::string dir = *p; |
|
1818 |
|
1819 //dir = regexprep (dir_elts{j}, "//+", "/"); |
|
1820 //dir = regexprep (dir, "/$", ""); |
|
1821 |
5867
|
1822 if (append) |
|
1823 load_path::append (dir, true); |
5832
|
1824 else |
5867
|
1825 load_path::prepend (dir, true); |
5832
|
1826 } |
|
1827 } |
|
1828 else |
|
1829 error ("addpath: expecting all args to be character strings"); |
|
1830 } |
|
1831 } |
|
1832 else |
|
1833 print_usage (); |
|
1834 |
|
1835 return retval; |
|
1836 } |
|
1837 |
|
1838 DEFCMD (rmpath, args, nargout, |
|
1839 "-*- texinfo -*-\n\ |
6678
|
1840 @deftypefn {Built-in Function} {} rmpath (@var{dir1}, @dots{})\n\ |
5832
|
1841 Remove @var{dir1}, @dots{} from the current function search path.\n\ |
|
1842 \n\ |
|
1843 @seealso{path, addpath, genpath, pathdef, savepath, pathsep}\n\ |
|
1844 @end deftypefn") |
|
1845 { |
|
1846 // Originally by Etienne Grossmann. Heavily modified and translated |
|
1847 // to C++ by jwe. |
|
1848 |
|
1849 octave_value retval; |
|
1850 |
|
1851 if (nargout > 0) |
|
1852 retval = load_path::path (); |
|
1853 |
|
1854 int nargin = args.length (); |
|
1855 |
|
1856 if (nargin > 0) |
|
1857 { |
|
1858 for (int i = 0; i < nargin; i++) |
|
1859 { |
|
1860 std::string arg = args(i).string_value (); |
|
1861 |
|
1862 if (! error_state) |
|
1863 { |
|
1864 std::list<std::string> dir_elts = split_path (arg); |
|
1865 |
|
1866 for (std::list<std::string>::const_iterator p = dir_elts.begin (); |
|
1867 p != dir_elts.end (); |
|
1868 p++) |
|
1869 { |
|
1870 std::string dir = *p; |
|
1871 |
|
1872 //dir = regexprep (dir_elts{j}, "//+", "/"); |
|
1873 //dir = regexprep (dir, "/$", ""); |
|
1874 |
|
1875 if (! load_path::remove (dir)) |
|
1876 warning ("rmpath: %s: not found", dir.c_str ()); |
|
1877 } |
|
1878 } |
|
1879 else |
|
1880 error ("addpath: expecting all args to be character strings"); |
|
1881 } |
|
1882 } |
|
1883 else |
|
1884 print_usage (); |
|
1885 |
|
1886 return retval; |
|
1887 } |
|
1888 |
|
1889 /* |
|
1890 ;;; Local Variables: *** |
|
1891 ;;; mode: C++ *** |
|
1892 ;;; End: *** |
|
1893 */ |