Mercurial > hg > octave-nkf
annotate src/utils.cc @ 7818:5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
author | David Bateman <dbateman@free.fr> |
---|---|
date | Sun, 25 May 2008 15:04:37 +0200 |
parents | 82be108cc558 |
children | 63dbb85452cc |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 2002, 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
240 | 24 #ifdef HAVE_CONFIG_H |
1192 | 25 #include <config.h> |
1 | 26 #endif |
27 | |
3716 | 28 #include <cerrno> |
1343 | 29 #include <climits> |
1346 | 30 #include <cstring> |
1343 | 31 |
3503 | 32 #include <fstream> |
33 #include <iostream> | |
1728 | 34 #include <string> |
35 | |
1350 | 36 #ifdef HAVE_UNISTD_H |
2442 | 37 #ifdef HAVE_SYS_TYPES_H |
1 | 38 #include <sys/types.h> |
2442 | 39 #endif |
1 | 40 #include <unistd.h> |
41 #endif | |
367 | 42 |
4302 | 43 #include "quit.h" |
44 | |
3040 | 45 #include "dir-ops.h" |
46 #include "file-ops.h" | |
2926 | 47 #include "file-stat.h" |
4732 | 48 #include "lo-mappers.h" |
1651 | 49 #include "oct-cmplx.h" |
2926 | 50 #include "oct-env.h" |
3040 | 51 #include "pathsearch.h" |
1755 | 52 #include "str-vec.h" |
1651 | 53 |
4216 | 54 #include "Cell.h" |
2492 | 55 #include <defaults.h> |
1352 | 56 #include "defun.h" |
57 #include "dirfns.h" | |
58 #include "error.h" | |
59 #include "gripes.h" | |
60 #include "input.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" |
1352 | 65 #include "pager.h" |
1690 | 66 #include "sysdep.h" |
1750 | 67 #include "toplev.h" |
1 | 68 #include "unwind-prot.h" |
1352 | 69 #include "utils.h" |
70 #include "variables.h" | |
1 | 71 |
4143 | 72 // Return TRUE if S is a valid identifier. |
73 | |
74 bool | |
75 valid_identifier (const char *s) | |
76 { | |
5290 | 77 if (! s || ! (isalpha (*s) || *s == '_' || *s == '$')) |
4143 | 78 return false; |
79 | |
80 while (*++s != '\0') | |
5290 | 81 if (! (isalnum (*s) || *s == '_' || *s == '$')) |
4143 | 82 return false; |
83 | |
84 return true; | |
85 } | |
86 | |
87 bool | |
88 valid_identifier (const std::string& s) | |
89 { | |
90 return valid_identifier (s.c_str ()); | |
91 } | |
92 | |
4264 | 93 DEFCMD (isvarname, args, , |
5040 | 94 "-*- texinfo -*-\n\ |
95 @deftypefn {Built-in Function} {} isvarname (@var{name})\n\ | |
4264 | 96 Return true if @var{name} is a valid variable name\n\ |
97 @end deftypefn") | |
98 { | |
99 octave_value retval; | |
100 | |
101 int argc = args.length () + 1; | |
102 | |
4610 | 103 string_vector argv = args.make_argv ("isvarname"); |
4264 | 104 |
105 if (error_state) | |
106 return retval; | |
107 | |
108 if (argc == 2) | |
109 retval = valid_identifier (argv[1]); | |
110 else | |
5823 | 111 print_usage (); |
4264 | 112 |
113 return retval; | |
114 } | |
115 | |
6323 | 116 // Return TRUE if F and G are both names for the same file. |
117 | |
118 bool | |
119 same_file (const std::string& f, const std::string& g) | |
120 { | |
6598 | 121 return same_file_internal (f, g); |
6323 | 122 } |
123 | |
1 | 124 int |
3523 | 125 almost_match (const std::string& std, const std::string& s, int min_match_len, |
526 | 126 int case_sens) |
1 | 127 { |
1755 | 128 int stdlen = std.length (); |
129 int slen = s.length (); | |
1 | 130 |
131 return (slen <= stdlen | |
132 && slen >= min_match_len | |
287 | 133 && (case_sens |
1755 | 134 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
3350 | 135 : (octave_strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287 | 136 } |
137 | |
581 | 138 // Ugh. |
139 | |
287 | 140 int |
3523 | 141 keyword_almost_match (const char * const *std, int *min_len, const std::string& s, |
287 | 142 int min_toks_to_match, int max_toks) |
143 { | |
144 int status = 0; | |
145 int tok_count = 0; | |
146 int toks_matched = 0; | |
147 | |
1755 | 148 if (s.empty () || max_toks < 1) |
287 | 149 return status; |
150 | |
1755 | 151 char *kw = strsave (s.c_str ()); |
287 | 152 |
153 char *t = kw; | |
154 while (*t != '\0') | |
155 { | |
156 if (*t == '\t') | |
157 *t = ' '; | |
158 t++; | |
159 } | |
160 | |
161 char *beg = kw; | |
162 while (*beg == ' ') | |
163 beg++; | |
164 | |
165 if (*beg == '\0') | |
166 return status; | |
167 | |
168 | |
3072 | 169 const char **to_match = new const char * [max_toks + 1]; |
170 const char * const *s1 = std; | |
171 const char **s2 = to_match; | |
287 | 172 |
526 | 173 if (! s1 || ! s2) |
287 | 174 goto done; |
175 | |
176 s2[tok_count] = beg; | |
177 char *end; | |
526 | 178 while ((end = strchr (beg, ' ')) != 0) |
287 | 179 { |
180 *end = '\0'; | |
181 beg = end + 1; | |
182 | |
183 while (*beg == ' ') | |
184 beg++; | |
185 | |
186 if (*beg == '\0') | |
187 break; | |
188 | |
189 tok_count++; | |
190 if (tok_count >= max_toks) | |
191 goto done; | |
192 | |
193 s2[tok_count] = beg; | |
194 } | |
526 | 195 s2[tok_count+1] = 0; |
287 | 196 |
197 s2 = to_match; | |
198 | |
199 for (;;) | |
200 { | |
201 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) | |
202 goto done; | |
203 | |
204 toks_matched++; | |
205 | |
206 s1++; | |
207 s2++; | |
208 | |
209 if (! *s2) | |
210 { | |
211 status = (toks_matched >= min_toks_to_match); | |
212 goto done; | |
213 } | |
214 | |
215 if (! *s1) | |
216 goto done; | |
217 } | |
218 | |
219 done: | |
220 | |
221 delete [] kw; | |
222 delete [] to_match; | |
223 | |
224 return status; | |
1 | 225 } |
226 | |
2234 | 227 // Return non-zero if either NR or NC is zero. Return -1 if this |
228 // should be considered fatal; return 1 if this is ok. | |
229 | |
230 int | |
5275 | 231 empty_arg (const char * /* name */, octave_idx_type nr, octave_idx_type nc) |
2234 | 232 { |
4478 | 233 return (nr == 0 || nc == 0); |
2234 | 234 } |
235 | |
581 | 236 // See if the given file is in the path. |
237 | |
3536 | 238 std::string |
4243 | 239 search_path_for_file (const std::string& path, const string_vector& names) |
686 | 240 { |
3174 | 241 dir_path p (path); |
686 | 242 |
4243 | 243 return octave_env::make_absolute (p.find_first_of (names), |
244 octave_env::getcwd ()); | |
686 | 245 } |
246 | |
4216 | 247 // Find all locations of the given file in the path. |
248 | |
249 string_vector | |
4243 | 250 search_path_for_all_files (const std::string& path, const string_vector& names) |
4216 | 251 { |
252 dir_path p (path); | |
253 | |
4243 | 254 string_vector sv = p.find_all_first_of (names); |
4216 | 255 |
6379 | 256 octave_idx_type len = sv.length (); |
4216 | 257 |
6379 | 258 for (octave_idx_type i = 0; i < len; i++) |
4216 | 259 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); |
260 | |
261 return sv; | |
262 } | |
263 | |
264 static string_vector | |
265 make_absolute (const string_vector& sv) | |
266 { | |
6379 | 267 octave_idx_type len = sv.length (); |
268 | |
269 string_vector retval (len); | |
4216 | 270 |
6379 | 271 for (octave_idx_type i = 0; i < len; i++) |
272 retval[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); | |
273 | |
274 return retval; | |
4216 | 275 } |
276 | |
3195 | 277 DEFUN (file_in_loadpath, args, , |
3446 | 278 "-*- texinfo -*-\n\ |
4216 | 279 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\ |
280 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\ | |
3195 | 281 \n\ |
5448 | 282 Return the absolute name of @var{file} if it can be found in\n\ |
5814 | 283 the list of directories specified by @code{path}.\n\ |
4216 | 284 If no file is found, return an empty matrix.\n\ |
285 \n\ | |
5448 | 286 If the first argument is a cell array of strings, search each\n\ |
4243 | 287 directory of the loadpath for element of the cell array and return\n\ |
288 the first that matches.\n\ | |
289 \n\ | |
4216 | 290 If the second optional argument @code{\"all\"} is supplied, return\n\ |
291 a cell array containing the list of all files that have the same\n\ | |
292 name in the path. If no files are found, return an empty cell array.\n\ | |
5814 | 293 @seealso{file_in_path, path}\n\ |
5642 | 294 @end deftypefn") |
3195 | 295 { |
4216 | 296 octave_value retval; |
3195 | 297 |
4243 | 298 int nargin = args.length (); |
3195 | 299 |
4243 | 300 if (nargin == 1 || nargin == 2) |
4216 | 301 { |
4243 | 302 string_vector names = args(0).all_strings (); |
303 | |
304 if (! error_state && names.length () > 0) | |
305 { | |
306 if (nargin == 1) | |
307 { | |
308 std::string fname = octave_env::make_absolute | |
5832 | 309 (load_path::find_first_of (names), octave_env::getcwd ()); |
4243 | 310 |
311 if (fname.empty ()) | |
312 retval = Matrix (); | |
313 else | |
314 retval = fname; | |
315 } | |
316 else if (nargin == 2) | |
317 { | |
318 std::string opt = args(1).string_value (); | |
319 | |
320 if (! error_state && opt == "all") | |
5832 | 321 retval = Cell (make_absolute (load_path::find_all_first_of (names))); |
4243 | 322 else |
4249 | 323 error ("file_in_loadpath: invalid option"); |
4243 | 324 } |
325 } | |
4216 | 326 else |
4243 | 327 error ("file_in_loadpath: expecting string as first argument"); |
4216 | 328 } |
3195 | 329 else |
5823 | 330 print_usage (); |
3195 | 331 |
332 return retval; | |
333 } | |
334 | |
1957 | 335 DEFUN (file_in_path, args, , |
3301 | 336 "-*- texinfo -*-\n\ |
337 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ | |
4216 | 338 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\ |
5448 | 339 Return the absolute name of @var{file} if it can be found in\n\ |
3301 | 340 @var{path}. The value of @var{path} should be a colon-separated list of\n\ |
5814 | 341 directories in the format described for @code{path}. If no file\n\ |
5794 | 342 is found, return an empty matrix. For example,\n\ |
3301 | 343 \n\ |
344 @example\n\ | |
5456 | 345 file_in_path (EXEC_PATH, \"sh\")\n\ |
346 @result{} \"/bin/sh\"\n\ | |
3301 | 347 @end example\n\ |
4216 | 348 \n\ |
5448 | 349 If the second argument is a cell array of strings, search each\n\ |
4243 | 350 directory of the path for element of the cell array and return\n\ |
351 the first that matches.\n\ | |
352 \n\ | |
4216 | 353 If the third optional argument @code{\"all\"} is supplied, return\n\ |
354 a cell array containing the list of all files that have the same\n\ | |
355 name in the path. If no files are found, return an empty cell array.\n\ | |
4249 | 356 @seealso{file_in_loadpath}\n\ |
3301 | 357 @end deftypefn") |
686 | 358 { |
4216 | 359 octave_value retval; |
686 | 360 |
4243 | 361 int nargin = args.length (); |
1755 | 362 |
4243 | 363 if (nargin == 2 || nargin == 3) |
686 | 364 { |
4243 | 365 std::string path = args(0).string_value (); |
366 | |
367 if (! error_state) | |
368 { | |
4249 | 369 string_vector names = args(1).all_strings (); |
4243 | 370 |
371 if (! error_state && names.length () > 0) | |
372 { | |
373 if (nargin == 2) | |
374 { | |
375 std::string fname = search_path_for_file (path, names); | |
686 | 376 |
4243 | 377 if (fname.empty ()) |
378 retval = Matrix (); | |
379 else | |
380 retval = fname; | |
381 } | |
382 else if (nargin == 3) | |
383 { | |
4249 | 384 std::string opt = args(2).string_value (); |
4243 | 385 |
386 if (! error_state && opt == "all") | |
387 retval = Cell (make_absolute (search_path_for_all_files (path, names))); | |
388 else | |
4249 | 389 error ("file_in_path: invalid option"); |
4243 | 390 } |
391 } | |
392 else | |
393 error ("file_in_path: expecting string as second argument"); | |
394 } | |
1755 | 395 else |
4243 | 396 error ("file_in_path: expecting string as first argument"); |
686 | 397 } |
398 else | |
5823 | 399 print_usage (); |
686 | 400 |
401 return retval; | |
402 } | |
403 | |
3536 | 404 std::string |
3523 | 405 file_in_path (const std::string& name, const std::string& suffix) |
526 | 406 { |
3523 | 407 std::string nm = name; |
526 | 408 |
1755 | 409 if (! suffix.empty ()) |
410 nm.append (suffix); | |
686 | 411 |
5832 | 412 return octave_env::make_absolute |
413 (load_path::find_file (nm), octave_env::getcwd ()); | |
526 | 414 } |
415 | |
581 | 416 // See if there is an function file in the path. If so, return the |
417 // full path to the file. | |
418 | |
3536 | 419 std::string |
3523 | 420 fcn_file_in_path (const std::string& name) |
526 | 421 { |
3523 | 422 std::string retval; |
908 | 423 |
1755 | 424 int len = name.length (); |
425 | |
426 if (len > 0) | |
427 { | |
5832 | 428 if (octave_env::absolute_pathname (name)) |
429 { | |
430 file_stat fs (name); | |
431 | |
432 if (fs.exists ()) | |
433 retval = name; | |
434 } | |
435 else if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') | |
436 retval = load_path::find_fcn_file (name.substr (0, len-2)); | |
908 | 437 else |
7818
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
438 { |
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
439 std::string fname = name; |
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
440 size_t pos = name.find_first_of (Vfilemarker); |
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
441 if (pos != NPOS) |
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
442 fname = name.substr (0, pos); |
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
443 |
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
444 retval = load_path::find_fcn_file (fname); |
5640a70cbab1
Add Ffilemarker and fix for 'dbstep in'
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
445 } |
908 | 446 } |
1755 | 447 |
448 return retval; | |
526 | 449 } |
450 | |
5864 | 451 // See if there is a .oct file in the path. If so, return the |
581 | 452 // full path to the file. |
453 | |
3536 | 454 std::string |
3523 | 455 oct_file_in_path (const std::string& name) |
572 | 456 { |
3523 | 457 std::string retval; |
908 | 458 |
1755 | 459 int len = name.length (); |
460 | |
461 if (len > 0) | |
462 { | |
5832 | 463 if (octave_env::absolute_pathname (name)) |
464 { | |
465 file_stat fs (name); | |
466 | |
467 if (fs.exists ()) | |
468 retval = name; | |
469 } | |
470 else if (len > 4 && name [len - 4] == '.' && name [len - 3] == 'o' | |
471 && name [len - 2] == 'c' && name [len - 1] == 't') | |
472 retval = load_path::find_oct_file (name.substr (0, len-4)); | |
908 | 473 else |
5832 | 474 retval = load_path::find_oct_file (name); |
908 | 475 } |
1755 | 476 |
477 return retval; | |
572 | 478 } |
479 | |
5864 | 480 // See if there is a .mex file in the path. If so, return the |
481 // full path to the file. | |
482 | |
483 std::string | |
484 mex_file_in_path (const std::string& name) | |
485 { | |
486 std::string retval; | |
487 | |
488 int len = name.length (); | |
489 | |
490 if (len > 0) | |
491 { | |
492 if (octave_env::absolute_pathname (name)) | |
493 { | |
494 file_stat fs (name); | |
495 | |
496 if (fs.exists ()) | |
497 retval = name; | |
498 } | |
499 else if (len > 4 && name [len - 4] == '.' && name [len - 3] == 'm' | |
500 && name [len - 2] == 'e' && name [len - 1] == 'x') | |
501 retval = load_path::find_mex_file (name.substr (0, len-4)); | |
502 else | |
503 retval = load_path::find_mex_file (name); | |
504 } | |
505 | |
506 return retval; | |
507 } | |
508 | |
3103 | 509 // Replace backslash escapes in a string with the real values. |
510 | |
3536 | 511 std::string |
3523 | 512 do_string_escapes (const std::string& s) |
3103 | 513 { |
3523 | 514 std::string retval; |
3103 | 515 |
516 size_t i = 0; | |
517 size_t j = 0; | |
518 size_t len = s.length (); | |
519 | |
520 retval.resize (len); | |
521 | |
522 while (j < len) | |
523 { | |
524 if (s[j] == '\\' && j+1 < len) | |
525 { | |
526 switch (s[++j]) | |
527 { | |
3893 | 528 case '0': |
529 retval[i] = '\0'; | |
530 break; | |
531 | |
3103 | 532 case 'a': |
533 retval[i] = '\a'; | |
534 break; | |
535 | |
536 case 'b': // backspace | |
537 retval[i] = '\b'; | |
538 break; | |
539 | |
540 case 'f': // formfeed | |
541 retval[i] = '\f'; | |
542 break; | |
543 | |
544 case 'n': // newline | |
545 retval[i] = '\n'; | |
546 break; | |
547 | |
548 case 'r': // carriage return | |
549 retval[i] = '\r'; | |
550 break; | |
551 | |
552 case 't': // horizontal tab | |
553 retval[i] = '\t'; | |
554 break; | |
555 | |
556 case 'v': // vertical tab | |
557 retval[i] = '\v'; | |
558 break; | |
559 | |
560 case '\\': // backslash | |
561 retval[i] = '\\'; | |
562 break; | |
563 | |
564 case '\'': // quote | |
565 retval[i] = '\''; | |
566 break; | |
567 | |
568 case '"': // double quote | |
569 retval[i] = '"'; | |
570 break; | |
571 | |
572 default: | |
573 warning ("unrecognized escape sequence `\\%c' --\ | |
574 converting to `%c'", s[j], s[j]); | |
575 retval[i] = s[j]; | |
576 break; | |
577 } | |
578 } | |
579 else | |
580 { | |
581 retval[i] = s[j]; | |
582 } | |
583 | |
584 i++; | |
585 j++; | |
586 } | |
587 | |
3105 | 588 retval.resize (i); |
3103 | 589 |
590 return retval; | |
591 } | |
592 | |
593 DEFUN (do_string_escapes, args, , | |
3446 | 594 "-*- texinfo -*-\n\ |
595 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ | |
596 Convert special characters in @var{string} to their escaped forms.\n\ | |
597 @end deftypefn") | |
3103 | 598 { |
599 octave_value retval; | |
600 | |
601 int nargin = args.length (); | |
602 | |
603 if (nargin == 1) | |
604 { | |
605 if (args(0).is_string ()) | |
606 retval = do_string_escapes (args(0).string_value ()); | |
607 else | |
608 error ("do_string_escapes: argument must be a string"); | |
609 } | |
610 else | |
5823 | 611 print_usage (); |
3103 | 612 |
613 return retval; | |
614 } | |
615 | |
1755 | 616 const char * |
801 | 617 undo_string_escape (char c) |
618 { | |
619 if (! c) | |
1755 | 620 return ""; |
801 | 621 |
622 switch (c) | |
623 { | |
3893 | 624 case '\0': |
625 return "\\0"; | |
626 | |
801 | 627 case '\a': |
628 return "\\a"; | |
629 | |
630 case '\b': // backspace | |
631 return "\\b"; | |
632 | |
633 case '\f': // formfeed | |
634 return "\\f"; | |
635 | |
636 case '\n': // newline | |
637 return "\\n"; | |
638 | |
639 case '\r': // carriage return | |
640 return "\\r"; | |
641 | |
642 case '\t': // horizontal tab | |
643 return "\\t"; | |
644 | |
645 case '\v': // vertical tab | |
646 return "\\v"; | |
647 | |
648 case '\\': // backslash | |
649 return "\\\\"; | |
650 | |
651 case '"': // double quote | |
652 return "\\\""; | |
653 | |
654 default: | |
1755 | 655 { |
656 static char retval[2]; | |
657 retval[0] = c; | |
658 retval[1] = '\0'; | |
659 return retval; | |
660 } | |
801 | 661 } |
662 } | |
663 | |
3536 | 664 std::string |
3523 | 665 undo_string_escapes (const std::string& s) |
801 | 666 { |
3523 | 667 std::string retval; |
801 | 668 |
1755 | 669 for (size_t i = 0; i < s.length (); i++) |
670 retval.append (undo_string_escape (s[i])); | |
801 | 671 |
1755 | 672 return retval; |
801 | 673 } |
674 | |
1957 | 675 DEFUN (undo_string_escapes, args, , |
3361 | 676 "-*- texinfo -*-\n\ |
677 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ | |
678 Converts special characters in strings back to their escaped forms. For\n\ | |
679 example, the expression\n\ | |
680 \n\ | |
681 @example\n\ | |
682 bell = \"\\a\";\n\ | |
683 @end example\n\ | |
684 \n\ | |
685 @noindent\n\ | |
686 assigns the value of the alert character (control-g, ASCII code 7) to\n\ | |
687 the string variable @code{bell}. If this string is printed, the\n\ | |
688 system will ring the terminal bell (if it is possible). This is\n\ | |
689 normally the desired outcome. However, sometimes it is useful to be\n\ | |
690 able to print the original representation of the string, with the\n\ | |
691 special characters replaced by their escape sequences. For example,\n\ | |
692 \n\ | |
693 @example\n\ | |
694 octave:13> undo_string_escapes (bell)\n\ | |
695 ans = \\a\n\ | |
696 @end example\n\ | |
697 \n\ | |
698 @noindent\n\ | |
699 replaces the unprintable alert character with its printable\n\ | |
700 representation.\n\ | |
701 @end deftypefn") | |
801 | 702 { |
2086 | 703 octave_value retval; |
801 | 704 |
705 int nargin = args.length (); | |
706 | |
3103 | 707 if (nargin == 1) |
708 { | |
709 if (args(0).is_string ()) | |
710 retval = undo_string_escapes (args(0).string_value ()); | |
711 else | |
712 error ("undo_string_escapes: argument must be a string"); | |
713 } | |
801 | 714 else |
5823 | 715 print_usage (); |
801 | 716 |
717 return retval; | |
718 } | |
719 | |
5465 | 720 DEFUNX ("errno", Ferrno, args, , |
3716 | 721 "-*- texinfo -*-\n\ |
5465 | 722 @deftypefn {Built-in Function} {@var{err} =} errno ()\n\ |
723 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{val})\n\ | |
724 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{name})\n\ | |
725 Return the current value of the system-dependent variable errno,\n\ | |
726 set its value to @var{val} and return the previous value, or return\n\ | |
727 the named error code given @var{name} as a character string, or -1\n\ | |
728 if @var{name} is not found.\n\ | |
3716 | 729 @end deftypefn") |
730 { | |
731 octave_value retval; | |
732 | |
5465 | 733 int nargin = args.length (); |
734 | |
735 if (nargin == 1) | |
736 { | |
737 if (args(0).is_string ()) | |
738 { | |
739 std::string nm = args(0).string_value (); | |
740 | |
741 if (! error_state) | |
742 retval = octave_errno::lookup (nm); | |
743 else | |
744 error ("errno: expecting character string argument"); | |
745 } | |
746 else | |
747 { | |
748 int val = args(0).int_value (); | |
749 | |
750 if (! error_state) | |
751 retval = octave_errno::set (val); | |
752 else | |
753 error ("errno: expecting integer argument"); | |
754 } | |
755 } | |
756 else if (nargin == 0) | |
757 retval = octave_errno::get (); | |
3716 | 758 else |
5823 | 759 print_usage (); |
3716 | 760 |
761 return retval; | |
762 } | |
763 | |
5465 | 764 DEFUN (errno_list, args, , |
765 "-*- texinfo -*-\n\ | |
766 @deftypefn {Built-in Function} {} errno_list ()\n\ | |
767 Return a structure containing the system-dependent errno values.\n\ | |
768 @end deftypefn") | |
769 { | |
770 octave_value retval; | |
771 | |
772 if (args.length () == 0) | |
773 retval = octave_errno::list (); | |
774 else | |
5823 | 775 print_usage (); |
5465 | 776 |
777 return retval; | |
778 } | |
3716 | 779 |
2285 | 780 static void |
5275 | 781 check_dimensions (octave_idx_type& nr, octave_idx_type& nc, const char *warnfor) |
3354 | 782 { |
783 if (nr < 0 || nc < 0) | |
784 { | |
5781 | 785 warning_with_id ("Octave:neg-dim-as-zero", |
786 "%s: converting negative dimension to zero", warnfor); | |
3354 | 787 |
4457 | 788 nr = (nr < 0) ? 0 : nr; |
789 nc = (nc < 0) ? 0 : nc; | |
3354 | 790 } |
791 } | |
792 | |
793 void | |
4513 | 794 check_dimensions (dim_vector& dim, const char *warnfor) |
4481 | 795 { |
796 bool neg = false; | |
797 | |
798 for (int i = 0; i < dim.length (); i++) | |
799 { | |
800 if (dim(i) < 0) | |
801 { | |
802 dim(i) = 0; | |
803 neg = true; | |
804 } | |
805 } | |
806 | |
5781 | 807 if (neg) |
808 warning_with_id ("Octave:neg-dim-as-zero", | |
809 "%s: converting negative dimension to zero", warnfor); | |
4481 | 810 } |
811 | |
812 | |
813 void | |
814 get_dimensions (const octave_value& a, const char *warn_for, | |
4513 | 815 dim_vector& dim) |
4481 | 816 { |
817 if (a.is_scalar_type ()) | |
818 { | |
819 dim.resize (2); | |
4732 | 820 dim(0) = a.int_value (); |
4481 | 821 dim(1) = dim(0); |
822 } | |
823 else | |
824 { | |
5275 | 825 octave_idx_type nr = a.rows (); |
826 octave_idx_type nc = a.columns (); | |
4481 | 827 |
828 if (nr == 1 || nc == 1) | |
829 { | |
830 Array<double> v = a.vector_value (); | |
831 | |
832 if (error_state) | |
833 return; | |
834 | |
5275 | 835 octave_idx_type n = v.length (); |
4481 | 836 dim.resize (n); |
5275 | 837 for (octave_idx_type i = 0; i < n; i++) |
4783 | 838 dim(i) = static_cast<int> (fix (v(i))); |
4481 | 839 } |
840 else | |
5257 | 841 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
4481 | 842 } |
843 | |
5258 | 844 if (! error_state) |
845 check_dimensions (dim, warn_for); // May set error_state. | |
4481 | 846 } |
847 | |
848 | |
849 void | |
3354 | 850 get_dimensions (const octave_value& a, const char *warn_for, |
5275 | 851 octave_idx_type& nr, octave_idx_type& nc) |
3354 | 852 { |
853 if (a.is_scalar_type ()) | |
854 { | |
4732 | 855 nr = nc = a.int_value (); |
3354 | 856 } |
857 else | |
858 { | |
859 nr = a.rows (); | |
860 nc = a.columns (); | |
861 | |
862 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) | |
863 { | |
3419 | 864 Array<double> v = a.vector_value (); |
3354 | 865 |
866 if (error_state) | |
867 return; | |
868 | |
5275 | 869 nr = static_cast<octave_idx_type> (fix (v (0))); |
870 nc = static_cast<octave_idx_type> (fix (v (1))); | |
3354 | 871 } |
872 else | |
5257 | 873 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
3354 | 874 } |
875 | |
5258 | 876 if (! error_state) |
877 check_dimensions (nr, nc, warn_for); // May set error_state. | |
3354 | 878 } |
879 | |
880 void | |
881 get_dimensions (const octave_value& a, const octave_value& b, | |
5275 | 882 const char *warn_for, octave_idx_type& nr, octave_idx_type& nc) |
3354 | 883 { |
4732 | 884 nr = a.is_empty () ? 0 : a.int_value (); |
885 nc = b.is_empty () ? 0 : b.int_value (); | |
3354 | 886 |
887 if (error_state) | |
888 error ("%s: expecting two scalar arguments", warn_for); | |
889 else | |
890 check_dimensions (nr, nc, warn_for); // May set error_state. | |
891 } | |
892 | |
4478 | 893 Matrix |
5275 | 894 identity_matrix (octave_idx_type nr, octave_idx_type nc) |
4478 | 895 { |
896 Matrix m (nr, nc, 0.0); | |
897 | |
898 if (nr > 0 && nc > 0) | |
899 { | |
5275 | 900 octave_idx_type n = std::min (nr, nc); |
4478 | 901 |
5275 | 902 for (octave_idx_type i = 0; i < n; i++) |
4478 | 903 m (i, i) = 1.0; |
904 } | |
905 | |
906 return m; | |
907 } | |
908 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
909 FloatMatrix |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
910 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
|
911 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
912 FloatMatrix m (nr, nc, 0.0); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
913 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
914 if (nr > 0 && nc > 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
915 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
916 octave_idx_type n = std::min (nr, nc); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
917 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
918 for (octave_idx_type i = 0; i < n; i++) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
919 m (i, i) = 1.0; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
920 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
921 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
922 return m; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
923 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7481
diff
changeset
|
924 |
3620 | 925 extern int |
3622 | 926 octave_format (std::ostream& os, const char *fmt, ...) |
3620 | 927 { |
928 int retval = -1; | |
929 | |
930 va_list args; | |
931 va_start (args, fmt); | |
932 | |
933 retval = octave_vformat (os, fmt, args); | |
934 | |
935 va_end (args); | |
936 | |
937 return retval; | |
938 } | |
939 | |
940 extern int | |
3622 | 941 octave_vformat (std::ostream& os, const char *fmt, va_list args) |
3620 | 942 { |
943 int retval = -1; | |
944 | |
3775 | 945 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY |
3620 | 946 |
4135 | 947 std::streambuf *sb = os.rdbuf (); |
948 | |
949 if (sb) | |
4302 | 950 { |
951 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
952 | |
953 retval = sb->vform (fmt, args); | |
954 | |
955 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
956 } | |
3620 | 957 |
958 #else | |
959 | |
960 char *s = octave_vsnprintf (fmt, args); | |
961 | |
962 if (s) | |
963 { | |
964 os << s; | |
965 | |
966 retval = strlen (s); | |
967 } | |
968 | |
969 #endif | |
970 | |
971 return retval; | |
972 } | |
973 | |
5775 | 974 /* FIXME -- we really need a configure test for this. */ |
4302 | 975 |
6694 | 976 #if defined __GNUC__ && __GNUC__ >= 3 && ! defined __MINGW32__ |
4302 | 977 #define HAVE_C99_VSNPRINTF 1 |
978 #endif | |
979 | |
980 // We manage storage. User should not free it, and its contents are | |
981 // only valid until next call to vsnprintf. | |
982 | |
983 // Interrupts might happen if someone makes a call with something that | |
984 // will require a very large buffer. If we are interrupted in that | |
985 // case, we should make the buffer size smaller for the next call. | |
986 | |
987 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF \ | |
988 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ | |
989 delete [] buf; \ | |
990 buf = 0; \ | |
991 size = initial_size; \ | |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
992 octave_rethrow_exception (); \ |
4302 | 993 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
994 | |
4485 | 995 #if defined __GNUC__ && defined __va_copy |
996 #define SAVE_ARGS(saved_args, args) __va_copy (saved_args, args) | |
997 #elif defined va_copy | |
998 #define SAVE_ARGS(saved_args, args) va_copy (saved_args, args) | |
999 #else | |
1000 #define SAVE_ARGS(saved_args, args) saved_args = args | |
1001 #endif | |
1002 | |
4302 | 1003 char * |
1004 octave_vsnprintf (const char *fmt, va_list args) | |
1005 { | |
1006 static const size_t initial_size = 100; | |
1007 | |
1008 static size_t size = initial_size; | |
1009 | |
1010 static char *buf = 0; | |
1011 | |
4482 | 1012 #if defined (HAVE_C99_VSNPRINTF) |
1013 size_t nchars; | |
1014 #else | |
4351 | 1015 int nchars; |
4482 | 1016 #endif |
4302 | 1017 |
1018 if (! buf) | |
1019 buf = new char [size]; | |
1020 | |
1021 if (! buf) | |
1022 return 0; | |
1023 | |
1024 #if defined (HAVE_C99_VSNPRINTF) | |
1025 | |
4485 | 1026 // Note that the caller is responsible for calling va_end on args. |
1027 // We will do it for saved_args. | |
1028 | |
1029 va_list saved_args; | |
1030 | |
1031 SAVE_ARGS (saved_args, args); | |
1032 | |
4302 | 1033 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
1034 | |
1035 nchars = octave_raw_vsnprintf (buf, size, fmt, args); | |
1036 | |
1037 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
1038 | |
1039 if (nchars >= size) | |
1040 { | |
1041 size = nchars + 1; | |
1042 | |
1043 delete [] buf; | |
1044 | |
1045 buf = new char [size]; | |
1046 | |
1047 if (buf) | |
1048 { | |
1049 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; | |
1050 | |
4485 | 1051 octave_raw_vsnprintf (buf, size, fmt, saved_args); |
4302 | 1052 |
1053 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
1054 } | |
1055 } | |
1056 | |
4485 | 1057 va_end (saved_args); |
1058 | |
4302 | 1059 #else |
1060 | |
1061 while (1) | |
1062 { | |
4485 | 1063 va_list saved_args; |
1064 | |
1065 SAVE_ARGS (saved_args, args); | |
1066 | |
4302 | 1067 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
1068 | |
4485 | 1069 nchars = octave_raw_vsnprintf (buf, size, fmt, saved_args); |
1070 | |
1071 va_end (saved_args); | |
4302 | 1072 |
1073 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
1074 | |
4351 | 1075 if (nchars > -1 && nchars < size-1) |
4302 | 1076 return buf; |
1077 else | |
1078 { | |
1079 delete [] buf; | |
1080 | |
1081 size *= 2; | |
1082 | |
1083 buf = new char [size]; | |
1084 | |
1085 if (! buf) | |
1086 return 0; | |
1087 } | |
1088 } | |
1089 | |
1090 #endif | |
1091 | |
1092 return buf; | |
1093 } | |
1094 | |
1095 char * | |
1096 octave_snprintf (const char *fmt, ...) | |
1097 { | |
1098 char *retval = 0; | |
1099 | |
1100 va_list args; | |
1101 va_start (args, fmt); | |
1102 | |
1103 retval = octave_vsnprintf (fmt, args); | |
1104 | |
1105 va_end (args); | |
1106 | |
1107 return retval; | |
1108 } | |
1109 | |
4086 | 1110 void |
1111 octave_sleep (double seconds) | |
1112 { | |
1113 if (seconds > 0) | |
1114 { | |
1115 double t; | |
1116 | |
4093 | 1117 unsigned int usec |
4783 | 1118 = static_cast<unsigned int> (modf (seconds, &t) * 1000000); |
4086 | 1119 |
4093 | 1120 unsigned int sec |
4783 | 1121 = (t > UINT_MAX) ? UINT_MAX : static_cast<unsigned int> (t); |
4086 | 1122 |
5770 | 1123 // Versions of these functions that accept unsigned int args are |
1124 // defined in cutils.c. | |
4086 | 1125 octave_sleep (sec); |
1126 octave_usleep (usec); | |
1127 } | |
1128 } | |
1129 | |
572 | 1130 /* |
1 | 1131 ;;; Local Variables: *** |
1132 ;;; mode: C++ *** | |
1133 ;;; End: *** | |
1134 */ |