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