1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
3716
|
27 #include <cerrno> |
1343
|
28 #include <climits> |
1346
|
29 #include <cstring> |
1343
|
30 |
3503
|
31 #include <fstream> |
|
32 #include <iostream> |
1728
|
33 #include <string> |
|
34 |
1350
|
35 #ifdef HAVE_UNISTD_H |
2442
|
36 #ifdef HAVE_SYS_TYPES_H |
1
|
37 #include <sys/types.h> |
2442
|
38 #endif |
1
|
39 #include <unistd.h> |
|
40 #endif |
367
|
41 |
3566
|
42 // Include setjmp.h, not csetjmp since the latter might only define |
|
43 // the ANSI standard C interface. |
|
44 |
|
45 #include <setjmp.h> |
|
46 |
3040
|
47 #include "dir-ops.h" |
|
48 #include "file-ops.h" |
2926
|
49 #include "file-stat.h" |
4051
|
50 #include "lo-sstream.h" |
1651
|
51 #include "oct-cmplx.h" |
2926
|
52 #include "oct-env.h" |
3040
|
53 #include "pathsearch.h" |
1755
|
54 #include "str-vec.h" |
1651
|
55 |
4216
|
56 #include "Cell.h" |
2492
|
57 #include <defaults.h> |
1352
|
58 #include "defun.h" |
|
59 #include "dirfns.h" |
|
60 #include "error.h" |
|
61 #include "gripes.h" |
|
62 #include "input.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 |
3354
|
72 // Should expressions like ones (-1, 5) result in an empty matrix or |
|
73 // an error? A positive value means yes. A negative value means |
|
74 // yes, but print a warning message. Zero means it should be |
|
75 // considered an error. |
|
76 static int Vtreat_neg_dim_as_zero; |
|
77 |
4143
|
78 // Return TRUE if S is a valid identifier. |
|
79 |
|
80 bool |
|
81 valid_identifier (const char *s) |
|
82 { |
4264
|
83 if (! s || ! (isalpha (*s) || *s == '_')) |
4143
|
84 return false; |
|
85 |
|
86 while (*++s != '\0') |
|
87 if (! (isalnum (*s) || *s == '_')) |
|
88 return false; |
|
89 |
|
90 return true; |
|
91 } |
|
92 |
|
93 bool |
|
94 valid_identifier (const std::string& s) |
|
95 { |
|
96 return valid_identifier (s.c_str ()); |
|
97 } |
|
98 |
4264
|
99 DEFCMD (isvarname, args, , |
|
100 "@deftypefn {Built-in Function} {} isvarname (@var{name})\n\ |
|
101 Return true if @var{name} is a valid variable name\n\ |
|
102 @end deftypefn") |
|
103 { |
|
104 octave_value retval; |
|
105 |
|
106 int argc = args.length () + 1; |
|
107 |
|
108 string_vector argv = args.make_argv ("help"); |
|
109 |
|
110 if (error_state) |
|
111 return retval; |
|
112 |
|
113 if (argc == 2) |
|
114 retval = valid_identifier (argv[1]); |
|
115 else |
|
116 print_usage ("isvarname"); |
|
117 |
|
118 return retval; |
|
119 } |
|
120 |
1
|
121 int |
3523
|
122 almost_match (const std::string& std, const std::string& s, int min_match_len, |
526
|
123 int case_sens) |
1
|
124 { |
1755
|
125 int stdlen = std.length (); |
|
126 int slen = s.length (); |
1
|
127 |
|
128 return (slen <= stdlen |
|
129 && slen >= min_match_len |
287
|
130 && (case_sens |
1755
|
131 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
3350
|
132 : (octave_strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
133 } |
|
134 |
581
|
135 // Ugh. |
|
136 |
287
|
137 int |
3523
|
138 keyword_almost_match (const char * const *std, int *min_len, const std::string& s, |
287
|
139 int min_toks_to_match, int max_toks) |
|
140 { |
|
141 int status = 0; |
|
142 int tok_count = 0; |
|
143 int toks_matched = 0; |
|
144 |
1755
|
145 if (s.empty () || max_toks < 1) |
287
|
146 return status; |
|
147 |
1755
|
148 char *kw = strsave (s.c_str ()); |
287
|
149 |
|
150 char *t = kw; |
|
151 while (*t != '\0') |
|
152 { |
|
153 if (*t == '\t') |
|
154 *t = ' '; |
|
155 t++; |
|
156 } |
|
157 |
|
158 char *beg = kw; |
|
159 while (*beg == ' ') |
|
160 beg++; |
|
161 |
|
162 if (*beg == '\0') |
|
163 return status; |
|
164 |
|
165 |
3072
|
166 const char **to_match = new const char * [max_toks + 1]; |
|
167 const char * const *s1 = std; |
|
168 const char **s2 = to_match; |
287
|
169 |
526
|
170 if (! s1 || ! s2) |
287
|
171 goto done; |
|
172 |
|
173 s2[tok_count] = beg; |
|
174 char *end; |
526
|
175 while ((end = strchr (beg, ' ')) != 0) |
287
|
176 { |
|
177 *end = '\0'; |
|
178 beg = end + 1; |
|
179 |
|
180 while (*beg == ' ') |
|
181 beg++; |
|
182 |
|
183 if (*beg == '\0') |
|
184 break; |
|
185 |
|
186 tok_count++; |
|
187 if (tok_count >= max_toks) |
|
188 goto done; |
|
189 |
|
190 s2[tok_count] = beg; |
|
191 } |
526
|
192 s2[tok_count+1] = 0; |
287
|
193 |
|
194 s2 = to_match; |
|
195 |
|
196 for (;;) |
|
197 { |
|
198 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
199 goto done; |
|
200 |
|
201 toks_matched++; |
|
202 |
|
203 s1++; |
|
204 s2++; |
|
205 |
|
206 if (! *s2) |
|
207 { |
|
208 status = (toks_matched >= min_toks_to_match); |
|
209 goto done; |
|
210 } |
|
211 |
|
212 if (! *s1) |
|
213 goto done; |
|
214 } |
|
215 |
|
216 done: |
|
217 |
|
218 delete [] kw; |
|
219 delete [] to_match; |
|
220 |
|
221 return status; |
1
|
222 } |
|
223 |
2234
|
224 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
225 // should be considered fatal; return 1 if this is ok. |
|
226 |
|
227 int |
|
228 empty_arg (const char *name, int nr, int nc) |
|
229 { |
|
230 int is_empty = 0; |
|
231 |
|
232 if (nr == 0 || nc == 0) |
|
233 { |
|
234 int flag = Vpropagate_empty_matrices; |
|
235 |
|
236 if (flag < 0) |
|
237 { |
|
238 gripe_empty_arg (name, 0); |
|
239 is_empty = 1; |
|
240 } |
|
241 else if (flag == 0) |
|
242 { |
|
243 gripe_empty_arg (name, 1); |
|
244 is_empty = -1; |
|
245 } |
|
246 else |
|
247 is_empty = 1; |
|
248 } |
|
249 |
|
250 return is_empty; |
|
251 } |
|
252 |
581
|
253 // See if the given file is in the path. |
|
254 |
3536
|
255 std::string |
4243
|
256 search_path_for_file (const std::string& path, const string_vector& names) |
686
|
257 { |
3174
|
258 dir_path p (path); |
686
|
259 |
4243
|
260 return octave_env::make_absolute (p.find_first_of (names), |
|
261 octave_env::getcwd ()); |
686
|
262 } |
|
263 |
4216
|
264 // Find all locations of the given file in the path. |
|
265 |
|
266 string_vector |
4243
|
267 search_path_for_all_files (const std::string& path, const string_vector& names) |
4216
|
268 { |
|
269 dir_path p (path); |
|
270 |
4243
|
271 string_vector sv = p.find_all_first_of (names); |
4216
|
272 |
|
273 int len = sv.length (); |
|
274 |
|
275 for (int i = 0; i < len; i++) |
|
276 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); |
|
277 |
|
278 return sv; |
|
279 } |
|
280 |
|
281 static string_vector |
|
282 make_absolute (const string_vector& sv) |
|
283 { |
|
284 int len = sv.length (); |
|
285 |
|
286 for (int i = 0; i < len; i++) |
|
287 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); |
|
288 |
|
289 return sv; |
|
290 } |
|
291 |
3195
|
292 DEFUN (file_in_loadpath, args, , |
3446
|
293 "-*- texinfo -*-\n\ |
4216
|
294 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\ |
|
295 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\ |
3195
|
296 \n\ |
4216
|
297 Return the absolute name name of @var{file} if it can be found in\n\ |
|
298 the list of directories specified by @code{LOADPATH}.\n\ |
|
299 If no file is found, return an empty matrix.\n\ |
|
300 \n\ |
4243
|
301 If the first argument is a cell array of of strings, search each\n\ |
|
302 directory of the loadpath for element of the cell array and return\n\ |
|
303 the first that matches.\n\ |
|
304 \n\ |
4216
|
305 If the second optional argument @code{\"all\"} is supplied, return\n\ |
|
306 a cell array containing the list of all files that have the same\n\ |
|
307 name in the path. If no files are found, return an empty cell array.\n\ |
3446
|
308 @end deftypefn\n\ |
|
309 @seealso{file_in_path}") |
3195
|
310 { |
4216
|
311 octave_value retval; |
3195
|
312 |
4243
|
313 int nargin = args.length (); |
3195
|
314 |
4243
|
315 if (nargin == 1 || nargin == 2) |
4216
|
316 { |
4243
|
317 string_vector names = args(0).all_strings (); |
|
318 |
|
319 if (! error_state && names.length () > 0) |
|
320 { |
|
321 if (nargin == 1) |
|
322 { |
|
323 std::string fname = octave_env::make_absolute |
|
324 (Vload_path_dir_path.find_first_of (names), |
|
325 octave_env::getcwd ()); |
|
326 |
|
327 if (fname.empty ()) |
|
328 retval = Matrix (); |
|
329 else |
|
330 retval = fname; |
|
331 } |
|
332 else if (nargin == 2) |
|
333 { |
|
334 std::string opt = args(1).string_value (); |
|
335 |
|
336 if (! error_state && opt == "all") |
|
337 retval = Cell (make_absolute (Vload_path_dir_path.find_all_first_of (names))); |
|
338 else |
4249
|
339 error ("file_in_loadpath: invalid option"); |
4243
|
340 } |
|
341 } |
4216
|
342 else |
4243
|
343 error ("file_in_loadpath: expecting string as first argument"); |
4216
|
344 } |
3195
|
345 else |
|
346 print_usage ("file_in_loadpath"); |
|
347 |
|
348 return retval; |
|
349 } |
|
350 |
1957
|
351 DEFUN (file_in_path, args, , |
3301
|
352 "-*- texinfo -*-\n\ |
|
353 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ |
4216
|
354 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\ |
3301
|
355 Return the absolute name name of @var{file} if it can be found in\n\ |
|
356 @var{path}. The value of @var{path} should be a colon-separated list of\n\ |
|
357 directories in the format described for the built-in variable\n\ |
4216
|
358 @code{LOADPATH}. If no file is found, return an empty matrix.\n\ |
3301
|
359 For example,\n\ |
|
360 \n\ |
|
361 @example\n\ |
|
362 file_in_path (LOADPATH, \"nargchk.m\")\n\ |
|
363 @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\ |
|
364 @end example\n\ |
4216
|
365 \n\ |
4243
|
366 If the second argument is a cell array of of strings, search each\n\ |
|
367 directory of the path for element of the cell array and return\n\ |
|
368 the first that matches.\n\ |
|
369 \n\ |
4216
|
370 If the third optional argument @code{\"all\"} is supplied, return\n\ |
|
371 a cell array containing the list of all files that have the same\n\ |
|
372 name in the path. If no files are found, return an empty cell array.\n\ |
4249
|
373 @seealso{file_in_loadpath}\n\ |
3301
|
374 @end deftypefn") |
686
|
375 { |
4216
|
376 octave_value retval; |
686
|
377 |
4243
|
378 int nargin = args.length (); |
1755
|
379 |
4243
|
380 if (nargin == 2 || nargin == 3) |
686
|
381 { |
4243
|
382 std::string path = args(0).string_value (); |
|
383 |
|
384 if (! error_state) |
|
385 { |
4249
|
386 string_vector names = args(1).all_strings (); |
4243
|
387 |
|
388 if (! error_state && names.length () > 0) |
|
389 { |
|
390 if (nargin == 2) |
|
391 { |
|
392 std::string fname = search_path_for_file (path, names); |
686
|
393 |
4243
|
394 if (fname.empty ()) |
|
395 retval = Matrix (); |
|
396 else |
|
397 retval = fname; |
|
398 } |
|
399 else if (nargin == 3) |
|
400 { |
4249
|
401 std::string opt = args(2).string_value (); |
4243
|
402 |
|
403 if (! error_state && opt == "all") |
|
404 retval = Cell (make_absolute (search_path_for_all_files (path, names))); |
|
405 else |
4249
|
406 error ("file_in_path: invalid option"); |
4243
|
407 } |
|
408 } |
|
409 else |
|
410 error ("file_in_path: expecting string as second argument"); |
|
411 } |
1755
|
412 else |
4243
|
413 error ("file_in_path: expecting string as first argument"); |
686
|
414 } |
|
415 else |
|
416 print_usage ("file_in_path"); |
|
417 |
|
418 return retval; |
|
419 } |
|
420 |
3536
|
421 std::string |
3523
|
422 file_in_path (const std::string& name, const std::string& suffix) |
526
|
423 { |
3523
|
424 std::string nm = name; |
526
|
425 |
1755
|
426 if (! suffix.empty ()) |
|
427 nm.append (suffix); |
686
|
428 |
3174
|
429 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
430 octave_env::getcwd ()); |
526
|
431 } |
|
432 |
581
|
433 // See if there is an function file in the path. If so, return the |
|
434 // full path to the file. |
|
435 |
3536
|
436 std::string |
3523
|
437 fcn_file_in_path (const std::string& name) |
526
|
438 { |
3523
|
439 std::string retval; |
908
|
440 |
1755
|
441 int len = name.length (); |
|
442 |
|
443 if (len > 0) |
|
444 { |
|
445 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
446 retval = file_in_path (name, ""); |
908
|
447 else |
1755
|
448 retval = file_in_path (name, ".m"); |
908
|
449 } |
1755
|
450 |
|
451 return retval; |
526
|
452 } |
|
453 |
581
|
454 // See if there is an octave file in the path. If so, return the |
|
455 // full path to the file. |
|
456 |
3536
|
457 std::string |
3523
|
458 oct_file_in_path (const std::string& name) |
572
|
459 { |
3523
|
460 std::string retval; |
908
|
461 |
1755
|
462 int len = name.length (); |
|
463 |
|
464 if (len > 0) |
|
465 { |
|
466 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
467 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
468 retval = file_in_path (name, ""); |
908
|
469 else |
1755
|
470 retval = file_in_path (name, ".oct"); |
908
|
471 } |
1755
|
472 |
|
473 return retval; |
572
|
474 } |
|
475 |
3103
|
476 // Replace backslash escapes in a string with the real values. |
|
477 |
3536
|
478 std::string |
3523
|
479 do_string_escapes (const std::string& s) |
3103
|
480 { |
3523
|
481 std::string retval; |
3103
|
482 |
|
483 size_t i = 0; |
|
484 size_t j = 0; |
|
485 size_t len = s.length (); |
|
486 |
|
487 retval.resize (len); |
|
488 |
|
489 while (j < len) |
|
490 { |
|
491 if (s[j] == '\\' && j+1 < len) |
|
492 { |
|
493 switch (s[++j]) |
|
494 { |
3893
|
495 case '0': |
|
496 retval[i] = '\0'; |
|
497 break; |
|
498 |
3103
|
499 case 'a': |
|
500 retval[i] = '\a'; |
|
501 break; |
|
502 |
|
503 case 'b': // backspace |
|
504 retval[i] = '\b'; |
|
505 break; |
|
506 |
|
507 case 'f': // formfeed |
|
508 retval[i] = '\f'; |
|
509 break; |
|
510 |
|
511 case 'n': // newline |
|
512 retval[i] = '\n'; |
|
513 break; |
|
514 |
|
515 case 'r': // carriage return |
|
516 retval[i] = '\r'; |
|
517 break; |
|
518 |
|
519 case 't': // horizontal tab |
|
520 retval[i] = '\t'; |
|
521 break; |
|
522 |
|
523 case 'v': // vertical tab |
|
524 retval[i] = '\v'; |
|
525 break; |
|
526 |
|
527 case '\\': // backslash |
|
528 retval[i] = '\\'; |
|
529 break; |
|
530 |
|
531 case '\'': // quote |
|
532 retval[i] = '\''; |
|
533 break; |
|
534 |
|
535 case '"': // double quote |
|
536 retval[i] = '"'; |
|
537 break; |
|
538 |
|
539 default: |
|
540 warning ("unrecognized escape sequence `\\%c' --\ |
|
541 converting to `%c'", s[j], s[j]); |
|
542 retval[i] = s[j]; |
|
543 break; |
|
544 } |
|
545 } |
|
546 else |
|
547 { |
|
548 retval[i] = s[j]; |
|
549 } |
|
550 |
|
551 i++; |
|
552 j++; |
|
553 } |
|
554 |
3105
|
555 retval.resize (i); |
3103
|
556 |
|
557 return retval; |
|
558 } |
|
559 |
|
560 DEFUN (do_string_escapes, args, , |
3446
|
561 "-*- texinfo -*-\n\ |
|
562 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ |
|
563 Convert special characters in @var{string} to their escaped forms.\n\ |
|
564 @end deftypefn") |
3103
|
565 { |
|
566 octave_value retval; |
|
567 |
|
568 int nargin = args.length (); |
|
569 |
|
570 if (nargin == 1) |
|
571 { |
|
572 if (args(0).is_string ()) |
|
573 retval = do_string_escapes (args(0).string_value ()); |
|
574 else |
|
575 error ("do_string_escapes: argument must be a string"); |
|
576 } |
|
577 else |
|
578 print_usage ("do_string_escapes"); |
|
579 |
|
580 return retval; |
|
581 } |
|
582 |
1755
|
583 const char * |
801
|
584 undo_string_escape (char c) |
|
585 { |
|
586 if (! c) |
1755
|
587 return ""; |
801
|
588 |
|
589 switch (c) |
|
590 { |
3893
|
591 case '\0': |
|
592 return "\\0"; |
|
593 |
801
|
594 case '\a': |
|
595 return "\\a"; |
|
596 |
|
597 case '\b': // backspace |
|
598 return "\\b"; |
|
599 |
|
600 case '\f': // formfeed |
|
601 return "\\f"; |
|
602 |
|
603 case '\n': // newline |
|
604 return "\\n"; |
|
605 |
|
606 case '\r': // carriage return |
|
607 return "\\r"; |
|
608 |
|
609 case '\t': // horizontal tab |
|
610 return "\\t"; |
|
611 |
|
612 case '\v': // vertical tab |
|
613 return "\\v"; |
|
614 |
|
615 case '\\': // backslash |
|
616 return "\\\\"; |
|
617 |
|
618 case '"': // double quote |
|
619 return "\\\""; |
|
620 |
|
621 default: |
1755
|
622 { |
|
623 static char retval[2]; |
|
624 retval[0] = c; |
|
625 retval[1] = '\0'; |
|
626 return retval; |
|
627 } |
801
|
628 } |
|
629 } |
|
630 |
3536
|
631 std::string |
3523
|
632 undo_string_escapes (const std::string& s) |
801
|
633 { |
3523
|
634 std::string retval; |
801
|
635 |
1755
|
636 for (size_t i = 0; i < s.length (); i++) |
|
637 retval.append (undo_string_escape (s[i])); |
801
|
638 |
1755
|
639 return retval; |
801
|
640 } |
|
641 |
1957
|
642 DEFUN (undo_string_escapes, args, , |
3361
|
643 "-*- texinfo -*-\n\ |
|
644 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ |
|
645 Converts special characters in strings back to their escaped forms. For\n\ |
|
646 example, the expression\n\ |
|
647 \n\ |
|
648 @example\n\ |
|
649 bell = \"\\a\";\n\ |
|
650 @end example\n\ |
|
651 \n\ |
|
652 @noindent\n\ |
|
653 assigns the value of the alert character (control-g, ASCII code 7) to\n\ |
|
654 the string variable @code{bell}. If this string is printed, the\n\ |
|
655 system will ring the terminal bell (if it is possible). This is\n\ |
|
656 normally the desired outcome. However, sometimes it is useful to be\n\ |
|
657 able to print the original representation of the string, with the\n\ |
|
658 special characters replaced by their escape sequences. For example,\n\ |
|
659 \n\ |
|
660 @example\n\ |
|
661 octave:13> undo_string_escapes (bell)\n\ |
|
662 ans = \\a\n\ |
|
663 @end example\n\ |
|
664 \n\ |
|
665 @noindent\n\ |
|
666 replaces the unprintable alert character with its printable\n\ |
|
667 representation.\n\ |
|
668 @end deftypefn") |
801
|
669 { |
2086
|
670 octave_value retval; |
801
|
671 |
|
672 int nargin = args.length (); |
|
673 |
3103
|
674 if (nargin == 1) |
|
675 { |
|
676 if (args(0).is_string ()) |
|
677 retval = undo_string_escapes (args(0).string_value ()); |
|
678 else |
|
679 error ("undo_string_escapes: argument must be a string"); |
|
680 } |
801
|
681 else |
1023
|
682 print_usage ("undo_string_escapes"); |
801
|
683 |
|
684 return retval; |
|
685 } |
|
686 |
4243
|
687 DEFUN (find_first_of_in_loadpath, args, , "") |
|
688 { |
|
689 octave_value retval; |
|
690 |
|
691 if (args.length () == 1) |
|
692 { |
|
693 string_vector names = args(0).all_strings (); |
|
694 |
|
695 if (! error_state) |
|
696 retval = Vload_path_dir_path.find_all_first_of (names); |
|
697 } |
|
698 else |
|
699 print_usage ("find_first_of_in_loadpath"); |
|
700 |
|
701 return retval; |
|
702 } |
|
703 |
|
704 |
3879
|
705 // #if 0 |
3716
|
706 |
|
707 // Octave could use some way to access the value of ERRNO, but this is |
|
708 // probably not the best interface, so don't depend on it... |
|
709 |
|
710 DEFUN (ERRNO, args, , |
|
711 "-*- texinfo -*-\n\ |
|
712 @deftypefn {Built-in Function} {@var{system_error_number}} errno ()\n\ |
|
713 Return the current value of the system-dependent variable errno.\n\ |
|
714 @end deftypefn") |
|
715 { |
|
716 octave_value retval; |
|
717 |
|
718 if (args.length () == 0) |
4254
|
719 retval = errno; |
3716
|
720 else |
|
721 print_usage ("errno"); |
|
722 |
|
723 return retval; |
|
724 } |
|
725 |
3879
|
726 // #endif |
3716
|
727 |
2285
|
728 static void |
3523
|
729 warn_old_style_preference (bool val, const std::string& sval) |
2285
|
730 { |
|
731 warning |
|
732 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
733 sval.c_str (), (val ? 1 : 0)); |
|
734 } |
|
735 |
2204
|
736 // Check the value of a string variable to see if it it's ok to do |
|
737 // something. |
|
738 // |
|
739 // return of 1 => always ok. |
|
740 // return of 0 => never ok. |
|
741 // return of -1 => ok, but give me warning (default). |
|
742 |
|
743 int |
3523
|
744 check_preference (const std::string& var) |
2204
|
745 { |
|
746 int pref = -1; |
|
747 |
3523
|
748 std::string val = builtin_string_variable (var); |
2204
|
749 |
|
750 if (val.empty ()) |
|
751 { |
|
752 double dval = 0; |
|
753 if (builtin_real_scalar_variable (var, dval)) |
|
754 pref = NINT (dval); |
|
755 } |
|
756 else |
|
757 { |
3565
|
758 if (val == "yes" || val == "true") |
2285
|
759 { |
|
760 warn_old_style_preference (true, val); |
|
761 pref = 1; |
|
762 } |
3565
|
763 else if (val == "never" || val == "no" || val == "false") |
2285
|
764 { |
|
765 warn_old_style_preference (false, val); |
|
766 pref = 0; |
|
767 } |
2204
|
768 } |
|
769 |
|
770 return pref; |
|
771 } |
|
772 |
3354
|
773 static void |
|
774 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
775 { |
|
776 if (nr < 0 || nc < 0) |
|
777 { |
|
778 if (Vtreat_neg_dim_as_zero) |
|
779 { |
|
780 nr = (nr < 0) ? 0 : nr; |
|
781 nc = (nc < 0) ? 0 : nc; |
|
782 |
|
783 if (Vtreat_neg_dim_as_zero < 0) |
|
784 warning ("%s: converting negative dimension to zero", |
|
785 warnfor); |
|
786 } |
|
787 else |
|
788 error ("%s: can't create a matrix with negative dimensions", |
|
789 warnfor); |
|
790 } |
|
791 } |
|
792 |
|
793 void |
|
794 get_dimensions (const octave_value& a, const char *warn_for, |
|
795 int& nr, int& nc) |
|
796 { |
|
797 if (a.is_scalar_type ()) |
|
798 { |
|
799 nr = nc = a.nint_value (); |
|
800 } |
|
801 else |
|
802 { |
|
803 nr = a.rows (); |
|
804 nc = a.columns (); |
|
805 |
|
806 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
807 { |
3419
|
808 Array<double> v = a.vector_value (); |
3354
|
809 |
|
810 if (error_state) |
|
811 return; |
|
812 |
|
813 nr = NINT (v (0)); |
|
814 nc = NINT (v (1)); |
|
815 } |
|
816 else |
|
817 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
818 } |
|
819 |
|
820 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
821 } |
|
822 |
|
823 void |
|
824 get_dimensions (const octave_value& a, const octave_value& b, |
|
825 const char *warn_for, int& nr, int& nc) |
|
826 { |
|
827 nr = a.is_empty () ? 0 : a.nint_value (); |
|
828 nc = b.is_empty () ? 0 : b.nint_value (); |
|
829 |
|
830 if (error_state) |
|
831 error ("%s: expecting two scalar arguments", warn_for); |
|
832 else |
|
833 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
834 } |
|
835 |
3620
|
836 extern int |
3622
|
837 octave_format (std::ostream& os, const char *fmt, ...) |
3620
|
838 { |
|
839 int retval = -1; |
|
840 |
|
841 va_list args; |
|
842 va_start (args, fmt); |
|
843 |
|
844 retval = octave_vformat (os, fmt, args); |
|
845 |
|
846 va_end (args); |
|
847 |
|
848 return retval; |
|
849 } |
|
850 |
|
851 extern int |
3622
|
852 octave_vformat (std::ostream& os, const char *fmt, va_list args) |
3620
|
853 { |
|
854 int retval = -1; |
|
855 |
3775
|
856 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY |
3620
|
857 |
4135
|
858 std::streambuf *sb = os.rdbuf (); |
|
859 |
|
860 if (sb) |
|
861 retval = sb->vform (fmt, args); |
3620
|
862 |
|
863 #else |
|
864 |
|
865 char *s = octave_vsnprintf (fmt, args); |
|
866 |
|
867 if (s) |
|
868 { |
|
869 os << s; |
|
870 |
|
871 retval = strlen (s); |
|
872 } |
|
873 |
|
874 #endif |
|
875 |
|
876 return retval; |
|
877 } |
|
878 |
4086
|
879 void |
|
880 octave_sleep (double seconds) |
|
881 { |
|
882 if (seconds > 0) |
|
883 { |
|
884 double t; |
|
885 |
4093
|
886 unsigned int usec |
|
887 = static_cast <unsigned int> (modf (seconds, &t) * 1000000); |
4086
|
888 |
4093
|
889 unsigned int sec |
|
890 = (t > UINT_MAX) ? UINT_MAX : static_cast <unsigned int> (t); |
4086
|
891 |
|
892 octave_sleep (sec); |
|
893 octave_usleep (usec); |
|
894 } |
|
895 } |
|
896 |
3354
|
897 static int |
|
898 treat_neg_dim_as_zero (void) |
|
899 { |
|
900 Vtreat_neg_dim_as_zero = check_preference ("treat_neg_dim_as_zero"); |
|
901 |
|
902 return 0; |
|
903 } |
|
904 |
|
905 void |
|
906 symbols_of_utils (void) |
|
907 { |
4233
|
908 DEFVAR (treat_neg_dim_as_zero, false, treat_neg_dim_as_zero, |
3369
|
909 "-*- texinfo -*-\n\ |
|
910 @defvr {Built-in Variable} treat_neg_dim_as_zero\n\ |
|
911 If the value of @code{treat_neg_dim_as_zero} is nonzero, expressions\n\ |
|
912 like\n\ |
|
913 \n\ |
|
914 @example\n\ |
|
915 eye (-1)\n\ |
|
916 @end example\n\ |
|
917 \n\ |
|
918 @noindent\n\ |
|
919 produce an empty matrix (i.e., row and column dimensions are zero).\n\ |
|
920 Otherwise, an error message is printed and control is returned to the\n\ |
|
921 top level. The default value is 0.\n\ |
|
922 @end defvr"); |
3354
|
923 } |
|
924 |
572
|
925 /* |
1
|
926 ;;; Local Variables: *** |
|
927 ;;; mode: C++ *** |
|
928 ;;; End: *** |
|
929 */ |