Mercurial > octave-nkf
comparison src/load-path.cc @ 5867:b305874f50ef
[project @ 2006-06-28 22:11:51 by jwe]
author | jwe |
---|---|
date | Wed, 28 Jun 2006 22:11:51 +0000 |
parents | e884ab4f29ee |
children | b9fd54407c8d |
comparison
equal
deleted
inserted
replaced
5866:f9ac7ebf0e19 | 5867:b305874f50ef |
---|---|
372 xpath += dir_path::path_sep_str + tpath; | 372 xpath += dir_path::path_sep_str + tpath; |
373 | 373 |
374 if (Vsystem_path != ":") | 374 if (Vsystem_path != ":") |
375 xpath += Vsystem_path; | 375 xpath += Vsystem_path; |
376 | 376 |
377 do_set (xpath + ":::"); | 377 do_set (xpath + ":::", false); |
378 } | 378 } |
379 | 379 |
380 void | 380 void |
381 load_path::do_clear (void) | 381 load_path::do_clear (void) |
382 { | 382 { |
383 dir_info_list.clear (); | 383 dir_info_list.clear (); |
384 fcn_map.clear (); | 384 fcn_map.clear (); |
385 | |
386 do_append (".", false); | |
385 } | 387 } |
386 | 388 |
387 static std::list<std::string> | 389 static std::list<std::string> |
388 split_path (const std::string& p) | 390 split_path (const std::string& p) |
389 { | 391 { |
416 | 418 |
417 return retval; | 419 return retval; |
418 } | 420 } |
419 | 421 |
420 void | 422 void |
421 load_path::do_set (const std::string& p) | 423 load_path::do_set (const std::string& p, bool warn) |
422 { | 424 { |
423 do_clear (); | 425 do_clear (); |
424 | 426 |
425 std::list<std::string> elts = split_path (p); | 427 std::list<std::string> elts = split_path (p); |
426 | 428 |
431 add_hook = 0; | 433 add_hook = 0; |
432 | 434 |
433 for (std::list<std::string>::const_iterator i = elts.begin (); | 435 for (std::list<std::string>::const_iterator i = elts.begin (); |
434 i != elts.end (); | 436 i != elts.end (); |
435 i++) | 437 i++) |
436 do_append (*i); | 438 do_append (*i, warn); |
437 | 439 |
438 // Restore add hook and execute for all newly added directories. | 440 // Restore add hook and execute for all newly added directories. |
439 | 441 |
440 unwind_protect::run (); | 442 unwind_protect::run (); |
441 | 443 |
447 add_hook (i->dir_name); | 449 add_hook (i->dir_name); |
448 } | 450 } |
449 } | 451 } |
450 | 452 |
451 void | 453 void |
452 load_path::do_append (const std::string& dir) | 454 load_path::do_append (const std::string& dir, bool warn) |
453 { | 455 { |
454 if (! dir.empty ()) | 456 if (! dir.empty ()) |
455 { | 457 do_add (dir, true, warn); |
456 dir_info_list_iterator i = find_dir_info (dir); | |
457 | |
458 if (i != dir_info_list.end ()) | |
459 move (i, true); | |
460 else | |
461 { | |
462 dir_info di (dir); | |
463 | |
464 if (! error_state) | |
465 { | |
466 dir_info_list.push_back (di); | |
467 | |
468 add_to_fcn_map (di, true); | |
469 | |
470 if (add_hook) | |
471 add_hook (dir); | |
472 } | |
473 } | |
474 } | |
475 } | 458 } |
476 | 459 |
477 void | 460 void |
478 load_path::do_prepend (const std::string& dir) | 461 load_path::do_prepend (const std::string& dir, bool warn) |
479 { | 462 { |
480 if (! dir.empty ()) | 463 if (! dir.empty ()) |
481 { | 464 do_add (dir, false, warn); |
482 dir_info_list_iterator i = find_dir_info (dir); | 465 } |
483 | 466 |
484 if (i != dir_info_list.end ()) | 467 void |
485 move (i, false); | 468 load_path::do_add (const std::string& dir, bool at_end, bool warn) |
486 else | 469 { |
487 { | 470 dir_info_list_iterator i = find_dir_info (dir); |
488 dir_info di (dir); | 471 |
489 | 472 if (i != dir_info_list.end ()) |
490 if (! error_state) | 473 move (i, at_end); |
491 { | 474 else |
492 dir_info_list.push_front (di); | 475 { |
493 | 476 file_stat fs (dir); |
494 add_to_fcn_map (di, false); | 477 |
495 | 478 if (fs) |
496 if (add_hook) | 479 { |
497 add_hook (dir); | 480 if (fs.is_dir ()) |
498 } | 481 { |
499 } | 482 dir_info di (dir); |
500 | 483 |
501 // FIXME -- is there a better way to do this? | 484 if (! error_state) |
502 | 485 { |
503 i = find_dir_info ("."); | 486 if (at_end) |
504 | 487 dir_info_list.push_back (di); |
505 if (i != dir_info_list.end ()) | 488 else |
506 move (i, false); | 489 dir_info_list.push_front (di); |
507 else | 490 |
508 panic_impossible (); | 491 add_to_fcn_map (di, true); |
509 } | 492 |
493 if (add_hook) | |
494 add_hook (dir); | |
495 } | |
496 } | |
497 else if (warn) | |
498 warning ("addpath: %s: not a directory", dir.c_str ()); | |
499 } | |
500 else if (warn) | |
501 { | |
502 std::string msg = fs.error (); | |
503 warning ("addpath: %s: %s", dir.c_str (), msg.c_str ()); | |
504 } | |
505 } | |
506 | |
507 // FIXME -- is there a better way to do this? | |
508 | |
509 i = find_dir_info ("."); | |
510 | |
511 if (i != dir_info_list.end ()) | |
512 { | |
513 if (i != dir_info_list.begin () && warn) | |
514 warning ("addpath: \".\" is always first in the path"); | |
515 | |
516 move (i, false); | |
517 } | |
518 else | |
519 panic_impossible (); | |
510 } | 520 } |
511 | 521 |
512 bool | 522 bool |
513 load_path::do_remove (const std::string& dir) | 523 load_path::do_remove (const std::string& dir) |
514 { | 524 { |
515 bool retval = false; | 525 bool retval = false; |
516 | 526 |
517 if (! dir.empty ()) | 527 if (! dir.empty ()) |
518 { | 528 { |
519 if (dir == ".") | 529 if (dir == ".") |
520 warning ("rmpath: can't remove \".\" from path"); | 530 { |
521 | 531 warning ("rmpath: can't remove \".\" from path"); |
522 dir_info_list_iterator i = find_dir_info (dir); | 532 |
523 | 533 // Avoid additional warnings. |
524 if (i != dir_info_list.end ()) | |
525 { | |
526 retval = true; | 534 retval = true; |
527 | 535 } |
528 string_vector fcn_files = i->fcn_files; | 536 else |
529 | 537 { |
530 dir_info_list.erase (i); | 538 dir_info_list_iterator i = find_dir_info (dir); |
531 | 539 |
532 octave_idx_type len = fcn_files.length (); | 540 if (i != dir_info_list.end ()) |
533 | 541 { |
534 for (octave_idx_type k = 0; k < len; k++) | 542 retval = true; |
535 { | 543 |
536 std::string fname = fcn_files[k]; | 544 string_vector fcn_files = i->fcn_files; |
537 | 545 |
538 std::string ext; | 546 dir_info_list.erase (i); |
539 std::string base = fname; | 547 |
540 | 548 octave_idx_type len = fcn_files.length (); |
541 size_t pos = fname.rfind ('.'); | 549 |
542 | 550 for (octave_idx_type k = 0; k < len; k++) |
543 if (pos != NPOS) | 551 { |
544 { | 552 std::string fname = fcn_files[k]; |
545 base = fname.substr (0, pos); | 553 |
546 ext = fname.substr (pos); | 554 std::string ext; |
547 } | 555 std::string base = fname; |
548 | 556 |
549 std::list<file_info>& file_info_list = fcn_map[base]; | 557 size_t pos = fname.rfind ('.'); |
550 | 558 |
551 for (std::list<file_info>::iterator p = file_info_list.begin (); | 559 if (pos != NPOS) |
552 p != file_info_list.end (); | |
553 p++) | |
554 { | |
555 if (p->dir_name == dir) | |
556 { | 560 { |
557 file_info_list.erase (p); | 561 base = fname.substr (0, pos); |
558 | 562 ext = fname.substr (pos); |
559 if (file_info_list.empty ()) | |
560 fcn_map.erase (fname); | |
561 | |
562 break; | |
563 } | 563 } |
564 } | 564 |
565 } | 565 std::list<file_info>& file_info_list = fcn_map[base]; |
566 | 566 |
567 if (remove_hook) | 567 for (std::list<file_info>::iterator p = file_info_list.begin (); |
568 remove_hook (dir); | 568 p != file_info_list.end (); |
569 p++) | |
570 { | |
571 if (p->dir_name == dir) | |
572 { | |
573 file_info_list.erase (p); | |
574 | |
575 if (file_info_list.empty ()) | |
576 fcn_map.erase (fname); | |
577 | |
578 break; | |
579 } | |
580 } | |
581 } | |
582 | |
583 if (remove_hook) | |
584 remove_hook (dir); | |
585 } | |
569 } | 586 } |
570 } | 587 } |
571 | 588 |
572 return retval; | 589 return retval; |
573 } | 590 } |
1287 if (argc > 1) | 1304 if (argc > 1) |
1288 { | 1305 { |
1289 std::string path = argv[1]; | 1306 std::string path = argv[1]; |
1290 | 1307 |
1291 for (int i = 2; i < argc; i++) | 1308 for (int i = 2; i < argc; i++) |
1292 path += dir_path::path_sep_str; | 1309 path += dir_path::path_sep_str + argv[i]; |
1293 | 1310 |
1294 size_t plen = path.length (); | 1311 load_path::set (path, true); |
1295 | |
1296 if (! ((plen == 1 && path[0] == ':') | |
1297 || (plen > 1 | |
1298 && path.substr (0, 2) == ("." + dir_path::path_sep_str)))) | |
1299 path = "." + dir_path::path_sep_str + path; | |
1300 | |
1301 load_path::set (path); | |
1302 } | 1312 } |
1303 | 1313 |
1304 if (nargout > 0) | 1314 if (nargout > 0) |
1305 retval = load_path::path (); | 1315 retval = load_path::path (); |
1306 else if (argc == 1 && nargout == 0) | 1316 else if (argc == 1 && nargout == 0) |
1379 error ("addpath: expecting final argument to be 1 or 0"); | 1389 error ("addpath: expecting final argument to be 1 or 0"); |
1380 return retval; | 1390 return retval; |
1381 } | 1391 } |
1382 } | 1392 } |
1383 | 1393 |
1384 std::list<std::string> xpath = load_path::dir_list (); | |
1385 | |
1386 // Strip "." for now. Calling path to set the path will restore it. | |
1387 | |
1388 xpath.remove ("."); | |
1389 | |
1390 for (int i = 0; i < nargin; i++) | 1394 for (int i = 0; i < nargin; i++) |
1391 { | 1395 { |
1392 std::string arg = args(i).string_value (); | 1396 std::string arg = args(i).string_value (); |
1393 | 1397 |
1394 if (! error_state) | 1398 if (! error_state) |
1402 std::string dir = *p; | 1406 std::string dir = *p; |
1403 | 1407 |
1404 //dir = regexprep (dir_elts{j}, "//+", "/"); | 1408 //dir = regexprep (dir_elts{j}, "//+", "/"); |
1405 //dir = regexprep (dir, "/$", ""); | 1409 //dir = regexprep (dir, "/$", ""); |
1406 | 1410 |
1407 if (dir == "." && append) | 1411 if (append) |
1408 warning ("addpath: \".\" is always first in the path"); | 1412 load_path::append (dir, true); |
1409 | |
1410 file_stat fs (dir); | |
1411 | |
1412 if (fs) | |
1413 { | |
1414 if (fs.is_dir ()) | |
1415 { | |
1416 if (append) | |
1417 load_path::append (dir); | |
1418 else | |
1419 load_path::prepend (dir); | |
1420 } | |
1421 else | |
1422 warning ("addpath: %s: not a directory", dir.c_str ()); | |
1423 } | |
1424 else | 1413 else |
1425 { | 1414 load_path::prepend (dir, true); |
1426 std::string msg = fs.error (); | |
1427 warning ("addpath: %s: %s", dir.c_str (), msg.c_str ()); | |
1428 } | |
1429 } | 1415 } |
1430 } | 1416 } |
1431 else | 1417 else |
1432 error ("addpath: expecting all args to be character strings"); | 1418 error ("addpath: expecting all args to be character strings"); |
1433 } | 1419 } |
1456 | 1442 |
1457 int nargin = args.length (); | 1443 int nargin = args.length (); |
1458 | 1444 |
1459 if (nargin > 0) | 1445 if (nargin > 0) |
1460 { | 1446 { |
1461 std::list<std::string> xpath = load_path::dir_list (); | |
1462 | |
1463 for (int i = 0; i < nargin; i++) | 1447 for (int i = 0; i < nargin; i++) |
1464 { | 1448 { |
1465 std::string arg = args(i).string_value (); | 1449 std::string arg = args(i).string_value (); |
1466 | 1450 |
1467 if (! error_state) | 1451 if (! error_state) |