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