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