2117
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2117
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3268
|
27 #include <cassert> |
3633
|
28 #include <cstdarg> |
2215
|
29 #include <cstring> |
|
30 |
3503
|
31 #include <iomanip> |
|
32 #include <strstream> |
3559
|
33 #include <fstream> |
3535
|
34 #include <string> |
2117
|
35 |
|
36 #include "lo-ieee.h" |
|
37 #include "lo-mappers.h" |
|
38 #include "lo-utils.h" |
|
39 #include "str-vec.h" |
|
40 |
|
41 #include "error.h" |
3342
|
42 #include "input.h" |
2117
|
43 #include "oct-stream.h" |
2877
|
44 #include "oct-obj.h" |
2117
|
45 #include "utils.h" |
|
46 |
3633
|
47 static istream& |
|
48 octave_scan (istream& is, const char *fmt, ...) |
|
49 { |
|
50 #if defined (__GNUG__) |
|
51 |
|
52 va_list args; |
|
53 va_start (args, fmt); |
|
54 |
|
55 is.vscan (fmt, args); |
|
56 |
|
57 va_end (args); |
|
58 |
|
59 #endif |
|
60 |
|
61 return is; |
|
62 } |
|
63 |
2117
|
64 // Possible values for conv_err: |
|
65 // |
|
66 // 1 : not a real scalar |
2902
|
67 // 2 : value is NaN |
|
68 // 3 : value is not an integer |
2117
|
69 |
|
70 static int |
|
71 convert_to_valid_int (const octave_value& tc, int& conv_err) |
|
72 { |
|
73 int retval = 0; |
|
74 |
|
75 conv_err = 0; |
|
76 |
2902
|
77 double dval = tc.double_value (); |
|
78 |
|
79 if (! error_state) |
2117
|
80 { |
2902
|
81 if (! xisnan (dval)) |
2117
|
82 { |
2902
|
83 int ival = NINT (dval); |
|
84 |
|
85 if (ival == dval) |
|
86 retval = ival; |
2117
|
87 else |
|
88 conv_err = 3; |
|
89 } |
|
90 else |
|
91 conv_err = 2; |
|
92 } |
|
93 else |
|
94 conv_err = 1; |
|
95 |
|
96 return retval; |
|
97 } |
|
98 |
|
99 static int |
|
100 get_size (double d, const char *warn_for) |
|
101 { |
|
102 int retval = -1; |
|
103 |
|
104 if (! xisnan (d)) |
|
105 { |
|
106 if (! xisinf (d)) |
|
107 { |
3268
|
108 if (d >= 0.0) |
2117
|
109 retval = NINT (d); |
|
110 else |
|
111 ::error ("%s: negative value invalid as size specification", |
|
112 warn_for); |
|
113 } |
|
114 else |
|
115 retval = -1; |
|
116 } |
|
117 else |
|
118 ::error ("%s: NaN is invalid as size specification", warn_for); |
|
119 |
|
120 return retval; |
|
121 } |
|
122 |
|
123 static void |
3268
|
124 get_size (const Matrix& size, int& nr, int& nc, bool& one_elt_size_spec, |
|
125 const char *warn_for) |
2117
|
126 { |
|
127 nr = -1; |
|
128 nc = -1; |
|
129 |
3268
|
130 one_elt_size_spec = false; |
|
131 |
2117
|
132 double dnr = -1.0; |
|
133 double dnc = -1.0; |
|
134 |
2601
|
135 int sz_nr = size.rows (); |
|
136 int sz_nc = size.cols (); |
|
137 |
|
138 if (sz_nr == 1 && sz_nc == 1) |
|
139 { |
3268
|
140 one_elt_size_spec = true; |
|
141 |
2601
|
142 dnr = size (0, 0); |
|
143 dnc = 1.0; |
|
144 } |
|
145 else if (sz_nr == 1 && sz_nc > 0) |
2117
|
146 { |
2305
|
147 dnr = size (0, 0); |
2117
|
148 |
2601
|
149 if (sz_nc == 2) |
2305
|
150 dnc = size (0, 1); |
2601
|
151 else if (sz_nc > 2) |
2117
|
152 ::error ("%s: invalid size specification", warn_for); |
|
153 } |
2601
|
154 else if (sz_nc == 1 && sz_nr > 0) |
2117
|
155 { |
2305
|
156 dnr = size (0, 0); |
2117
|
157 |
2601
|
158 if (sz_nr == 2) |
2305
|
159 dnc = size (1, 0); |
2601
|
160 else if (sz_nr > 2) |
2117
|
161 ::error ("%s: invalid size specification", warn_for); |
|
162 } |
|
163 else |
|
164 ::error ("%s: invalid size specification", warn_for); |
|
165 |
|
166 if (! error_state) |
|
167 { |
|
168 nr = get_size (dnr, warn_for); |
|
169 |
3268
|
170 if (! error_state && dnc >= 0.0) |
2117
|
171 nc = get_size (dnc, warn_for); |
|
172 } |
|
173 } |
|
174 |
3523
|
175 scanf_format_list::scanf_format_list (const std::string& s) |
2117
|
176 : nconv (0), curr_idx (0), list (16), buf (0) |
|
177 { |
|
178 int num_elts = 0; |
|
179 |
|
180 int n = s.length (); |
|
181 |
|
182 int i = 0; |
|
183 |
2215
|
184 int width = 0; |
2117
|
185 bool discard = false; |
|
186 char modifier = '\0'; |
|
187 char type = '\0'; |
|
188 |
|
189 bool have_more = true; |
|
190 |
|
191 while (i < n) |
|
192 { |
|
193 have_more = true; |
|
194 |
|
195 if (! buf) |
3523
|
196 buf = new std::ostrstream (); |
2117
|
197 |
|
198 if (s[i] == '%') |
|
199 { |
3483
|
200 // Process percent-escape conversion type. |
|
201 |
2215
|
202 process_conversion (s, i, n, width, discard, type, modifier, |
|
203 num_elts); |
2117
|
204 have_more = (buf != 0); |
|
205 } |
3483
|
206 else if (isspace (s[i])) |
2117
|
207 { |
3483
|
208 type = scanf_format_elt::whitespace_conversion; |
|
209 |
2215
|
210 width = 0; |
2117
|
211 discard = false; |
|
212 modifier = '\0'; |
3483
|
213 *buf << " "; |
|
214 |
|
215 while (++i < n && isspace (s[i])) |
|
216 /* skip whitespace */; |
|
217 |
|
218 add_elt_to_list (width, discard, type, modifier, num_elts); |
|
219 |
|
220 have_more = false; |
|
221 } |
|
222 else |
|
223 { |
|
224 type = scanf_format_elt::literal_conversion; |
|
225 |
|
226 width = 0; |
|
227 discard = false; |
|
228 modifier = '\0'; |
|
229 |
|
230 while (i < n && ! isspace (s[i]) && s[i] != '%') |
|
231 *buf << s[i++]; |
|
232 |
|
233 add_elt_to_list (width, discard, type, modifier, num_elts); |
|
234 |
|
235 have_more = false; |
2117
|
236 } |
|
237 |
|
238 if (nconv < 0) |
|
239 { |
|
240 have_more = false; |
|
241 break; |
|
242 } |
|
243 } |
|
244 |
|
245 if (have_more) |
2215
|
246 add_elt_to_list (width, discard, type, modifier, num_elts); |
2117
|
247 |
|
248 list.resize (num_elts); |
|
249 |
|
250 delete buf; |
|
251 } |
|
252 |
|
253 scanf_format_list::~scanf_format_list (void) |
|
254 { |
|
255 int n = list.length (); |
|
256 |
|
257 for (int i = 0; i < n; i++) |
|
258 { |
3340
|
259 scanf_format_elt *elt = list(i); |
2117
|
260 delete elt; |
|
261 } |
|
262 } |
|
263 |
|
264 void |
2215
|
265 scanf_format_list::add_elt_to_list (int width, bool discard, char type, |
3483
|
266 char modifier, int& num_elts, |
3523
|
267 const std::string& char_class) |
2117
|
268 { |
|
269 if (buf) |
|
270 { |
3538
|
271 *buf << std::ends; |
2117
|
272 |
|
273 char *text = buf->str (); |
|
274 |
|
275 if (text) |
|
276 { |
|
277 if (*text) |
|
278 { |
|
279 scanf_format_elt *elt |
3483
|
280 = new scanf_format_elt (text, width, discard, type, |
|
281 modifier, char_class); |
2117
|
282 |
|
283 if (num_elts == list.length ()) |
|
284 list.resize (2 * num_elts); |
|
285 |
3340
|
286 list(num_elts++) = elt; |
2117
|
287 } |
|
288 else |
|
289 delete [] text; |
|
290 } |
|
291 |
|
292 delete buf; |
|
293 buf = 0; |
|
294 } |
|
295 } |
|
296 |
3535
|
297 static std::string |
3523
|
298 expand_char_class (const std::string& s) |
3483
|
299 { |
3523
|
300 std::string retval; |
3483
|
301 |
|
302 size_t len = s.length (); |
|
303 |
|
304 size_t i = 0; |
|
305 |
|
306 while (i < len) |
|
307 { |
|
308 unsigned char c = s[i++]; |
|
309 |
|
310 if (c == '-' && i > 1 && i < len |
|
311 && (unsigned char) s[i-2] <= (unsigned char) s[i]) |
|
312 { |
|
313 // Add all characters from the range except the first (we |
|
314 // already added it below). |
|
315 |
|
316 for (c = s[i-2]+1; c < s[i]; c++) |
|
317 retval += c; |
|
318 } |
|
319 else |
|
320 { |
|
321 // Add the character to the class. Only add '-' if it is |
|
322 // the last character in the class. |
|
323 |
|
324 if (c != '-' || i == len) |
|
325 retval += c; |
|
326 } |
|
327 } |
|
328 |
|
329 return retval; |
|
330 } |
|
331 |
2117
|
332 void |
3523
|
333 scanf_format_list::process_conversion (const std::string& s, int& i, int n, |
2215
|
334 int& width, bool& discard, char& type, |
2117
|
335 char& modifier, int& num_elts) |
|
336 { |
2215
|
337 width = 0; |
2117
|
338 discard = false; |
|
339 modifier = '\0'; |
|
340 type = '\0'; |
|
341 |
|
342 *buf << s[i++]; |
|
343 |
|
344 bool have_width = false; |
|
345 |
|
346 while (i < n) |
|
347 { |
|
348 switch (s[i]) |
|
349 { |
|
350 case '*': |
|
351 if (discard) |
|
352 nconv = -1; |
|
353 else |
|
354 { |
|
355 discard = true; |
|
356 *buf << s[i++]; |
|
357 } |
|
358 break; |
|
359 |
|
360 case '0': case '1': case '2': case '3': case '4': |
|
361 case '5': case '6': case '7': case '8': case '9': |
|
362 if (have_width) |
|
363 nconv = -1; |
|
364 else |
|
365 { |
2215
|
366 char c = s[i++]; |
|
367 width = width * 10 + c - '0'; |
2117
|
368 have_width = true; |
2215
|
369 *buf << c; |
2117
|
370 while (i < n && isdigit (s[i])) |
2215
|
371 { |
|
372 c = s[i++]; |
|
373 width = width * 10 + c - '0'; |
|
374 *buf << c; |
|
375 } |
2117
|
376 } |
|
377 break; |
|
378 |
|
379 case 'h': case 'l': case 'L': |
|
380 if (modifier != '\0') |
|
381 nconv = -1; |
|
382 else |
2663
|
383 modifier = s[i++]; |
2117
|
384 break; |
|
385 |
|
386 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
387 if (modifier == 'L') |
|
388 { |
|
389 nconv = -1; |
|
390 break; |
|
391 } |
|
392 goto fini; |
|
393 |
|
394 case 'e': case 'f': case 'g': |
|
395 if (modifier == 'h') |
|
396 { |
|
397 nconv = -1; |
|
398 break; |
|
399 } |
2663
|
400 |
|
401 // No float or long double conversions, thanks. |
|
402 *buf << 'l'; |
|
403 |
2117
|
404 goto fini; |
|
405 |
|
406 case 'c': case 's': case 'p': case '%': case '[': |
|
407 if (modifier != '\0') |
|
408 { |
|
409 nconv = -1; |
|
410 break; |
|
411 } |
|
412 goto fini; |
|
413 |
|
414 fini: |
|
415 { |
2215
|
416 if (finish_conversion (s, i, n, width, discard, type, |
2117
|
417 modifier, num_elts) == 0) |
|
418 return; |
|
419 } |
|
420 break; |
|
421 |
|
422 default: |
|
423 nconv = -1; |
|
424 break; |
|
425 } |
|
426 |
|
427 if (nconv < 0) |
|
428 break; |
|
429 } |
|
430 |
|
431 nconv = -1; |
|
432 } |
|
433 |
|
434 int |
3523
|
435 scanf_format_list::finish_conversion (const std::string& s, int& i, int n, |
2215
|
436 int& width, bool discard, char& type, |
2117
|
437 char modifier, int& num_elts) |
|
438 { |
|
439 int retval = 0; |
|
440 |
3523
|
441 std::string char_class; |
3483
|
442 |
2117
|
443 if (s[i] == '%') |
|
444 *buf << s[i++]; |
|
445 else |
|
446 { |
3483
|
447 int beg_idx = -1; |
|
448 int end_idx = -1; |
|
449 |
2117
|
450 type = s[i]; |
|
451 |
|
452 if (s[i] == '[') |
|
453 { |
|
454 *buf << s[i++]; |
|
455 |
|
456 if (i < n) |
|
457 { |
3483
|
458 beg_idx = i; |
|
459 |
2117
|
460 if (s[i] == '^') |
|
461 { |
|
462 type = '^'; |
|
463 *buf << s[i++]; |
3483
|
464 |
|
465 if (i < n) |
|
466 { |
|
467 beg_idx = i; |
|
468 |
|
469 if (s[i] == ']') |
|
470 *buf << s[i++]; |
|
471 } |
2117
|
472 } |
|
473 else if (s[i] == ']') |
|
474 *buf << s[i++]; |
|
475 } |
|
476 |
|
477 while (i < n && s[i] != ']') |
|
478 *buf << s[i++]; |
|
479 |
|
480 if (i < n && s[i] == ']') |
3483
|
481 { |
|
482 end_idx = i-1; |
|
483 *buf << s[i++]; |
|
484 } |
2117
|
485 |
|
486 if (s[i-1] != ']') |
|
487 retval = nconv = -1; |
|
488 } |
|
489 else |
2215
|
490 *buf << s[i++]; |
2117
|
491 |
|
492 nconv++; |
|
493 |
|
494 if (nconv > 0) |
3483
|
495 { |
|
496 if (beg_idx >= 0 && end_idx >= 0) |
|
497 char_class = expand_char_class (s.substr (beg_idx, |
|
498 end_idx - beg_idx +1)); |
|
499 |
|
500 add_elt_to_list (width, discard, type, modifier, num_elts, |
|
501 char_class); |
|
502 } |
2117
|
503 } |
|
504 |
|
505 return retval; |
|
506 } |
|
507 |
|
508 void |
|
509 scanf_format_list::printme (void) const |
|
510 { |
|
511 int n = list.length (); |
|
512 |
|
513 for (int i = 0; i < n; i++) |
|
514 { |
3340
|
515 scanf_format_elt *elt = list(i); |
2117
|
516 |
3531
|
517 std::cerr |
|
518 << "width: " << elt->width << "\n" |
|
519 << "discard: " << elt->discard << "\n" |
|
520 << "type: "; |
3483
|
521 |
|
522 if (elt->type == scanf_format_elt::literal_conversion) |
3531
|
523 std::cerr << "literal text\n"; |
3483
|
524 else if (elt->type == scanf_format_elt::whitespace_conversion) |
3531
|
525 std::cerr << "whitespace\n"; |
3483
|
526 else |
3531
|
527 std::cerr << elt->type << "\n"; |
|
528 |
|
529 std::cerr |
|
530 << "modifier: " << elt->modifier << "\n" |
|
531 << "char_class: `" << undo_string_escapes (elt->char_class) << "'\n" |
|
532 << "text: `" << undo_string_escapes (elt->text) << "'\n\n"; |
2117
|
533 } |
|
534 } |
|
535 |
|
536 bool |
|
537 scanf_format_list::all_character_conversions (void) |
|
538 { |
|
539 int n = list.length (); |
|
540 |
|
541 if (n > 0) |
|
542 { |
|
543 for (int i = 0; i < n; i++) |
|
544 { |
3340
|
545 scanf_format_elt *elt = list(i); |
2117
|
546 |
|
547 switch (elt->type) |
|
548 { |
3483
|
549 case 'c': case 's': case '%': case '[': case '^': |
|
550 case scanf_format_elt::literal_conversion: |
|
551 case scanf_format_elt::whitespace_conversion: |
2117
|
552 break; |
|
553 |
|
554 default: |
|
555 return false; |
|
556 break; |
|
557 } |
|
558 } |
|
559 |
|
560 return true; |
|
561 } |
|
562 else |
|
563 return false; |
|
564 } |
|
565 |
|
566 bool |
|
567 scanf_format_list::all_numeric_conversions (void) |
|
568 { |
|
569 int n = list.length (); |
|
570 |
|
571 if (n > 0) |
|
572 { |
|
573 for (int i = 0; i < n; i++) |
|
574 { |
3340
|
575 scanf_format_elt *elt = list(i); |
2117
|
576 |
|
577 switch (elt->type) |
|
578 { |
|
579 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
580 case 'e': case 'f': case 'g': |
|
581 break; |
|
582 |
|
583 default: |
|
584 return false; |
|
585 break; |
|
586 } |
|
587 } |
|
588 |
|
589 return true; |
|
590 } |
|
591 else |
|
592 return false; |
|
593 } |
|
594 |
|
595 // Ugh again. |
|
596 |
3523
|
597 printf_format_list::printf_format_list (const std::string& s) |
2117
|
598 : nconv (0), curr_idx (0), list (16), buf (0) |
|
599 { |
|
600 int num_elts = 0; |
|
601 |
|
602 int n = s.length (); |
|
603 |
|
604 int i = 0; |
|
605 |
|
606 int args = 0; |
|
607 char modifier = '\0'; |
|
608 char type = '\0'; |
|
609 |
|
610 bool have_more = true; |
|
611 |
|
612 while (i < n) |
|
613 { |
|
614 have_more = true; |
|
615 |
|
616 if (! buf) |
3523
|
617 buf = new std::ostrstream (); |
2117
|
618 |
|
619 switch (s[i]) |
|
620 { |
|
621 case '%': |
|
622 process_conversion (s, i, n, args, type, modifier, num_elts); |
|
623 have_more = (buf != 0); |
|
624 break; |
|
625 |
|
626 default: |
|
627 args = 0; |
|
628 modifier = '\0'; |
|
629 type = '\0'; |
|
630 *buf << s[i++]; |
|
631 break; |
|
632 } |
|
633 |
|
634 if (nconv < 0) |
|
635 { |
|
636 have_more = false; |
|
637 break; |
|
638 } |
|
639 } |
|
640 |
|
641 if (have_more) |
|
642 add_elt_to_list (args, type, modifier, num_elts); |
|
643 |
|
644 list.resize (num_elts); |
|
645 |
|
646 delete buf; |
|
647 } |
|
648 |
|
649 printf_format_list::~printf_format_list (void) |
|
650 { |
|
651 int n = list.length (); |
|
652 |
|
653 for (int i = 0; i < n; i++) |
|
654 { |
3340
|
655 printf_format_elt *elt = list(i); |
2117
|
656 delete elt; |
|
657 } |
|
658 } |
|
659 |
|
660 void |
|
661 printf_format_list::add_elt_to_list (int args, char type, char modifier, |
|
662 int& num_elts) |
|
663 { |
|
664 if (buf) |
|
665 { |
3538
|
666 *buf << std::ends; |
2117
|
667 |
|
668 char *text = buf->str (); |
|
669 |
|
670 if (text) |
|
671 { |
|
672 if (*text) |
|
673 { |
|
674 printf_format_elt *elt |
|
675 = new printf_format_elt (text, args, type, modifier); |
|
676 |
|
677 if (num_elts == list.length ()) |
|
678 list.resize (2 * num_elts); |
|
679 |
3340
|
680 list(num_elts++) = elt; |
2117
|
681 } |
|
682 else |
|
683 delete [] text; |
|
684 } |
|
685 |
|
686 delete buf; |
|
687 buf = 0; |
|
688 } |
|
689 } |
|
690 |
|
691 void |
3523
|
692 printf_format_list::process_conversion (const std::string& s, int& i, int n, |
2117
|
693 int& args, char& modifier, |
|
694 char& type, int& num_elts) |
|
695 { |
|
696 args = 0; |
|
697 modifier = '\0'; |
|
698 type = '\0'; |
|
699 |
|
700 *buf << s[i++]; |
|
701 |
|
702 bool next = false; |
|
703 |
|
704 while (i < n) |
|
705 { |
|
706 switch (s[i]) |
|
707 { |
|
708 case '-': case '+': case ' ': case '0': case '#': |
|
709 *buf << s[i++]; |
|
710 break; |
|
711 |
|
712 default: |
|
713 next = true; |
|
714 break; |
|
715 } |
|
716 |
|
717 if (next) |
|
718 break; |
|
719 } |
|
720 |
|
721 if (i < n) |
|
722 { |
|
723 if (s[i] == '*') |
|
724 { |
|
725 args++; |
|
726 *buf << s[i++]; |
|
727 } |
|
728 else |
|
729 { |
|
730 while (i < n && isdigit (s[i])) |
|
731 *buf << s[i++]; |
|
732 } |
|
733 } |
|
734 |
|
735 if (i < n && s[i] == '.') |
|
736 { |
|
737 *buf << s[i++]; |
|
738 |
|
739 if (i < n) |
|
740 { |
|
741 if (s[i] == '*') |
|
742 { |
|
743 args++; |
|
744 *buf << s[i++]; |
|
745 } |
|
746 else |
|
747 { |
|
748 while (i < n && isdigit (s[i])) |
|
749 *buf << s[i++]; |
|
750 } |
|
751 } |
|
752 } |
|
753 |
|
754 if (i < n) |
|
755 { |
|
756 switch (s[i]) |
|
757 { |
|
758 case 'h': case 'l': case 'L': |
|
759 modifier = s[i]; |
|
760 *buf << s[i++]; |
|
761 break; |
|
762 |
|
763 default: |
|
764 break; |
|
765 } |
|
766 } |
|
767 |
|
768 if (i < n) |
|
769 finish_conversion (s, i, args, modifier, type, num_elts); |
|
770 else |
|
771 nconv = -1; |
|
772 } |
|
773 |
|
774 void |
3523
|
775 printf_format_list::finish_conversion (const std::string& s, int& i, |
2117
|
776 int args, char modifier, |
|
777 char& type, int& num_elts) |
|
778 |
|
779 { |
|
780 switch (s[i]) |
|
781 { |
|
782 case 'd': case 'i': case 'o': case 'x': case 'X': |
|
783 case 'u': case 'c': |
|
784 if (modifier == 'L') |
|
785 { |
|
786 nconv = -1; |
|
787 break; |
|
788 } |
|
789 goto fini; |
|
790 |
|
791 case 'f': case 'e': case 'E': case 'g': case 'G': |
|
792 if (modifier == 'h' || modifier == 'l') |
|
793 { |
|
794 nconv = -1; |
|
795 break; |
|
796 } |
|
797 goto fini; |
|
798 |
|
799 case 's': case 'p': case '%': |
|
800 if (modifier != '\0') |
|
801 { |
|
802 nconv = -1; |
|
803 break; |
|
804 } |
|
805 goto fini; |
|
806 |
|
807 fini: |
|
808 |
|
809 if (s[i] == '%' && args == 0) |
|
810 *buf << s[i++]; |
|
811 else |
|
812 { |
|
813 if (s[i] != '%') |
|
814 args++; |
|
815 |
|
816 type = s[i]; |
|
817 |
|
818 *buf << s[i++]; |
|
819 |
|
820 add_elt_to_list (args, type, modifier, num_elts); |
|
821 |
|
822 nconv++; |
|
823 } |
|
824 break; |
|
825 |
|
826 default: |
|
827 nconv = -1; |
|
828 break; |
|
829 } |
|
830 } |
|
831 |
|
832 void |
|
833 printf_format_list::printme (void) const |
|
834 { |
|
835 int n = list.length (); |
|
836 |
|
837 for (int i = 0; i < n; i++) |
|
838 { |
3340
|
839 printf_format_elt *elt = list(i); |
2117
|
840 |
3531
|
841 std::cerr << elt->args<< "\t" |
|
842 << elt->type << "\t" |
|
843 << elt->modifier << "\t" |
|
844 << undo_string_escapes (elt->text) << "\n"; |
2117
|
845 } |
|
846 } |
|
847 |
3145
|
848 int |
3148
|
849 octave_base_stream::file_number (void) |
3145
|
850 { |
|
851 // Kluge alert! |
|
852 |
|
853 if (name () == "stdin") |
|
854 return 0; |
|
855 |
|
856 if (name () == "stdout") |
|
857 return 1; |
|
858 |
|
859 if (name () == "stderr") |
|
860 return 2; |
|
861 |
|
862 int retval = -1; |
|
863 |
3523
|
864 std::istream *is = input_stream (); |
|
865 std::ostream *os = output_stream (); |
3145
|
866 |
3553
|
867 int i_fid = is ? ((std::filebuf *) (is->rdbuf ()))->fd () : -1; |
|
868 int o_fid = os ? ((std::filebuf *) (os->rdbuf ()))->fd () : -1; |
3145
|
869 |
|
870 if (i_fid >= 0) |
|
871 { |
|
872 if (o_fid >= 0) |
|
873 retval = (i_fid == o_fid) ? i_fid : -1; |
|
874 else |
|
875 retval = i_fid; |
|
876 } |
|
877 else if (o_fid >= 0) |
|
878 retval = o_fid; |
|
879 |
|
880 return retval; |
|
881 } |
|
882 |
2117
|
883 void |
3523
|
884 octave_base_stream::error (const std::string& msg) |
2117
|
885 { |
|
886 fail = true; |
|
887 errmsg = msg; |
|
888 } |
|
889 |
|
890 void |
|
891 octave_base_stream::clear (void) |
|
892 { |
|
893 fail = false; |
|
894 errmsg = ""; |
|
895 } |
|
896 |
|
897 // Functions that are defined for all input streams (input streams |
|
898 // are those that define is). |
|
899 |
3536
|
900 std::string |
2117
|
901 octave_base_stream::do_gets (int max_len, bool& err, |
|
902 bool strip_newline, const char *fcn) |
|
903 { |
3523
|
904 std::string retval; |
2117
|
905 |
|
906 err = false; |
|
907 |
3523
|
908 std::istream *isp = input_stream (); |
2117
|
909 |
|
910 if (isp) |
|
911 { |
3523
|
912 std::istream& is = *isp; |
2117
|
913 |
|
914 // XXX FIXME XXX -- this should probably be converted to use |
|
915 // sstream when that is available. |
3523
|
916 std::ostrstream buf; |
2117
|
917 |
|
918 int c = 0; |
3553
|
919 int char_count = 0; |
2117
|
920 int newline_stripped = 0; |
|
921 |
|
922 while (is && (c = is.get ()) != EOF) |
|
923 { |
3553
|
924 char_count++; |
2117
|
925 |
|
926 if (c == '\n') |
|
927 { |
|
928 if (! strip_newline) |
|
929 buf << (char) c; |
|
930 else |
|
931 newline_stripped = 1; |
|
932 |
|
933 break; |
|
934 } |
|
935 else |
|
936 buf << (char) c; |
|
937 |
3553
|
938 if (max_len > 0 && char_count == max_len) |
2117
|
939 break; |
|
940 } |
|
941 |
|
942 if (is.fail ()) |
|
943 { |
|
944 err = true; |
3523
|
945 std::string msg = fcn; |
2117
|
946 msg.append (": read error"); |
|
947 error (msg); |
|
948 } |
3553
|
949 else if (char_count == 0 && is.eof ()) |
2117
|
950 { |
|
951 err = true; |
3523
|
952 std::string msg = fcn; |
2117
|
953 msg.append (": at end of file"); |
|
954 error (msg); |
|
955 } |
|
956 else |
|
957 { |
3538
|
958 buf << std::ends; |
2117
|
959 char *tmp = buf.str (); |
|
960 retval = tmp; |
|
961 delete [] tmp; |
|
962 } |
|
963 } |
|
964 else |
|
965 { |
|
966 err = true; |
|
967 invalid_operation (fcn, "reading"); |
|
968 } |
|
969 |
|
970 return retval; |
|
971 } |
|
972 |
3536
|
973 std::string |
2117
|
974 octave_base_stream::getl (int max_len, bool& err) |
|
975 { |
|
976 return do_gets (max_len, err, true, "fgetl"); |
|
977 } |
|
978 |
3536
|
979 std::string |
2117
|
980 octave_base_stream::gets (int max_len, bool& err) |
|
981 { |
|
982 return do_gets (max_len, err, false, "fgets"); |
|
983 } |
|
984 |
|
985 octave_value |
2317
|
986 octave_base_stream::read (const Matrix& size, |
|
987 oct_data_conv::data_type dt, int skip, |
3553
|
988 oct_mach_info::float_format ffmt, |
|
989 int& char_count) |
2117
|
990 { |
2317
|
991 Matrix retval; |
|
992 |
3553
|
993 char_count = 0; |
2117
|
994 |
3523
|
995 std::istream *isp = input_stream (); |
2117
|
996 |
|
997 if (isp) |
|
998 { |
3523
|
999 std::istream& is = *isp; |
2117
|
1000 |
|
1001 int nr = -1; |
|
1002 int nc = -1; |
|
1003 |
3268
|
1004 bool ignore; |
|
1005 |
|
1006 get_size (size, nr, nc, ignore, "fread"); |
2117
|
1007 |
|
1008 if (! error_state) |
2317
|
1009 { |
3553
|
1010 if (ffmt == oct_mach_info::unknown) |
|
1011 ffmt = float_format (); |
|
1012 |
|
1013 int tmp = retval.read (is, nr, nc, dt, skip, ffmt); |
2317
|
1014 |
|
1015 if (tmp < 0) |
|
1016 error ("fread: read error"); |
|
1017 else |
3553
|
1018 char_count = tmp; |
2317
|
1019 } |
2117
|
1020 } |
|
1021 else |
|
1022 invalid_operation ("fread", "reading"); |
|
1023 |
|
1024 return retval; |
|
1025 } |
|
1026 |
2572
|
1027 template <class T> |
|
1028 void |
3523
|
1029 do_scanf_conv (std::istream& is, const char *fmt, T valptr, Matrix& mval, |
3559
|
1030 double *data, int& idx, int& conversion_count, int nr, |
3268
|
1031 int max_size, bool discard) |
2572
|
1032 { |
3633
|
1033 octave_scan (is, fmt, valptr); |
2572
|
1034 |
|
1035 if (is) |
|
1036 { |
|
1037 if (idx == max_size && ! discard) |
|
1038 { |
|
1039 max_size *= 2; |
|
1040 |
|
1041 if (nr > 0) |
|
1042 mval.resize (nr, max_size / nr, 0.0); |
|
1043 else |
|
1044 mval.resize (max_size, 1, 0.0); |
|
1045 |
|
1046 data = mval.fortran_vec (); |
|
1047 } |
|
1048 |
|
1049 if (! discard) |
3268
|
1050 { |
3559
|
1051 conversion_count++; |
3268
|
1052 data[idx++] = *(valptr); |
|
1053 } |
2572
|
1054 } |
|
1055 } |
|
1056 |
|
1057 template void |
3523
|
1058 do_scanf_conv (std::istream&, const char*, int*, Matrix&, double*, int&, |
3268
|
1059 int&, int, int, bool); |
2572
|
1060 |
3233
|
1061 template void |
3523
|
1062 do_scanf_conv (std::istream&, const char*, long int*, Matrix&, double*, int&, |
3268
|
1063 int&, int, int, bool); |
3233
|
1064 |
|
1065 template void |
3523
|
1066 do_scanf_conv (std::istream&, const char*, short int*, Matrix&, double*, int&, |
3268
|
1067 int&, int, int, bool); |
3233
|
1068 |
2600
|
1069 #if 0 |
2572
|
1070 template void |
3523
|
1071 do_scanf_conv (std::istream&, const char*, float*, Matrix&, double*, int&, |
3268
|
1072 int&, int, int, bool); |
2600
|
1073 #endif |
2572
|
1074 |
|
1075 template void |
3523
|
1076 do_scanf_conv (std::istream&, const char*, double*, Matrix&, double*, int&, |
3268
|
1077 int&, int, int, bool); |
2572
|
1078 |
3483
|
1079 #define DO_WHITESPACE_CONVERSION() \ |
|
1080 do \ |
|
1081 { \ |
|
1082 int c = EOF; \ |
|
1083 \ |
|
1084 while (is && (c = is.get ()) != EOF && isspace (c)) \ |
|
1085 /* skip whitespace */; \ |
|
1086 \ |
|
1087 if (c != EOF) \ |
|
1088 is.putback (c); \ |
|
1089 } \ |
|
1090 while (0) |
|
1091 |
|
1092 #define DO_LITERAL_CONVERSION() \ |
|
1093 do \ |
|
1094 { \ |
|
1095 int c = EOF; \ |
|
1096 \ |
|
1097 int n = strlen (fmt); \ |
|
1098 int i = 0; \ |
|
1099 \ |
|
1100 while (i < n && is && (c = is.get ()) != EOF) \ |
|
1101 { \ |
|
1102 if (c == fmt[i]) \ |
|
1103 { \ |
|
1104 i++; \ |
|
1105 continue; \ |
|
1106 } \ |
|
1107 else \ |
|
1108 { \ |
|
1109 is.putback (c); \ |
|
1110 break; \ |
|
1111 } \ |
|
1112 } \ |
|
1113 \ |
|
1114 if (i != n) \ |
3538
|
1115 is.setstate (std::ios::failbit); \ |
3483
|
1116 } \ |
|
1117 while (0) |
|
1118 |
|
1119 #define BEGIN_C_CONVERSION() \ |
3538
|
1120 is.unsetf (std::ios::skipws); \ |
3483
|
1121 \ |
|
1122 int width = elt->width ? elt->width : 1; \ |
|
1123 \ |
|
1124 char *tmp = new char[width + 1]; \ |
|
1125 \ |
|
1126 int c = EOF; \ |
|
1127 int n = 0; \ |
|
1128 \ |
|
1129 while (is && n < width && (c = is.get ()) != EOF) \ |
|
1130 tmp[n++] = (char) c; \ |
|
1131 \ |
|
1132 tmp[n] = '\0' |
|
1133 |
|
1134 // For a `%s' format, skip initial whitespace and then read until the |
|
1135 // next whitespace character. |
|
1136 #define BEGIN_S_CONVERSION() \ |
|
1137 int width = elt->width; \ |
|
1138 \ |
|
1139 char *tmp = 0; \ |
|
1140 \ |
|
1141 do \ |
|
1142 { \ |
|
1143 if (width) \ |
|
1144 { \ |
|
1145 tmp = new char [width+1]; \ |
|
1146 \ |
3633
|
1147 octave_scan (is, fmt, tmp); \ |
3483
|
1148 \ |
|
1149 tmp[width] = '\0'; \ |
|
1150 } \ |
|
1151 else \ |
|
1152 { \ |
3548
|
1153 std::ostrstream buf; \ |
3483
|
1154 \ |
|
1155 int c = EOF; \ |
|
1156 \ |
|
1157 while (is && (c = is.get ()) != EOF && isspace (c)) \ |
|
1158 /* skip leading whitespace */; \ |
|
1159 \ |
|
1160 if (is && c != EOF) \ |
|
1161 buf << (char) c; \ |
|
1162 \ |
|
1163 while (is && (c = is.get ()) != EOF && ! isspace (c)) \ |
|
1164 buf << (char) c; \ |
|
1165 \ |
|
1166 if (isspace (c)) \ |
|
1167 is.putback (c); \ |
|
1168 \ |
3538
|
1169 buf << std::ends; \ |
3483
|
1170 \ |
|
1171 tmp = buf.str (); \ |
|
1172 } \ |
|
1173 } \ |
|
1174 while (0) |
|
1175 |
|
1176 // This format must match a nonempty sequence of characters. |
|
1177 #define BEGIN_CHAR_CLASS_CONVERSION() \ |
|
1178 int width = elt->width; \ |
|
1179 \ |
|
1180 char *tmp = 0; \ |
|
1181 \ |
|
1182 do \ |
|
1183 { \ |
|
1184 if (width) \ |
|
1185 { \ |
|
1186 tmp = new char[width+1]; \ |
|
1187 \ |
3633
|
1188 octave_scan (is, fmt, tmp); \ |
3483
|
1189 \ |
|
1190 tmp[width] = '\0'; \ |
|
1191 } \ |
|
1192 else \ |
|
1193 { \ |
3548
|
1194 std::ostrstream buf; \ |
3483
|
1195 \ |
3523
|
1196 std::string char_class = elt->char_class; \ |
3483
|
1197 \ |
|
1198 int c = EOF; \ |
|
1199 \ |
|
1200 if (elt->type == '[') \ |
|
1201 { \ |
|
1202 while (is && (c = is.get ()) != EOF \ |
|
1203 && char_class.find (c) != NPOS) \ |
|
1204 buf << (char) c; \ |
|
1205 } \ |
|
1206 else \ |
|
1207 { \ |
|
1208 while (is && (c = is.get ()) != EOF \ |
|
1209 && char_class.find (c) == NPOS) \ |
|
1210 buf << (char) c; \ |
|
1211 } \ |
|
1212 \ |
|
1213 if (c != EOF) \ |
|
1214 is.putback (c); \ |
|
1215 \ |
3538
|
1216 buf << std::ends; \ |
3483
|
1217 \ |
|
1218 tmp = buf.str (); \ |
|
1219 \ |
|
1220 if (strlen (tmp) == 0) \ |
3538
|
1221 is.setstate (std::ios::failbit); \ |
3483
|
1222 } \ |
|
1223 } \ |
|
1224 while (0) |
|
1225 |
3410
|
1226 #define FINISH_CHARACTER_CONVERSION() \ |
|
1227 do \ |
|
1228 { \ |
|
1229 width = strlen (tmp); \ |
|
1230 \ |
|
1231 if (is) \ |
|
1232 { \ |
|
1233 int i = 0; \ |
|
1234 \ |
|
1235 if (! discard) \ |
|
1236 { \ |
|
1237 conversion_count++; \ |
|
1238 \ |
|
1239 while (i < width && tmp[i] != '\0') \ |
|
1240 { \ |
|
1241 if (data_index == max_size) \ |
|
1242 { \ |
|
1243 max_size *= 2; \ |
|
1244 \ |
|
1245 if (nr > 0) \ |
|
1246 mval.resize (nr, max_size / nr, 0.0); \ |
|
1247 else \ |
|
1248 { \ |
|
1249 if (all_char_conv && one_elt_size_spec) \ |
|
1250 mval.resize (1, max_size, 0.0); \ |
|
1251 else \ |
|
1252 mval.resize (max_size, 1, 0.0); \ |
|
1253 } \ |
|
1254 \ |
|
1255 data = mval.fortran_vec (); \ |
|
1256 } \ |
|
1257 \ |
|
1258 data[data_index++] = tmp[i++]; \ |
|
1259 } \ |
|
1260 } \ |
|
1261 } \ |
|
1262 \ |
|
1263 delete [] tmp; \ |
|
1264 } \ |
|
1265 while (0) |
2117
|
1266 |
|
1267 octave_value |
|
1268 octave_base_stream::do_scanf (scanf_format_list& fmt_list, |
3268
|
1269 int nr, int nc, bool one_elt_size_spec, |
|
1270 int& conversion_count) |
2117
|
1271 { |
3268
|
1272 conversion_count = 0; |
|
1273 |
|
1274 int data_index = 0; |
2121
|
1275 |
2117
|
1276 octave_value retval = Matrix (); |
|
1277 |
3268
|
1278 if (nr == 0 || nc == 0) |
|
1279 { |
|
1280 if (one_elt_size_spec) |
|
1281 nc = 0; |
|
1282 |
|
1283 return Matrix (nr, nc, 0.0); |
|
1284 } |
|
1285 |
3523
|
1286 std::istream *isp = input_stream (); |
2117
|
1287 |
|
1288 bool all_char_conv = fmt_list.all_character_conversions (); |
|
1289 |
|
1290 Matrix mval; |
|
1291 double *data = 0; |
|
1292 int max_size = 0; |
3268
|
1293 int max_conv = 0; |
2117
|
1294 |
|
1295 int final_nr = 0; |
|
1296 int final_nc = 0; |
|
1297 |
3268
|
1298 if (all_char_conv) |
|
1299 { |
|
1300 if (one_elt_size_spec) |
|
1301 { |
3410
|
1302 max_size = 512; |
|
1303 mval.resize (1, max_size, 0.0); |
3268
|
1304 data = mval.fortran_vec (); |
3410
|
1305 |
3268
|
1306 if (nr > 0) |
|
1307 max_conv = nr; |
|
1308 } |
|
1309 else if (nr > 0 && nc > 0) |
|
1310 { |
3410
|
1311 mval.resize (nr, nc, 0.0); |
3268
|
1312 data = mval.fortran_vec (); |
3410
|
1313 max_size = max_conv = nr * nc; |
3268
|
1314 } |
|
1315 } |
|
1316 else if (nr > 0) |
2117
|
1317 { |
|
1318 if (nc > 0) |
|
1319 { |
|
1320 mval.resize (nr, nc, 0.0); |
|
1321 data = mval.fortran_vec (); |
|
1322 max_size = nr * nc; |
3268
|
1323 |
|
1324 max_conv = max_size; |
2117
|
1325 } |
|
1326 else |
|
1327 { |
|
1328 mval.resize (nr, 32, 0.0); |
|
1329 data = mval.fortran_vec (); |
2121
|
1330 max_size = nr * 32; |
2117
|
1331 } |
|
1332 } |
|
1333 else |
|
1334 { |
|
1335 mval.resize (32, 1, 0.0); |
|
1336 data = mval.fortran_vec (); |
|
1337 max_size = 32; |
|
1338 } |
|
1339 |
|
1340 if (isp) |
|
1341 { |
3523
|
1342 std::istream& is = *isp; |
2117
|
1343 |
|
1344 const scanf_format_elt *elt = fmt_list.first (); |
|
1345 |
3538
|
1346 std::ios::fmtflags flags = is.flags (); |
2213
|
1347 |
2117
|
1348 for (;;) |
|
1349 { |
|
1350 if (elt) |
|
1351 { |
3268
|
1352 if (max_conv > 0 && conversion_count == max_conv) |
|
1353 { |
|
1354 if (all_char_conv && one_elt_size_spec) |
|
1355 { |
|
1356 final_nr = 1; |
|
1357 final_nc = data_index; |
|
1358 } |
|
1359 else |
|
1360 { |
|
1361 final_nr = nr; |
|
1362 final_nc = (data_index - 1) / nr + 1; |
|
1363 } |
|
1364 |
|
1365 break; |
|
1366 } |
|
1367 else if (data_index == max_size) |
2117
|
1368 { |
3410
|
1369 max_size *= 2; |
|
1370 |
2687
|
1371 if (nr > 0) |
3410
|
1372 mval.resize (nr, max_size / nr, 0.0); |
2687
|
1373 else |
|
1374 { |
3410
|
1375 if (all_char_conv && one_elt_size_spec) |
|
1376 mval.resize (1, max_size, 0.0); |
|
1377 else |
|
1378 mval.resize (max_size, 1, 0.0); |
2687
|
1379 } |
3410
|
1380 |
|
1381 data = mval.fortran_vec (); |
2117
|
1382 } |
|
1383 |
|
1384 const char *fmt = elt->text; |
|
1385 |
|
1386 bool discard = elt->discard; |
|
1387 |
|
1388 switch (elt->type) |
|
1389 { |
3483
|
1390 case scanf_format_elt::whitespace_conversion: |
|
1391 DO_WHITESPACE_CONVERSION (); |
|
1392 break; |
|
1393 |
|
1394 case scanf_format_elt::literal_conversion: |
|
1395 DO_LITERAL_CONVERSION (); |
|
1396 break; |
|
1397 |
2117
|
1398 case '%': |
|
1399 { |
|
1400 int dummy; |
|
1401 |
3633
|
1402 octave_scan (is, fmt, &dummy); |
2117
|
1403 } |
3483
|
1404 break; |
2117
|
1405 |
|
1406 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
1407 { |
3233
|
1408 switch (elt->modifier) |
|
1409 { |
|
1410 case 'h': |
|
1411 { |
|
1412 short int tmp; |
3268
|
1413 do_scanf_conv (is, fmt, &tmp, mval, data, |
|
1414 data_index, conversion_count, |
3233
|
1415 nr, max_size, discard); |
|
1416 } |
3483
|
1417 break; |
3233
|
1418 |
|
1419 case 'l': |
|
1420 { |
|
1421 long int tmp; |
3268
|
1422 do_scanf_conv (is, fmt, &tmp, mval, data, |
|
1423 data_index, conversion_count, |
3233
|
1424 nr, max_size, discard); |
|
1425 } |
3483
|
1426 break; |
3233
|
1427 |
|
1428 default: |
|
1429 { |
|
1430 int tmp; |
3268
|
1431 do_scanf_conv (is, fmt, &tmp, mval, data, |
|
1432 data_index, conversion_count, |
3233
|
1433 nr, max_size, discard); |
|
1434 } |
3483
|
1435 break; |
3233
|
1436 } |
2117
|
1437 } |
3483
|
1438 break; |
2117
|
1439 |
|
1440 case 'e': case 'f': case 'g': |
|
1441 { |
2600
|
1442 double tmp; |
|
1443 |
3268
|
1444 do_scanf_conv (is, fmt, &tmp, mval, data, |
|
1445 data_index, conversion_count, |
2600
|
1446 nr, max_size, discard); |
2117
|
1447 } |
3483
|
1448 break; |
2117
|
1449 |
2213
|
1450 case 'c': |
3410
|
1451 { |
3483
|
1452 BEGIN_C_CONVERSION (); |
3410
|
1453 |
|
1454 FINISH_CHARACTER_CONVERSION (); |
3483
|
1455 |
|
1456 is.setf (flags); |
3410
|
1457 } |
|
1458 break; |
2213
|
1459 |
2117
|
1460 case 's': |
|
1461 { |
3483
|
1462 BEGIN_S_CONVERSION (); |
3268
|
1463 |
3410
|
1464 FINISH_CHARACTER_CONVERSION (); |
2117
|
1465 } |
3483
|
1466 break; |
|
1467 |
|
1468 case '[': case '^': |
|
1469 { |
|
1470 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1471 |
|
1472 FINISH_CHARACTER_CONVERSION (); |
|
1473 } |
|
1474 break; |
|
1475 |
|
1476 case 'p': |
2215
|
1477 error ("fscanf: unsupported format specifier"); |
2117
|
1478 break; |
|
1479 |
|
1480 default: |
|
1481 error ("fscanf: internal format error"); |
|
1482 break; |
|
1483 } |
|
1484 |
|
1485 if (! ok ()) |
|
1486 { |
|
1487 break; |
|
1488 } |
|
1489 else if (! is) |
|
1490 { |
3268
|
1491 if (all_char_conv) |
2117
|
1492 { |
3268
|
1493 if (one_elt_size_spec) |
|
1494 { |
|
1495 final_nr = 1; |
|
1496 final_nc = data_index; |
|
1497 } |
|
1498 else if (data_index > nr) |
2117
|
1499 { |
2759
|
1500 final_nr = nr; |
3268
|
1501 final_nc = (data_index - 1) / nr + 1; |
2117
|
1502 } |
|
1503 else |
|
1504 { |
3268
|
1505 final_nr = data_index; |
|
1506 final_nc = 1; |
|
1507 } |
|
1508 } |
|
1509 else if (nr > 0) |
|
1510 { |
|
1511 if (data_index > nr) |
|
1512 { |
|
1513 final_nr = nr; |
|
1514 final_nc = (data_index - 1) / nr + 1; |
|
1515 } |
|
1516 else |
|
1517 { |
|
1518 final_nr = data_index; |
2117
|
1519 final_nc = 1; |
|
1520 } |
|
1521 } |
|
1522 else |
|
1523 { |
3268
|
1524 final_nr = data_index; |
2759
|
1525 final_nc = 1; |
|
1526 } |
|
1527 |
3337
|
1528 // If it looks like we have a matching failure, then |
|
1529 // reset the failbit in the stream state. |
|
1530 |
3538
|
1531 if (is.rdstate () & std::ios::failbit) |
|
1532 is.clear (is.rdstate () & (~std::ios::failbit)); |
3337
|
1533 |
2759
|
1534 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1535 |
|
1536 if (interactive && name () == "stdin") |
2759
|
1537 { |
|
1538 is.clear (); |
|
1539 |
|
1540 // Skip to end of line. |
|
1541 |
|
1542 bool err; |
|
1543 do_gets (-1, err, false, "fscanf"); |
2117
|
1544 } |
|
1545 |
|
1546 break; |
|
1547 } |
|
1548 } |
|
1549 else |
|
1550 { |
|
1551 error ("fscanf: internal format error"); |
|
1552 break; |
|
1553 } |
|
1554 |
|
1555 elt = fmt_list.next (); |
|
1556 } |
|
1557 } |
|
1558 |
|
1559 if (ok ()) |
|
1560 { |
2121
|
1561 mval.resize (final_nr, final_nc, 0.0); |
2117
|
1562 |
3268
|
1563 retval = mval; |
|
1564 |
2117
|
1565 if (all_char_conv) |
3268
|
1566 retval = retval.convert_to_str (); |
2117
|
1567 } |
|
1568 |
|
1569 return retval; |
|
1570 } |
|
1571 |
|
1572 octave_value |
3553
|
1573 octave_base_stream::scanf (const std::string& fmt, const Matrix& size, |
3559
|
1574 int& conversion_count) |
2117
|
1575 { |
|
1576 octave_value retval = Matrix (); |
|
1577 |
3559
|
1578 conversion_count = 0; |
2117
|
1579 |
3523
|
1580 std::istream *isp = input_stream (); |
2117
|
1581 |
|
1582 if (isp) |
|
1583 { |
3523
|
1584 std::istream& is = *isp; |
2117
|
1585 |
|
1586 scanf_format_list fmt_list (fmt); |
|
1587 |
|
1588 switch (fmt_list.num_conversions ()) |
|
1589 { |
|
1590 case -1: |
|
1591 ::error ("fscanf: invalid format specified"); |
|
1592 break; |
|
1593 |
|
1594 case 0: |
|
1595 { |
|
1596 const scanf_format_elt *elt = fmt_list.first (); |
|
1597 |
|
1598 if (elt) |
|
1599 { |
|
1600 is.clear (); |
|
1601 |
3633
|
1602 octave_scan (is, elt->text); |
2117
|
1603 |
|
1604 if (! is) |
|
1605 { |
3337
|
1606 // If it looks like we have a matching failure, then |
|
1607 // reset the failbit in the stream state. |
|
1608 |
3538
|
1609 if (is.rdstate () & std::ios::failbit) |
|
1610 is.clear (is.rdstate () & (~std::ios::failbit)); |
3337
|
1611 else |
|
1612 error ("fscanf: read error"); |
2117
|
1613 |
|
1614 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1615 |
|
1616 if (interactive && name () == "stdin") |
2215
|
1617 { |
|
1618 is.clear (); |
|
1619 |
|
1620 // Skip to end of line. |
|
1621 |
|
1622 bool err; |
|
1623 do_gets (-1, err, false, "fscanf"); |
|
1624 } |
|
1625 } |
|
1626 } |
|
1627 } |
|
1628 break; |
|
1629 |
|
1630 default: |
|
1631 { |
|
1632 int nr = -1; |
|
1633 int nc = -1; |
|
1634 |
3268
|
1635 bool one_elt_size_spec; |
|
1636 |
|
1637 get_size (size, nr, nc, one_elt_size_spec, "fscanf"); |
2215
|
1638 |
|
1639 if (! error_state) |
3559
|
1640 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, |
|
1641 conversion_count); |
2215
|
1642 } |
|
1643 break; |
|
1644 } |
|
1645 } |
|
1646 else |
2712
|
1647 invalid_operation ("fscanf", "reading"); |
2572
|
1648 |
|
1649 return retval; |
|
1650 } |
|
1651 |
2712
|
1652 bool |
|
1653 octave_base_stream::do_oscanf (const scanf_format_elt *elt, |
|
1654 octave_value& retval) |
2572
|
1655 { |
2712
|
1656 bool quit = false; |
2215
|
1657 |
3523
|
1658 std::istream *isp = input_stream (); |
2215
|
1659 |
|
1660 if (isp) |
|
1661 { |
3523
|
1662 std::istream& is = *isp; |
2215
|
1663 |
3538
|
1664 std::ios::fmtflags flags = is.flags (); |
2215
|
1665 |
|
1666 if (elt) |
|
1667 { |
|
1668 const char *fmt = elt->text; |
|
1669 |
|
1670 bool discard = elt->discard; |
|
1671 |
|
1672 switch (elt->type) |
|
1673 { |
3483
|
1674 case scanf_format_elt::whitespace_conversion: |
|
1675 DO_WHITESPACE_CONVERSION (); |
|
1676 break; |
|
1677 |
|
1678 case scanf_format_elt::literal_conversion: |
|
1679 DO_LITERAL_CONVERSION (); |
|
1680 break; |
|
1681 |
2215
|
1682 case '%': |
|
1683 { |
|
1684 int dummy; |
|
1685 |
3633
|
1686 if (! octave_scan (is, fmt, &dummy)) |
2712
|
1687 quit = true; |
2215
|
1688 } |
|
1689 break; |
|
1690 |
|
1691 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
1692 { |
|
1693 int tmp; |
|
1694 |
3633
|
1695 if (octave_scan (is, fmt, &tmp)) |
2712
|
1696 { |
|
1697 if (! discard) |
2800
|
1698 retval = static_cast<double> (tmp); |
2712
|
1699 } |
|
1700 else |
|
1701 quit = true; |
2215
|
1702 } |
|
1703 break; |
|
1704 |
|
1705 case 'e': case 'f': case 'g': |
|
1706 { |
2600
|
1707 double tmp; |
|
1708 |
3633
|
1709 if (octave_scan (is, fmt, &tmp)) |
2712
|
1710 { |
|
1711 if (! discard) |
|
1712 retval = tmp; |
|
1713 } |
|
1714 else |
|
1715 quit = true; |
2215
|
1716 } |
|
1717 break; |
|
1718 |
|
1719 case 'c': |
|
1720 { |
3483
|
1721 BEGIN_C_CONVERSION (); |
|
1722 |
|
1723 if (! discard) |
|
1724 retval = tmp; |
|
1725 |
|
1726 delete [] tmp; |
|
1727 |
|
1728 if (! is) |
2712
|
1729 quit = true; |
2215
|
1730 |
|
1731 is.setf (flags); |
|
1732 } |
|
1733 break; |
|
1734 |
|
1735 case 's': |
|
1736 { |
3483
|
1737 BEGIN_S_CONVERSION (); |
|
1738 |
|
1739 if (! discard) |
|
1740 retval = tmp; |
2572
|
1741 |
2215
|
1742 delete [] tmp; |
3268
|
1743 |
|
1744 if (! is) |
|
1745 quit = true; |
2215
|
1746 } |
|
1747 break; |
|
1748 |
3483
|
1749 case '[': case '^': |
|
1750 { |
|
1751 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1752 |
|
1753 if (! discard) |
|
1754 retval = tmp; |
|
1755 |
|
1756 delete [] tmp; |
|
1757 |
|
1758 if (! is) |
|
1759 quit = true; |
|
1760 } |
|
1761 break; |
|
1762 |
|
1763 case 'p': |
2215
|
1764 error ("fscanf: unsupported format specifier"); |
|
1765 break; |
|
1766 |
|
1767 default: |
|
1768 error ("fscanf: internal format error"); |
|
1769 break; |
|
1770 } |
|
1771 } |
|
1772 |
|
1773 if (ok () && is.fail ()) |
|
1774 { |
|
1775 error ("fscanf: read error"); |
3483
|
1776 |
2215
|
1777 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1778 |
|
1779 if (interactive && name () == "stdin") |
2215
|
1780 { |
|
1781 // Skip to end of line. |
|
1782 |
|
1783 bool err; |
|
1784 do_gets (-1, err, false, "fscanf"); |
|
1785 } |
|
1786 } |
|
1787 } |
|
1788 |
2712
|
1789 return quit; |
2215
|
1790 } |
|
1791 |
|
1792 octave_value_list |
3523
|
1793 octave_base_stream::oscanf (const std::string& fmt) |
2215
|
1794 { |
|
1795 octave_value_list retval; |
|
1796 |
3523
|
1797 std::istream *isp = input_stream (); |
2215
|
1798 |
|
1799 if (isp) |
|
1800 { |
3523
|
1801 std::istream& is = *isp; |
2215
|
1802 |
|
1803 scanf_format_list fmt_list (fmt); |
|
1804 |
|
1805 int nconv = fmt_list.num_conversions (); |
|
1806 |
|
1807 switch (nconv) |
|
1808 { |
|
1809 case -1: |
|
1810 ::error ("fscanf: invalid format specified"); |
|
1811 break; |
|
1812 |
|
1813 case 0: |
|
1814 { |
|
1815 const scanf_format_elt *elt = fmt_list.first (); |
|
1816 |
|
1817 if (elt) |
|
1818 { |
|
1819 is.clear (); |
|
1820 |
3633
|
1821 octave_scan (is, elt->text); |
2215
|
1822 |
|
1823 if (! is) |
|
1824 { |
|
1825 error ("fscanf: read error"); |
|
1826 |
|
1827 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1828 |
|
1829 if (interactive && name () == "stdin") |
2117
|
1830 { |
|
1831 is.clear (); |
|
1832 |
|
1833 // Skip to end of line. |
|
1834 |
|
1835 bool err; |
|
1836 do_gets (-1, err, false, "fscanf"); |
|
1837 } |
|
1838 } |
|
1839 } |
|
1840 } |
|
1841 break; |
|
1842 |
|
1843 default: |
|
1844 { |
3483
|
1845 is.clear (); |
|
1846 |
2215
|
1847 int len = fmt_list.length (); |
|
1848 |
3491
|
1849 retval.resize (nconv+1, Matrix ()); |
2215
|
1850 |
|
1851 const scanf_format_elt *elt = fmt_list.first (); |
|
1852 |
2572
|
1853 int num_values = 0; |
|
1854 |
2712
|
1855 bool quit = false; |
|
1856 |
3483
|
1857 for (int i = 0; i < len; i++) |
2215
|
1858 { |
2712
|
1859 octave_value tmp; |
|
1860 |
|
1861 quit = do_oscanf (elt, tmp); |
|
1862 |
|
1863 if (quit) |
2215
|
1864 break; |
2712
|
1865 else |
|
1866 { |
|
1867 if (tmp.is_defined ()) |
|
1868 retval (num_values++) = tmp; |
|
1869 |
|
1870 if (! ok ()) |
|
1871 break; |
|
1872 elt = fmt_list.next (); |
|
1873 } |
2215
|
1874 } |
|
1875 |
3491
|
1876 retval (nconv) = static_cast<double> (num_values); |
2572
|
1877 |
2712
|
1878 if (! quit) |
|
1879 { |
|
1880 // Pick up any trailing stuff. |
|
1881 if (ok () && len > nconv) |
|
1882 { |
|
1883 octave_value tmp; |
|
1884 do_oscanf (elt, tmp); |
|
1885 } |
|
1886 } |
2117
|
1887 } |
|
1888 break; |
|
1889 } |
|
1890 } |
|
1891 else |
2712
|
1892 invalid_operation ("fscanf", "reading"); |
2117
|
1893 |
|
1894 return retval; |
|
1895 } |
|
1896 |
|
1897 // Functions that are defined for all output streams (output streams |
|
1898 // are those that define os). |
|
1899 |
|
1900 int |
|
1901 octave_base_stream::flush (void) |
|
1902 { |
|
1903 int retval = -1; |
|
1904 |
3523
|
1905 std::ostream *os = output_stream (); |
2117
|
1906 |
|
1907 if (os) |
|
1908 { |
|
1909 os->flush (); |
|
1910 |
|
1911 if (os->good ()) |
|
1912 retval = 0; |
|
1913 } |
|
1914 else |
|
1915 invalid_operation ("fflush", "writing"); |
|
1916 |
|
1917 return retval; |
|
1918 } |
|
1919 |
|
1920 int |
2317
|
1921 octave_base_stream::write (const octave_value& data, |
|
1922 oct_data_conv::data_type dt, int skip, |
3553
|
1923 oct_mach_info::float_format ffmt) |
2117
|
1924 { |
|
1925 int retval = -1; |
|
1926 |
3523
|
1927 std::ostream *osp = output_stream (); |
2117
|
1928 |
|
1929 if (osp) |
|
1930 { |
3523
|
1931 std::ostream& os = *osp; |
2117
|
1932 |
|
1933 Matrix mval = data.matrix_value (); |
|
1934 |
|
1935 if (! error_state) |
|
1936 { |
3553
|
1937 if (ffmt == oct_mach_info::unknown) |
|
1938 ffmt = float_format (); |
|
1939 |
|
1940 int tmp = mval.write (os, dt, skip, ffmt); |
2317
|
1941 |
|
1942 if (tmp < 0) |
|
1943 error ("fwrite: write error"); |
|
1944 else |
|
1945 retval = tmp; |
2117
|
1946 } |
|
1947 } |
|
1948 else |
|
1949 invalid_operation ("fwrite", "writing"); |
|
1950 |
|
1951 return retval; |
|
1952 } |
|
1953 |
|
1954 class |
|
1955 printf_value_cache |
|
1956 { |
|
1957 public: |
|
1958 |
|
1959 enum state { ok, list_exhausted, conversion_error }; |
|
1960 |
|
1961 printf_value_cache (const octave_value_list& args) |
|
1962 : values (args), val_idx (0), elt_idx (0), |
|
1963 n_vals (values.length ()), n_elts (0), data (0), |
|
1964 curr_state (ok) { } |
|
1965 |
|
1966 ~printf_value_cache (void) { } |
|
1967 |
|
1968 // Get the current value as a double and advance the internal pointer. |
|
1969 double double_value (void); |
|
1970 |
|
1971 // Get the current value as an int and advance the internal pointer. |
|
1972 int int_value (void); |
|
1973 |
|
1974 // Get the current value as a string and advance the internal pointer. |
3523
|
1975 std::string string_value (void); |
2117
|
1976 |
3145
|
1977 operator bool () const { return (curr_state == ok); } |
2117
|
1978 |
|
1979 bool no_more_values (void) { return curr_state == list_exhausted; } |
|
1980 |
|
1981 bool looking_at_string (void); |
|
1982 |
|
1983 private: |
|
1984 |
|
1985 const octave_value_list values; |
|
1986 int val_idx; |
|
1987 int elt_idx; |
|
1988 int n_vals; |
|
1989 int n_elts; |
|
1990 const double *data; |
|
1991 Matrix curr_val; |
|
1992 state curr_state; |
|
1993 |
|
1994 // Must create value cache with values! |
|
1995 |
|
1996 printf_value_cache (void); |
|
1997 |
|
1998 // No copying! |
|
1999 |
|
2000 printf_value_cache (const printf_value_cache&); |
|
2001 |
|
2002 printf_value_cache& operator = (const printf_value_cache&); |
|
2003 }; |
|
2004 |
|
2005 bool |
|
2006 printf_value_cache::looking_at_string (void) |
|
2007 { |
|
2008 bool retval = false; |
|
2009 |
|
2010 int idx = -1; |
|
2011 |
|
2012 if (elt_idx == 0) |
|
2013 idx = val_idx; |
|
2014 else if (elt_idx >= n_elts) |
|
2015 idx = val_idx + 1; |
|
2016 |
|
2017 if (idx >= 0 && idx < n_vals) |
|
2018 { |
|
2019 octave_value tmp_val = values (idx); |
|
2020 |
2613
|
2021 // An empty string has zero rows and zero columns. |
|
2022 |
|
2023 if (tmp_val.is_string ()) |
|
2024 { |
|
2025 int nr = tmp_val.rows (); |
|
2026 |
|
2027 retval = (nr == 1 || (nr == 0 && tmp_val.columns () == 0)); |
|
2028 } |
2117
|
2029 } |
|
2030 |
|
2031 return retval; |
|
2032 } |
|
2033 |
|
2034 double |
|
2035 printf_value_cache::double_value (void) |
|
2036 { |
|
2037 double retval = 0.0; |
|
2038 |
|
2039 while (val_idx < n_vals) |
|
2040 { |
|
2041 if (! data) |
|
2042 { |
|
2043 octave_value tmp_val = values (val_idx); |
|
2044 |
|
2045 curr_val = tmp_val.matrix_value (); |
|
2046 |
|
2047 if (! error_state) |
|
2048 { |
|
2049 elt_idx = 0; |
|
2050 n_elts = curr_val.length (); |
|
2051 data = curr_val.data (); |
|
2052 } |
|
2053 else |
|
2054 { |
|
2055 curr_state = conversion_error; |
|
2056 break; |
|
2057 } |
|
2058 } |
|
2059 |
|
2060 if (elt_idx < n_elts) |
|
2061 { |
|
2062 return data[elt_idx++]; |
|
2063 break; |
|
2064 } |
|
2065 else |
|
2066 { |
|
2067 val_idx++; |
|
2068 data = 0; |
|
2069 continue; |
|
2070 } |
|
2071 } |
|
2072 |
|
2073 curr_state = list_exhausted; |
|
2074 |
|
2075 return retval; |
|
2076 } |
|
2077 |
|
2078 int |
|
2079 printf_value_cache::int_value (void) |
|
2080 { |
|
2081 int retval = 0; |
|
2082 |
|
2083 double dval = double_value (); |
|
2084 |
|
2085 if (! error_state) |
|
2086 { |
|
2087 if (D_NINT (dval) == dval) |
|
2088 retval = NINT (dval); |
|
2089 else |
|
2090 curr_state = conversion_error; |
|
2091 } |
|
2092 |
|
2093 return retval; |
|
2094 } |
|
2095 |
3536
|
2096 std::string |
2117
|
2097 printf_value_cache::string_value (void) |
|
2098 { |
3523
|
2099 std::string retval; |
2117
|
2100 |
|
2101 if (looking_at_string ()) |
|
2102 { |
|
2103 if (elt_idx != 0) |
|
2104 { |
|
2105 val_idx++; |
|
2106 elt_idx = 0; |
|
2107 data = 0; |
|
2108 } |
|
2109 |
|
2110 retval = values (val_idx++).string_value (); |
|
2111 } |
|
2112 else |
|
2113 curr_state = conversion_error; |
|
2114 |
|
2115 return retval; |
|
2116 } |
|
2117 |
|
2118 // Ugh again and again. |
|
2119 |
2572
|
2120 template <class T> |
3620
|
2121 int |
3523
|
2122 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
2572
|
2123 int sa_2, bool have_arg, T arg) |
|
2124 { |
3620
|
2125 int retval = 0; |
|
2126 |
2572
|
2127 switch (nsa) |
|
2128 { |
|
2129 case 2: |
|
2130 if (have_arg) |
3620
|
2131 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572
|
2132 else |
3620
|
2133 retval = octave_format (os, fmt, sa_1, sa_2); |
2572
|
2134 break; |
|
2135 |
|
2136 case 1: |
|
2137 if (have_arg) |
3620
|
2138 retval = octave_format (os, fmt, sa_1, arg); |
2572
|
2139 else |
3620
|
2140 retval = octave_format (os, fmt, sa_1); |
2572
|
2141 break; |
|
2142 |
|
2143 case 0: |
|
2144 if (have_arg) |
3620
|
2145 retval = octave_format (os, fmt, arg); |
2572
|
2146 else |
3620
|
2147 retval = octave_format (os, fmt); |
2572
|
2148 break; |
|
2149 |
|
2150 default: |
|
2151 ::error ("fprintf: internal error handling format"); |
|
2152 break; |
|
2153 } |
3620
|
2154 |
|
2155 return retval; |
2572
|
2156 } |
|
2157 |
3620
|
2158 template int |
3523
|
2159 do_printf_conv (std::ostream&, const char*, int, int, int, bool, int); |
2572
|
2160 |
3620
|
2161 template int |
3523
|
2162 do_printf_conv (std::ostream&, const char*, int, int, int, bool, long); |
2572
|
2163 |
3620
|
2164 template int |
3523
|
2165 do_printf_conv (std::ostream&, const char*, int, int, int, bool, double); |
2572
|
2166 |
3620
|
2167 template int |
3523
|
2168 do_printf_conv (std::ostream&, const char*, int, int, int, bool, const char*); |
2117
|
2169 |
|
2170 int |
|
2171 octave_base_stream::do_printf (printf_format_list& fmt_list, |
|
2172 const octave_value_list& args) |
|
2173 { |
3620
|
2174 int retval = 0; |
2117
|
2175 |
3523
|
2176 std::ostream *osp = output_stream (); |
2117
|
2177 |
|
2178 if (osp) |
|
2179 { |
3523
|
2180 std::ostream& os = *osp; |
2117
|
2181 |
|
2182 const printf_format_elt *elt = fmt_list.first (); |
|
2183 |
|
2184 printf_value_cache val_cache (args); |
|
2185 |
|
2186 for (;;) |
|
2187 { |
|
2188 if (elt) |
|
2189 { |
|
2190 int args = elt->args; |
|
2191 int nsa = args; |
|
2192 |
|
2193 int doing_percent = elt->type == '%'; |
|
2194 |
|
2195 if (args > 0 && ! doing_percent) |
|
2196 nsa--; |
|
2197 |
|
2198 int sa_1 = 0; |
|
2199 int sa_2 = 0; |
|
2200 |
|
2201 if (nsa > 0) |
|
2202 { |
|
2203 sa_1 = val_cache.int_value (); |
|
2204 |
|
2205 if (! val_cache) |
|
2206 break; |
|
2207 else |
|
2208 { |
|
2209 if (nsa > 1) |
|
2210 { |
|
2211 sa_2 = val_cache.int_value (); |
|
2212 |
|
2213 if (! val_cache) |
|
2214 break; |
|
2215 } |
|
2216 } |
|
2217 } |
|
2218 |
|
2219 const char *fmt = elt->text; |
|
2220 |
|
2221 if (doing_percent || args == 0) |
3620
|
2222 retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, |
|
2223 false, 0.0); |
2117
|
2224 else |
|
2225 { |
|
2226 if (elt->type == 's' && val_cache.looking_at_string ()) |
|
2227 { |
3523
|
2228 std::string val = val_cache.string_value (); |
2117
|
2229 |
|
2230 if (val_cache) |
3620
|
2231 retval += do_printf_conv (os, fmt, nsa, sa_1, |
|
2232 sa_2, true, val.c_str ()); |
2117
|
2233 else |
|
2234 break; |
|
2235 } |
|
2236 else |
|
2237 { |
|
2238 double val = val_cache.double_value (); |
|
2239 |
|
2240 if (val_cache) |
|
2241 { |
|
2242 switch (elt->type) |
|
2243 { |
|
2244 case 'd': case 'i': case 'o': case 'x': |
|
2245 case 'X': case 'u': case 'c': |
|
2246 { |
|
2247 if (elt->modifier == 'l') |
3620
|
2248 retval |
|
2249 += do_printf_conv (os, fmt, nsa, |
|
2250 sa_1, sa_2, true, |
|
2251 static_cast<long> (val)); |
2117
|
2252 else |
3620
|
2253 retval |
|
2254 += do_printf_conv (os, fmt, nsa, |
|
2255 sa_1, sa_2, true, |
|
2256 static_cast<int> (val)); |
2117
|
2257 } |
|
2258 break; |
|
2259 |
|
2260 case 'f': case 'e': case 'E': |
|
2261 case 'g': case 'G': |
3620
|
2262 retval |
|
2263 += do_printf_conv (os, fmt, nsa, sa_1, |
|
2264 sa_2, true, val); |
2117
|
2265 break; |
|
2266 |
|
2267 default: |
|
2268 error ("fprintf: invalid format specifier"); |
|
2269 return -1; |
|
2270 break; |
|
2271 } |
|
2272 } |
|
2273 else |
|
2274 break; |
|
2275 } |
|
2276 |
|
2277 if (val_cache.no_more_values ()) |
3620
|
2278 break; |
2117
|
2279 } |
|
2280 |
3620
|
2281 if (! os) |
2117
|
2282 { |
|
2283 error ("fprintf: write error"); |
|
2284 retval = -1; |
|
2285 break; |
|
2286 } |
|
2287 } |
|
2288 else |
|
2289 { |
|
2290 ::error ("fprintf: internal error handling format"); |
|
2291 retval = -1; |
|
2292 break; |
|
2293 } |
|
2294 |
|
2295 elt = fmt_list.next (); |
|
2296 } |
|
2297 } |
|
2298 |
|
2299 return retval; |
|
2300 } |
|
2301 |
|
2302 int |
3523
|
2303 octave_base_stream::printf (const std::string& fmt, const octave_value_list& args) |
2117
|
2304 { |
|
2305 int retval = -1; |
|
2306 |
3523
|
2307 std::ostream *osp = output_stream (); |
2117
|
2308 |
|
2309 if (osp) |
|
2310 { |
3523
|
2311 std::ostream& os = *osp; |
2117
|
2312 |
|
2313 printf_format_list fmt_list (fmt); |
|
2314 |
|
2315 switch (fmt_list.num_conversions ()) |
|
2316 { |
|
2317 case -1: |
|
2318 ::error ("fprintf: invalid format specified"); |
|
2319 break; |
|
2320 |
|
2321 case 0: |
|
2322 { |
|
2323 const printf_format_elt *elt = fmt_list.first (); |
|
2324 |
|
2325 if (elt) |
|
2326 { |
3627
|
2327 retval = octave_format (os, elt->text); |
|
2328 |
|
2329 if (! os) |
|
2330 { |
|
2331 retval = -1; |
|
2332 error ("fprintf: write error"); |
|
2333 } |
2117
|
2334 } |
|
2335 } |
|
2336 break; |
|
2337 |
|
2338 default: |
|
2339 { |
|
2340 if (args.length () == 0) |
|
2341 ::error ("fprintf: no arguments available for specified format"); |
|
2342 else |
|
2343 retval = do_printf (fmt_list, args); |
|
2344 } |
|
2345 break; |
|
2346 } |
|
2347 } |
|
2348 else |
|
2349 invalid_operation ("fprintf", "writing"); |
|
2350 |
|
2351 return retval; |
|
2352 } |
|
2353 |
|
2354 int |
3523
|
2355 octave_base_stream::puts (const std::string& s) |
2117
|
2356 { |
|
2357 int retval = -1; |
|
2358 |
3523
|
2359 std::ostream *osp = output_stream (); |
2117
|
2360 |
|
2361 if (osp) |
|
2362 { |
3523
|
2363 std::ostream& os = *osp; |
2117
|
2364 |
|
2365 os << s; |
|
2366 |
|
2367 if (os) |
|
2368 { |
|
2369 // XXX FIXME XXX -- why does this seem to be necessary? |
|
2370 // Without it, output from a loop like |
|
2371 // |
|
2372 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
|
2373 // |
|
2374 // doesn't seem to go to the pager immediately. |
|
2375 |
|
2376 os.flush (); |
|
2377 |
|
2378 if (os) |
|
2379 retval = 0; |
|
2380 else |
|
2381 error ("fputs: write error"); |
|
2382 } |
|
2383 else |
|
2384 error ("fputs: write error"); |
|
2385 } |
|
2386 else |
|
2387 invalid_operation ("fputs", "writing"); |
|
2388 |
|
2389 return retval; |
|
2390 } |
|
2391 |
|
2392 int |
|
2393 octave_base_stream::rewind (void) |
|
2394 { |
3538
|
2395 return seek (0, std::ios::beg); |
2117
|
2396 } |
|
2397 |
|
2398 // Return current error message for this stream. |
|
2399 |
3536
|
2400 std::string |
2435
|
2401 octave_base_stream::error (bool clear_err, int& err_num) |
2117
|
2402 { |
2435
|
2403 err_num = fail ? -1 : 0; |
2117
|
2404 |
3523
|
2405 std::string tmp = errmsg; |
2117
|
2406 |
|
2407 if (clear_err) |
|
2408 clear (); |
|
2409 |
|
2410 return tmp; |
|
2411 } |
|
2412 |
|
2413 void |
|
2414 octave_base_stream::invalid_operation (const char *op, const char *rw) |
|
2415 { |
3523
|
2416 std::string msg = op; |
2117
|
2417 msg.append (": stream not open for "); |
|
2418 msg.append (rw); |
|
2419 error (msg); |
|
2420 } |
|
2421 |
3552
|
2422 octave_stream::octave_stream (octave_base_stream *bs) |
3340
|
2423 : rep (bs) |
|
2424 { |
|
2425 if (rep) |
|
2426 rep->count = 1; |
|
2427 } |
|
2428 |
|
2429 octave_stream::~octave_stream (void) |
|
2430 { |
|
2431 if (rep && --rep->count == 0) |
|
2432 delete rep; |
|
2433 } |
|
2434 |
|
2435 octave_stream::octave_stream (const octave_stream& s) |
|
2436 : rep (s.rep) |
|
2437 { |
|
2438 if (rep) |
|
2439 rep->count++; |
|
2440 } |
|
2441 |
|
2442 octave_stream& |
|
2443 octave_stream::operator = (const octave_stream& s) |
|
2444 { |
|
2445 if (rep != s.rep) |
|
2446 { |
|
2447 if (rep && --rep->count == 0) |
|
2448 delete rep; |
|
2449 |
|
2450 rep = s.rep; |
|
2451 |
|
2452 if (rep) |
|
2453 rep->count++; |
|
2454 } |
|
2455 |
|
2456 return *this; |
|
2457 } |
|
2458 |
2117
|
2459 int |
|
2460 octave_stream::flush (void) |
|
2461 { |
|
2462 int retval = -1; |
|
2463 |
|
2464 if (stream_ok ("fflush")) |
|
2465 retval = rep->flush (); |
|
2466 |
|
2467 return retval; |
|
2468 } |
|
2469 |
3536
|
2470 std::string |
2117
|
2471 octave_stream::getl (int max_len, bool& err) |
|
2472 { |
3523
|
2473 std::string retval; |
2117
|
2474 |
|
2475 if (stream_ok ("getl")) |
|
2476 retval = rep->getl (max_len, err); |
|
2477 |
|
2478 return retval; |
|
2479 } |
|
2480 |
3536
|
2481 std::string |
2117
|
2482 octave_stream::getl (const octave_value& tc_max_len, bool& err) |
|
2483 { |
3523
|
2484 std::string retval; |
2117
|
2485 |
|
2486 err = false; |
|
2487 |
|
2488 int conv_err = 0; |
|
2489 |
|
2490 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2491 |
|
2492 if (conv_err || max_len < 0) |
|
2493 { |
|
2494 err = true; |
|
2495 ::error ("fgetl: invalid maximum length specified"); |
|
2496 } |
|
2497 else |
|
2498 retval = getl (max_len, err); |
|
2499 |
|
2500 return retval; |
|
2501 } |
|
2502 |
3536
|
2503 std::string |
2117
|
2504 octave_stream::gets (int max_len, bool& err) |
|
2505 { |
3523
|
2506 std::string retval; |
2117
|
2507 |
|
2508 if (stream_ok ("fgets")) |
|
2509 retval = rep->gets (max_len, err); |
|
2510 |
|
2511 return retval; |
|
2512 } |
|
2513 |
3536
|
2514 std::string |
2117
|
2515 octave_stream::gets (const octave_value& tc_max_len, bool& err) |
|
2516 { |
3523
|
2517 std::string retval; |
2117
|
2518 |
|
2519 err = false; |
|
2520 |
|
2521 int conv_err = 0; |
|
2522 |
|
2523 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2524 |
|
2525 if (conv_err || max_len < 0) |
|
2526 { |
|
2527 err = true; |
|
2528 ::error ("fgets: invalid maximum length specified"); |
|
2529 } |
|
2530 else |
|
2531 retval = gets (max_len, err); |
|
2532 |
|
2533 return retval; |
|
2534 } |
|
2535 |
|
2536 int |
3546
|
2537 octave_stream::seek (std::streamoff offset, std::ios::seek_dir origin) |
2117
|
2538 { |
|
2539 int retval = -1; |
|
2540 |
|
2541 if (stream_ok ("fseek")) |
|
2542 retval = rep->seek (offset, origin); |
|
2543 |
|
2544 return retval; |
|
2545 } |
|
2546 |
|
2547 int |
|
2548 octave_stream::seek (const octave_value& tc_offset, |
|
2549 const octave_value& tc_origin) |
|
2550 { |
|
2551 int retval = -1; |
|
2552 |
|
2553 int conv_err = 0; |
|
2554 |
|
2555 int xoffset = convert_to_valid_int (tc_offset, conv_err); |
|
2556 |
|
2557 if (! conv_err) |
|
2558 { |
3538
|
2559 std::ios::seek_dir origin = std::ios::beg; |
2117
|
2560 |
2341
|
2561 if (tc_origin.is_string ()) |
2117
|
2562 { |
3523
|
2563 std::string xorigin = tc_origin.string_value (); |
2341
|
2564 |
|
2565 if (xorigin == "bof") |
3538
|
2566 origin = std::ios::beg; |
2341
|
2567 else if (xorigin == "cof") |
3538
|
2568 origin = std::ios::cur; |
2341
|
2569 else if (xorigin == "eof") |
3538
|
2570 origin = std::ios::end; |
2117
|
2571 else |
|
2572 conv_err = -1; |
|
2573 } |
2341
|
2574 else |
|
2575 { |
|
2576 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
|
2577 |
|
2578 if (! conv_err) |
|
2579 { |
|
2580 if (xorigin == -1) |
3538
|
2581 origin = std::ios::beg; |
2341
|
2582 else if (xorigin == 0) |
3538
|
2583 origin = std::ios::cur; |
2341
|
2584 else if (xorigin == 1) |
3538
|
2585 origin = std::ios::end; |
2341
|
2586 else |
|
2587 conv_err = -1; |
|
2588 } |
|
2589 } |
2117
|
2590 |
|
2591 if (! conv_err) |
|
2592 retval = seek (xoffset, origin); |
|
2593 else |
|
2594 error ("fseek: invalid value for origin"); |
|
2595 } |
|
2596 else |
|
2597 error ("fseek: invalid value for offset"); |
|
2598 |
|
2599 return retval; |
|
2600 } |
|
2601 |
|
2602 long |
|
2603 octave_stream::tell (void) const |
|
2604 { |
|
2605 long retval = -1; |
|
2606 |
|
2607 if (stream_ok ("tell")) |
|
2608 retval = rep->tell (); |
|
2609 |
|
2610 return retval; |
|
2611 } |
|
2612 |
|
2613 int |
|
2614 octave_stream::rewind (void) |
|
2615 { |
|
2616 int retval = -1; |
|
2617 |
|
2618 if (stream_ok ("frewind")) |
|
2619 retval = rep->rewind (); |
|
2620 |
|
2621 return retval; |
|
2622 } |
|
2623 |
3340
|
2624 bool |
|
2625 octave_stream::is_open (void) const |
|
2626 { |
|
2627 bool retval = false; |
|
2628 |
|
2629 if (stream_ok ("is_open")) |
|
2630 retval = rep->is_open (); |
|
2631 |
|
2632 return retval; |
|
2633 } |
|
2634 |
|
2635 void |
|
2636 octave_stream::close (void) |
|
2637 { |
|
2638 if (stream_ok ("close")) |
|
2639 rep->close (); |
|
2640 } |
|
2641 |
2117
|
2642 octave_value |
|
2643 octave_stream::read (const Matrix& size, |
2317
|
2644 oct_data_conv::data_type dt, int skip, |
|
2645 oct_mach_info::float_format flt_fmt, int& count) |
2117
|
2646 { |
|
2647 octave_value retval; |
|
2648 |
|
2649 if (stream_ok ("fread")) |
2317
|
2650 retval = rep->read (size, dt, skip, flt_fmt, count); |
2117
|
2651 |
|
2652 return retval; |
|
2653 } |
|
2654 |
|
2655 int |
|
2656 octave_stream::write (const octave_value& data, |
2317
|
2657 oct_data_conv::data_type dt, int skip, |
|
2658 oct_mach_info::float_format flt_fmt) |
2117
|
2659 { |
|
2660 int retval = -1; |
|
2661 |
|
2662 if (stream_ok ("fwrite")) |
2317
|
2663 retval = rep->write (data, dt, skip, flt_fmt); |
2117
|
2664 |
|
2665 return retval; |
|
2666 } |
|
2667 |
|
2668 octave_value |
3523
|
2669 octave_stream::scanf (const std::string& fmt, const Matrix& size, int& count) |
2117
|
2670 { |
|
2671 octave_value retval; |
|
2672 |
|
2673 if (stream_ok ("fscanf")) |
|
2674 retval = rep->scanf (fmt, size, count); |
|
2675 |
|
2676 return retval; |
|
2677 } |
|
2678 |
2215
|
2679 octave_value_list |
3523
|
2680 octave_stream::oscanf (const std::string& fmt) |
2215
|
2681 { |
|
2682 octave_value_list retval; |
|
2683 |
|
2684 if (stream_ok ("fscanf")) |
|
2685 retval = rep->oscanf (fmt); |
|
2686 |
|
2687 return retval; |
|
2688 } |
|
2689 |
2117
|
2690 int |
3523
|
2691 octave_stream::printf (const std::string& fmt, const octave_value_list& args) |
2117
|
2692 { |
|
2693 int retval = -1; |
|
2694 |
|
2695 if (stream_ok ("fprintf")) |
|
2696 retval = rep->printf (fmt, args); |
|
2697 |
|
2698 return retval; |
|
2699 } |
|
2700 |
|
2701 int |
3523
|
2702 octave_stream::puts (const std::string& s) |
2117
|
2703 { |
|
2704 int retval = -1; |
|
2705 |
|
2706 if (stream_ok ("fputs")) |
|
2707 retval = rep->puts (s); |
|
2708 |
|
2709 return retval; |
|
2710 } |
|
2711 |
|
2712 // XXX FIXME XXX -- maybe this should work for string arrays too. |
|
2713 |
|
2714 int |
|
2715 octave_stream::puts (const octave_value& tc_s) |
|
2716 { |
|
2717 int retval = -1; |
|
2718 |
|
2719 if (tc_s.is_string ()) |
|
2720 { |
3523
|
2721 std::string s = tc_s.string_value (); |
2117
|
2722 retval = rep->puts (s); |
|
2723 } |
|
2724 else |
|
2725 error ("fputs: argument must be a string"); |
|
2726 |
|
2727 return retval; |
|
2728 } |
|
2729 |
|
2730 bool |
|
2731 octave_stream::eof (void) const |
|
2732 { |
|
2733 int retval = -1; |
|
2734 |
|
2735 if (stream_ok ("feof")) |
|
2736 retval = rep->eof (); |
|
2737 |
|
2738 return retval; |
|
2739 } |
|
2740 |
3536
|
2741 std::string |
2435
|
2742 octave_stream::error (bool clear, int& err_num) |
2117
|
2743 { |
3523
|
2744 std::string retval; |
2117
|
2745 |
|
2746 if (stream_ok ("ferror", false)) |
2435
|
2747 retval = rep->error (clear, err_num); |
2117
|
2748 |
|
2749 return retval; |
|
2750 } |
|
2751 |
3536
|
2752 std::string |
3340
|
2753 octave_stream::name (void) const |
2117
|
2754 { |
3523
|
2755 std::string retval; |
2117
|
2756 |
|
2757 if (stream_ok ("name")) |
|
2758 retval = rep->name (); |
|
2759 |
|
2760 return retval; |
|
2761 } |
|
2762 |
|
2763 int |
3340
|
2764 octave_stream::mode (void) const |
2117
|
2765 { |
|
2766 int retval = 0; |
|
2767 |
|
2768 if (stream_ok ("mode")) |
|
2769 retval = rep->mode (); |
|
2770 |
|
2771 return retval; |
|
2772 } |
|
2773 |
2317
|
2774 oct_mach_info::float_format |
3340
|
2775 octave_stream::float_format (void) const |
2117
|
2776 { |
2317
|
2777 oct_mach_info::float_format retval = oct_mach_info::unknown; |
|
2778 |
|
2779 if (stream_ok ("float_format")) |
|
2780 retval = rep->float_format (); |
2117
|
2781 |
|
2782 return retval; |
|
2783 } |
|
2784 |
3536
|
2785 std::string |
2117
|
2786 octave_stream::mode_as_string (int mode) |
|
2787 { |
3523
|
2788 std::string retval = "???"; |
2117
|
2789 |
|
2790 switch (mode) |
|
2791 { |
3538
|
2792 case std::ios::in: |
2117
|
2793 retval = "r"; |
|
2794 break; |
|
2795 |
3538
|
2796 case std::ios::out: |
|
2797 case std::ios::out | std::ios::trunc: |
2117
|
2798 retval = "w"; |
|
2799 break; |
|
2800 |
3538
|
2801 case std::ios::out | std::ios::app: |
2117
|
2802 retval = "a"; |
|
2803 break; |
|
2804 |
3538
|
2805 case std::ios::in | std::ios::out: |
2117
|
2806 retval = "r+"; |
|
2807 break; |
|
2808 |
3538
|
2809 case std::ios::in | std::ios::out | std::ios::trunc: |
2117
|
2810 retval = "w+"; |
|
2811 break; |
|
2812 |
3538
|
2813 case std::ios::in | std::ios::out | std::ios::app: |
2117
|
2814 retval = "a+"; |
|
2815 break; |
|
2816 |
3552
|
2817 case std::ios::in | std::ios::binary: |
2117
|
2818 retval = "rb"; |
|
2819 break; |
|
2820 |
3552
|
2821 case std::ios::out | std::ios::binary: |
|
2822 case std::ios::out | std::ios::trunc | std::ios::binary: |
2117
|
2823 retval = "wb"; |
|
2824 break; |
|
2825 |
3552
|
2826 case std::ios::out | std::ios::app | std::ios::binary: |
2117
|
2827 retval = "ab"; |
|
2828 break; |
|
2829 |
3552
|
2830 case std::ios::in | std::ios::out | std::ios::binary: |
2117
|
2831 retval = "r+b"; |
|
2832 break; |
|
2833 |
3552
|
2834 case std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary: |
2117
|
2835 retval = "w+b"; |
|
2836 break; |
|
2837 |
3552
|
2838 case std::ios::in | std::ios::out | std::ios::app | std::ios::binary: |
2117
|
2839 retval = "a+b"; |
|
2840 break; |
|
2841 |
|
2842 default: |
|
2843 break; |
|
2844 } |
|
2845 |
|
2846 return retval; |
|
2847 } |
|
2848 |
|
2849 void |
|
2850 octave_stream::invalid_stream_error (const char *op) const |
|
2851 { |
|
2852 ::error ("%s: attempt to use invalid I/O stream", op); |
|
2853 } |
|
2854 |
|
2855 octave_stream_list *octave_stream_list::instance = 0; |
|
2856 |
2926
|
2857 bool |
|
2858 octave_stream_list::instance_ok (void) |
|
2859 { |
|
2860 bool retval = true; |
|
2861 |
|
2862 if (! instance) |
|
2863 instance = new octave_stream_list (); |
|
2864 |
|
2865 if (! instance) |
|
2866 { |
|
2867 ::error ("unable to create stream list object!"); |
|
2868 |
|
2869 retval = false; |
|
2870 } |
|
2871 |
|
2872 return retval; |
|
2873 } |
|
2874 |
|
2875 octave_value |
3340
|
2876 octave_stream_list::insert (const octave_stream& os) |
2926
|
2877 { |
3340
|
2878 return (instance_ok ()) ? instance->do_insert (os) : octave_value (-1.0); |
2926
|
2879 } |
|
2880 |
3340
|
2881 octave_stream |
3523
|
2882 octave_stream_list::lookup (int fid, const std::string& who) |
2926
|
2883 { |
3341
|
2884 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
2885 } |
|
2886 |
3340
|
2887 octave_stream |
3523
|
2888 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926
|
2889 { |
3341
|
2890 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
2891 } |
|
2892 |
|
2893 int |
3523
|
2894 octave_stream_list::remove (int fid, const std::string& who) |
2926
|
2895 { |
3341
|
2896 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
2897 } |
|
2898 |
|
2899 int |
3523
|
2900 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926
|
2901 { |
3341
|
2902 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
2903 } |
|
2904 |
|
2905 void |
|
2906 octave_stream_list::clear (void) |
|
2907 { |
|
2908 if (instance) |
|
2909 instance->do_clear (); |
|
2910 } |
|
2911 |
|
2912 string_vector |
|
2913 octave_stream_list::get_info (int fid) |
|
2914 { |
|
2915 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
2916 } |
|
2917 |
|
2918 string_vector |
|
2919 octave_stream_list::get_info (const octave_value& fid) |
|
2920 { |
|
2921 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
2922 } |
|
2923 |
3536
|
2924 std::string |
2926
|
2925 octave_stream_list::list_open_files (void) |
|
2926 { |
3523
|
2927 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926
|
2928 } |
|
2929 |
|
2930 octave_value |
|
2931 octave_stream_list::open_file_numbers (void) |
|
2932 { |
|
2933 return (instance_ok ()) |
|
2934 ? instance->do_open_file_numbers () : octave_value (); |
|
2935 } |
|
2936 |
|
2937 int |
|
2938 octave_stream_list::get_file_number (const octave_value& fid) |
|
2939 { |
|
2940 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; |
|
2941 } |
|
2942 |
2902
|
2943 octave_value |
3340
|
2944 octave_stream_list::do_insert (const octave_stream& os) |
2117
|
2945 { |
3340
|
2946 octave_value retval; |
|
2947 |
2902
|
2948 int stream_number = -1; |
|
2949 |
3340
|
2950 // Insert item in first open slot, increasing size of list if |
|
2951 // necessary. |
|
2952 |
|
2953 for (int i = 0; i < curr_len; i++) |
2117
|
2954 { |
3340
|
2955 octave_stream tmp = list(i); |
|
2956 |
|
2957 if (! tmp) |
2117
|
2958 { |
3340
|
2959 list(i) = os; |
|
2960 stream_number = i; |
|
2961 break; |
2117
|
2962 } |
|
2963 } |
3340
|
2964 |
|
2965 if (stream_number < 0) |
|
2966 { |
|
2967 int total_len = list.length (); |
|
2968 |
|
2969 if (curr_len == total_len) |
|
2970 list.resize (total_len * 2); |
|
2971 |
|
2972 list(curr_len) = os; |
|
2973 stream_number = curr_len; |
|
2974 curr_len++; |
|
2975 } |
2117
|
2976 |
2902
|
2977 return octave_value (os, stream_number); |
2117
|
2978 } |
|
2979 |
3341
|
2980 static void |
3523
|
2981 gripe_invalid_file_id (int fid, const std::string& who) |
3341
|
2982 { |
|
2983 if (who.empty ()) |
|
2984 ::error ("invalid stream number = %d", fid); |
|
2985 else |
|
2986 ::error ("%s: invalid stream number = %d", who.c_str (), fid); |
|
2987 } |
|
2988 |
3340
|
2989 octave_stream |
3523
|
2990 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117
|
2991 { |
3340
|
2992 octave_stream retval; |
2117
|
2993 |
|
2994 if (fid >= 0 && fid < curr_len) |
3340
|
2995 retval = list(fid); |
3341
|
2996 else |
|
2997 gripe_invalid_file_id (fid, who); |
2117
|
2998 |
|
2999 return retval; |
|
3000 } |
|
3001 |
3340
|
3002 octave_stream |
3341
|
3003 octave_stream_list::do_lookup (const octave_value& fid, |
3523
|
3004 const std::string& who) const |
2117
|
3005 { |
3340
|
3006 octave_stream retval; |
2117
|
3007 |
|
3008 int i = get_file_number (fid); |
|
3009 |
|
3010 if (! error_state) |
3341
|
3011 retval = do_lookup (i, who); |
2117
|
3012 |
|
3013 return retval; |
|
3014 } |
|
3015 |
|
3016 int |
3523
|
3017 octave_stream_list::do_remove (int fid, const std::string& who) |
2117
|
3018 { |
|
3019 int retval = -1; |
|
3020 |
3531
|
3021 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
|
3022 // (std::cerr). |
2117
|
3023 |
|
3024 if (fid > 2 && fid < curr_len) |
|
3025 { |
3340
|
3026 octave_stream os = list(fid); |
2117
|
3027 |
3341
|
3028 if (os.is_valid ()) |
2117
|
3029 { |
3340
|
3030 os.close (); |
|
3031 list(fid) = octave_stream (); |
2117
|
3032 retval = 0; |
|
3033 } |
3341
|
3034 else |
|
3035 gripe_invalid_file_id (fid, who); |
2117
|
3036 } |
3341
|
3037 else |
|
3038 gripe_invalid_file_id (fid, who); |
2117
|
3039 |
|
3040 return retval; |
|
3041 } |
|
3042 |
|
3043 int |
3523
|
3044 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117
|
3045 { |
|
3046 int retval = -1; |
|
3047 |
|
3048 int i = get_file_number (fid); |
|
3049 |
|
3050 if (! error_state) |
3341
|
3051 retval = do_remove (i, who); |
2117
|
3052 |
|
3053 return retval; |
|
3054 } |
|
3055 |
|
3056 void |
|
3057 octave_stream_list::do_clear (void) |
|
3058 { |
|
3059 // Do flush stdout and stderr. |
|
3060 |
3340
|
3061 list(0) . flush (); |
|
3062 list(1) . flush (); |
2117
|
3063 |
|
3064 // But don't delete them or stdin. |
|
3065 |
|
3066 for (int i = 3; i < curr_len; i++) |
3340
|
3067 list(i) = octave_stream (); |
2117
|
3068 } |
|
3069 |
|
3070 string_vector |
|
3071 octave_stream_list::do_get_info (int fid) const |
|
3072 { |
|
3073 string_vector retval; |
|
3074 |
3340
|
3075 octave_stream os = do_lookup (fid); |
2117
|
3076 |
3341
|
3077 if (os.is_valid ()) |
2117
|
3078 { |
|
3079 retval.resize (3); |
|
3080 |
3340
|
3081 retval(0) = os.name (); |
|
3082 retval(1) = octave_stream::mode_as_string (os.mode ()); |
|
3083 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
2117
|
3084 } |
|
3085 else |
3341
|
3086 ::error ("invalid file id = %d", fid); |
2117
|
3087 |
|
3088 return retval; |
|
3089 } |
|
3090 |
|
3091 string_vector |
|
3092 octave_stream_list::do_get_info (const octave_value& fid) const |
|
3093 { |
|
3094 string_vector retval; |
|
3095 |
|
3096 int conv_err = 0; |
|
3097 |
|
3098 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3099 |
|
3100 if (! conv_err) |
|
3101 retval = do_get_info (int_fid); |
|
3102 else |
2915
|
3103 ::error ("file id must be a file object or integer value"); |
2117
|
3104 |
|
3105 return retval; |
|
3106 } |
|
3107 |
3536
|
3108 std::string |
2117
|
3109 octave_stream_list::do_list_open_files (void) const |
|
3110 { |
3523
|
3111 std::string retval; |
2117
|
3112 |
|
3113 // XXX FIXME XXX -- this should probably be converted to use sstream |
|
3114 // when that is available. |
3523
|
3115 std::ostrstream buf; |
2117
|
3116 |
|
3117 buf << "\n" |
|
3118 << " number mode arch name\n" |
|
3119 << " ------ ---- ---- ----\n"; |
|
3120 |
|
3121 for (int i = 0; i < curr_len; i++) |
|
3122 { |
3340
|
3123 octave_stream os = list(i); |
2117
|
3124 |
|
3125 if (os) |
|
3126 { |
3523
|
3127 std::string mode = octave_stream::mode_as_string (os.mode ()); |
|
3128 |
|
3129 std::string arch = |
3340
|
3130 oct_mach_info::float_format_as_string (os.float_format ()); |
|
3131 |
3523
|
3132 std::string name = os.name (); |
2117
|
3133 |
3013
|
3134 buf << " " |
3559
|
3135 << std::setiosflags (std::ios::right) |
|
3136 << std::setw (4) << i << " " |
|
3137 << std::setiosflags (std::ios::left) |
|
3138 << std::setw (3) << mode.c_str () << " " |
|
3139 << std::setw (9) << arch.c_str () << " " |
3013
|
3140 << name << "\n"; |
2117
|
3141 } |
|
3142 } |
|
3143 |
3538
|
3144 buf << "\n" << std::ends; |
2117
|
3145 |
|
3146 char *tmp = buf.str (); |
|
3147 |
|
3148 retval = tmp; |
|
3149 |
|
3150 delete [] tmp; |
|
3151 |
|
3152 return retval; |
|
3153 } |
|
3154 |
|
3155 octave_value |
|
3156 octave_stream_list::do_open_file_numbers (void) const |
|
3157 { |
|
3158 Matrix retval (1, curr_len, 0.0); |
|
3159 |
|
3160 int num_open = 0; |
|
3161 |
|
3162 // Skip stdin, stdout, and stderr. |
|
3163 |
|
3164 for (int i = 3; i < curr_len; i++) |
|
3165 { |
3340
|
3166 if (list(i)) |
2117
|
3167 retval (0, num_open++) = i; |
|
3168 } |
|
3169 |
|
3170 retval.resize ((num_open > 0), num_open); |
|
3171 |
|
3172 return retval; |
|
3173 } |
|
3174 |
|
3175 int |
2609
|
3176 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117
|
3177 { |
|
3178 int retval = -1; |
|
3179 |
|
3180 if (fid.is_string ()) |
|
3181 { |
3523
|
3182 std::string nm = fid.string_value (); |
2117
|
3183 |
3531
|
3184 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
|
3185 // are unnamed. |
2117
|
3186 |
|
3187 for (int i = 3; i < curr_len; i++) |
|
3188 { |
3340
|
3189 octave_stream os = list(i); |
|
3190 |
|
3191 if (os && os.name () == nm) |
2117
|
3192 { |
|
3193 retval = i; |
|
3194 break; |
|
3195 } |
|
3196 } |
|
3197 } |
|
3198 else |
|
3199 { |
|
3200 int conv_err = 0; |
|
3201 |
|
3202 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3203 |
|
3204 if (conv_err) |
3523
|
3205 ::error ("file id must be a file object, std::string, or integer value"); |
2117
|
3206 else |
|
3207 retval = int_fid; |
|
3208 } |
|
3209 |
|
3210 return retval; |
|
3211 } |
|
3212 |
|
3213 /* |
|
3214 ;;; Local Variables: *** |
|
3215 ;;; mode: C++ *** |
|
3216 ;;; End: *** |
|
3217 */ |