comparison libinterp/corefcn/variables.cc @ 18856:de8c67ba7ac4

Use Matlab return hierarchy for exist() codes when no type is specified. * variables.cc (symbol_exist): Simplify code by removing obsolete code testing for structures in the input name. Re-order testing blocks so that built-in code (5) is returned before generic file code (2). * variables.cc (Fexist): Add more comprehensive %!tests for exist.
author Rik <rik@octave.org>
date Wed, 25 Jun 2014 12:51:55 -0700
parents 87381dbbe25e
children a1dde4d4c45c
comparison
equal deleted inserted replaced
18852:47d4b680d0e0 18856:de8c67ba7ac4
386 } 386 }
387 387
388 int 388 int
389 symbol_exist (const std::string& name, const std::string& type) 389 symbol_exist (const std::string& name, const std::string& type)
390 { 390 {
391 std::string struct_elts; 391 if (is_keyword (name))
392 std::string symbol_name = name;
393
394 size_t pos = name.find ('.');
395
396 if (pos != std::string::npos && pos > 0)
397 {
398 struct_elts = name.substr (pos+1);
399 symbol_name = name.substr (0, pos);
400 }
401 else if (is_keyword (symbol_name))
402 return 0; 392 return 0;
403 393
404 bool search_any = type == "any"; 394 bool search_any = type == "any";
405 bool search_var = type == "var"; 395 bool search_var = type == "var";
406 bool search_dir = type == "dir"; 396 bool search_dir = type == "dir";
407 bool search_file = type == "file"; 397 bool search_file = type == "file";
408 bool search_builtin = type == "builtin"; 398 bool search_builtin = type == "builtin";
409 399
410 if (search_any || search_var) 400 if (search_any || search_var)
411 { 401 {
412 bool not_a_struct = struct_elts.empty ();
413 bool var_ok = not_a_struct; // || val.is_map_element (struct_elts)
414
415 octave_value val = symbol_table::varval (name); 402 octave_value val = symbol_table::varval (name);
416 403
417 if (var_ok && (val.is_constant () || val.is_object () 404 if (val.is_constant () || val.is_object ()
418 || val.is_function_handle () 405 || val.is_function_handle ()
419 || val.is_anonymous_function () 406 || val.is_anonymous_function ()
420 || val.is_inline_function ())) 407 || val.is_inline_function ())
421 return 1; 408 return 1;
422 409
423 if (search_var) 410 if (search_var)
424 return 0; 411 return 0;
412 }
413
414 // We shouldn't need to look in the global symbol table, since any name
415 // that is visible in the current scope will be in the local symbol table.
416
417 octave_value val = safe_symbol_lookup (name);
418
419 if (val.is_defined ())
420 {
421 if ((search_any || search_builtin)
422 && val.is_builtin_function ())
423 return 5;
425 } 424 }
426 425
427 if (search_any || search_file || search_dir) 426 if (search_any || search_file || search_dir)
428 { 427 {
429 std::string file_name = lookup_autoload (name); 428 std::string file_name = lookup_autoload (name);
462 461
463 if (search_file || search_dir) 462 if (search_file || search_dir)
464 return 0; 463 return 0;
465 } 464 }
466 465
467 // We shouldn't need to look in the global symbol table, since any 466 if (val.is_defined ())
468 // name that is visible in the current scope will be in the local 467 {
469 // symbol table.
470
471 octave_value val = safe_symbol_lookup (symbol_name);
472
473 if (val.is_defined () && struct_elts.empty ())
474 {
475 if ((search_any || search_builtin)
476 && val.is_builtin_function ())
477 return 5;
478
479 if ((search_any || search_file) 468 if ((search_any || search_file)
480 && (val.is_user_function () || val.is_dld_function ())) 469 && (val.is_user_function () || val.is_dld_function ()))
481 { 470 {
482 octave_function *f = val.function_value (true); 471 octave_function *f = val.function_value (true);
483 std::string s = f ? f->fcn_file_name () : std::string (); 472 std::string s = f ? f->fcn_file_name () : std::string ();
484 473
474 // FIXME: I believe that by this point in the code the only
475 // return value is 103. User functions should have
476 // been located above. Maybe replace entire if block
477 // code with "return 103;"
485 return s.empty () ? 103 : (val.is_user_function () ? 2 : 3); 478 return s.empty () ? 103 : (val.is_user_function () ? 2 : 3);
486 } 479 }
487 } 480 }
488 481
489 return 0; 482 return 0;
581 574
582 return retval; 575 return retval;
583 } 576 }
584 577
585 /* 578 /*
579 %!shared dirtmp, __var1
580 %! dirtmp = P_tmpdir ();
581 %! __var1 = 1;
582
583 %!assert (exist ("__%Highly_unlikely_name%__"), 0)
584 %!assert (exist ("__var1"), 1)
585 %!assert (exist ("__var1", "var"), 1)
586 %!assert (exist ("__var1", "builtin"), 0)
587 %!assert (exist ("__var1", "dir"), 0)
588 %!assert (exist ("__var1", "file"), 0)
589
586 %!test 590 %!test
587 %! if (isunix ()) 591 %! if (isunix ())
588 %! assert (exist ("/tmp") == 7); 592 %! assert (exist ("/bin/sh"), 2);
589 %! assert (exist ("/tmp", "file") == 7); 593 %! assert (exist ("/bin/sh", "file"), 2);
590 %! assert (exist ("/tmp", "dir") == 7); 594 %! assert (exist ("/bin/sh", "dir"), 0);
591 %! assert (exist ("/bin/sh") == 2); 595 %! assert (exist ("/dev/null"), 2);
592 %! assert (exist ("/bin/sh", "file") == 2); 596 %! assert (exist ("/dev/null", "file"), 2);
593 %! assert (exist ("/bin/sh", "dir") == 0); 597 %! assert (exist ("/dev/null", "dir"), 0);
594 %! assert (exist ("/dev/null") == 2);
595 %! assert (exist ("/dev/null", "file") == 2);
596 %! assert (exist ("/dev/null", "dir") == 0);
597 %! endif 598 %! endif
599
600 %!assert (exist ("colon"), 2)
601 %!assert (exist ("colon.m"), 2)
602
603 %!testif HAVE_CHOLMOD
604 %! assert (exist ("chol"), 3);
605 %! assert (exist ("chol", "file"), 3);
606 %! assert (exist ("chol", "builtin"), 0);
607
608 %!assert (exist ("sin"), 5)
609
610 %!assert (exist (dirtmp), 7)
611 %!assert (exist (dirtmp, "dir"), 7)
612 %!assert (exist (dirtmp, "file"), 7)
613
598 */ 614 */
599 615
600 octave_value 616 octave_value
601 lookup_function_handle (const std::string& nm) 617 lookup_function_handle (const std::string& nm)
602 { 618 {