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