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