annotate libinterp/corefcn/variables.cc @ 30304:b568c29bb5be

Add support for "class" option to exist() function (bug #46260) * variables.cc (symbol_exist): Add support for "class" input by checking symbol table for function which for which is_classdef_meta () returns true. * variables.cc (Fexist): Update documentation to describe support for "class" input type. Remove warning emitted when "class" type used. Update BIST tests.
author Rik <rik@octave.org>
date Fri, 19 Nov 2021 20:57:09 -0800
parents 428cccc75cc9
children eff73c13fcf3 796f54d4ddbf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29032
diff changeset
3 // Copyright (C) 1993-2021 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21272
diff changeset
27 # include "config.h"
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
28 #endif
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
29
1468
cdb87ef617e8 [project @ 1995-09-22 08:09:53 by jwe]
jwe
parents: 1465
diff changeset
30 #include <cstdio>
1343
94bedeb289e5 [project @ 1995-09-04 00:29:21 by jwe]
jwe
parents: 1315
diff changeset
31 #include <cstring>
605
4f65175911a6 [project @ 1994-08-13 20:10:39 by jwe]
jwe
parents: 598
diff changeset
32
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
33 #include <iomanip>
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
34 #include <list>
4207
fa3482b34599 [project @ 2002-12-03 18:22:05 by jwe]
jwe
parents: 4179
diff changeset
35 #include <set>
1728
42b4f904f1af [project @ 1996-01-09 11:36:01 by jwe]
jwe
parents: 1719
diff changeset
36 #include <string>
42b4f904f1af [project @ 1996-01-09 11:36:01 by jwe]
jwe
parents: 1719
diff changeset
37
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2921
diff changeset
38 #include "file-stat.h"
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2921
diff changeset
39 #include "oct-env.h"
4604
cba347c642e2 [project @ 2003-11-13 04:38:05 by jwe]
jwe
parents: 4587
diff changeset
40 #include "file-ops.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2921
diff changeset
41 #include "glob-match.h"
16940
99122191d3dd maint: Rename regexp.h to lo-regexp.h, regexp.cc to lo-regexp.cc in liboctave.
Rik <rik@octave.org>
parents: 16892
diff changeset
42 #include "lo-regexp.h"
1755
3a9462b655f1 [project @ 1996-01-22 04:47:22 by jwe]
jwe
parents: 1742
diff changeset
43 #include "str-vec.h"
3a9462b655f1 [project @ 1996-01-22 04:47:22 by jwe]
jwe
parents: 1742
diff changeset
44
4435
dd7af4fa1942 [project @ 2003-06-23 17:52:36 by jwe]
jwe
parents: 4357
diff changeset
45 #include "Cell.h"
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
46 #include "defun.h"
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
47 #include "error.h"
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21095
diff changeset
48 #include "errwarn.h"
27263
99aa1bcb8848 rename octave_link and octave_link_events classes, move inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27261
diff changeset
49 #include "event-manager.h"
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
50 #include "help.h"
3165
e4bbfc196e53 [project @ 1998-04-16 03:01:47 by jwe]
jwe
parents: 3162
diff changeset
51 #include "input.h"
23438
d24d01273bd0 eliminate load-path singleton
John W. Eaton <jwe@octave.org>
parents: 23332
diff changeset
52 #include "interpreter-private.h"
22094
9203833cab7d move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents: 22091
diff changeset
53 #include "interpreter.h"
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
54 #include "lex.h"
5832
5e41e06f6a78 [project @ 2006-05-26 21:41:32 by jwe]
jwe
parents: 5823
diff changeset
55 #include "load-path.h"
20925
667861ffffab Extracted header class octave-preserve-stream-state from utils.{cc/h}.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20909
diff changeset
56 #include "octave-preserve-stream-state.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2921
diff changeset
57 #include "oct-map.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20939
diff changeset
58 #include "ovl.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2921
diff changeset
59 #include "ov.h"
9240
f27a8c07f0b2 clear -classes and support.
Robert T. Short <octave@phaselockedsystems.com>
parents: 9180
diff changeset
60 #include "ov-class.h"
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3681
diff changeset
61 #include "ov-usr-fcn.h"
605
4f65175911a6 [project @ 1994-08-13 20:10:39 by jwe]
jwe
parents: 598
diff changeset
62 #include "pager.h"
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
63 #include "parse.h"
27016
9b261300a001 move file-scope static autoload map variable and functions to evaluator
John W. Eaton <jwe@octave.org>
parents: 26843
diff changeset
64 #include "pt-eval.h"
26088
2b92498c111e move symbol_info and symbol_info_list classes to separate file
John W. Eaton <jwe@octave.org>
parents: 26087
diff changeset
65 #include "syminfo.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2921
diff changeset
66 #include "symtab.h"
26843
f07542e3a9b9 Fix "exist" for drive letters and UNC shares on Windows (bug #55824).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26687
diff changeset
67 #include "sysdep.h"
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
68 #include "unwind-prot.h"
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
69 #include "utils.h"
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
70 #include "variables.h"
2205
bd389b53befa [project @ 1996-05-15 06:15:45 by jwe]
jwe
parents: 2181
diff changeset
71
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
72 // Attributes of variables and functions.
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
73
2086
bfb775fb6fe8 [project @ 1996-04-25 05:55:19 by jwe]
jwe
parents: 2076
diff changeset
74 // Is this octave_value a valid function?
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
75
2975
b9c107cbe388 [project @ 1997-05-15 20:01:00 by jwe]
jwe
parents: 2963
diff changeset
76 octave_function *
4345
4e23bfdd6172 [project @ 2003-02-20 20:45:49 by jwe]
jwe
parents: 4342
diff changeset
77 is_valid_function (const std::string& fcn_name,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
78 const std::string& warn_for, bool warn)
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
79 {
23457
21baad6b35c4 maint: Use C++11 nullptr rather than 0 or NULL when possible.
Rik <rik@octave.org>
parents: 23450
diff changeset
80 octave_function *ans = nullptr;
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
81
1755
3a9462b655f1 [project @ 1996-01-22 04:47:22 by jwe]
jwe
parents: 1742
diff changeset
82 if (! fcn_name.empty ())
3618
8cea69ad95ae [project @ 2000-03-22 20:51:45 by jwe]
jwe
parents: 3565
diff changeset
83 {
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
84 octave::symbol_table& symtab
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
85 = octave::__get_symbol_table__ ("is_valid_function");
23599
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
86
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
87 octave_value val = symtab.find_function (fcn_name);
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
88
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
89 if (val.is_defined ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
90 ans = val.function_value (true);
3618
8cea69ad95ae [project @ 2000-03-22 20:51:45 by jwe]
jwe
parents: 3565
diff changeset
91 }
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
92
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
93 // FIXME: Should this be "err" and "error_for", rather than warn?
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
94 if (! ans && warn)
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
95 error ("%s: the symbol '%s' is not valid as a function",
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
96 warn_for.c_str (), fcn_name.c_str ());
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
97
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
98 return ans;
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
99 }
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
100
2975
b9c107cbe388 [project @ 1997-05-15 20:01:00 by jwe]
jwe
parents: 2963
diff changeset
101 octave_function *
4345
4e23bfdd6172 [project @ 2003-02-20 20:45:49 by jwe]
jwe
parents: 4342
diff changeset
102 is_valid_function (const octave_value& arg,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
103 const std::string& warn_for, bool warn)
3178
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
104 {
23457
21baad6b35c4 maint: Use C++11 nullptr rather than 0 or NULL when possible.
Rik <rik@octave.org>
parents: 23450
diff changeset
105 octave_function *ans = nullptr;
3178
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
106
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3476
diff changeset
107 std::string fcn_name;
3178
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
108
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
109 if (arg.is_string ())
4700
ca3a1d687bba [project @ 2004-01-21 03:28:31 by jwe]
jwe
parents: 4604
diff changeset
110 {
ca3a1d687bba [project @ 2004-01-21 03:28:31 by jwe]
jwe
parents: 4604
diff changeset
111 fcn_name = arg.string_value ();
3178
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
112
19743
67f2c76f9f4d Remove unnecessary checking of error_state after is_string validation.
Rik <rik@octave.org>
parents: 19718
diff changeset
113 ans = is_valid_function (fcn_name, warn_for, warn);
4700
ca3a1d687bba [project @ 2004-01-21 03:28:31 by jwe]
jwe
parents: 4604
diff changeset
114 }
3178
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
115 else if (warn)
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
116 // FIXME: Should this be "err" and "error_for", rather than warn?
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22333
diff changeset
117 error ("%s: argument must be a string containing function name",
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22333
diff changeset
118 warn_for.c_str ());
3178
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
119
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
120 return ans;
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
121 }
09a3064a3a17 [project @ 1998-06-23 03:46:42 by jwe]
jwe
parents: 3165
diff changeset
122
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29657
diff changeset
123 OCTAVE_NAMESPACE_BEGIN
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29657
diff changeset
124
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
125 DEFMETHOD (isglobal, interp, args, ,
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
126 doc: /* -*- texinfo -*-
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
127 @deftypefn {} {} isglobal (@var{name})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
128 Return true if @var{name} is a globally visible variable.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
129
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
130 For example:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
131
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
132 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
133 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
134 global x
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
135 isglobal ("x")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
136 @result{} 1
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
137 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
138 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
139 @seealso{isvarname, exist}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
140 @end deftypefn */)
5930
a703198cb8b8 [project @ 2006-08-15 06:06:15 by jwe]
jwe
parents: 5929
diff changeset
141 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
142 if (args.length () != 1)
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
143 print_usage ();
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
144
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
145 std::string name = args(0).xstring_value ("isglobal: NAME must be a string");
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
146
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
147 return ovl (interp.isglobal (name));
5930
a703198cb8b8 [project @ 2006-08-15 06:06:15 by jwe]
jwe
parents: 5929
diff changeset
148 }
a703198cb8b8 [project @ 2006-08-15 06:06:15 by jwe]
jwe
parents: 5929
diff changeset
149
21389
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
150 /*
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
151 %!test
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
152 %! global x;
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21568
diff changeset
153 %! assert (isglobal ("x"), true);
25762
97ea2329d5d8 Use nested functions to prevent leaking globals in BIST tests.
Rik <rik@octave.org>
parents: 25646
diff changeset
154 %! clear -global x; # cleanup after test
21389
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
155
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
156 %!error isglobal ()
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
157 %!error isglobal ("a", "b")
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
158 %!error isglobal (1)
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
159 */
64efef931bd0 Add BIST tests for bitpack, bitunpack, isglobal, and typecast
Mike Miller <mtmiller@octave.org>
parents: 21317
diff changeset
160
23143
898c33f4b9c0 declare some functions that are only used once "static"
John W. Eaton <jwe@octave.org>
parents: 23110
diff changeset
161 static int
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
162 symbol_exist (interpreter& interp, const std::string& name,
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
163 const std::string& type = "any")
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
164 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
165 if (iskeyword (name))
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
166 return 0;
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
167
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
168 bool search_any = type == "any";
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
169 bool search_var = type == "var";
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
170 bool search_dir = type == "dir";
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
171 bool search_file = type == "file";
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
172 bool search_builtin = type == "builtin";
20206
3c8260fd0837 Validate second option to exist () (bug #42618).
Rik <rik@octave.org>
parents: 20181
diff changeset
173 bool search_class = type == "class";
3c8260fd0837 Validate second option to exist () (bug #42618).
Rik <rik@octave.org>
parents: 20181
diff changeset
174
25951
7a507e71d922 style fixes
John W. Eaton <jwe@octave.org>
parents: 25463
diff changeset
175 if (! (search_any || search_var || search_dir || search_file
7a507e71d922 style fixes
John W. Eaton <jwe@octave.org>
parents: 25463
diff changeset
176 || search_builtin || search_class))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
177 error (R"(exist: unrecognized type argument "%s")", type.c_str ());
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
178
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
179 if (search_any || search_var)
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
180 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
181 octave_value val = interp.varval (name);
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
182
23587
0c468af9dc00 maint: Deprecate is_object and replace with isobject.
Rik <rik@octave.org>
parents: 23584
diff changeset
183 if (val.is_constant () || val.isobject ()
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
184 || val.is_function_handle ()
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
185 || val.is_anonymous_function ()
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
186 || val.is_inline_function ())
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
187 return 1;
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
188
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
189 if (search_var)
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
190 return 0;
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
191 }
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
192
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
193 symbol_table& symtab = interp.get_symbol_table ();
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
194
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
195 // We shouldn't need to look in the global symbol table, since any name
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
196 // that is visible in the current scope will be in the local symbol table.
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
197
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
198 if (search_any || search_file || search_dir || search_class)
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
199 {
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
200 bool have_fcn_ext = false;
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
201
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
202 std::string xname = name;
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
203 std::string ext;
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
204
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
205 std::size_t pos = name.rfind ('.');
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
206
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
207 if (pos != std::string::npos)
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
208 {
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
209 ext = name.substr (pos+1);
27016
9b261300a001 move file-scope static autoload map variable and functions to evaluator
John W. Eaton <jwe@octave.org>
parents: 26843
diff changeset
210
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
211 if (ext == "m" || ext == "oct" || ext == "mex")
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
212 {
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
213 xname = name.substr (0, pos);
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
214 have_fcn_ext = true;
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
215 }
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
216 }
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
217
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
218 std::string file_name;
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
219
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
220 if (search_any || search_file || search_class)
23438
d24d01273bd0 eliminate load-path singleton
John W. Eaton <jwe@octave.org>
parents: 23332
diff changeset
221 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
222 load_path& lp = interp.get_load_path ();
23438
d24d01273bd0 eliminate load-path singleton
John W. Eaton <jwe@octave.org>
parents: 23332
diff changeset
223
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
224 // Look for class constructor first
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
225 file_name = lp.find_method (xname, xname);
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
226
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
227 if (have_fcn_ext && ! file_name.empty ())
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
228 {
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
229 // Verify extension of file_name found matches ext of name.
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
230 pos = file_name.rfind ('.');
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
231
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
232 if (pos != std::string::npos)
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
233 {
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
234 std::string fext = file_name.substr (pos+1);
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
235
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
236 if (ext != fext)
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
237 file_name = "";
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
238 }
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
239 }
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
240
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
241 if (search_any && file_name.empty ())
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
242 {
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
243 // Command line function which Matlab does not support
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
244 octave_value val = symtab.find_cmdline_function (xname);
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
245
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
246 if (val.is_defined ())
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
247 return 103;
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
248 }
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
249
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
250 // Autoloads can only have simple names without extensions.
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
251 if (! have_fcn_ext && file_name.empty ())
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
252 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
253 tree_evaluator& tw = interp.get_evaluator ();
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
254
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
255 file_name = tw.lookup_autoload (name);
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
256 }
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
257
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
258 // If nothing found, look for function using original name.
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
259 if (file_name.empty ())
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
260 file_name = lp.find_fcn (name);
23438
d24d01273bd0 eliminate load-path singleton
John W. Eaton <jwe@octave.org>
parents: 23332
diff changeset
261 }
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
262
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
263 std::size_t len = file_name.length ();
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
264
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
265 if (len > 0 && (search_any || search_file || search_class))
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
266 {
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
267 if (search_any || search_file)
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
268 {
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
269 if (len > 4 && (file_name.substr (len-4) == ".oct"
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
270 || file_name.substr (len-4) == ".mex"))
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
271 return 3;
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
272 }
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
273
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
274 if (search_class)
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
275 {
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
276 octave_value oval = symtab.find_function (name);
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
277 if (oval.is_defined () && oval.is_classdef_meta ())
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
278 return 8;
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
279 else
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
280 return 0;
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
281 }
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
282
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
283 return 2;
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
284 }
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
285
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
286 // Nothing found in symbol table, try searching in path
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
287 file_name = file_in_path (name, "");
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
288
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
289 if (file_name.empty ())
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
290 file_name = name;
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
291
26843
f07542e3a9b9 Fix "exist" for drive letters and UNC shares on Windows (bug #55824).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26687
diff changeset
292 // "stat" doesn't work on UNC shares and drive letters.
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
293 if ((search_any || search_file) && drive_or_unc_share (file_name))
26843
f07542e3a9b9 Fix "exist" for drive letters and UNC shares on Windows (bug #55824).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26687
diff changeset
294 return 7;
f07542e3a9b9 Fix "exist" for drive letters and UNC shares on Windows (bug #55824).
Markus Mützel <markus.muetzel@gmx.de>
parents: 26687
diff changeset
295
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
296 sys::file_stat fs (file_name);
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
297
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
298 if (fs)
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
299 {
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
300 if (search_any || search_file)
19861
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
301 {
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
302 if (fs.is_dir ())
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
303 return 7;
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
304
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
305 len = file_name.length ();
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
306
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
307 if (len > 4 && (file_name.substr (len-4) == ".oct"
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
308 || file_name.substr (len-4) == ".mex"))
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
309 return 3;
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
310 else
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
311 return 2;
19755f4fc851 maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19750
diff changeset
312 }
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
313 else if (search_dir && fs.is_dir ())
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
314 return 7;
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
315 }
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
316
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
317 if (search_file || search_dir)
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
318 return 0;
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
319 }
1277
db4f4009d6e8 [project @ 1995-04-24 20:35:06 by jwe]
jwe
parents: 1271
diff changeset
320
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
321 if ((search_any || search_builtin)
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
322 && symtab.is_built_in_function_name (name))
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
323 return 5;
4016
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
324
17963
2ca3a2f46d93 improve performance of exist when searching for specific items
John W. Eaton <jwe@octave.org>
parents: 17861
diff changeset
325 return 0;
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
326 }
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
327
23927
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
328 int
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
329 symbol_exist (const std::string& name, const std::string& type)
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
330 {
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
331 octave::interpreter& interp = octave::__get_interpreter__ ("symbol_exist");
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
332
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29657
diff changeset
333 return octave::symbol_exist (interp, name, type);
23927
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
334 }
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
335
e3a36f84d01d provide variable-editor widget for the GUI
Michael Barnes <mjbcode@runbox.com>
parents: 23829
diff changeset
336
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22196
diff changeset
337 #define GET_IDX(LEN) \
26372
2d9ae06f621b Fix static analyzer detected V1003 issues with expansions in macros (bug #55347).
Rik <rik@octave.org>
parents: 26169
diff changeset
338 static_cast<int> (((LEN)-1) * static_cast<double> (rand ()) / RAND_MAX)
4962
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
339
4954
ed0f3cb6d3d4 [project @ 2004-09-01 21:24:53 by jwe]
jwe
parents: 4930
diff changeset
340 std::string
ed0f3cb6d3d4 [project @ 2004-09-01 21:24:53 by jwe]
jwe
parents: 4930
diff changeset
341 unique_symbol_name (const std::string& basename)
ed0f3cb6d3d4 [project @ 2004-09-01 21:24:53 by jwe]
jwe
parents: 4930
diff changeset
342 {
4962
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
343 static const std::string alpha
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
344 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
345
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
346 static std::size_t len = alpha.length ();
4962
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
347
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
348 std::string nm = basename + alpha[GET_IDX (len)];
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
349
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
350 std::size_t pos = nm.length ();
4962
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
351
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
352 if (nm.substr (0, 2) == "__")
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
353 nm.append ("__");
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
354
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
355 octave::interpreter& interp
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
356 = octave::__get_interpreter__ ("unique_symbol_name");
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
357
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
358 while (symbol_exist (interp, nm, "any"))
4962
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
359 nm.insert (pos++, 1, alpha[GET_IDX (len)]);
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
360
0ab18cbe8b5f [project @ 2004-09-02 16:56:17 by jwe]
jwe
parents: 4954
diff changeset
361 return nm;
4954
ed0f3cb6d3d4 [project @ 2004-09-01 21:24:53 by jwe]
jwe
parents: 4930
diff changeset
362 }
ed0f3cb6d3d4 [project @ 2004-09-01 21:24:53 by jwe]
jwe
parents: 4930
diff changeset
363
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
364 DEFMETHOD (exist, interp, args, ,
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
365 doc: /* -*- texinfo -*-
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
366 @deftypefn {} {@var{c} =} exist (@var{name})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
367 @deftypefnx {} {@var{c} =} exist (@var{name}, @var{type})
24485
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
368 Check for the existence of @var{name} as a variable, function, file, directory,
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
369 or class.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
370
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
371 The return code @var{c} is one of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
372
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
373 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
374 @item 1
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
375 @var{name} is a variable.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
376
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
377 @item 2
24485
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
378 @var{name} is an absolute filename, an ordinary file in Octave's @code{path},
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
379 or (after appending @samp{.m}) a function file in Octave's @code{path}.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
380
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
381 @item 3
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
382 @var{name} is a @samp{.oct} or @samp{.mex} file in Octave's @code{path}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
383
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
384 @item 5
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
385 @var{name} is a built-in function.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
386
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
387 @item 7
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
388 @var{name} is a directory.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
389
24485
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
390 @item 8
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
391 @var{name} is a classdef class.
24485
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
392
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
393 @item 103
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
394 @var{name} is a function not associated with a file (entered on the command
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
395 line).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
396
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
397 @item 0
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
398 @var{name} does not exist.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
399 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
400
24485
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
401 If the optional argument @var{type} is supplied, check only for symbols of the
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
402 specified type. Valid types are
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
403
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
404 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
405 @item @qcode{"var"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
406 Check only for variables.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
407
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
408 @item @qcode{"builtin"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
409 Check only for built-in functions.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
410
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
411 @item @qcode{"dir"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
412 Check only for directories.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
413
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
414 @item @qcode{"file"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
415 Check only for files and directories.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
416
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
417 @item @qcode{"class"}
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
418 Check only for classdef classes.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
419 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
420
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
421 If no type is given, and there are multiple possible matches for name,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
422 @code{exist} will return a code according to the following priority list:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
423 variable, built-in function, oct-file, directory, file, class.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
424
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
425 @code{exist} returns 2 if a regular file called @var{name} is present in
24485
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
426 Octave's search path. For information about other types of files not on the
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
427 search path use some combination of the functions @code{file_in_path} and
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
428 @code{stat} instead.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
429
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
430 Programming Note: If @var{name} is implemented by a buggy .oct/.mex file,
24485
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
431 calling @var{exist} may cause Octave to crash. To maintain high performance,
ad04b8055d91 doc: Note that return code 8 (class) is not implemented for exist().
Rik <rik@octave.org>
parents: 24449
diff changeset
432 Octave trusts .oct/.mex files instead of @nospell{sandboxing} them.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
433
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
434 @seealso{file_in_loadpath, file_in_path, dir_in_loadpath, stat}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
435 @end deftypefn */)
4016
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
436 {
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
437 int nargin = args.length ();
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
438
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
439 if (nargin < 1 || nargin > 2)
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
440 print_usage ();
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
441
29356
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
442 // For compatibility with undocumented Matlab behavior, return 0 if
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
443 // there is an empty built-in object as the only argument.
29376
107091f22e65 Return 0 if first argument to exist() is empty regardless of nargin value (bug #59950).
Rik <rik@octave.org>
parents: 29359
diff changeset
444 if (args(0).builtin_type () != btyp_unknown && args(0).isempty ())
29356
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
445 return ovl (0);
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
446
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
447 // Also for compatibility, return 0 if the second argument is an empty
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
448 // built-in object.
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
449 if (nargin == 2 && args(1).builtin_type () != btyp_unknown
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
450 && args(1).isempty ())
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
451 return ovl (0);
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
452
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
453 std::string name = args(0).xstring_value ("exist: NAME must be a string");
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
454
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
455 if (nargin == 2)
4016
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
456 {
29356
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
457 std::string type
65c57984a65e compatibility with undocumented Matlab behavior for exist function (bug #59950)
John W. Eaton <jwe@octave.org>
parents: 29033
diff changeset
458 = args(1).xstring_value ("exist: TYPE must be a string");
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
459
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
460 return ovl (symbol_exist (interp, name, type));
4016
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
461 }
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
462 else
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
463 return ovl (symbol_exist (interp, name));
4016
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
464 }
139f5273d0f8 [project @ 2002-08-02 22:55:51 by jwe]
jwe
parents: 4010
diff changeset
465
10975
2d14817353a6 allow exist to work correctly for special files; recognize directories when searching for files
John W. Eaton <jwe@octave.org>
parents: 10846
diff changeset
466 /*
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
467 %!shared dirtmp, __var1
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
468 %! dirtmp = P_tmpdir ();
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
469 %! __var1 = 1;
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
470
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
471 %!assert (exist ("__%Highly_unlikely_name%__"), 0)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
472 %!assert (exist ("__var1"), 1)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
473 %!assert (exist ("__var1", "var"), 1)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
474 %!assert (exist ("__var1", "builtin"), 0)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
475 %!assert (exist ("__var1", "dir"), 0)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
476 %!assert (exist ("__var1", "file"), 0)
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
477 %!assert (exist ("__var1", "class"), 0)
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
478
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
479 %!testif ; isunix ()
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
480 %! assert (exist ("/bin/sh"), 2);
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
481 %! assert (exist ("/bin/sh", "file"), 2);
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
482 %! assert (exist ("/bin/sh", "dir"), 0);
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
483 %! assert (exist ("/dev/null"), 2);
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
484 %! assert (exist ("/dev/null", "file"), 2);
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
485 %! assert (exist ("/dev/null", "dir"), 0);
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
486
19750
2111917d34c4 fix tests failing after changeset c913247c85a8
John W. Eaton <jwe@octave.org>
parents: 19743
diff changeset
487 %!assert (exist ("print_usage"), 2)
2111917d34c4 fix tests failing after changeset c913247c85a8
John W. Eaton <jwe@octave.org>
parents: 19743
diff changeset
488 %!assert (exist ("print_usage.m"), 2)
2111917d34c4 fix tests failing after changeset c913247c85a8
John W. Eaton <jwe@octave.org>
parents: 19743
diff changeset
489 %!assert (exist ("print_usage", "file"), 2)
2111917d34c4 fix tests failing after changeset c913247c85a8
John W. Eaton <jwe@octave.org>
parents: 19743
diff changeset
490 %!assert (exist ("print_usage", "dir"), 0)
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
491
20713
2469d78a1d8b Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents: 20711
diff changeset
492 ## Don't search path for rooted relative filenames
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
493 %!assert (exist ("plot.m", "file"), 2)
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
494 %!assert (exist ("./plot.m", "file"), 0)
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
495 %!assert (exist ("./%nonexistentfile%", "file"), 0)
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
496 %!assert (exist ("%nonexistentfile%", "file"), 0)
20181
aa36fb998a4d maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents: 20178
diff changeset
497
20713
2469d78a1d8b Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents: 20711
diff changeset
498 ## Don't search path for absolute filenames
19915
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
499 %!test
26677
33f79d94f21f Create any BIST temporary files in writable temporary directory (bug #55565).
Rik <rik@octave.org>
parents: 26661
diff changeset
500 %! tname = tempname ();
19915
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
501 %! unwind_protect
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
502 %! ## open/close file to create it, equivalent of touch
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
503 %! fid = fopen (tname, "w");
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
504 %! fclose (fid);
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
505 %! [~, fname] = fileparts (tname);
26679
2b6aa96a5c82 create BIST temporary files in tempdir instead of P_tmpdir (bug #55565)
Mike Miller <mtmiller@octave.org>
parents: 26677
diff changeset
506 %! assert (exist (fullfile (tempdir (), fname), "file"), 2);
19915
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
507 %! unwind_protect_cleanup
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
508 %! unlink (tname);
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
509 %! end_unwind_protect
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
510 %! assert (exist (fullfile (pwd (), "%nonexistentfile%"), "file"), 0);
85c5a81c6ddd Rewrite BIST tests for exist to pass on Windows (bug #44445).
Rik <rik@octave.org>
parents: 19864
diff changeset
511
28907
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
512 %!assert (exist ("fftw"), 3)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
513 %!assert (exist ("fftw.oct"), 3)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
514 %!assert (exist ("fftw", "file"), 3)
11f1207111c5 maint: Don't use semicolon at end of single-line BIST tests.
Rik <rik@octave.org>
parents: 28888
diff changeset
515 %!assert (exist ("fftw", "builtin"), 0)
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
516
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
517 %!assert (exist ("ftp"), 2)
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
518 %!assert (exist ("ftp.m"), 2)
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
519 %!assert (exist ("@ftp/ftp"), 2)
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
520 %!assert (exist ("@ftp/ftp.m"), 2)
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
521 %!assert (exist ("ftp", "class"), 0)
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
522
30304
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
523 %!assert (exist ("inputParser"), 2)
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
524 %!assert (exist ("inputParser.m"), 2)
b568c29bb5be Add support for "class" option to exist() function (bug #46260)
Rik <rik@octave.org>
parents: 30001
diff changeset
525 %!assert (exist ("inputParser", "class"), 8)
29032
2cbfd91aafb9 improve behavior of exist for finding class constructors (bug #59304)
John W. Eaton <jwe@octave.org>
parents: 28504
diff changeset
526
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
527 %!assert (exist ("sin"), 5)
18861
da6ffbf75edf Simplify exist() code for recognizing command line functions.
Rik <rik@octave.org>
parents: 18858
diff changeset
528 %!assert (exist ("sin", "builtin"), 5)
da6ffbf75edf Simplify exist() code for recognizing command line functions.
Rik <rik@octave.org>
parents: 18858
diff changeset
529 %!assert (exist ("sin", "file"), 0)
18856
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
530
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
531 %!assert (exist (dirtmp), 7)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
532 %!assert (exist (dirtmp, "dir"), 7)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
533 %!assert (exist (dirtmp, "file"), 7)
de8c67ba7ac4 Use Matlab return hierarchy for exist() codes when no type is specified.
Rik <rik@octave.org>
parents: 18282
diff changeset
534
18880
ca0d9844dfd7 Fix input validation for exist() which did not error on non-string input.
Rik <rik@octave.org>
parents: 18879
diff changeset
535 %!error exist ()
ca0d9844dfd7 Fix input validation for exist() which did not error on non-string input.
Rik <rik@octave.org>
parents: 18879
diff changeset
536 %!error exist (1,2,3)
ca0d9844dfd7 Fix input validation for exist() which did not error on non-string input.
Rik <rik@octave.org>
parents: 18879
diff changeset
537 %!error <TYPE must be a string> exist ("a", 1)
ca0d9844dfd7 Fix input validation for exist() which did not error on non-string input.
Rik <rik@octave.org>
parents: 18879
diff changeset
538 %!error <NAME must be a string> exist (1)
20206
3c8260fd0837 Validate second option to exist () (bug #42618).
Rik <rik@octave.org>
parents: 20181
diff changeset
539 %!error <unrecognized type argument "foobar"> exist ("a", "foobar")
18880
ca0d9844dfd7 Fix input validation for exist() which did not error on non-string input.
Rik <rik@octave.org>
parents: 18879
diff changeset
540
10975
2d14817353a6 allow exist to work correctly for special files; recognize directories when searching for files
John W. Eaton <jwe@octave.org>
parents: 10846
diff changeset
541 */
2d14817353a6 allow exist to work correctly for special files; recognize directories when searching for files
John W. Eaton <jwe@octave.org>
parents: 10846
diff changeset
542
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
543 // Variable values.
195
13c6086c325c [project @ 1993-11-06 10:12:29 by jwe]
jwe
parents: 191
diff changeset
544
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
545 static bool
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
546 wants_local_change (const octave_value_list& args, int& nargin)
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
547 {
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
548 bool retval = false;
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
549
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
550 if (nargin == 2)
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
551 {
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
552 if (! args(1).is_string () || args(1).string_value () != "local")
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
553 error_with_cfn (R"(second argument must be "local")");
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
554
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
555 nargin = 1;
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
556 retval = true;
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
557 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
558
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
559 return retval;
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
560 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
561
23704
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
562 static octave::unwind_protect *
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
563 curr_fcn_unwind_protect_frame (void)
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
564 {
27204
9a3e92d02a03 eliminate direct access to call stack in variables functions
John W. Eaton <jwe@octave.org>
parents: 27160
diff changeset
565 octave::tree_evaluator& tw
9a3e92d02a03 eliminate direct access to call stack in variables functions
John W. Eaton <jwe@octave.org>
parents: 27160
diff changeset
566 = octave::__get_evaluator__ ("curr_fcn_unwind_protect_frame");
23553
14723784b9f2 don't use singleton for call_stack
John W. Eaton <jwe@octave.org>
parents: 23466
diff changeset
567
27204
9a3e92d02a03 eliminate direct access to call stack in variables functions
John W. Eaton <jwe@octave.org>
parents: 27160
diff changeset
568 return tw.curr_fcn_unwind_protect_frame ();
23704
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
569 }
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
570
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
571 template <typename T>
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
572 static bool
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
573 try_local_protect (T& var)
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
574 {
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
575 octave::unwind_protect *frame = curr_fcn_unwind_protect_frame ();
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
576
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
577 if (frame)
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
578 {
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
579 frame->protect_var (var);
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
580 return true;
c495549e9a94 refactor local protection for internal variables
John W. Eaton <jwe@octave.org>
parents: 23703
diff changeset
581 }
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
582 else
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
583 return false;
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
584 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
585
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
586 octave_value
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
587 set_internal_variable (bool& var, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
588 int nargout, const char *nm)
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
589 {
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
590 octave_value retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
591
5800
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
592 int nargin = args.length ();
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
593
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
594 if (nargout > 0 || nargin == 0)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
595 retval = var;
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
596
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
597 if (wants_local_change (args, nargin))
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
598 {
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
599 if (! try_local_protect (var))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
600 warning (R"("local" has no effect outside a function)");
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
601 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
602
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
603 if (nargin > 1)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
604 print_usage ();
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
605
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
606 if (nargin == 1)
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
607 {
20711
7b608fadc663 Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents: 20704
diff changeset
608 bool bval = args(0).xbool_value ("%s: argument must be a logical value", nm);
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
609
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
610 var = bval;
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
611 }
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
612
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
613 return retval;
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
614 }
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
615
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
616 octave_value
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
617 set_internal_variable (char& var, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
618 int nargout, const char *nm)
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
619 {
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
620 octave_value retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
621
5800
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
622 int nargin = args.length ();
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
623
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
624 if (nargout > 0 || nargin == 0)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
625 retval = var;
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
626
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
627 if (wants_local_change (args, nargin))
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
628 {
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
629 if (! try_local_protect (var))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
630 warning (R"("local" has no effect outside a function)");
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
631 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
632
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
633 if (nargin > 1)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
634 print_usage ();
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
635
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
636 if (nargin == 1)
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
637 {
20700
68e3a747ca02 rename octave_value value extractors that accept error message args
John W. Eaton <jwe@octave.org>
parents: 20581
diff changeset
638 std::string sval = args(0).xstring_value ("%s: argument must be a single character", nm);
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
639
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
640 switch (sval.length ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
641 {
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
642 case 1:
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
643 var = sval[0];
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
644 break;
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
645
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
646 case 0:
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
647 var = '\0';
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
648 break;
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
649
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
650 default:
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
651 error ("%s: argument must be a single character", nm);
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
652 break;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
653 }
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
654 }
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
655
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
656 return retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
657 }
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
658
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
659 octave_value
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
660 set_internal_variable (int& var, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
661 int nargout, const char *nm,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
662 int minval, int maxval)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
663 {
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
664 octave_value retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
665
5800
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
666 int nargin = args.length ();
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
667
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
668 if (nargout > 0 || nargin == 0)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
669 retval = var;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
670
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
671 if (wants_local_change (args, nargin))
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
672 {
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
673 if (! try_local_protect (var))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
674 warning (R"("local" has no effect outside a function)");
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
675 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
676
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
677 if (nargin > 1)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
678 print_usage ();
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
679
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
680 if (nargin == 1)
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
681 {
20711
7b608fadc663 Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents: 20704
diff changeset
682 int ival = args(0).xint_value ("%s: argument must be an integer value", nm);
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
683
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
684 if (ival < minval)
20711
7b608fadc663 Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents: 20704
diff changeset
685 error ("%s: arg must be greater than %d", nm, minval);
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
686 if (ival > maxval)
21055
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 21017
diff changeset
687 error ("%s: arg must be less than or equal to %d", nm, maxval);
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
688
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
689 var = ival;
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
690 }
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
691
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
692 return retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
693 }
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
694
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
695 octave_value
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
696 set_internal_variable (double& var, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
697 int nargout, const char *nm,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
698 double minval, double maxval)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
699 {
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
700 octave_value retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
701
5800
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
702 int nargin = args.length ();
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
703
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
704 if (nargout > 0 || nargin == 0)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
705 retval = var;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
706
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
707 if (wants_local_change (args, nargin))
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
708 {
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
709 if (! try_local_protect (var))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
710 warning (R"("local" has no effect outside a function)");
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
711 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
712
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
713 if (nargin > 1)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
714 print_usage ();
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
715
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
716 if (nargin == 1)
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
717 {
20711
7b608fadc663 Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents: 20704
diff changeset
718 double dval = args(0).xscalar_value ("%s: argument must be a scalar value", nm);
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
719
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
720 if (dval < minval)
26107
05dfcb24ef12 Supply missing arguments to variable length *printf functions (bug #55046).
Rik <rik@octave.org>
parents: 26089
diff changeset
721 error ("%s: argument must be greater than %g", nm, minval);
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
722 if (dval > maxval)
26107
05dfcb24ef12 Supply missing arguments to variable length *printf functions (bug #55046).
Rik <rik@octave.org>
parents: 26089
diff changeset
723 error ("%s: argument must be less than or equal to %g", nm, maxval);
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
724
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
725 var = dval;
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
726 }
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
727
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
728 return retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
729 }
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
730
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
731 octave_value
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
732 set_internal_variable (std::string& var, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
733 int nargout, const char *nm, bool empty_ok)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
734 {
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
735 octave_value retval;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
736
5800
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
737 int nargin = args.length ();
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
738
e8be7fe586f9 [project @ 2006-05-10 06:50:45 by jwe]
jwe
parents: 5794
diff changeset
739 if (nargout > 0 || nargin == 0)
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
740 retval = var;
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
741
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
742 if (wants_local_change (args, nargin))
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
743 {
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
744 if (! try_local_protect (var))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
745 warning (R"("local" has no effect outside a function)");
10637
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
746 }
9cd5aa83fa62 implement 'local' parameter to pseudo-variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
747
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
748 if (nargin > 1)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
749 print_usage ();
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
750
5794
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
751 if (nargin == 1)
1138ced03f14 [project @ 2006-05-08 20:23:04 by jwe]
jwe
parents: 5791
diff changeset
752 {
20700
68e3a747ca02 rename octave_value value extractors that accept error message args
John W. Eaton <jwe@octave.org>
parents: 20581
diff changeset
753 std::string sval = args(0).xstring_value ("%s: first argument must be a string", nm);
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
754
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
755 if (! empty_ok && sval.empty ())
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
756 error ("%s: value must not be empty", nm);
21224
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
757
2e64b5899d1f maint: invert if/else/error pattern in variables.cc
Rik <rik@octave.org>
parents: 21200
diff changeset
758 var = sval;
5791
70215aff5ccf [project @ 2006-05-04 18:38:45 by jwe]
jwe
parents: 5781
diff changeset
759 }
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
760
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
761 return retval;
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
762 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
763
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
764 octave_value
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
765 set_internal_variable (int& var, const octave_value_list& args,
10640
5c594472f75e determine string enum length by trailing null rather than sizeof
Jaroslav Hajek <highegg@gmail.com>
parents: 10638
diff changeset
766 int nargout, const char *nm, const char **choices)
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
767 {
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
768 octave_value retval;
10640
5c594472f75e determine string enum length by trailing null rather than sizeof
Jaroslav Hajek <highegg@gmail.com>
parents: 10638
diff changeset
769 int nchoices = 0;
23795
980f39c3ab90 Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23704
diff changeset
770 while (choices[nchoices] != nullptr)
10640
5c594472f75e determine string enum length by trailing null rather than sizeof
Jaroslav Hajek <highegg@gmail.com>
parents: 10638
diff changeset
771 nchoices++;
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
772
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
773 int nargin = args.length ();
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
774
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
775 assert (var < nchoices);
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
776
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
777 if (nargout > 0 || nargin == 0)
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
778 retval = choices[var];
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
779
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
780 if (wants_local_change (args, nargin))
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
781 {
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
782 if (! try_local_protect (var))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
783 warning (R"("local" has no effect outside a function)");
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
784 }
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
785
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
786 if (nargin > 1)
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
787 print_usage ();
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
788
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
789 if (nargin == 1)
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
790 {
20700
68e3a747ca02 rename octave_value value extractors that accept error message args
John W. Eaton <jwe@octave.org>
parents: 20581
diff changeset
791 std::string sval = args(0).xstring_value ("%s: first argument must be a string", nm);
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
792
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
793 int i = 0;
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
794 for (; i < nchoices; i++)
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
795 {
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
796 if (sval == choices[i])
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
797 {
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
798 var = i;
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
799 break;
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
800 }
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
801 }
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
802 if (i == nchoices)
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
803 error (R"(%s: value not allowed ("%s"))", nm, sval.c_str ());
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
804 }
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
805
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
806 return retval;
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
807 }
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10637
diff changeset
808
21272
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
809 octave_value
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
810 set_internal_variable (std::string& var, const octave_value_list& args,
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
811 int nargout, const char *nm, const char **choices)
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
812 {
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
813 octave_value retval;
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
814 int nchoices = 0;
23795
980f39c3ab90 Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23704
diff changeset
815 while (choices[nchoices] != nullptr)
21272
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
816 nchoices++;
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
817
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
818 int nargin = args.length ();
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
819
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
820 if (nargout > 0 || nargin == 0)
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
821 retval = var;
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
822
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
823 if (wants_local_change (args, nargin))
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
824 {
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
825 if (! try_local_protect (var))
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
826 warning (R"("local" has no effect outside a function)");
21272
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
827 }
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
828
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
829 if (nargin > 1)
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
830 print_usage ();
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
831
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
832 if (nargin == 1)
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
833 {
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
834 std::string sval = args(0).xstring_value ("%s: first argument must be a string", nm);
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
835
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
836 int i = 0;
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
837 for (; i < nchoices; i++)
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
838 {
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
839 if (sval == choices[i])
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
840 {
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
841 var = sval;
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
842 break;
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
843 }
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
844 }
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
845 if (i == nchoices)
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23795
diff changeset
846 error (R"(%s: value not allowed ("%s"))", nm, sval.c_str ());
21272
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
847 }
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
848
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
849 return retval;
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
850 }
987c1a79d33f new set_internal_variable variant
John W. Eaton <jwe@octave.org>
parents: 21224
diff changeset
851
27873
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
852 // NOTE: Calling Fmlock directly (without an associated stack frame)
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
853 // will probably not do what you expect because it will lock the calling
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
854 // function. You should use interpreter::mlock directly if you want to
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
855 // lock a .oct function. For .mex, you would normally use mexLock.
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
856 //
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
857 // FIXME: with the current implementation, calling "builtin ('mlock')"
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
858 // will also not do what you expect. Is there any reasonable way to fix
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
859 // that?
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
860
23553
14723784b9f2 don't use singleton for call_stack
John W. Eaton <jwe@octave.org>
parents: 23466
diff changeset
861 DEFMETHOD (mlock, interp, args, ,
14723784b9f2 don't use singleton for call_stack
John W. Eaton <jwe@octave.org>
parents: 23466
diff changeset
862 doc: /* -*- texinfo -*-
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
863 @deftypefn {} {} mlock ()
26168
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
864 Lock the current function into memory so that it can't be removed with
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
865 @code{clear}.
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
866 @seealso{munlock, mislocked, persistent, clear}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
867 @end deftypefn */)
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
868 {
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
869 if (args.length () != 0)
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
870 print_usage ();
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
871
27873
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
872 interp.mlock (true);
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20925
diff changeset
873
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
874 return ovl ();
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
875 }
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
876
23553
14723784b9f2 don't use singleton for call_stack
John W. Eaton <jwe@octave.org>
parents: 23466
diff changeset
877 DEFMETHOD (munlock, interp, args, ,
14723784b9f2 don't use singleton for call_stack
John W. Eaton <jwe@octave.org>
parents: 23466
diff changeset
878 doc: /* -*- texinfo -*-
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
879 @deftypefn {} {} munlock ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
880 @deftypefnx {} {} munlock (@var{fcn})
26168
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
881 Unlock the named function @var{fcn} so that it may be removed from memory with
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
882 @code{clear}.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
883
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
884 If no function is named then unlock the current function.
26168
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
885 @seealso{mlock, mislocked, persistent, clear}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
886 @end deftypefn */)
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
887 {
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
888 int nargin = args.length ();
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
889
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
890 if (nargin > 1)
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
891 print_usage ();
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
892
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
893 if (nargin == 1)
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
894 {
27873
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
895 std::string name
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
896 = args(0).xstring_value ("munlock: FCN must be a string");
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
897
23703
6eb5f6199a5a move mlock, munlock, and mislocked to interpreter class
John W. Eaton <jwe@octave.org>
parents: 23702
diff changeset
898 interp.munlock (name);
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
899 }
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
900 else
27873
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
901 interp.munlock (true);
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
902
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
903 return ovl ();
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
904 }
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
905
23553
14723784b9f2 don't use singleton for call_stack
John W. Eaton <jwe@octave.org>
parents: 23466
diff changeset
906 DEFMETHOD (mislocked, interp, args, ,
14723784b9f2 don't use singleton for call_stack
John W. Eaton <jwe@octave.org>
parents: 23466
diff changeset
907 doc: /* -*- texinfo -*-
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
908 @deftypefn {} {} mislocked ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
909 @deftypefnx {} {} mislocked (@var{fcn})
26168
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
910 Return true if the named function @var{fcn} is locked in memory.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
911
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
912 If no function is named then return true if the current function is locked.
26168
0a3561379dbe Clarify documentation around locking functions and clear.
Rik <rik@octave.org>
parents: 26167
diff changeset
913 @seealso{mlock, munlock, persistent, clear}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
914 @end deftypefn */)
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
915 {
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
916 int nargin = args.length ();
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
917
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
918 if (nargin > 1)
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
919 print_usage ();
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
920
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
921 if (nargin == 1)
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
922 {
27873
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
923 std::string name
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
924 = args(0).xstring_value ("mislocked: FCN must be a string");
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20556
diff changeset
925
27873
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
926 return ovl (interp.mislocked (name));
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
927 }
20799
c349d4c91ce2 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20769
diff changeset
928 else
27873
020d0e8f7ac6 reafactor mlock, munlock, and mislocked functions
John W. Eaton <jwe@octave.org>
parents: 27479
diff changeset
929 return ovl (interp.mislocked (true));
4319
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
930 }
05973ead74eb [project @ 2003-02-13 21:03:04 by jwe]
jwe
parents: 4280
diff changeset
931
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
932 // Deleting names from the symbol tables.
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
933
3681
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
934 static inline bool
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11558
diff changeset
935 name_matches_any_pattern (const std::string& nm, const string_vector& argv,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
936 int argc, int idx, bool have_regexp = false)
3681
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
937 {
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
938 bool retval = false;
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
939
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
940 for (int k = idx; k < argc; k++)
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
941 {
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
942 std::string patstr = argv[k];
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
943 if (! patstr.empty ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
944 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
945 if (have_regexp)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
946 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
947 if (regexp::is_match (patstr, nm))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
948 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
949 retval = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
950 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
951 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
952 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
953 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
954 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
955 glob_match pattern (patstr);
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents: 7761
diff changeset
956
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
957 if (pattern.match (nm))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
958 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
959 retval = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
960 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
961 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
962 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
963 }
3681
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
964 }
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
965
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
966 return retval;
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
967 }
df54d394acc0 [project @ 2000-06-26 17:46:58 by jwe]
jwe
parents: 3618
diff changeset
968
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
969 static inline void
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
970 maybe_warn_exclusive (bool exclusive)
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
971 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
972 if (exclusive)
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
973 warning ("clear: ignoring --exclusive option");
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
974 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
975
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
976 static void
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
977 do_clear_functions (interpreter& interp,
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
978 const string_vector& argv, int argc, int idx,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
979 bool exclusive = false)
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
980 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
981 if (idx == argc)
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
982 interp.clear_functions ();
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
983 else
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
984 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
985 if (exclusive)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
986 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
987 std::list<std::string> fcns = interp.user_function_names ();
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
988
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
989 for (const auto& name : fcns)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
990 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
991 if (! name_matches_any_pattern (name, argv, argc, idx))
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
992 interp.clear_function (name);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
993 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
994 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
995 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
996 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
997 while (idx < argc)
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
998 interp.clear_function_pattern (argv[idx++]);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
999 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1000 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1001 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1002
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
1003 static void
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
1004 do_clear_globals (interpreter& interp,
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
1005 const string_vector& argv, int argc, int idx,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1006 bool exclusive = false)
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1007 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1008 if (idx == argc)
7752
40c428ea3408 initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents: 7626
diff changeset
1009 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1010 std::list<std::string> gvars = interp.global_variable_names ();
7752
40c428ea3408 initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents: 7626
diff changeset
1011
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1012 for (const auto& name : gvars)
24352
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1013 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1014 interp.clear_variable (name);
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1015 interp.clear_global_variable (name);
24352
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1016 }
7752
40c428ea3408 initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents: 7626
diff changeset
1017 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1018 else
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1019 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1020 if (exclusive)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1021 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1022 std::list<std::string> gvars = interp.global_variable_names ();
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1023
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1024 for (const auto& name : gvars)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1025 {
24352
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1026 if (! name_matches_any_pattern (name, argv, argc, idx))
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1027 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1028 interp.clear_variable (name);
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1029 interp.clear_global_variable (name);
24352
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1030 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1031 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1032 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1033 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1034 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1035 while (idx < argc)
24352
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1036 {
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1037 std::string pattern = argv[idx++];
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1038
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1039 interp.clear_variable_pattern (pattern);
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1040 interp.clear_global_variable_pattern (pattern);
24352
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
1041 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1042 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1043 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1044 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1045
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
1046 static void
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
1047 do_clear_variables (interpreter& interp,
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
1048 const string_vector& argv, int argc, int idx,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1049 bool exclusive = false, bool have_regexp = false)
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1050 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1051 if (idx == argc)
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1052 interp.clear_variables ();
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1053 else
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1054 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1055 if (exclusive)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1056 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1057 std::list<std::string> lvars = interp.variable_names ();
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1058
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1059 for (const auto& name : lvars)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1060 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1061 if (! name_matches_any_pattern (name, argv, argc, idx,
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1062 have_regexp))
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1063 interp.clear_variable (name);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1064 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1065 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1066 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1067 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1068 if (have_regexp)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1069 while (idx < argc)
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1070 interp.clear_variable_regexp (argv[idx++]);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1071 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1072 while (idx < argc)
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1073 interp.clear_variable_pattern (argv[idx++]);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1074 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1075 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1076 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1077
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
1078 static void
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
1079 do_clear_symbols (interpreter& interp,
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
1080 const string_vector& argv, int argc, int idx,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1081 bool exclusive = false)
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1082 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1083 if (idx == argc)
23611
91c8f006ed8b remove additional functions from symbol_table class
John W. Eaton <jwe@octave.org>
parents: 23604
diff changeset
1084 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1085 interp.clear_variables ();
23611
91c8f006ed8b remove additional functions from symbol_table class
John W. Eaton <jwe@octave.org>
parents: 23604
diff changeset
1086 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1087 else
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1088 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1089 if (exclusive)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1090 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17756
diff changeset
1091 // FIXME: is this really what we want, or do we
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1092 // somehow want to only clear the functions that are not
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1093 // shadowed by local variables? It seems that would be a
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1094 // bit harder to do.
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1095
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1096 do_clear_variables (interp, argv, argc, idx, exclusive);
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1097 do_clear_functions (interp, argv, argc, idx, exclusive);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1098 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1099 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1100 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1101 while (idx < argc)
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1102 interp.clear_symbol_pattern (argv[idx++]);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1103 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1104 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1105 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1106
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1107 static void
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
1108 do_matlab_compatible_clear (interpreter& interp,
23701
a6e49930ddd2 avoid some global access to interpreter and symbol table
John W. Eaton <jwe@octave.org>
parents: 23699
diff changeset
1109 const string_vector& argv, int argc, int idx)
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1110 {
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1111 // This is supposed to be mostly Matlab compatible.
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1112
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1113 for (; idx < argc; idx++)
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1114 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1115 if (argv[idx] == "all" && ! interp.is_local_variable ("all"))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1116 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1117 interp.clear_all ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1118 }
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
1119 else if (argv[idx] == "functions"
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1120 && ! interp.is_local_variable ("functions"))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1121 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1122 do_clear_functions (interp, argv, argc, ++idx);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1123 }
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
1124 else if (argv[idx] == "global"
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1125 && ! interp.is_local_variable ("global"))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1126 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1127 do_clear_globals (interp, argv, argc, ++idx);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1128 }
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7065
diff changeset
1129 else if (argv[idx] == "variables"
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1130 && ! interp.is_local_variable ("variables"))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1131 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1132 interp.clear_variables ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1133 }
9240
f27a8c07f0b2 clear -classes and support.
Robert T. Short <octave@phaselockedsystems.com>
parents: 9180
diff changeset
1134 else if (argv[idx] == "classes"
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1135 && ! interp.is_local_variable ("classes"))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1136 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1137 interp.clear_objects ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1138 octave_class::clear_exemplar_map ();
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1139 interp.clear_all ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1140 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1141 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1142 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1143 interp.clear_symbol_pattern (argv[idx]);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1144 }
4009
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1145 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1146 }
27e461aed956 [project @ 2002-07-31 09:33:03 by jwe]
jwe
parents: 3968
diff changeset
1147
23599
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
1148 DEFMETHOD (clear, interp, args, ,
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
1149 doc: /* -*- texinfo -*-
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1150 @deftypefn {} {} clear
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1151 @deftypefnx {} {} clear @var{pattern} @dots{}
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1152 @deftypefnx {} {} clear @var{options} @var{pattern} @dots{}
28504
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1153 Delete the names matching the given @var{pattern}s thereby freeing memory.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1154
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1155 The @var{pattern} may contain the following special characters:
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1156
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1157 @table @code
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1158 @item ?
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1159 Match any single character.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1160
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1161 @item *
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1162 Match zero or more characters.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1163
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1164 @item [ @var{list} ]
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1165 Match the list of characters specified by @var{list}. If the first character
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1166 is @code{!} or @code{^}, match all characters except those specified by
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1167 @var{list}. For example, the pattern @code{[a-zA-Z]} will match all lowercase
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1168 and uppercase alphabetic characters.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1169 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1170
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1171 For example, the command
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1172
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1173 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1174 clear foo b*r
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1175 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1176
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1177 @noindent
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1178 clears the name @code{foo} and all names that begin with the letter @samp{b}
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1179 and end with the letter @samp{r}.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1180
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1181 If @code{clear} is called without any arguments, all user-defined variables
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1182 are cleared from the current workspace (i.e., local variables). Any global
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1183 variables present will no longer be visible in the current workspace, but they
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1184 will continue to exist in the global workspace. Functions are unaffected by
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1185 this form of @code{clear}.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1186
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1187 The following options are available in both long and short form
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1188
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1189 @table @code
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1190 @item all, -all, -a
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1191 Clear all local and global user-defined variables, and all functions from the
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1192 symbol table.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1193
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1194 @item -exclusive, -x
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1195 Clear variables that do @strong{not} match the following pattern.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1196
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1197 @item functions, -functions, -f
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1198 Clear function names from the function symbol table. Persistent variables
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1199 will be re-initialized to their default value unless the function has been
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1200 locked in memory with @code{mlock}.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1201
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1202 @item global, -global, -g
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1203 Clear global variable names.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1204
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1205 @item variables, -variables, -v
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1206 Clear local variable names.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1207
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1208 @item classes, -classes, -c
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1209 Clear the class structure table and all objects.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1210
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1211 @item -regexp, -r
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1212 The @var{pattern} arguments are treated as regular expressions and any matches
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1213 will be cleared.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1214 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1215
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1216 With the exception of @option{-exclusive} and @option{-regexp}, all long
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1217 options can be used without the dash as well. Note that, aside from
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1218 @option{-exclusive}, only one other option may appear. All options must
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1219 appear before any patterns.
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1220
28504
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1221 Programming Notes: The command @code{clear @var{name}} only clears the variable
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1222 @var{name} when both a variable and a (shadowed) function named @var{name}
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1223 are currently defined. For example, suppose you have defined a function
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1224 @code{foo}, and then hidden it by performing the assignment @code{foo = 2}.
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1225 Executing the command @code{clear foo} once will clear the variable
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1226 definition and restore the definition of @code{foo} as a function.
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1227 Executing @code{clear foo} a second time will clear the function definition.
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1228
28504
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1229 When a local variable name, which is linked to a global variable, is cleared
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1230 only the local copy of the variable is removed. The global copy is untouched
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1231 and can be restored with @code{global @var{global_varname}}. Conversely,
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1232 @code{clear -g @var{global_varname}} will remove both the local and global
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1233 variables.
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1234
d747d57989e2 doc: Better document how global variables and clear() interact (bug #57604).
Rik <rik@octave.org>
parents: 28024
diff changeset
1235 @seealso{clearvars, who, whos, exist, mlock}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1236 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
1237 {
1755
3a9462b655f1 [project @ 1996-01-22 04:47:22 by jwe]
jwe
parents: 1742
diff changeset
1238 int argc = args.length () + 1;
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
1239
1968
a2e206524aa0 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents: 1957
diff changeset
1240 string_vector argv = args.make_argv ("clear");
1755
3a9462b655f1 [project @ 1996-01-22 04:47:22 by jwe]
jwe
parents: 1742
diff changeset
1241
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1242 if (argc == 1)
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1243 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1244 do_clear_variables (interp, argv, argc, true);
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1245
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
1246 event_manager& evmgr = interp.get_event_manager ();
27261
dccdc3b001a2 eliminate static functions from octave_link class
John W. Eaton <jwe@octave.org>
parents: 27204
diff changeset
1247
27263
99aa1bcb8848 rename octave_link and octave_link_events classes, move inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27261
diff changeset
1248 evmgr.clear_workspace ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1249 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1250 else
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
1251 {
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1252 int idx = 0;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1253
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1254 bool clear_all = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1255 bool clear_functions = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1256 bool clear_globals = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1257 bool clear_variables = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1258 bool clear_objects = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1259 bool exclusive = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1260 bool have_regexp = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1261 bool have_dash_option = false;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1262
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1263 while (++idx < argc)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1264 {
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1265 if (argv[idx] == "-all" || argv[idx] == "-a")
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1266 {
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1267 if (have_dash_option)
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1268 print_usage ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1269
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1270 have_dash_option = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1271 clear_all = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1272 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1273 else if (argv[idx] == "-exclusive" || argv[idx] == "-x")
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1274 {
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1275 exclusive = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1276 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1277 else if (argv[idx] == "-functions" || argv[idx] == "-f")
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1278 {
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1279 if (have_dash_option)
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1280 print_usage ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1281
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1282 have_dash_option = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1283 clear_functions = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1284 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1285 else if (argv[idx] == "-global" || argv[idx] == "-g")
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1286 {
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1287 if (have_dash_option)
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1288 print_usage ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1289
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1290 have_dash_option = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1291 clear_globals = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1292 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1293 else if (argv[idx] == "-variables" || argv[idx] == "-v")
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1294 {
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1295 if (have_dash_option)
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1296 print_usage ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1297
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1298 have_dash_option = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1299 clear_variables = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1300 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1301 else if (argv[idx] == "-classes" || argv[idx] == "-c")
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1302 {
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1303 if (have_dash_option)
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1304 print_usage ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1305
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1306 have_dash_option = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1307 clear_objects = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1308 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1309 else if (argv[idx] == "-regexp" || argv[idx] == "-r")
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1310 {
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1311 if (have_dash_option)
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1312 print_usage ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1313
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1314 have_dash_option = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1315 have_regexp = true;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1316 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1317 else
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1318 break;
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1319 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1320
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1321 if (idx <= argc)
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1322 {
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1323 if (! have_dash_option && ! exclusive)
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1324 do_matlab_compatible_clear (interp, argv, argc, idx);
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1325 else
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1326 {
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1327 if (clear_all)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1328 {
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1329 maybe_warn_exclusive (exclusive);
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1330
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1331 if (++idx < argc)
20897
9aad16a799c9 maint: Replace argc variable name with nargin in C++ code.
Rik <rik@octave.org>
parents: 20853
diff changeset
1332 warning ("clear: ignoring extra arguments after -all");
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1333
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1334 interp.clear_all ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1335 }
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1336 else if (have_regexp)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1337 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1338 do_clear_variables (interp, argv, argc, idx, exclusive, true);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1339 }
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1340 else if (clear_functions)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1341 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1342 do_clear_functions (interp, argv, argc, idx, exclusive);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1343 }
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1344 else if (clear_globals)
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1345 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1346 do_clear_globals (interp, argv, argc, idx, exclusive);
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1347 }
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1348 else if (clear_variables)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1349 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1350 do_clear_variables (interp, argv, argc, idx, exclusive);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1351 }
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1352 else if (clear_objects)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1353 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1354 interp.clear_objects ();
20556
4bed806ee3d4 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20535
diff changeset
1355 octave_class::clear_exemplar_map ();
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1356 interp.clear_all ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1357 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1358 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1359 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1360 do_clear_symbols (interp, argv, argc, idx, exclusive);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1361 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1362 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
1363 }
593
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
1364 }
6f948c6251a9 [project @ 1994-08-09 04:29:10 by jwe]
jwe
parents: 584
diff changeset
1365
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
1366 return ovl ();
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
1367 }
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
1368
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1369 /*
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1370 ## This test must be wrapped in its own function or the 'clear' command will
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1371 ## break the %!test environment.
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1372 %!function __test_clear_no_args__ ()
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1373 %! global x
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1374 %! x = 3;
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1375 %! clear
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1376 %! assert (! exist ("x", "var")); # x is not in the current workspace anymore
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1377 %! global x # but still lives in the global workspace
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1378 %! assert (exist ("x", "var"));
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1379 %!endfunction
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1380
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1381 %!test
26678
2c246d188151 variables.cc: Don't leak variables into global namespace from BIST tests.
Rik <rik@octave.org>
parents: 26434
diff changeset
1382 %! unwind_protect
2c246d188151 variables.cc: Don't leak variables into global namespace from BIST tests.
Rik <rik@octave.org>
parents: 26434
diff changeset
1383 %! __test_clear_no_args__ ();
2c246d188151 variables.cc: Don't leak variables into global namespace from BIST tests.
Rik <rik@octave.org>
parents: 26434
diff changeset
1384 %! unwind_protect_cleanup
2c246d188151 variables.cc: Don't leak variables into global namespace from BIST tests.
Rik <rik@octave.org>
parents: 26434
diff changeset
1385 %! clear -g x
2c246d188151 variables.cc: Don't leak variables into global namespace from BIST tests.
Rik <rik@octave.org>
parents: 26434
diff changeset
1386 %! end_unwind_protect
26169
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1387
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1388 ## Test that multiple options cannot be given
096b38cac97f Improve input validation to reject multiple options for clear (bug #53565).
Rik <rik@octave.org>
parents: 26168
diff changeset
1389 %!error clear -f -g
26167
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1390 */
247126168d23 Don't remove globals when 'clear' called without arguments (bug #39790).
Julien Bect <julien.bect@supelec.fr>
parents: 26113
diff changeset
1391
15562
8ed107220a3e maint: Rename unimplemented.m to __unimplemented__.m.
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 15528
diff changeset
1392 static std::string Vmissing_function_hook = "__unimplemented__";
10443
34e51d4e199b implement smart warnings about missing Matlab functionality
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
1393
34e51d4e199b implement smart warnings about missing Matlab functionality
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
1394 DEFUN (missing_function_hook, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1395 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1396 @deftypefn {} {@var{val} =} missing_function_hook ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1397 @deftypefnx {} {@var{old_val} =} missing_function_hook (@var{new_val})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1398 @deftypefnx {} {} missing_function_hook (@var{new_val}, "local")
27479
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1399 Query or set the internal variable that specifies the function to call
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1400 to provide extra information when an unknown identifier is referenced.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1401
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1402 When called from inside a function with the @qcode{"local"} option, the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1403 variable is changed locally for the function and any subroutines it calls.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1404 The original variable value is restored when exiting the function.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1405 @seealso{missing_component_hook}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1406 @end deftypefn */)
10443
34e51d4e199b implement smart warnings about missing Matlab functionality
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
1407 {
30001
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1408 return set_internal_variable (Vmissing_function_hook, args, nargout,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1409 "missing_function_hook");
10443
34e51d4e199b implement smart warnings about missing Matlab functionality
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
1410 }
34e51d4e199b implement smart warnings about missing Matlab functionality
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
1411
27479
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1412 std::string
23599
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
1413 maybe_missing_function_hook (const std::string& name)
10443
34e51d4e199b implement smart warnings about missing Matlab functionality
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
1414 {
27160
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1415 octave::interpreter& interp
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1416 = octave::__get_interpreter__ ("maybe_missing_function_hook");
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1417
10444
537d9fbba9c0 don't call missing_function_hook inside try block
Jaroslav Hajek <highegg@gmail.com>
parents: 10443
diff changeset
1418 // Don't do this if we're handling errors.
27471
fd32c1a9b1bd revamp error handling
John W. Eaton <jwe@octave.org>
parents: 27408
diff changeset
1419 if (Vmissing_function_hook.empty ())
27479
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1420 return "";
23599
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
1421
27160
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1422 octave::symbol_table& symtab = interp.get_symbol_table ();
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1423
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1424 octave_value val = symtab.find_function (Vmissing_function_hook);
15528
8d2b3db8b5b0 Allow missing_function_hook to fail silently
Mike Miller <mtmiller@ieee.org>
parents: 15508
diff changeset
1425
27160
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1426 if (val.is_defined ())
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1427 {
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1428 // Ensure auto-restoration.
28823
26cfccfee9a0 Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents: 28749
diff changeset
1429 octave::unwind_protect_var<std::string>
26cfccfee9a0 Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents: 28749
diff changeset
1430 restore_var (Vmissing_function_hook);
15528
8d2b3db8b5b0 Allow missing_function_hook to fail silently
Mike Miller <mtmiller@ieee.org>
parents: 15508
diff changeset
1431
27160
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1432 // Clear the variable prior to calling the function.
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1433 const std::string func_name = Vmissing_function_hook;
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1434 Vmissing_function_hook.clear ();
15528
8d2b3db8b5b0 Allow missing_function_hook to fail silently
Mike Miller <mtmiller@ieee.org>
parents: 15508
diff changeset
1435
27160
6b0c61a5a0f0 move global error configuration and status variables inside a class
John W. Eaton <jwe@octave.org>
parents: 27016
diff changeset
1436 // Call.
27479
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1437 octave_value_list tmp = octave::feval (func_name, octave_value (name), 1);
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1438
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1439 if (tmp.length () == 1 && tmp(0).is_string ())
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1440 return tmp(0).string_value ();
10467
13c1f15c67fa guard against recursive calls of missing_function_hook
Jaroslav Hajek <highegg@gmail.com>
parents: 10444
diff changeset
1441 }
27479
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1442
e8c417f6ef83 make unimplmented message an error and append to undefined identifier error
John W. Eaton <jwe@octave.org>
parents: 27471
diff changeset
1443 return "";
10443
34e51d4e199b implement smart warnings about missing Matlab functionality
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
1444 }
11558
1e4dfc7a9487 use .argn. to store argument names for inputname function
John W. Eaton <jwe@octave.org>
parents: 11556
diff changeset
1445
23599
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
1446 DEFMETHOD (__varval__, interp, args, ,
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23587
diff changeset
1447 doc: /* -*- texinfo -*-
28749
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1448 @deftypefn {} {@var{value} =} __varval__ (@var{name})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1449 Return the value of the variable @var{name} directly from the symbol table.
28749
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1450
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1451 If @var{name} does not exist then nothing is returned, not even an empty matrix
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1452 (@code{[]}), since there would be no way to distinguish between a variable
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1453 not found in the symbol table and a variable who's value was @code{[]}.
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1454
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1455 A standard usage pattern is to code a @code{try}/@code{catch} block around a
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1456 call to @code{__varval__}.
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1457
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1458 Example Code
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1459
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1460 @example
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1461 @group
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1462 try
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1463 @var{val} = __varval__ (@var{name});
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1464 catch
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1465 ## No variable @var{name} found in symbol table
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1466 @var{val} = NA; # Substitute Not Available (NA)
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1467 error ("@var{name} not found"); # or, throw an error.
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1468 end_try_catch
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1469 @end group
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1470 @end example
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1471
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1472 Programming Note: The magic @var{name} @qcode{".argn."} will retrieve the text
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1473 of input arguments to a function and is used by @code{inputname} internally.
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1474 @seealso{inputname}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1475 @end deftypefn */)
11558
1e4dfc7a9487 use .argn. to store argument names for inputname function
John W. Eaton <jwe@octave.org>
parents: 11556
diff changeset
1476 {
20760
15eefcabcb31 doc: Add docstring for internal function __varval__.
Rik <rik@octave.org>
parents: 20756
diff changeset
1477 if (args.length () != 1)
11558
1e4dfc7a9487 use .argn. to store argument names for inputname function
John W. Eaton <jwe@octave.org>
parents: 11556
diff changeset
1478 print_usage ();
1e4dfc7a9487 use .argn. to store argument names for inputname function
John W. Eaton <jwe@octave.org>
parents: 11556
diff changeset
1479
28749
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1480 std::string name = args(0).xstring_value ("__varval__: NAME must be a string");
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1481
28749
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1482 // We need this kluge to implement inputname in a .m file.
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1483 if (name == ".argn.")
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1484 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
1485 tree_evaluator& tw = interp.get_evaluator ();
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1486
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
1487 return tw.get_auto_fcn_var (stack_frame::ARG_NAMES);
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1488 }
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26446
diff changeset
1489
28749
4e10e25f0fc6 inputname.m: Closer compatibility with Matlab for special values (i,j,NaN,Inf) (bug #59103)
Rik <rik@octave.org>
parents: 28505
diff changeset
1490 return interp.varval (name);
11558
1e4dfc7a9487 use .argn. to store argument names for inputname function
John W. Eaton <jwe@octave.org>
parents: 11556
diff changeset
1491 }
17516
21656a949661 Add hook function to handle missing Octave components
Mike Miller <mtmiller@ieee.org>
parents: 17355
diff changeset
1492
21656a949661 Add hook function to handle missing Octave components
Mike Miller <mtmiller@ieee.org>
parents: 17355
diff changeset
1493 static std::string Vmissing_component_hook;
21656a949661 Add hook function to handle missing Octave components
Mike Miller <mtmiller@ieee.org>
parents: 17355
diff changeset
1494
21656a949661 Add hook function to handle missing Octave components
Mike Miller <mtmiller@ieee.org>
parents: 17355
diff changeset
1495 DEFUN (missing_component_hook, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1496 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1497 @deftypefn {} {@var{val} =} missing_component_hook ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1498 @deftypefnx {} {@var{old_val} =} missing_component_hook (@var{new_val})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1499 @deftypefnx {} {} missing_component_hook (@var{new_val}, "local")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1500 Query or set the internal variable that specifies the function to call when
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1501 a component of Octave is missing.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1502
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1503 This can be useful for packagers that may split the Octave installation into
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1504 multiple sub-packages, for example, to provide a hint to users for how to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1505 install the missing components.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1506
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1507 When called from inside a function with the @qcode{"local"} option, the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1508 variable is changed locally for the function and any subroutines it calls.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1509 The original variable value is restored when exiting the function.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1510
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1511 The hook function is expected to be of the form
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1512
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1513 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1514 @var{fcn} (@var{component})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1515 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1516
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1517 Octave will call @var{fcn} with the name of the function that requires the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1518 component and a string describing the missing component. The hook function
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1519 should return an error message to be displayed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1520 @seealso{missing_function_hook}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21885
diff changeset
1521 @end deftypefn */)
17516
21656a949661 Add hook function to handle missing Octave components
Mike Miller <mtmiller@ieee.org>
parents: 17355
diff changeset
1522 {
30001
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1523 return set_internal_variable (Vmissing_component_hook, args, nargout,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1524 "missing_component_hook");
17516
21656a949661 Add hook function to handle missing Octave components
Mike Miller <mtmiller@ieee.org>
parents: 17355
diff changeset
1525 }
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29657
diff changeset
1526
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29657
diff changeset
1527 OCTAVE_NAMESPACE_END
30001
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1528
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1529 // DEPRECATED in Octave 6
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1530
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1531 octave_function *
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1532 extract_function (const octave_value& arg, const std::string& warn_for,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1533 const std::string& fname, const std::string& header,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1534 const std::string& trailer)
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1535 {
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1536 octave_function *retval = is_valid_function (arg, warn_for, 0);
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1537
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1538 if (! retval)
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1539 {
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1540 std::string s = arg.xstring_value ("%s: argument must be a string",
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1541 warn_for.c_str ());
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1542
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1543 std::string cmd = header;
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1544 cmd.append (s);
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1545 cmd.append (trailer);
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1546
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1547 int parse_status;
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1548
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1549 octave::interpreter& interp
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1550 = octave::__get_interpreter__ ("extract_function");
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1551
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1552 interp.eval_string (cmd, true, parse_status, 0);
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1553
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1554 if (parse_status != 0)
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1555 error ("%s: '%s' is not valid as a function",
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1556 warn_for.c_str (), fname.c_str ());
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1557
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1558 retval = is_valid_function (fname, warn_for, 0);
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1559
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1560 if (! retval)
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1561 error ("%s: '%s' is not valid as a function",
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1562 warn_for.c_str (), fname.c_str ());
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1563
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1564 warning ("%s: passing function body as a string is obsolete; please use anonymous functions",
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1565 warn_for.c_str ());
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1566 }
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1567
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1568 return retval;
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
1569 }