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 |
3523
|
234 search_path_for_file (const std::string& path, const std::string& name) |
686
|
235 { |
3174
|
236 dir_path p (path); |
686
|
237 |
2926
|
238 return octave_env::make_absolute (p.find (name), octave_env::getcwd ()); |
686
|
239 } |
|
240 |
4216
|
241 // Find all locations of the given file in the path. |
|
242 |
|
243 string_vector |
|
244 search_path_for_all_files (const std::string& path, const std::string& name) |
|
245 { |
|
246 dir_path p (path); |
|
247 |
|
248 string_vector sv = p.find_all (name); |
|
249 |
|
250 int len = sv.length (); |
|
251 |
|
252 for (int i = 0; i < len; i++) |
|
253 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); |
|
254 |
|
255 return sv; |
|
256 } |
|
257 |
|
258 static string_vector |
|
259 make_absolute (const string_vector& sv) |
|
260 { |
|
261 int len = sv.length (); |
|
262 |
|
263 for (int i = 0; i < len; i++) |
|
264 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); |
|
265 |
|
266 return sv; |
|
267 } |
|
268 |
3195
|
269 DEFUN (file_in_loadpath, args, , |
3446
|
270 "-*- texinfo -*-\n\ |
4216
|
271 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\ |
|
272 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\ |
3195
|
273 \n\ |
4216
|
274 Return the absolute name name of @var{file} if it can be found in\n\ |
|
275 the list of directories specified by @code{LOADPATH}.\n\ |
|
276 If no file is found, return an empty matrix.\n\ |
|
277 \n\ |
|
278 If the second optional argument @code{\"all\"} is supplied, return\n\ |
|
279 a cell array containing the list of all files that have the same\n\ |
|
280 name in the path. If no files are found, return an empty cell array.\n\ |
3446
|
281 @end deftypefn\n\ |
|
282 @seealso{file_in_path}") |
3195
|
283 { |
4216
|
284 octave_value retval; |
3195
|
285 |
|
286 int argc = args.length () + 1; |
|
287 |
|
288 string_vector argv = args.make_argv ("file_in_loadpath"); |
|
289 |
|
290 if (error_state) |
|
291 return retval; |
|
292 |
3225
|
293 if (argc == 2) |
4216
|
294 { |
|
295 std::string fname |
|
296 = octave_env::make_absolute (Vload_path_dir_path.find (argv[1]), |
|
297 octave_env::getcwd ()); |
|
298 if (fname.empty ()) |
|
299 retval = Matrix (); |
|
300 else |
|
301 retval = fname; |
|
302 } |
|
303 else if (argc == 3 && argv[2] == "all") |
|
304 retval = Cell (make_absolute (Vload_path_dir_path.find_all (argv[1]))); |
3195
|
305 else |
|
306 print_usage ("file_in_loadpath"); |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
1957
|
311 DEFUN (file_in_path, args, , |
3301
|
312 "-*- texinfo -*-\n\ |
|
313 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ |
4216
|
314 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\ |
3301
|
315 Return the absolute name name of @var{file} if it can be found in\n\ |
|
316 @var{path}. The value of @var{path} should be a colon-separated list of\n\ |
|
317 directories in the format described for the built-in variable\n\ |
4216
|
318 @code{LOADPATH}. If no file is found, return an empty matrix.\n\ |
3301
|
319 For example,\n\ |
|
320 \n\ |
|
321 @example\n\ |
|
322 file_in_path (LOADPATH, \"nargchk.m\")\n\ |
|
323 @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\ |
|
324 @end example\n\ |
4216
|
325 \n\ |
|
326 If the third optional argument @code{\"all\"} is supplied, return\n\ |
|
327 a cell array containing the list of all files that have the same\n\ |
|
328 name in the path. If no files are found, return an empty cell array.\n\ |
3301
|
329 @end deftypefn") |
686
|
330 { |
4216
|
331 octave_value retval; |
686
|
332 |
1755
|
333 int argc = args.length () + 1; |
|
334 |
1968
|
335 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
336 |
|
337 if (error_state) |
|
338 return retval; |
686
|
339 |
|
340 if (argc == 3) |
|
341 { |
3523
|
342 std::string fname = search_path_for_file (argv[1], argv[2]); |
686
|
343 |
1755
|
344 if (fname.empty ()) |
|
345 retval = Matrix (); |
|
346 else |
686
|
347 retval = fname; |
|
348 } |
4216
|
349 else if (argc == 4 && argv[3] == "all") |
|
350 retval = Cell (make_absolute (search_path_for_all_files (argv[1], argv[2]))); |
686
|
351 else |
|
352 print_usage ("file_in_path"); |
|
353 |
|
354 return retval; |
|
355 } |
|
356 |
3536
|
357 std::string |
3523
|
358 file_in_path (const std::string& name, const std::string& suffix) |
526
|
359 { |
3523
|
360 std::string nm = name; |
526
|
361 |
1755
|
362 if (! suffix.empty ()) |
|
363 nm.append (suffix); |
686
|
364 |
3174
|
365 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
366 octave_env::getcwd ()); |
526
|
367 } |
|
368 |
581
|
369 // See if there is an function file in the path. If so, return the |
|
370 // full path to the file. |
|
371 |
3536
|
372 std::string |
3523
|
373 fcn_file_in_path (const std::string& name) |
526
|
374 { |
3523
|
375 std::string retval; |
908
|
376 |
1755
|
377 int len = name.length (); |
|
378 |
|
379 if (len > 0) |
|
380 { |
|
381 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
382 retval = file_in_path (name, ""); |
908
|
383 else |
1755
|
384 retval = file_in_path (name, ".m"); |
908
|
385 } |
1755
|
386 |
|
387 return retval; |
526
|
388 } |
|
389 |
581
|
390 // See if there is an octave file in the path. If so, return the |
|
391 // full path to the file. |
|
392 |
3536
|
393 std::string |
3523
|
394 oct_file_in_path (const std::string& name) |
572
|
395 { |
3523
|
396 std::string retval; |
908
|
397 |
1755
|
398 int len = name.length (); |
|
399 |
|
400 if (len > 0) |
|
401 { |
|
402 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
403 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
404 retval = file_in_path (name, ""); |
908
|
405 else |
1755
|
406 retval = file_in_path (name, ".oct"); |
908
|
407 } |
1755
|
408 |
|
409 return retval; |
572
|
410 } |
|
411 |
3103
|
412 // Replace backslash escapes in a string with the real values. |
|
413 |
3536
|
414 std::string |
3523
|
415 do_string_escapes (const std::string& s) |
3103
|
416 { |
3523
|
417 std::string retval; |
3103
|
418 |
|
419 size_t i = 0; |
|
420 size_t j = 0; |
|
421 size_t len = s.length (); |
|
422 |
|
423 retval.resize (len); |
|
424 |
|
425 while (j < len) |
|
426 { |
|
427 if (s[j] == '\\' && j+1 < len) |
|
428 { |
|
429 switch (s[++j]) |
|
430 { |
3893
|
431 case '0': |
|
432 retval[i] = '\0'; |
|
433 break; |
|
434 |
3103
|
435 case 'a': |
|
436 retval[i] = '\a'; |
|
437 break; |
|
438 |
|
439 case 'b': // backspace |
|
440 retval[i] = '\b'; |
|
441 break; |
|
442 |
|
443 case 'f': // formfeed |
|
444 retval[i] = '\f'; |
|
445 break; |
|
446 |
|
447 case 'n': // newline |
|
448 retval[i] = '\n'; |
|
449 break; |
|
450 |
|
451 case 'r': // carriage return |
|
452 retval[i] = '\r'; |
|
453 break; |
|
454 |
|
455 case 't': // horizontal tab |
|
456 retval[i] = '\t'; |
|
457 break; |
|
458 |
|
459 case 'v': // vertical tab |
|
460 retval[i] = '\v'; |
|
461 break; |
|
462 |
|
463 case '\\': // backslash |
|
464 retval[i] = '\\'; |
|
465 break; |
|
466 |
|
467 case '\'': // quote |
|
468 retval[i] = '\''; |
|
469 break; |
|
470 |
|
471 case '"': // double quote |
|
472 retval[i] = '"'; |
|
473 break; |
|
474 |
|
475 default: |
|
476 warning ("unrecognized escape sequence `\\%c' --\ |
|
477 converting to `%c'", s[j], s[j]); |
|
478 retval[i] = s[j]; |
|
479 break; |
|
480 } |
|
481 } |
|
482 else |
|
483 { |
|
484 retval[i] = s[j]; |
|
485 } |
|
486 |
|
487 i++; |
|
488 j++; |
|
489 } |
|
490 |
3105
|
491 retval.resize (i); |
3103
|
492 |
|
493 return retval; |
|
494 } |
|
495 |
|
496 DEFUN (do_string_escapes, args, , |
3446
|
497 "-*- texinfo -*-\n\ |
|
498 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ |
|
499 Convert special characters in @var{string} to their escaped forms.\n\ |
|
500 @end deftypefn") |
3103
|
501 { |
|
502 octave_value retval; |
|
503 |
|
504 int nargin = args.length (); |
|
505 |
|
506 if (nargin == 1) |
|
507 { |
|
508 if (args(0).is_string ()) |
|
509 retval = do_string_escapes (args(0).string_value ()); |
|
510 else |
|
511 error ("do_string_escapes: argument must be a string"); |
|
512 } |
|
513 else |
|
514 print_usage ("do_string_escapes"); |
|
515 |
|
516 return retval; |
|
517 } |
|
518 |
1755
|
519 const char * |
801
|
520 undo_string_escape (char c) |
|
521 { |
|
522 if (! c) |
1755
|
523 return ""; |
801
|
524 |
|
525 switch (c) |
|
526 { |
3893
|
527 case '\0': |
|
528 return "\\0"; |
|
529 |
801
|
530 case '\a': |
|
531 return "\\a"; |
|
532 |
|
533 case '\b': // backspace |
|
534 return "\\b"; |
|
535 |
|
536 case '\f': // formfeed |
|
537 return "\\f"; |
|
538 |
|
539 case '\n': // newline |
|
540 return "\\n"; |
|
541 |
|
542 case '\r': // carriage return |
|
543 return "\\r"; |
|
544 |
|
545 case '\t': // horizontal tab |
|
546 return "\\t"; |
|
547 |
|
548 case '\v': // vertical tab |
|
549 return "\\v"; |
|
550 |
|
551 case '\\': // backslash |
|
552 return "\\\\"; |
|
553 |
|
554 case '"': // double quote |
|
555 return "\\\""; |
|
556 |
|
557 default: |
1755
|
558 { |
|
559 static char retval[2]; |
|
560 retval[0] = c; |
|
561 retval[1] = '\0'; |
|
562 return retval; |
|
563 } |
801
|
564 } |
|
565 } |
|
566 |
3536
|
567 std::string |
3523
|
568 undo_string_escapes (const std::string& s) |
801
|
569 { |
3523
|
570 std::string retval; |
801
|
571 |
1755
|
572 for (size_t i = 0; i < s.length (); i++) |
|
573 retval.append (undo_string_escape (s[i])); |
801
|
574 |
1755
|
575 return retval; |
801
|
576 } |
|
577 |
1957
|
578 DEFUN (undo_string_escapes, args, , |
3361
|
579 "-*- texinfo -*-\n\ |
|
580 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ |
|
581 Converts special characters in strings back to their escaped forms. For\n\ |
|
582 example, the expression\n\ |
|
583 \n\ |
|
584 @example\n\ |
|
585 bell = \"\\a\";\n\ |
|
586 @end example\n\ |
|
587 \n\ |
|
588 @noindent\n\ |
|
589 assigns the value of the alert character (control-g, ASCII code 7) to\n\ |
|
590 the string variable @code{bell}. If this string is printed, the\n\ |
|
591 system will ring the terminal bell (if it is possible). This is\n\ |
|
592 normally the desired outcome. However, sometimes it is useful to be\n\ |
|
593 able to print the original representation of the string, with the\n\ |
|
594 special characters replaced by their escape sequences. For example,\n\ |
|
595 \n\ |
|
596 @example\n\ |
|
597 octave:13> undo_string_escapes (bell)\n\ |
|
598 ans = \\a\n\ |
|
599 @end example\n\ |
|
600 \n\ |
|
601 @noindent\n\ |
|
602 replaces the unprintable alert character with its printable\n\ |
|
603 representation.\n\ |
|
604 @end deftypefn") |
801
|
605 { |
2086
|
606 octave_value retval; |
801
|
607 |
|
608 int nargin = args.length (); |
|
609 |
3103
|
610 if (nargin == 1) |
|
611 { |
|
612 if (args(0).is_string ()) |
|
613 retval = undo_string_escapes (args(0).string_value ()); |
|
614 else |
|
615 error ("undo_string_escapes: argument must be a string"); |
|
616 } |
801
|
617 else |
1023
|
618 print_usage ("undo_string_escapes"); |
801
|
619 |
|
620 return retval; |
|
621 } |
|
622 |
3879
|
623 // #if 0 |
3716
|
624 |
|
625 // Octave could use some way to access the value of ERRNO, but this is |
|
626 // probably not the best interface, so don't depend on it... |
|
627 |
|
628 DEFUN (ERRNO, args, , |
|
629 "-*- texinfo -*-\n\ |
|
630 @deftypefn {Built-in Function} {@var{system_error_number}} errno ()\n\ |
|
631 Return the current value of the system-dependent variable errno.\n\ |
|
632 @end deftypefn") |
|
633 { |
|
634 octave_value retval; |
|
635 |
|
636 if (args.length () == 0) |
|
637 retval = static_cast<double> (errno); |
|
638 else |
|
639 print_usage ("errno"); |
|
640 |
|
641 return retval; |
|
642 } |
|
643 |
3879
|
644 // #endif |
3716
|
645 |
2285
|
646 static void |
3523
|
647 warn_old_style_preference (bool val, const std::string& sval) |
2285
|
648 { |
|
649 warning |
|
650 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
651 sval.c_str (), (val ? 1 : 0)); |
|
652 } |
|
653 |
2204
|
654 // Check the value of a string variable to see if it it's ok to do |
|
655 // something. |
|
656 // |
|
657 // return of 1 => always ok. |
|
658 // return of 0 => never ok. |
|
659 // return of -1 => ok, but give me warning (default). |
|
660 |
|
661 int |
3523
|
662 check_preference (const std::string& var) |
2204
|
663 { |
|
664 int pref = -1; |
|
665 |
3523
|
666 std::string val = builtin_string_variable (var); |
2204
|
667 |
|
668 if (val.empty ()) |
|
669 { |
|
670 double dval = 0; |
|
671 if (builtin_real_scalar_variable (var, dval)) |
|
672 pref = NINT (dval); |
|
673 } |
|
674 else |
|
675 { |
3565
|
676 if (val == "yes" || val == "true") |
2285
|
677 { |
|
678 warn_old_style_preference (true, val); |
|
679 pref = 1; |
|
680 } |
3565
|
681 else if (val == "never" || val == "no" || val == "false") |
2285
|
682 { |
|
683 warn_old_style_preference (false, val); |
|
684 pref = 0; |
|
685 } |
2204
|
686 } |
|
687 |
|
688 return pref; |
|
689 } |
|
690 |
3354
|
691 static void |
|
692 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
693 { |
|
694 if (nr < 0 || nc < 0) |
|
695 { |
|
696 if (Vtreat_neg_dim_as_zero) |
|
697 { |
|
698 nr = (nr < 0) ? 0 : nr; |
|
699 nc = (nc < 0) ? 0 : nc; |
|
700 |
|
701 if (Vtreat_neg_dim_as_zero < 0) |
|
702 warning ("%s: converting negative dimension to zero", |
|
703 warnfor); |
|
704 } |
|
705 else |
|
706 error ("%s: can't create a matrix with negative dimensions", |
|
707 warnfor); |
|
708 } |
|
709 } |
|
710 |
|
711 void |
|
712 get_dimensions (const octave_value& a, const char *warn_for, |
|
713 int& nr, int& nc) |
|
714 { |
|
715 if (a.is_scalar_type ()) |
|
716 { |
|
717 nr = nc = a.nint_value (); |
|
718 } |
|
719 else |
|
720 { |
|
721 nr = a.rows (); |
|
722 nc = a.columns (); |
|
723 |
|
724 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
725 { |
3419
|
726 Array<double> v = a.vector_value (); |
3354
|
727 |
|
728 if (error_state) |
|
729 return; |
|
730 |
|
731 nr = NINT (v (0)); |
|
732 nc = NINT (v (1)); |
|
733 } |
|
734 else |
|
735 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
736 } |
|
737 |
|
738 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
739 } |
|
740 |
|
741 void |
|
742 get_dimensions (const octave_value& a, const octave_value& b, |
|
743 const char *warn_for, int& nr, int& nc) |
|
744 { |
|
745 nr = a.is_empty () ? 0 : a.nint_value (); |
|
746 nc = b.is_empty () ? 0 : b.nint_value (); |
|
747 |
|
748 if (error_state) |
|
749 error ("%s: expecting two scalar arguments", warn_for); |
|
750 else |
|
751 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
752 } |
|
753 |
3620
|
754 extern int |
3622
|
755 octave_format (std::ostream& os, const char *fmt, ...) |
3620
|
756 { |
|
757 int retval = -1; |
|
758 |
|
759 va_list args; |
|
760 va_start (args, fmt); |
|
761 |
|
762 retval = octave_vformat (os, fmt, args); |
|
763 |
|
764 va_end (args); |
|
765 |
|
766 return retval; |
|
767 } |
|
768 |
|
769 extern int |
3622
|
770 octave_vformat (std::ostream& os, const char *fmt, va_list args) |
3620
|
771 { |
|
772 int retval = -1; |
|
773 |
3775
|
774 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY |
3620
|
775 |
4135
|
776 std::streambuf *sb = os.rdbuf (); |
|
777 |
|
778 if (sb) |
|
779 retval = sb->vform (fmt, args); |
3620
|
780 |
|
781 #else |
|
782 |
|
783 char *s = octave_vsnprintf (fmt, args); |
|
784 |
|
785 if (s) |
|
786 { |
|
787 os << s; |
|
788 |
|
789 retval = strlen (s); |
|
790 } |
|
791 |
|
792 #endif |
|
793 |
|
794 return retval; |
|
795 } |
|
796 |
4086
|
797 void |
|
798 octave_sleep (double seconds) |
|
799 { |
|
800 if (seconds > 0) |
|
801 { |
|
802 double t; |
|
803 |
4093
|
804 unsigned int usec |
|
805 = static_cast <unsigned int> (modf (seconds, &t) * 1000000); |
4086
|
806 |
4093
|
807 unsigned int sec |
|
808 = (t > UINT_MAX) ? UINT_MAX : static_cast <unsigned int> (t); |
4086
|
809 |
|
810 octave_sleep (sec); |
|
811 octave_usleep (usec); |
|
812 } |
|
813 } |
|
814 |
3354
|
815 static int |
|
816 treat_neg_dim_as_zero (void) |
|
817 { |
|
818 Vtreat_neg_dim_as_zero = check_preference ("treat_neg_dim_as_zero"); |
|
819 |
|
820 return 0; |
|
821 } |
|
822 |
|
823 void |
|
824 symbols_of_utils (void) |
|
825 { |
4233
|
826 DEFVAR (treat_neg_dim_as_zero, false, treat_neg_dim_as_zero, |
3369
|
827 "-*- texinfo -*-\n\ |
|
828 @defvr {Built-in Variable} treat_neg_dim_as_zero\n\ |
|
829 If the value of @code{treat_neg_dim_as_zero} is nonzero, expressions\n\ |
|
830 like\n\ |
|
831 \n\ |
|
832 @example\n\ |
|
833 eye (-1)\n\ |
|
834 @end example\n\ |
|
835 \n\ |
|
836 @noindent\n\ |
|
837 produce an empty matrix (i.e., row and column dimensions are zero).\n\ |
|
838 Otherwise, an error message is printed and control is returned to the\n\ |
|
839 top level. The default value is 0.\n\ |
|
840 @end defvr"); |
3354
|
841 } |
|
842 |
572
|
843 /* |
1
|
844 ;;; Local Variables: *** |
|
845 ;;; mode: C++ *** |
|
846 ;;; End: *** |
|
847 */ |