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