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