comparison src/load-path.cc @ 14376:7dd6ac033e69

Warn when the default path is overwritten * load-path.h (dir_info): New member, is_init, true if this directory was set during init. (dir_info::dir_info): Initialise is_init param. (load_path::do_clear): Pass the new elements to add after clearing. (load_path::clear): Pass dummy empty elements. (load_path::do_set, load_path::do_append, load_path::do_add): Pass new bool if adding or setting an init directory. * load-path.cc (load_path::do_initalize, load_path::do_append, load_path::do_add): Pass around extra is_init argument. (do_set): Pass an std::set of new directories to do_append. (do_clear): Check if the init directories are getting removed and warn if so.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Thu, 16 Feb 2012 20:19:12 -0500
parents 97883071e8e4
children 460a3c6d8bf1
comparison
equal deleted inserted replaced
14375:4e4519a26e50 14376:7dd6ac033e69
517 xpath += dir_path::path_sep_str () + sys_path; 517 xpath += dir_path::path_sep_str () + sys_path;
518 } 518 }
519 else 519 else
520 xpath = sys_path; 520 xpath = sys_path;
521 521
522 do_set (xpath, false); 522 do_set (xpath, false, true);
523 } 523 }
524 524
525 void 525 void
526 load_path::do_clear (void) 526 load_path::do_clear (std::set<std::string>& new_elts)
527 { 527 {
528 dir_info_list.clear (); 528 bool warn_default_path_clobbered = false;
529 for (dir_info_list_iterator i = dir_info_list.begin();
530 i != dir_info_list.end();
531 /* conditionally advance iterator in loop body */)
532 {
533 //Don't remove it if it's gonna be added again, but remove it from
534 //list of items to add, to avoid duplicates later on
535 std::set<std::string>::iterator j = new_elts.find(i->dir_name);
536 if (j != new_elts.end())
537 {
538 new_elts.erase(j);
539 i++;
540 }
541 else
542 {
543 //Warn if removing a default directory and not immediately adding
544 //it back again
545 if(i->is_init)
546 warn_default_path_clobbered = true;
547 i = dir_info_list.erase(i);
548 }
549 }
550
551 if (warn_default_path_clobbered)
552 warning_with_id ("Octave:remove-init-dir",
553 "default load path altered. Some built-in functions may "
554 "not be found. Try restoredefaultpath() to recover it.");
555
529 fcn_map.clear (); 556 fcn_map.clear ();
530 private_fcn_map.clear (); 557 private_fcn_map.clear ();
531 method_map.clear (); 558 method_map.clear ();
532 } 559 }
533 560
563 590
564 return retval; 591 return retval;
565 } 592 }
566 593
567 void 594 void
568 load_path::do_set (const std::string& p, bool warn) 595 load_path::do_set (const std::string& p, bool warn, bool is_init)
569 { 596 {
570 std::list<std::string> elts = split_path (p); 597 std::list<std::string> elts_l = split_path (p);
598 std::set<std::string> elts(elts_l.begin(), elts_l.end());
571 599
572 // Temporarily disable add hook. 600 // Temporarily disable add hook.
573 601
574 unwind_protect frame; 602 unwind_protect frame;
575 frame.protect_var (add_hook); 603 frame.protect_var (add_hook);
576 604
577 add_hook = 0; 605 add_hook = 0;
578 606
579 do_clear (); 607 do_clear (elts);
580 608
581 for (std::list<std::string>::const_iterator i = elts.begin (); 609 for (std::set<std::string>::const_iterator i = elts.begin ();
582 i != elts.end (); 610 i != elts.end ();
583 i++) 611 i++)
584 do_append (*i, warn); 612 do_append (*i, warn, is_init);
585 613
586 // Restore add hook and execute for all newly added directories. 614 // Restore add hook and execute for all newly added directories.
587 frame.run_top (); 615 frame.run_top ();
588 616
589 for (dir_info_list_iterator i = dir_info_list.begin (); 617 for (dir_info_list_iterator i = dir_info_list.begin ();
597 // Always prepend current directory. 625 // Always prepend current directory.
598 do_prepend (".", warn); 626 do_prepend (".", warn);
599 } 627 }
600 628
601 void 629 void
602 load_path::do_append (const std::string& dir, bool warn) 630 load_path::do_append (const std::string& dir, bool warn, bool is_init)
603 { 631 {
604 if (! dir.empty ()) 632 if (! dir.empty ())
605 do_add (dir, true, warn); 633 do_add (dir, true, warn, is_init);
606 } 634 }
607 635
608 void 636 void
609 load_path::do_prepend (const std::string& dir, bool warn) 637 load_path::do_prepend (const std::string& dir, bool warn)
610 { 638 {
629 657
630 return dir; 658 return dir;
631 } 659 }
632 660
633 void 661 void
634 load_path::do_add (const std::string& dir_arg, bool at_end, bool warn) 662 load_path::do_add (const std::string& dir_arg, bool at_end, bool warn,
663 bool is_init)
635 { 664 {
636 size_t len = dir_arg.length (); 665 size_t len = dir_arg.length ();
637 666
638 if (len > 1 && dir_arg.substr (len-2) == "//") 667 if (len > 1 && dir_arg.substr (len-2) == "//")
639 warning_with_id ("Octave:recursive-path-search", 668 warning_with_id ("Octave:recursive-path-search",
654 if (fs) 683 if (fs)
655 { 684 {
656 if (fs.is_dir ()) 685 if (fs.is_dir ())
657 { 686 {
658 dir_info di (dir); 687 dir_info di (dir);
688 di.is_init = is_init;
659 689
660 if (! error_state) 690 if (! error_state)
661 { 691 {
662 if (at_end) 692 if (at_end)
663 dir_info_list.push_back (di); 693 dir_info_list.push_back (di);