Mercurial > hg > octave-nkf
annotate src/utils.cc @ 10828:322f43e0e170
Grammarcheck .txi documentation files.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 28 Jul 2010 12:45:04 -0700 |
parents | 6c50b56824aa |
children | 89f4d7e294cc |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
8920 | 4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
240 | 24 #ifdef HAVE_CONFIG_H |
1192 | 25 #include <config.h> |
1 | 26 #endif |
27 | |
3716 | 28 #include <cerrno> |
1343 | 29 #include <climits> |
1346 | 30 #include <cstring> |
1343 | 31 |
3503 | 32 #include <fstream> |
33 #include <iostream> | |
1728 | 34 #include <string> |
35 | |
1 | 36 #include <sys/types.h> |
37 #include <unistd.h> | |
367 | 38 |
4302 | 39 #include "quit.h" |
40 | |
3040 | 41 #include "dir-ops.h" |
42 #include "file-ops.h" | |
2926 | 43 #include "file-stat.h" |
4732 | 44 #include "lo-mappers.h" |
1651 | 45 #include "oct-cmplx.h" |
2926 | 46 #include "oct-env.h" |
3040 | 47 #include "pathsearch.h" |
1755 | 48 #include "str-vec.h" |
1651 | 49 |
4216 | 50 #include "Cell.h" |
2492 | 51 #include <defaults.h> |
1352 | 52 #include "defun.h" |
53 #include "dirfns.h" | |
54 #include "error.h" | |
55 #include "gripes.h" | |
56 #include "input.h" | |
10502
f13bf183a003
isvarname: keywords are not valid variable names
Judd Storrs <jstorrs@gmail.com>
parents:
10315
diff
changeset
|
57 #include "lex.h" |
5832 | 58 #include "load-path.h" |
5465 | 59 #include "oct-errno.h" |
1742 | 60 #include "oct-hist.h" |
1750 | 61 #include "oct-obj.h" |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10605
diff
changeset
|
62 #include "ov-range.h" |
1352 | 63 #include "pager.h" |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10502
diff
changeset
|
64 #include "parse.h" |
1690 | 65 #include "sysdep.h" |
1750 | 66 #include "toplev.h" |
1 | 67 #include "unwind-prot.h" |
1352 | 68 #include "utils.h" |
69 #include "variables.h" | |
1 | 70 |
4143 | 71 // Return TRUE if S is a valid identifier. |
72 | |
73 bool | |
74 valid_identifier (const char *s) | |
75 { | |
5290 | 76 if (! s || ! (isalpha (*s) || *s == '_' || *s == '$')) |
4143 | 77 return false; |
78 | |
79 while (*++s != '\0') | |
5290 | 80 if (! (isalnum (*s) || *s == '_' || *s == '$')) |
4143 | 81 return false; |
82 | |
83 return true; | |
84 } | |
85 | |
86 bool | |
87 valid_identifier (const std::string& s) | |
88 { | |
89 return valid_identifier (s.c_str ()); | |
90 } | |
91 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8715
diff
changeset
|
92 DEFUN (isvarname, args, , |
5040 | 93 "-*- texinfo -*-\n\ |
94 @deftypefn {Built-in Function} {} isvarname (@var{name})\n\ | |
4264 | 95 Return true if @var{name} is a valid variable name\n\ |
96 @end deftypefn") | |
97 { | |
98 octave_value retval; | |
99 | |
100 int argc = args.length () + 1; | |
101 | |
4610 | 102 string_vector argv = args.make_argv ("isvarname"); |
4264 | 103 |
104 if (error_state) | |
105 return retval; | |
106 | |
107 if (argc == 2) | |
10502
f13bf183a003
isvarname: keywords are not valid variable names
Judd Storrs <jstorrs@gmail.com>
parents:
10315
diff
changeset
|
108 retval = valid_identifier (argv[1]) && ! is_keyword (argv[1]); |
4264 | 109 else |
5823 | 110 print_usage (); |
4264 | 111 |
112 return retval; | |
113 } | |
114 | |
6323 | 115 // Return TRUE if F and G are both names for the same file. |
116 | |
117 bool | |
118 same_file (const std::string& f, const std::string& g) | |
119 { | |
6598 | 120 return same_file_internal (f, g); |
6323 | 121 } |
122 | |
1 | 123 int |
3523 | 124 almost_match (const std::string& std, const std::string& s, int min_match_len, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
125 int case_sens) |
1 | 126 { |
1755 | 127 int stdlen = std.length (); |
128 int slen = s.length (); | |
1 | 129 |
130 return (slen <= stdlen | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
131 && slen >= min_match_len |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
132 && (case_sens |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
133 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
134 : (octave_strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287 | 135 } |
136 | |
581 | 137 // Ugh. |
138 | |
287 | 139 int |
3523 | 140 keyword_almost_match (const char * const *std, int *min_len, const std::string& s, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
141 int min_toks_to_match, int max_toks) |
287 | 142 { |
143 int status = 0; | |
144 int tok_count = 0; | |
145 int toks_matched = 0; | |
146 | |
1755 | 147 if (s.empty () || max_toks < 1) |
287 | 148 return status; |
149 | |
1755 | 150 char *kw = strsave (s.c_str ()); |
287 | 151 |
152 char *t = kw; | |
153 while (*t != '\0') | |
154 { | |
155 if (*t == '\t') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
156 *t = ' '; |
287 | 157 t++; |
158 } | |
159 | |
160 char *beg = kw; | |
161 while (*beg == ' ') | |
162 beg++; | |
163 | |
164 if (*beg == '\0') | |
165 return status; | |
166 | |
167 | |
3072 | 168 const char **to_match = new const char * [max_toks + 1]; |
169 const char * const *s1 = std; | |
170 const char **s2 = to_match; | |
287 | 171 |
526 | 172 if (! s1 || ! s2) |
287 | 173 goto done; |
174 | |
175 s2[tok_count] = beg; | |
176 char *end; | |
526 | 177 while ((end = strchr (beg, ' ')) != 0) |
287 | 178 { |
179 *end = '\0'; | |
180 beg = end + 1; | |
181 | |
182 while (*beg == ' ') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
183 beg++; |
287 | 184 |
185 if (*beg == '\0') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
186 break; |
287 | 187 |
188 tok_count++; | |
189 if (tok_count >= max_toks) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
190 goto done; |
287 | 191 |
192 s2[tok_count] = beg; | |
193 } | |
526 | 194 s2[tok_count+1] = 0; |
287 | 195 |
196 s2 = to_match; | |
197 | |
198 for (;;) | |
199 { | |
200 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
201 goto done; |
287 | 202 |
203 toks_matched++; | |
204 | |
205 s1++; | |
206 s2++; | |
207 | |
208 if (! *s2) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
209 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
210 status = (toks_matched >= min_toks_to_match); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
211 goto done; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
212 } |
287 | 213 |
214 if (! *s1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
215 goto done; |
287 | 216 } |
217 | |
218 done: | |
219 | |
220 delete [] kw; | |
221 delete [] to_match; | |
222 | |
223 return status; | |
1 | 224 } |
225 | |
2234 | 226 // Return non-zero if either NR or NC is zero. Return -1 if this |
227 // should be considered fatal; return 1 if this is ok. | |
228 | |
229 int | |
5275 | 230 empty_arg (const char * /* name */, octave_idx_type nr, octave_idx_type nc) |
2234 | 231 { |
4478 | 232 return (nr == 0 || nc == 0); |
2234 | 233 } |
234 | |
581 | 235 // See if the given file is in the path. |
236 | |
3536 | 237 std::string |
4243 | 238 search_path_for_file (const std::string& path, const string_vector& names) |
686 | 239 { |
3174 | 240 dir_path p (path); |
686 | 241 |
10250 | 242 return octave_env::make_absolute (p.find_first_of (names)); |
686 | 243 } |
244 | |
4216 | 245 // Find all locations of the given file in the path. |
246 | |
247 string_vector | |
4243 | 248 search_path_for_all_files (const std::string& path, const string_vector& names) |
4216 | 249 { |
250 dir_path p (path); | |
251 | |
4243 | 252 string_vector sv = p.find_all_first_of (names); |
4216 | 253 |
6379 | 254 octave_idx_type len = sv.length (); |
4216 | 255 |
6379 | 256 for (octave_idx_type i = 0; i < len; i++) |
10250 | 257 sv[i] = octave_env::make_absolute (sv[i]); |
4216 | 258 |
259 return sv; | |
260 } | |
261 | |
262 static string_vector | |
263 make_absolute (const string_vector& sv) | |
264 { | |
6379 | 265 octave_idx_type len = sv.length (); |
266 | |
267 string_vector retval (len); | |
4216 | 268 |
6379 | 269 for (octave_idx_type i = 0; i < len; i++) |
10250 | 270 retval[i] = octave_env::make_absolute (sv[i]); |
6379 | 271 |
272 return retval; | |
4216 | 273 } |
274 | |
3195 | 275 DEFUN (file_in_loadpath, args, , |
3446 | 276 "-*- texinfo -*-\n\ |
4216 | 277 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\ |
278 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\ | |
3195 | 279 \n\ |
5448 | 280 Return the absolute name of @var{file} if it can be found in\n\ |
5814 | 281 the list of directories specified by @code{path}.\n\ |
10646
6c50b56824aa
file_in_path, file_in_loadpath: return empty string instead of empty matrix if file not found
John W. Eaton <jwe@octave.org>
parents:
10613
diff
changeset
|
282 If no file is found, return an empty character string.\n\ |
4216 | 283 \n\ |
5448 | 284 If the first argument is a cell array of strings, search each\n\ |
4243 | 285 directory of the loadpath for element of the cell array and return\n\ |
286 the first that matches.\n\ | |
287 \n\ | |
4216 | 288 If the second optional argument @code{\"all\"} is supplied, return\n\ |
289 a cell array containing the list of all files that have the same\n\ | |
290 name in the path. If no files are found, return an empty cell array.\n\ | |
5814 | 291 @seealso{file_in_path, path}\n\ |
5642 | 292 @end deftypefn") |
3195 | 293 { |
4216 | 294 octave_value retval; |
3195 | 295 |
4243 | 296 int nargin = args.length (); |
3195 | 297 |
4243 | 298 if (nargin == 1 || nargin == 2) |
4216 | 299 { |
4243 | 300 string_vector names = args(0).all_strings (); |
301 | |
302 if (! error_state && names.length () > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
303 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
304 if (nargin == 1) |
10646
6c50b56824aa
file_in_path, file_in_loadpath: return empty string instead of empty matrix if file not found
John W. Eaton <jwe@octave.org>
parents:
10613
diff
changeset
|
305 retval = octave_env::make_absolute (load_path::find_first_of (names)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
306 else if (nargin == 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
307 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
308 std::string opt = args(1).string_value (); |
4243 | 309 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
310 if (! error_state && opt == "all") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
311 retval = Cell (make_absolute |
10250 | 312 (load_path::find_all_first_of (names))); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
313 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
314 error ("file_in_loadpath: invalid option"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
315 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
316 } |
4216 | 317 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
318 error ("file_in_loadpath: expecting string as first argument"); |
4216 | 319 } |
3195 | 320 else |
5823 | 321 print_usage (); |
3195 | 322 |
323 return retval; | |
324 } | |
325 | |
1957 | 326 DEFUN (file_in_path, args, , |
3301 | 327 "-*- texinfo -*-\n\ |
328 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ | |
4216 | 329 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\ |
5448 | 330 Return the absolute name of @var{file} if it can be found in\n\ |
3301 | 331 @var{path}. The value of @var{path} should be a colon-separated list of\n\ |
5814 | 332 directories in the format described for @code{path}. If no file\n\ |
10646
6c50b56824aa
file_in_path, file_in_loadpath: return empty string instead of empty matrix if file not found
John W. Eaton <jwe@octave.org>
parents:
10613
diff
changeset
|
333 is found, return an empty character string. For example,\n\ |
3301 | 334 \n\ |
335 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8929
diff
changeset
|
336 @group\n\ |
5456 | 337 file_in_path (EXEC_PATH, \"sh\")\n\ |
338 @result{} \"/bin/sh\"\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8929
diff
changeset
|
339 @end group\n\ |
3301 | 340 @end example\n\ |
4216 | 341 \n\ |
5448 | 342 If the second argument is a cell array of strings, search each\n\ |
4243 | 343 directory of the path for element of the cell array and return\n\ |
344 the first that matches.\n\ | |
345 \n\ | |
4216 | 346 If the third optional argument @code{\"all\"} is supplied, return\n\ |
347 a cell array containing the list of all files that have the same\n\ | |
348 name in the path. If no files are found, return an empty cell array.\n\ | |
4249 | 349 @seealso{file_in_loadpath}\n\ |
3301 | 350 @end deftypefn") |
686 | 351 { |
4216 | 352 octave_value retval; |
686 | 353 |
4243 | 354 int nargin = args.length (); |
1755 | 355 |
4243 | 356 if (nargin == 2 || nargin == 3) |
686 | 357 { |
4243 | 358 std::string path = args(0).string_value (); |
359 | |
360 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
361 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
362 string_vector names = args(1).all_strings (); |
4243 | 363 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
364 if (! error_state && names.length () > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
365 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
366 if (nargin == 2) |
10646
6c50b56824aa
file_in_path, file_in_loadpath: return empty string instead of empty matrix if file not found
John W. Eaton <jwe@octave.org>
parents:
10613
diff
changeset
|
367 retval = search_path_for_file (path, names); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
368 else if (nargin == 3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
369 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
370 std::string opt = args(2).string_value (); |
4243 | 371 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
372 if (! error_state && opt == "all") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
373 retval = Cell (make_absolute |
10250 | 374 (search_path_for_all_files (path, names))); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
375 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
376 error ("file_in_path: invalid option"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
377 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
378 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
379 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
380 error ("file_in_path: expecting string as second argument"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
381 } |
1755 | 382 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
383 error ("file_in_path: expecting string as first argument"); |
686 | 384 } |
385 else | |
5823 | 386 print_usage (); |
686 | 387 |
388 return retval; | |
389 } | |
390 | |
3536 | 391 std::string |
3523 | 392 file_in_path (const std::string& name, const std::string& suffix) |
526 | 393 { |
3523 | 394 std::string nm = name; |
526 | 395 |
1755 | 396 if (! suffix.empty ()) |
397 nm.append (suffix); | |
686 | 398 |
10250 | 399 return octave_env::make_absolute (load_path::find_file (nm)); |
526 | 400 } |
401 | |
581 | 402 // See if there is an function file in the path. If so, return the |
403 // full path to the file. | |
404 | |
3536 | 405 std::string |
3523 | 406 fcn_file_in_path (const std::string& name) |
526 | 407 { |
3523 | 408 std::string retval; |
908 | 409 |
1755 | 410 int len = name.length (); |
411 | |
412 if (len > 0) | |
413 { | |
5832 | 414 if (octave_env::absolute_pathname (name)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
415 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
416 file_stat fs (name); |
5832 | 417 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
418 if (fs.exists ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
419 retval = name; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
420 } |
5832 | 421 else if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
422 retval = load_path::find_fcn_file (name.substr (0, len-2)); |
908 | 423 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
424 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
425 std::string fname = name; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
426 size_t pos = name.find_first_of (Vfilemarker); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
427 if (pos != std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
428 fname = name.substr (0, pos); |
7818
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
429 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
430 retval = load_path::find_fcn_file (fname); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
431 } |
908 | 432 } |
1755 | 433 |
434 return retval; | |
526 | 435 } |
436 | |
8041
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
437 // See if there is a directory called "name" in the path and if it |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
438 // contains a Contents.m file return the full path to this file. |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
439 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
440 std::string |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
441 contents_file_in_path (const std::string& dir) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
442 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
443 std::string retval; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
444 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
445 if (dir.length () > 0) |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
446 { |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
447 std::string tcontents = file_ops::concat (load_path::find_dir (dir), |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
448 std::string ("Contents.m")); |
8041
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
449 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
450 file_stat fs (tcontents); |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
451 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
452 if (fs.exists ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
453 retval = octave_env::make_absolute (tcontents); |
8041
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
454 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
455 |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
456 return retval; |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
457 } |
a14bdf90be55
Add a search for Contents.m files to the help function
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
458 |
5864 | 459 // See if there is a .oct file in the path. If so, return the |
581 | 460 // full path to the file. |
461 | |
3536 | 462 std::string |
3523 | 463 oct_file_in_path (const std::string& name) |
572 | 464 { |
3523 | 465 std::string retval; |
908 | 466 |
1755 | 467 int len = name.length (); |
468 | |
469 if (len > 0) | |
470 { | |
5832 | 471 if (octave_env::absolute_pathname (name)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
472 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
473 file_stat fs (name); |
5832 | 474 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
475 if (fs.exists ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
476 retval = name; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
477 } |
5832 | 478 else if (len > 4 && name [len - 4] == '.' && name [len - 3] == 'o' |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
479 && name [len - 2] == 'c' && name [len - 1] == 't') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
480 retval = load_path::find_oct_file (name.substr (0, len-4)); |
908 | 481 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
482 retval = load_path::find_oct_file (name); |
908 | 483 } |
1755 | 484 |
485 return retval; | |
572 | 486 } |
487 | |
5864 | 488 // See if there is a .mex file in the path. If so, return the |
489 // full path to the file. | |
490 | |
491 std::string | |
492 mex_file_in_path (const std::string& name) | |
493 { | |
494 std::string retval; | |
495 | |
496 int len = name.length (); | |
497 | |
498 if (len > 0) | |
499 { | |
500 if (octave_env::absolute_pathname (name)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
501 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
502 file_stat fs (name); |
5864 | 503 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
504 if (fs.exists ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
505 retval = name; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
506 } |
5864 | 507 else if (len > 4 && name [len - 4] == '.' && name [len - 3] == 'm' |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
508 && name [len - 2] == 'e' && name [len - 1] == 'x') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
509 retval = load_path::find_mex_file (name.substr (0, len-4)); |
5864 | 510 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
511 retval = load_path::find_mex_file (name); |
5864 | 512 } |
513 | |
514 return retval; | |
515 } | |
516 | |
3103 | 517 // Replace backslash escapes in a string with the real values. |
518 | |
3536 | 519 std::string |
3523 | 520 do_string_escapes (const std::string& s) |
3103 | 521 { |
3523 | 522 std::string retval; |
3103 | 523 |
524 size_t i = 0; | |
525 size_t j = 0; | |
526 size_t len = s.length (); | |
527 | |
528 retval.resize (len); | |
529 | |
530 while (j < len) | |
531 { | |
532 if (s[j] == '\\' && j+1 < len) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
533 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
534 switch (s[++j]) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
535 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
536 case '0': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
537 retval[i] = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
538 break; |
3893 | 539 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
540 case 'a': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
541 retval[i] = '\a'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
542 break; |
3103 | 543 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
544 case 'b': // backspace |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
545 retval[i] = '\b'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
546 break; |
3103 | 547 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
548 case 'f': // formfeed |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
549 retval[i] = '\f'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
550 break; |
3103 | 551 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
552 case 'n': // newline |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
553 retval[i] = '\n'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
554 break; |
3103 | 555 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
556 case 'r': // carriage return |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
557 retval[i] = '\r'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
558 break; |
3103 | 559 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
560 case 't': // horizontal tab |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
561 retval[i] = '\t'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
562 break; |
3103 | 563 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
564 case 'v': // vertical tab |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
565 retval[i] = '\v'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
566 break; |
3103 | 567 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
568 case '\\': // backslash |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
569 retval[i] = '\\'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
570 break; |
3103 | 571 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
572 case '\'': // quote |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
573 retval[i] = '\''; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
574 break; |
3103 | 575 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
576 case '"': // double quote |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
577 retval[i] = '"'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
578 break; |
3103 | 579 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
580 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
581 warning ("unrecognized escape sequence `\\%c' --\ |
3103 | 582 converting to `%c'", s[j], s[j]); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
583 retval[i] = s[j]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
584 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
585 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
586 } |
3103 | 587 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
588 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
589 retval[i] = s[j]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
590 } |
3103 | 591 |
592 i++; | |
593 j++; | |
594 } | |
595 | |
3105 | 596 retval.resize (i); |
3103 | 597 |
598 return retval; | |
599 } | |
600 | |
601 DEFUN (do_string_escapes, args, , | |
3446 | 602 "-*- texinfo -*-\n\ |
603 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ | |
604 Convert special characters in @var{string} to their escaped forms.\n\ | |
605 @end deftypefn") | |
3103 | 606 { |
607 octave_value retval; | |
608 | |
609 int nargin = args.length (); | |
610 | |
611 if (nargin == 1) | |
612 { | |
613 if (args(0).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
614 retval = do_string_escapes (args(0).string_value ()); |
3103 | 615 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
616 error ("do_string_escapes: argument must be a string"); |
3103 | 617 } |
618 else | |
5823 | 619 print_usage (); |
3103 | 620 |
621 return retval; | |
622 } | |
623 | |
1755 | 624 const char * |
801 | 625 undo_string_escape (char c) |
626 { | |
627 if (! c) | |
1755 | 628 return ""; |
801 | 629 |
630 switch (c) | |
631 { | |
3893 | 632 case '\0': |
633 return "\\0"; | |
634 | |
801 | 635 case '\a': |
636 return "\\a"; | |
637 | |
638 case '\b': // backspace | |
639 return "\\b"; | |
640 | |
641 case '\f': // formfeed | |
642 return "\\f"; | |
643 | |
644 case '\n': // newline | |
645 return "\\n"; | |
646 | |
647 case '\r': // carriage return | |
648 return "\\r"; | |
649 | |
650 case '\t': // horizontal tab | |
651 return "\\t"; | |
652 | |
653 case '\v': // vertical tab | |
654 return "\\v"; | |
655 | |
656 case '\\': // backslash | |
657 return "\\\\"; | |
658 | |
659 case '"': // double quote | |
660 return "\\\""; | |
661 | |
662 default: | |
1755 | 663 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
664 static char retval[2]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
665 retval[0] = c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
666 retval[1] = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
667 return retval; |
1755 | 668 } |
801 | 669 } |
670 } | |
671 | |
3536 | 672 std::string |
3523 | 673 undo_string_escapes (const std::string& s) |
801 | 674 { |
3523 | 675 std::string retval; |
801 | 676 |
1755 | 677 for (size_t i = 0; i < s.length (); i++) |
678 retval.append (undo_string_escape (s[i])); | |
801 | 679 |
1755 | 680 return retval; |
801 | 681 } |
682 | |
1957 | 683 DEFUN (undo_string_escapes, args, , |
3361 | 684 "-*- texinfo -*-\n\ |
685 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ | |
686 Converts special characters in strings back to their escaped forms. For\n\ | |
687 example, the expression\n\ | |
688 \n\ | |
689 @example\n\ | |
690 bell = \"\\a\";\n\ | |
691 @end example\n\ | |
692 \n\ | |
693 @noindent\n\ | |
694 assigns the value of the alert character (control-g, ASCII code 7) to\n\ | |
695 the string variable @code{bell}. If this string is printed, the\n\ | |
696 system will ring the terminal bell (if it is possible). This is\n\ | |
697 normally the desired outcome. However, sometimes it is useful to be\n\ | |
698 able to print the original representation of the string, with the\n\ | |
699 special characters replaced by their escape sequences. For example,\n\ | |
700 \n\ | |
701 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8929
diff
changeset
|
702 @group\n\ |
3361 | 703 octave:13> undo_string_escapes (bell)\n\ |
704 ans = \\a\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8929
diff
changeset
|
705 @end group\n\ |
3361 | 706 @end example\n\ |
707 \n\ | |
708 @noindent\n\ | |
709 replaces the unprintable alert character with its printable\n\ | |
710 representation.\n\ | |
711 @end deftypefn") | |
801 | 712 { |
2086 | 713 octave_value retval; |
801 | 714 |
715 int nargin = args.length (); | |
716 | |
3103 | 717 if (nargin == 1) |
718 { | |
719 if (args(0).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
720 retval = undo_string_escapes (args(0).string_value ()); |
3103 | 721 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
722 error ("undo_string_escapes: argument must be a string"); |
3103 | 723 } |
801 | 724 else |
5823 | 725 print_usage (); |
801 | 726 |
727 return retval; | |
728 } | |
729 | |
8229 | 730 DEFUN (is_absolute_filename, args, , |
731 "-*- texinfo -*-\n\ | |
732 @deftypefn {Built-in Function} {} is_absolute_filename (@var{file})\n\ | |
733 Return true if @var{file} is an absolute filename.\n\ | |
734 @end deftypefn") | |
735 { | |
736 octave_value retval = false; | |
737 | |
738 if (args.length () == 1) | |
739 retval = (args(0).is_string () | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
740 && octave_env::absolute_pathname (args(0).string_value ())); |
8229 | 741 else |
742 print_usage (); | |
743 | |
744 return retval; | |
745 } | |
746 | |
747 DEFUN (is_rooted_relative_filename, args, , | |
748 "-*- texinfo -*-\n\ | |
749 @deftypefn {Built-in Function} {} is_rooted_relative_filename (@var{file})\n\ | |
750 Return true if @var{file} is a rooted-relative filename.\n\ | |
751 @end deftypefn") | |
752 { | |
753 octave_value retval = false; | |
754 | |
755 if (args.length () == 1) | |
756 retval = (args(0).is_string () | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
757 && octave_env::rooted_relative_pathname (args(0).string_value ())); |
8229 | 758 else |
759 print_usage (); | |
760 | |
761 return retval; | |
762 } | |
763 | |
764 DEFUN (make_absolute_filename, args, , | |
765 "-*- texinfo -*-\n\ | |
766 @deftypefn {Built-in Function} {} make_absolute_filename (@var{file})\n\ | |
767 Return the full name of @var{file}, relative to the current directory.\n\ | |
768 @end deftypefn") | |
769 { | |
770 octave_value retval = std::string (); | |
771 | |
772 if (args.length () == 1) | |
773 { | |
774 std::string nm = args(0).string_value (); | |
775 | |
776 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
777 retval = octave_env::make_absolute (nm); |
8229 | 778 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
779 error ("make_absolute_filename: expecting argument to be a file name"); |
8229 | 780 } |
781 else | |
782 print_usage (); | |
783 | |
784 return retval; | |
785 } | |
786 | |
787 DEFUN (find_dir_in_path, args, , | |
788 "-*- texinfo -*-\n\ | |
9806
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
789 @deftypefn {Built-in Function} {} find_dir_in_path (@var{dir}, \"all\")\n\ |
8229 | 790 Return the full name of the path element matching @var{dir}. The\n\ |
791 match is performed at the end of each path element. For example, if\n\ | |
792 @var{dir} is @code{\"foo/bar\"}, it matches the path element\n\ | |
793 @code{\"/some/dir/foo/bar\"}, but not @code{\"/some/dir/foo/bar/baz\"}\n\ | |
8715 | 794 or @code{\"/some/dir/allfoo/bar\"}.\n\ |
9806
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
795 \n\ |
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
796 The second argument is optional. If it is supplied, return a cell array\n\ |
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
797 containing all the directory names that match.\n\ |
8229 | 798 @end deftypefn") |
799 { | |
800 octave_value retval = std::string (); | |
801 | |
9806
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
802 int nargin = args.length (); |
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
803 |
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
804 std::string dir; |
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
805 |
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
806 if (nargin == 1 || nargin == 2) |
8229 | 807 { |
9806
8e345f2fe4d6
improved support for Contents.m files
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
808 dir = args(0).string_value (); |
8229 | 809 |
810 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
811 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
812 if (nargin == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
813 retval = load_path::find_dir (dir); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
814 else if (nargin == 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
815 retval = Cell (load_path::find_matching_dirs (dir)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
816 } |
8229 | 817 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
818 error ("find_dir_in_path: expecting argument to be a directory name"); |
8229 | 819 } |
820 else | |
821 print_usage (); | |
822 | |
823 return retval; | |
824 } | |
825 | |
5465 | 826 DEFUNX ("errno", Ferrno, args, , |
3716 | 827 "-*- texinfo -*-\n\ |
5465 | 828 @deftypefn {Built-in Function} {@var{err} =} errno ()\n\ |
829 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{val})\n\ | |
830 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{name})\n\ | |
831 Return the current value of the system-dependent variable errno,\n\ | |
832 set its value to @var{val} and return the previous value, or return\n\ | |
833 the named error code given @var{name} as a character string, or -1\n\ | |
834 if @var{name} is not found.\n\ | |
3716 | 835 @end deftypefn") |
836 { | |
837 octave_value retval; | |
838 | |
5465 | 839 int nargin = args.length (); |
840 | |
841 if (nargin == 1) | |
842 { | |
843 if (args(0).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
844 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
845 std::string nm = args(0).string_value (); |
5465 | 846 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
847 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
848 retval = octave_errno::lookup (nm); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
849 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
850 error ("errno: expecting character string argument"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
851 } |
5465 | 852 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
853 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
854 int val = args(0).int_value (); |
5465 | 855 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
856 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
857 retval = octave_errno::set (val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
858 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
859 error ("errno: expecting integer argument"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
860 } |
5465 | 861 } |
862 else if (nargin == 0) | |
863 retval = octave_errno::get (); | |
3716 | 864 else |
5823 | 865 print_usage (); |
3716 | 866 |
867 return retval; | |
868 } | |
869 | |
5465 | 870 DEFUN (errno_list, args, , |
871 "-*- texinfo -*-\n\ | |
872 @deftypefn {Built-in Function} {} errno_list ()\n\ | |
873 Return a structure containing the system-dependent errno values.\n\ | |
874 @end deftypefn") | |
875 { | |
876 octave_value retval; | |
877 | |
878 if (args.length () == 0) | |
879 retval = octave_errno::list (); | |
880 else | |
5823 | 881 print_usage (); |
5465 | 882 |
883 return retval; | |
884 } | |
3716 | 885 |
2285 | 886 static void |
5275 | 887 check_dimensions (octave_idx_type& nr, octave_idx_type& nc, const char *warnfor) |
3354 | 888 { |
889 if (nr < 0 || nc < 0) | |
890 { | |
5781 | 891 warning_with_id ("Octave:neg-dim-as-zero", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
892 "%s: converting negative dimension to zero", warnfor); |
3354 | 893 |
4457 | 894 nr = (nr < 0) ? 0 : nr; |
895 nc = (nc < 0) ? 0 : nc; | |
3354 | 896 } |
897 } | |
898 | |
899 void | |
4513 | 900 check_dimensions (dim_vector& dim, const char *warnfor) |
4481 | 901 { |
902 bool neg = false; | |
903 | |
904 for (int i = 0; i < dim.length (); i++) | |
905 { | |
906 if (dim(i) < 0) | |
907 { | |
908 dim(i) = 0; | |
909 neg = true; | |
910 } | |
911 } | |
912 | |
5781 | 913 if (neg) |
914 warning_with_id ("Octave:neg-dim-as-zero", | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
915 "%s: converting negative dimension to zero", warnfor); |
4481 | 916 } |
917 | |
918 | |
919 void | |
920 get_dimensions (const octave_value& a, const char *warn_for, | |
4513 | 921 dim_vector& dim) |
4481 | 922 { |
923 if (a.is_scalar_type ()) | |
924 { | |
925 dim.resize (2); | |
4732 | 926 dim(0) = a.int_value (); |
4481 | 927 dim(1) = dim(0); |
928 } | |
929 else | |
930 { | |
5275 | 931 octave_idx_type nr = a.rows (); |
932 octave_idx_type nc = a.columns (); | |
4481 | 933 |
934 if (nr == 1 || nc == 1) | |
935 { | |
936 Array<double> v = a.vector_value (); | |
937 | |
938 if (error_state) | |
939 return; | |
940 | |
5275 | 941 octave_idx_type n = v.length (); |
4481 | 942 dim.resize (n); |
5275 | 943 for (octave_idx_type i = 0; i < n; i++) |
4783 | 944 dim(i) = static_cast<int> (fix (v(i))); |
4481 | 945 } |
946 else | |
5257 | 947 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
4481 | 948 } |
949 | |
5258 | 950 if (! error_state) |
951 check_dimensions (dim, warn_for); // May set error_state. | |
4481 | 952 } |
953 | |
954 | |
955 void | |
3354 | 956 get_dimensions (const octave_value& a, const char *warn_for, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
957 octave_idx_type& nr, octave_idx_type& nc) |
3354 | 958 { |
959 if (a.is_scalar_type ()) | |
960 { | |
4732 | 961 nr = nc = a.int_value (); |
3354 | 962 } |
963 else | |
964 { | |
965 nr = a.rows (); | |
966 nc = a.columns (); | |
967 | |
968 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
969 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
970 Array<double> v = a.vector_value (); |
3354 | 971 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
972 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
973 return; |
3354 | 974 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
975 nr = static_cast<octave_idx_type> (fix (v (0))); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
976 nc = static_cast<octave_idx_type> (fix (v (1))); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
977 } |
3354 | 978 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
979 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
3354 | 980 } |
981 | |
5258 | 982 if (! error_state) |
983 check_dimensions (nr, nc, warn_for); // May set error_state. | |
3354 | 984 } |
985 | |
986 void | |
987 get_dimensions (const octave_value& a, const octave_value& b, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
988 const char *warn_for, octave_idx_type& nr, octave_idx_type& nc) |
3354 | 989 { |
4732 | 990 nr = a.is_empty () ? 0 : a.int_value (); |
991 nc = b.is_empty () ? 0 : b.int_value (); | |
3354 | 992 |
993 if (error_state) | |
994 error ("%s: expecting two scalar arguments", warn_for); | |
995 else | |
996 check_dimensions (nr, nc, warn_for); // May set error_state. | |
997 } | |
998 | |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
999 octave_idx_type |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1000 dims_to_numel (const dim_vector& dims, const octave_value_list& idx) |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1001 { |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1002 octave_idx_type retval; |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1003 |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1004 octave_idx_type len = idx.length (); |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1005 |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1006 if (len == 0) |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1007 retval = dims.numel (); |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1008 else |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1009 { |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1010 const dim_vector dv = dims.redim (len); |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1011 retval = 1; |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1012 for (octave_idx_type i = 0; i < len; i++) |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1013 { |
9842
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1014 octave_value idxi = idx(i); |
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1015 if (idxi.is_magic_colon ()) |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1016 retval *= dv(i); |
9842
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1017 else if (idxi.is_numeric_type ()) |
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1018 retval *= idxi.numel (); |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1019 else |
9842
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1020 { |
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1021 idx_vector jdx = idxi.index_vector (); |
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1022 if (error_state) |
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1023 break; |
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1024 retval *= jdx.length (dv(i)); |
10519b4d6507
fix indexed numel for non-numeric indices
Jaroslav Hajek <highegg@gmail.com>
parents:
9806
diff
changeset
|
1025 } |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1026 } |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1027 } |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1028 |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1029 return retval; |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1030 } |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9487
diff
changeset
|
1031 |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1032 void |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1033 decode_subscripts (const char* name, const octave_value& arg, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1034 std::string& type_string, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1035 std::list<octave_value_list>& idx) |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1036 { |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1037 Octave_map m = arg.map_value (); |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1038 |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1039 if (! error_state |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1040 && m.nfields () == 2 && m.contains ("type") && m.contains ("subs")) |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1041 { |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1042 Cell& type = m.contents ("type"); |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1043 Cell& subs = m.contents ("subs"); |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1044 |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1045 type_string = std::string (type.length(), '\0'); |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1046 |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1047 for (int k = 0; k < type.length (); k++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1048 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1049 std::string item = type(k).string_value (); |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1050 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1051 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1052 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1053 if (item == "{}") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1054 type_string[k] = '{'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1055 else if (item == "()") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1056 type_string[k] = '('; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1057 else if (item == ".") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1058 type_string[k] = '.'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1059 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1060 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1061 error("%s: invalid indexing type `%s'", name, item.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1062 return; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1063 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1064 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1065 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1066 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1067 error ("%s: expecting type(%d) to be a character string", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1068 name, k+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1069 return; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1070 } |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1071 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1072 octave_value_list idx_item; |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1073 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1074 if (subs(k).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1075 idx_item(0) = subs(k); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1076 else if (subs(k).is_cell ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1077 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1078 Cell subs_cell = subs(k).cell_value (); |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1079 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1080 for (int n = 0; n < subs_cell.length (); n++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1081 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1082 if (subs_cell(n).is_string () |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1083 && subs_cell(n).string_value () == ":") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1084 idx_item(n) = octave_value(octave_value::magic_colon_t); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1085 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1086 idx_item(n) = subs_cell(n); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1087 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1088 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1089 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1090 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1091 error ("%s: expecting subs(%d) to be a character string or cell array", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1092 name, k+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1093 return; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1094 } |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1095 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1096 idx.push_back (idx_item); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1097 } |
10033
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1098 } |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1099 else |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1100 error ("%s: second argument must be a structure with fields `type' and `subs'", name); |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1101 } |
f349847c4541
optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
Jaroslav Hajek <highegg@gmail.com>
parents:
9842
diff
changeset
|
1102 |
4478 | 1103 Matrix |
5275 | 1104 identity_matrix (octave_idx_type nr, octave_idx_type nc) |
4478 | 1105 { |
1106 Matrix m (nr, nc, 0.0); | |
1107 | |
1108 if (nr > 0 && nc > 0) | |
1109 { | |
5275 | 1110 octave_idx_type n = std::min (nr, nc); |
4478 | 1111 |
5275 | 1112 for (octave_idx_type i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1113 m (i, i) = 1.0; |
4478 | 1114 } |
1115 | |
1116 return m; | |
1117 } | |
1118 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1119 FloatMatrix |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1120 float_identity_matrix (octave_idx_type nr, octave_idx_type nc) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1121 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1122 FloatMatrix m (nr, nc, 0.0); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1123 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1124 if (nr > 0 && nc > 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1125 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1126 octave_idx_type n = std::min (nr, nc); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1127 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1128 for (octave_idx_type i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1129 m (i, i) = 1.0; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1130 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1131 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1132 return m; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1133 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
1134 |
8012
63dbb85452cc
fix extern decls in .cc files
John W. Eaton <jwe@octave.org>
parents:
7818
diff
changeset
|
1135 int |
3622 | 1136 octave_format (std::ostream& os, const char *fmt, ...) |
3620 | 1137 { |
1138 int retval = -1; | |
1139 | |
1140 va_list args; | |
1141 va_start (args, fmt); | |
1142 | |
1143 retval = octave_vformat (os, fmt, args); | |
1144 | |
1145 va_end (args); | |
1146 | |
1147 return retval; | |
1148 } | |
1149 | |
8012
63dbb85452cc
fix extern decls in .cc files
John W. Eaton <jwe@octave.org>
parents:
7818
diff
changeset
|
1150 int |
3622 | 1151 octave_vformat (std::ostream& os, const char *fmt, va_list args) |
3620 | 1152 { |
1153 int retval = -1; | |
1154 | |
3775 | 1155 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY |
3620 | 1156 |
4135 | 1157 std::streambuf *sb = os.rdbuf (); |
1158 | |
1159 if (sb) | |
4302 | 1160 { |
1161 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
1162 | |
1163 retval = sb->vform (fmt, args); | |
1164 | |
1165 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
1166 } | |
3620 | 1167 |
1168 #else | |
1169 | |
1170 char *s = octave_vsnprintf (fmt, args); | |
1171 | |
1172 if (s) | |
1173 { | |
1174 os << s; | |
1175 | |
1176 retval = strlen (s); | |
1177 } | |
1178 | |
1179 #endif | |
1180 | |
1181 return retval; | |
1182 } | |
1183 | |
4302 | 1184 // We manage storage. User should not free it, and its contents are |
1185 // only valid until next call to vsnprintf. | |
1186 | |
1187 // Interrupts might happen if someone makes a call with something that | |
1188 // will require a very large buffer. If we are interrupted in that | |
1189 // case, we should make the buffer size smaller for the next call. | |
1190 | |
1191 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF \ | |
1192 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ | |
1193 delete [] buf; \ | |
1194 buf = 0; \ | |
1195 size = initial_size; \ | |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1196 octave_rethrow_exception (); \ |
4302 | 1197 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
1198 | |
4485 | 1199 #if defined __GNUC__ && defined __va_copy |
1200 #define SAVE_ARGS(saved_args, args) __va_copy (saved_args, args) | |
1201 #elif defined va_copy | |
1202 #define SAVE_ARGS(saved_args, args) va_copy (saved_args, args) | |
1203 #else | |
1204 #define SAVE_ARGS(saved_args, args) saved_args = args | |
1205 #endif | |
1206 | |
4302 | 1207 char * |
1208 octave_vsnprintf (const char *fmt, va_list args) | |
1209 { | |
1210 static const size_t initial_size = 100; | |
1211 | |
1212 static size_t size = initial_size; | |
1213 | |
1214 static char *buf = 0; | |
1215 | |
8929
379297a149f0
utils.cc (octave_vsnprintf): avoid uninitialized variable warning
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
1216 int nchars = 0; |
4302 | 1217 |
1218 if (! buf) | |
1219 buf = new char [size]; | |
1220 | |
1221 if (! buf) | |
1222 return 0; | |
1223 | |
1224 while (1) | |
1225 { | |
4485 | 1226 va_list saved_args; |
1227 | |
1228 SAVE_ARGS (saved_args, args); | |
1229 | |
4302 | 1230 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
1231 | |
4485 | 1232 nchars = octave_raw_vsnprintf (buf, size, fmt, saved_args); |
1233 | |
1234 va_end (saved_args); | |
4302 | 1235 |
1236 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
1237 | |
10257
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1238 if (nchars > -1 && nchars < size) |
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1239 break; |
4302 | 1240 else |
10257
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1241 { |
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1242 delete [] buf; |
4302 | 1243 |
10257
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1244 size = nchars + 1;; |
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1245 |
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1246 buf = new char [size]; |
4302 | 1247 |
10257
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1248 if (! buf) |
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1249 return 0; |
cd550069240e
assume vsnprintf from gnulib; use sstream instead of snprintf
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
1250 } |
4302 | 1251 } |
1252 | |
1253 return buf; | |
1254 } | |
1255 | |
1256 char * | |
1257 octave_snprintf (const char *fmt, ...) | |
1258 { | |
1259 char *retval = 0; | |
1260 | |
1261 va_list args; | |
1262 va_start (args, fmt); | |
1263 | |
1264 retval = octave_vsnprintf (fmt, args); | |
1265 | |
1266 va_end (args); | |
1267 | |
1268 return retval; | |
1269 } | |
1270 | |
4086 | 1271 void |
1272 octave_sleep (double seconds) | |
1273 { | |
1274 if (seconds > 0) | |
1275 { | |
1276 double t; | |
1277 | |
4093 | 1278 unsigned int usec |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1279 = static_cast<unsigned int> (modf (seconds, &t) * 1000000); |
4086 | 1280 |
4093 | 1281 unsigned int sec |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10257
diff
changeset
|
1282 = (t > UINT_MAX) ? UINT_MAX : static_cast<unsigned int> (t); |
4086 | 1283 |
5770 | 1284 // Versions of these functions that accept unsigned int args are |
1285 // defined in cutils.c. | |
4086 | 1286 octave_sleep (sec); |
1287 octave_usleep (usec); | |
10070
897e62651c0a
correctly handle interrupts in pause()
Jaroslav Hajek <highegg@gmail.com>
parents:
10066
diff
changeset
|
1288 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
1289 octave_quit (); |
4086 | 1290 } |
1291 } | |
1292 | |
9487
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1293 DEFUN (isindex, args, , |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1294 "-*- texinfo -*-\n\ |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1295 @deftypefn {Built-in Function} {} isindex (@var{ind}, @var{n})\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9705
diff
changeset
|
1296 Returns true if @var{ind} is a valid index. Valid indices can be\n\ |
9487
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1297 either positive integers (though possibly real data), or logical arrays.\n\ |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1298 If present, @var{n} specifies the extent of the dimension to be indexed.\n\ |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1299 Note that, if possible, the internal conversion result is cached so that\n\ |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1300 subsequent indexing will not perform the checking again.\n\ |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1301 @end deftypefn") |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1302 { |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1303 octave_value retval; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1304 int nargin = args.length (); |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1305 octave_idx_type n = 0; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1306 |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1307 if (nargin == 2) |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1308 n = args(1).idx_type_value (); |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1309 else if (nargin != 1) |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1310 print_usage (); |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1311 |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1312 if (! error_state) |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1313 { |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10033
diff
changeset
|
1314 unwind_protect frame; |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10502
diff
changeset
|
1315 |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10605
diff
changeset
|
1316 frame.protect_var (Vallow_noninteger_range_as_index); |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10605
diff
changeset
|
1317 Vallow_noninteger_range_as_index = false; |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10502
diff
changeset
|
1318 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10033
diff
changeset
|
1319 frame.protect_var (error_state); |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10502
diff
changeset
|
1320 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10033
diff
changeset
|
1321 frame.protect_var (discard_error_messages); |
9487
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1322 discard_error_messages = true; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1323 |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1324 try |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1325 { |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1326 idx_vector idx = args(0).index_vector (); |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1327 if (! error_state) |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1328 { |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1329 if (nargin == 2) |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1330 retval = idx.extent (n) <= n; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1331 else |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1332 retval = true; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1333 } |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1334 else |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1335 retval = false; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1336 } |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1337 catch (octave_execution_exception) |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1338 { |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1339 retval = false; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1340 } |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1341 } |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1342 |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1343 return retval; |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1344 } |
2894af292e69
implement isindex function
Jaroslav Hajek <highegg@gmail.com>
parents:
9173
diff
changeset
|
1345 |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1346 octave_value_list |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1347 do_simple_cellfun (octave_value_list (*fun) (const octave_value_list&, int), |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1348 const char *fun_name, const octave_value_list& args, |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1349 int nargout) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1350 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1351 octave_value_list new_args = args, retval; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1352 int nargin = args.length (); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1353 OCTAVE_LOCAL_BUFFER (bool, iscell, nargin); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1354 OCTAVE_LOCAL_BUFFER (Cell, cells, nargin); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1355 OCTAVE_LOCAL_BUFFER (Cell, rcells, nargout); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1356 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1357 const Cell *ccells = cells; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1358 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1359 octave_idx_type numel = 1; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1360 dim_vector dims (1, 1); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1361 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1362 for (int i = 0; i < nargin; i++) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1363 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1364 octave_value arg = new_args(i); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1365 iscell[i] = arg.is_cell (); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1366 if (iscell[i]) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1367 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1368 cells[i] = arg.cell_value (); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1369 octave_idx_type n = ccells[i].numel (); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1370 if (n == 1) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1371 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1372 iscell[i] = false; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1373 new_args(i) = ccells[i](0); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1374 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1375 else if (numel == 1) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1376 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1377 numel = n; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1378 dims = ccells[i].dims (); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1379 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1380 else if (dims != ccells[i].dims ()) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1381 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1382 error ("%s: cell arguments must have matching sizes", fun_name); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1383 break; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1384 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1385 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1386 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1387 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1388 if (! error_state) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1389 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1390 for (int i = 0; i < nargout; i++) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1391 rcells[i].clear (dims); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1392 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1393 for (octave_idx_type j = 0; j < numel; j++) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1394 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1395 for (int i = 0; i < nargin; i++) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1396 if (iscell[i]) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1397 new_args(i) = ccells[i](j); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1398 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
1399 octave_quit (); |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1400 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1401 const octave_value_list tmp = fun (new_args, nargout); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1402 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1403 if (tmp.length () < nargout) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1404 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1405 error ("%s: do_simple_cellfun: internal error", fun_name); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1406 break; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1407 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1408 else |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1409 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1410 for (int i = 0; i < nargout; i++) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1411 rcells[i](j) = tmp(i); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1412 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1413 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1414 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1415 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1416 if (! error_state) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1417 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1418 retval.resize (nargout); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1419 for (int i = 0; i < nargout; i++) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1420 retval(i) = rcells[i]; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1421 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1422 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1423 return retval; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1424 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1425 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1426 octave_value |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1427 do_simple_cellfun (octave_value_list (*fun) (const octave_value_list&, int), |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1428 const char *fun_name, const octave_value_list& args) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1429 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1430 octave_value retval; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1431 const octave_value_list tmp = do_simple_cellfun (fun, fun_name, args, 1); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1432 if (tmp.length () > 0) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1433 retval = tmp(0); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1434 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1435 return retval; |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10070
diff
changeset
|
1436 } |