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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2117
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3268
|
28 #include <cassert> |
2215
|
29 #include <cstring> |
|
30 |
3503
|
31 #include <iomanip> |
3559
|
32 #include <fstream> |
5765
|
33 #include <sstream> |
3535
|
34 #include <string> |
2117
|
35 |
4944
|
36 #include <Array.h> |
|
37 #include <Array2.h> |
|
38 #include <Array3.h> |
|
39 |
|
40 #include <Array.cc> |
|
41 |
|
42 #include "byte-swap.h" |
2117
|
43 #include "lo-ieee.h" |
|
44 #include "lo-mappers.h" |
|
45 #include "lo-utils.h" |
|
46 #include "str-vec.h" |
4153
|
47 #include "quit.h" |
2117
|
48 |
|
49 #include "error.h" |
3342
|
50 #include "input.h" |
3775
|
51 #include "oct-stdstrm.h" |
2117
|
52 #include "oct-stream.h" |
2877
|
53 #include "oct-obj.h" |
2117
|
54 #include "utils.h" |
|
55 |
6109
|
56 #undef OCTAVE_API |
|
57 #define OCTAVE_API |
|
58 |
2117
|
59 // Possible values for conv_err: |
|
60 // |
|
61 // 1 : not a real scalar |
2902
|
62 // 2 : value is NaN |
|
63 // 3 : value is not an integer |
2117
|
64 |
|
65 static int |
|
66 convert_to_valid_int (const octave_value& tc, int& conv_err) |
|
67 { |
|
68 int retval = 0; |
|
69 |
|
70 conv_err = 0; |
|
71 |
2902
|
72 double dval = tc.double_value (); |
|
73 |
|
74 if (! error_state) |
2117
|
75 { |
5389
|
76 if (! lo_ieee_isnan (dval)) |
2117
|
77 { |
2902
|
78 int ival = NINT (dval); |
|
79 |
|
80 if (ival == dval) |
|
81 retval = ival; |
2117
|
82 else |
|
83 conv_err = 3; |
|
84 } |
|
85 else |
|
86 conv_err = 2; |
|
87 } |
|
88 else |
|
89 conv_err = 1; |
|
90 |
|
91 return retval; |
|
92 } |
|
93 |
|
94 static int |
4468
|
95 get_size (double d, const std::string& who) |
2117
|
96 { |
|
97 int retval = -1; |
|
98 |
5389
|
99 if (! lo_ieee_isnan (d)) |
2117
|
100 { |
|
101 if (! xisinf (d)) |
|
102 { |
3268
|
103 if (d >= 0.0) |
2117
|
104 retval = NINT (d); |
|
105 else |
|
106 ::error ("%s: negative value invalid as size specification", |
4468
|
107 who.c_str ()); |
2117
|
108 } |
|
109 else |
|
110 retval = -1; |
|
111 } |
|
112 else |
4468
|
113 ::error ("%s: NaN is invalid as size specification", who.c_str ()); |
2117
|
114 |
|
115 return retval; |
|
116 } |
|
117 |
|
118 static void |
5275
|
119 get_size (const Array<double>& size, octave_idx_type& nr, octave_idx_type& nc, bool& one_elt_size_spec, |
4468
|
120 const std::string& who) |
2117
|
121 { |
|
122 nr = -1; |
|
123 nc = -1; |
|
124 |
3268
|
125 one_elt_size_spec = false; |
|
126 |
2117
|
127 double dnr = -1.0; |
|
128 double dnc = -1.0; |
|
129 |
5275
|
130 octave_idx_type sz_len = size.length (); |
3810
|
131 |
|
132 if (sz_len == 1) |
2601
|
133 { |
3268
|
134 one_elt_size_spec = true; |
|
135 |
3810
|
136 dnr = size (0); |
4293
|
137 |
|
138 dnc = (dnr == 0.0) ? 0.0 : 1.0; |
2601
|
139 } |
3810
|
140 else if (sz_len == 2) |
2117
|
141 { |
3810
|
142 dnr = size (0); |
|
143 |
|
144 if (! xisinf (dnr)) |
|
145 dnc = size (1); |
|
146 else |
4468
|
147 ::error ("%s: invalid size specification", who.c_str ()); |
2117
|
148 } |
|
149 else |
4468
|
150 ::error ("%s: invalid size specification", who.c_str ()); |
2117
|
151 |
|
152 if (! error_state) |
|
153 { |
4468
|
154 nr = get_size (dnr, who); |
2117
|
155 |
3268
|
156 if (! error_state && dnc >= 0.0) |
4468
|
157 nc = get_size (dnc, who); |
2117
|
158 } |
|
159 } |
|
160 |
3523
|
161 scanf_format_list::scanf_format_list (const std::string& s) |
2117
|
162 : nconv (0), curr_idx (0), list (16), buf (0) |
|
163 { |
|
164 int num_elts = 0; |
|
165 |
|
166 int n = s.length (); |
|
167 |
|
168 int i = 0; |
|
169 |
2215
|
170 int width = 0; |
2117
|
171 bool discard = false; |
|
172 char modifier = '\0'; |
|
173 char type = '\0'; |
|
174 |
|
175 bool have_more = true; |
|
176 |
|
177 while (i < n) |
|
178 { |
|
179 have_more = true; |
|
180 |
|
181 if (! buf) |
5765
|
182 buf = new std::ostringstream (); |
2117
|
183 |
|
184 if (s[i] == '%') |
|
185 { |
3483
|
186 // Process percent-escape conversion type. |
|
187 |
2215
|
188 process_conversion (s, i, n, width, discard, type, modifier, |
|
189 num_elts); |
2117
|
190 have_more = (buf != 0); |
|
191 } |
3483
|
192 else if (isspace (s[i])) |
2117
|
193 { |
3483
|
194 type = scanf_format_elt::whitespace_conversion; |
|
195 |
2215
|
196 width = 0; |
2117
|
197 discard = false; |
|
198 modifier = '\0'; |
3483
|
199 *buf << " "; |
|
200 |
|
201 while (++i < n && isspace (s[i])) |
|
202 /* skip whitespace */; |
|
203 |
|
204 add_elt_to_list (width, discard, type, modifier, num_elts); |
|
205 |
|
206 have_more = false; |
|
207 } |
|
208 else |
|
209 { |
|
210 type = scanf_format_elt::literal_conversion; |
|
211 |
|
212 width = 0; |
|
213 discard = false; |
|
214 modifier = '\0'; |
|
215 |
|
216 while (i < n && ! isspace (s[i]) && s[i] != '%') |
|
217 *buf << s[i++]; |
|
218 |
|
219 add_elt_to_list (width, discard, type, modifier, num_elts); |
|
220 |
|
221 have_more = false; |
2117
|
222 } |
|
223 |
|
224 if (nconv < 0) |
|
225 { |
|
226 have_more = false; |
|
227 break; |
|
228 } |
|
229 } |
|
230 |
|
231 if (have_more) |
2215
|
232 add_elt_to_list (width, discard, type, modifier, num_elts); |
2117
|
233 |
|
234 list.resize (num_elts); |
|
235 |
|
236 delete buf; |
|
237 } |
|
238 |
|
239 scanf_format_list::~scanf_format_list (void) |
|
240 { |
5275
|
241 octave_idx_type n = list.length (); |
|
242 |
|
243 for (octave_idx_type i = 0; i < n; i++) |
2117
|
244 { |
3340
|
245 scanf_format_elt *elt = list(i); |
2117
|
246 delete elt; |
|
247 } |
|
248 } |
|
249 |
|
250 void |
2215
|
251 scanf_format_list::add_elt_to_list (int width, bool discard, char type, |
3483
|
252 char modifier, int& num_elts, |
3523
|
253 const std::string& char_class) |
2117
|
254 { |
|
255 if (buf) |
|
256 { |
5765
|
257 std::string text = buf->str (); |
4051
|
258 |
|
259 if (! text.empty ()) |
2117
|
260 { |
4051
|
261 scanf_format_elt *elt |
|
262 = new scanf_format_elt (text.c_str (), width, discard, type, |
|
263 modifier, char_class); |
|
264 |
|
265 if (num_elts == list.length ()) |
|
266 list.resize (2 * num_elts); |
|
267 |
|
268 list(num_elts++) = elt; |
2117
|
269 } |
|
270 |
|
271 delete buf; |
|
272 buf = 0; |
|
273 } |
|
274 } |
|
275 |
3535
|
276 static std::string |
3523
|
277 expand_char_class (const std::string& s) |
3483
|
278 { |
3523
|
279 std::string retval; |
3483
|
280 |
|
281 size_t len = s.length (); |
|
282 |
|
283 size_t i = 0; |
|
284 |
|
285 while (i < len) |
|
286 { |
|
287 unsigned char c = s[i++]; |
|
288 |
|
289 if (c == '-' && i > 1 && i < len |
5760
|
290 && static_cast<unsigned char> (s[i-2]) <= static_cast<unsigned char> (s[i])) |
3483
|
291 { |
|
292 // Add all characters from the range except the first (we |
|
293 // already added it below). |
|
294 |
|
295 for (c = s[i-2]+1; c < s[i]; c++) |
|
296 retval += c; |
|
297 } |
|
298 else |
|
299 { |
|
300 // Add the character to the class. Only add '-' if it is |
|
301 // the last character in the class. |
|
302 |
|
303 if (c != '-' || i == len) |
|
304 retval += c; |
|
305 } |
|
306 } |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
2117
|
311 void |
3523
|
312 scanf_format_list::process_conversion (const std::string& s, int& i, int n, |
2215
|
313 int& width, bool& discard, char& type, |
2117
|
314 char& modifier, int& num_elts) |
|
315 { |
2215
|
316 width = 0; |
2117
|
317 discard = false; |
|
318 modifier = '\0'; |
|
319 type = '\0'; |
|
320 |
|
321 *buf << s[i++]; |
|
322 |
|
323 bool have_width = false; |
|
324 |
|
325 while (i < n) |
|
326 { |
|
327 switch (s[i]) |
|
328 { |
|
329 case '*': |
|
330 if (discard) |
|
331 nconv = -1; |
|
332 else |
|
333 { |
|
334 discard = true; |
|
335 *buf << s[i++]; |
|
336 } |
|
337 break; |
|
338 |
|
339 case '0': case '1': case '2': case '3': case '4': |
|
340 case '5': case '6': case '7': case '8': case '9': |
|
341 if (have_width) |
|
342 nconv = -1; |
|
343 else |
|
344 { |
2215
|
345 char c = s[i++]; |
|
346 width = width * 10 + c - '0'; |
2117
|
347 have_width = true; |
2215
|
348 *buf << c; |
2117
|
349 while (i < n && isdigit (s[i])) |
2215
|
350 { |
|
351 c = s[i++]; |
|
352 width = width * 10 + c - '0'; |
|
353 *buf << c; |
|
354 } |
2117
|
355 } |
|
356 break; |
|
357 |
|
358 case 'h': case 'l': case 'L': |
|
359 if (modifier != '\0') |
|
360 nconv = -1; |
|
361 else |
2663
|
362 modifier = s[i++]; |
2117
|
363 break; |
|
364 |
|
365 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
366 if (modifier == 'L') |
|
367 { |
|
368 nconv = -1; |
|
369 break; |
|
370 } |
|
371 goto fini; |
|
372 |
|
373 case 'e': case 'f': case 'g': |
|
374 if (modifier == 'h') |
|
375 { |
|
376 nconv = -1; |
|
377 break; |
|
378 } |
2663
|
379 |
|
380 // No float or long double conversions, thanks. |
|
381 *buf << 'l'; |
|
382 |
2117
|
383 goto fini; |
|
384 |
|
385 case 'c': case 's': case 'p': case '%': case '[': |
|
386 if (modifier != '\0') |
|
387 { |
|
388 nconv = -1; |
|
389 break; |
|
390 } |
|
391 goto fini; |
|
392 |
|
393 fini: |
|
394 { |
2215
|
395 if (finish_conversion (s, i, n, width, discard, type, |
2117
|
396 modifier, num_elts) == 0) |
|
397 return; |
|
398 } |
|
399 break; |
|
400 |
|
401 default: |
|
402 nconv = -1; |
|
403 break; |
|
404 } |
|
405 |
|
406 if (nconv < 0) |
|
407 break; |
|
408 } |
|
409 |
|
410 nconv = -1; |
|
411 } |
|
412 |
|
413 int |
3523
|
414 scanf_format_list::finish_conversion (const std::string& s, int& i, int n, |
2215
|
415 int& width, bool discard, char& type, |
2117
|
416 char modifier, int& num_elts) |
|
417 { |
|
418 int retval = 0; |
|
419 |
3523
|
420 std::string char_class; |
3483
|
421 |
3640
|
422 int beg_idx = -1; |
|
423 int end_idx = -1; |
|
424 |
2117
|
425 if (s[i] == '%') |
3640
|
426 { |
|
427 type = '%'; |
|
428 *buf << s[i++]; |
|
429 } |
2117
|
430 else |
|
431 { |
|
432 type = s[i]; |
|
433 |
|
434 if (s[i] == '[') |
|
435 { |
|
436 *buf << s[i++]; |
|
437 |
|
438 if (i < n) |
|
439 { |
3483
|
440 beg_idx = i; |
|
441 |
2117
|
442 if (s[i] == '^') |
|
443 { |
|
444 type = '^'; |
|
445 *buf << s[i++]; |
3483
|
446 |
|
447 if (i < n) |
|
448 { |
|
449 beg_idx = i; |
|
450 |
|
451 if (s[i] == ']') |
|
452 *buf << s[i++]; |
|
453 } |
2117
|
454 } |
|
455 else if (s[i] == ']') |
|
456 *buf << s[i++]; |
|
457 } |
|
458 |
|
459 while (i < n && s[i] != ']') |
|
460 *buf << s[i++]; |
|
461 |
|
462 if (i < n && s[i] == ']') |
3483
|
463 { |
|
464 end_idx = i-1; |
|
465 *buf << s[i++]; |
|
466 } |
2117
|
467 |
|
468 if (s[i-1] != ']') |
|
469 retval = nconv = -1; |
|
470 } |
|
471 else |
2215
|
472 *buf << s[i++]; |
3640
|
473 } |
|
474 |
|
475 nconv++; |
|
476 |
|
477 if (nconv > 0) |
|
478 { |
|
479 if (beg_idx >= 0 && end_idx >= 0) |
|
480 char_class = expand_char_class (s.substr (beg_idx, |
|
481 end_idx - beg_idx + 1)); |
|
482 |
|
483 add_elt_to_list (width, discard, type, modifier, num_elts, char_class); |
2117
|
484 } |
|
485 |
|
486 return retval; |
|
487 } |
|
488 |
|
489 void |
|
490 scanf_format_list::printme (void) const |
|
491 { |
|
492 int n = list.length (); |
|
493 |
|
494 for (int i = 0; i < n; i++) |
|
495 { |
3340
|
496 scanf_format_elt *elt = list(i); |
2117
|
497 |
3531
|
498 std::cerr |
|
499 << "width: " << elt->width << "\n" |
|
500 << "discard: " << elt->discard << "\n" |
|
501 << "type: "; |
3483
|
502 |
|
503 if (elt->type == scanf_format_elt::literal_conversion) |
3531
|
504 std::cerr << "literal text\n"; |
3483
|
505 else if (elt->type == scanf_format_elt::whitespace_conversion) |
3531
|
506 std::cerr << "whitespace\n"; |
3483
|
507 else |
3531
|
508 std::cerr << elt->type << "\n"; |
|
509 |
|
510 std::cerr |
|
511 << "modifier: " << elt->modifier << "\n" |
|
512 << "char_class: `" << undo_string_escapes (elt->char_class) << "'\n" |
|
513 << "text: `" << undo_string_escapes (elt->text) << "'\n\n"; |
2117
|
514 } |
|
515 } |
|
516 |
|
517 bool |
|
518 scanf_format_list::all_character_conversions (void) |
|
519 { |
|
520 int n = list.length (); |
|
521 |
|
522 if (n > 0) |
|
523 { |
|
524 for (int i = 0; i < n; i++) |
|
525 { |
3340
|
526 scanf_format_elt *elt = list(i); |
2117
|
527 |
|
528 switch (elt->type) |
|
529 { |
3483
|
530 case 'c': case 's': case '%': case '[': case '^': |
|
531 case scanf_format_elt::literal_conversion: |
|
532 case scanf_format_elt::whitespace_conversion: |
2117
|
533 break; |
|
534 |
|
535 default: |
|
536 return false; |
|
537 break; |
|
538 } |
|
539 } |
|
540 |
|
541 return true; |
|
542 } |
|
543 else |
|
544 return false; |
|
545 } |
|
546 |
|
547 bool |
|
548 scanf_format_list::all_numeric_conversions (void) |
|
549 { |
|
550 int n = list.length (); |
|
551 |
|
552 if (n > 0) |
|
553 { |
|
554 for (int i = 0; i < n; i++) |
|
555 { |
3340
|
556 scanf_format_elt *elt = list(i); |
2117
|
557 |
|
558 switch (elt->type) |
|
559 { |
|
560 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
561 case 'e': case 'f': case 'g': |
|
562 break; |
|
563 |
|
564 default: |
|
565 return false; |
|
566 break; |
|
567 } |
|
568 } |
|
569 |
|
570 return true; |
|
571 } |
|
572 else |
|
573 return false; |
|
574 } |
|
575 |
|
576 // Ugh again. |
|
577 |
3523
|
578 printf_format_list::printf_format_list (const std::string& s) |
2117
|
579 : nconv (0), curr_idx (0), list (16), buf (0) |
|
580 { |
|
581 int num_elts = 0; |
|
582 |
|
583 int n = s.length (); |
|
584 |
|
585 int i = 0; |
|
586 |
|
587 int args = 0; |
3643
|
588 std::string flags; |
3640
|
589 int fw = 0; |
|
590 int prec = 0; |
2117
|
591 char modifier = '\0'; |
|
592 char type = '\0'; |
|
593 |
|
594 bool have_more = true; |
3640
|
595 bool empty_buf = true; |
2117
|
596 |
4223
|
597 if (n == 0) |
2117
|
598 { |
4223
|
599 printf_format_elt *elt |
|
600 = new printf_format_elt ("", args, fw, prec, flags, type, modifier); |
|
601 |
|
602 list(num_elts++) = elt; |
|
603 |
|
604 list.resize (num_elts); |
|
605 } |
|
606 else |
|
607 { |
|
608 while (i < n) |
3640
|
609 { |
4223
|
610 have_more = true; |
|
611 |
|
612 if (! buf) |
|
613 { |
5765
|
614 buf = new std::ostringstream (); |
4223
|
615 empty_buf = true; |
|
616 } |
|
617 |
|
618 switch (s[i]) |
|
619 { |
|
620 case '%': |
3640
|
621 { |
4223
|
622 if (empty_buf) |
|
623 { |
|
624 process_conversion (s, i, n, args, flags, fw, prec, |
|
625 type, modifier, num_elts); |
|
626 |
|
627 have_more = (buf != 0); |
|
628 } |
|
629 else |
|
630 add_elt_to_list (args, flags, fw, prec, type, modifier, |
|
631 num_elts); |
3640
|
632 } |
4223
|
633 break; |
|
634 |
|
635 default: |
|
636 { |
|
637 args = 0; |
|
638 flags = ""; |
|
639 fw = 0; |
|
640 prec = 0; |
|
641 modifier = '\0'; |
|
642 type = '\0'; |
|
643 *buf << s[i++]; |
|
644 empty_buf = false; |
|
645 } |
|
646 break; |
|
647 } |
|
648 |
|
649 if (nconv < 0) |
|
650 { |
|
651 have_more = false; |
|
652 break; |
|
653 } |
2117
|
654 } |
|
655 |
4223
|
656 if (have_more) |
|
657 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); |
|
658 |
|
659 list.resize (num_elts); |
|
660 |
|
661 delete buf; |
2117
|
662 } |
|
663 } |
|
664 |
|
665 printf_format_list::~printf_format_list (void) |
|
666 { |
|
667 int n = list.length (); |
|
668 |
|
669 for (int i = 0; i < n; i++) |
|
670 { |
3340
|
671 printf_format_elt *elt = list(i); |
2117
|
672 delete elt; |
|
673 } |
|
674 } |
|
675 |
|
676 void |
3640
|
677 printf_format_list::add_elt_to_list (int args, const std::string& flags, |
|
678 int fw, int prec, char type, |
|
679 char modifier, int& num_elts) |
2117
|
680 { |
|
681 if (buf) |
|
682 { |
5765
|
683 std::string text = buf->str (); |
4051
|
684 |
|
685 if (! text.empty ()) |
2117
|
686 { |
4051
|
687 printf_format_elt *elt |
|
688 = new printf_format_elt (text.c_str (), args, fw, prec, flags, |
|
689 type, modifier); |
|
690 |
|
691 if (num_elts == list.length ()) |
|
692 list.resize (2 * num_elts); |
|
693 |
|
694 list(num_elts++) = elt; |
2117
|
695 } |
|
696 |
|
697 delete buf; |
|
698 buf = 0; |
|
699 } |
|
700 } |
|
701 |
|
702 void |
3640
|
703 printf_format_list::process_conversion |
|
704 (const std::string& s, int& i, int n, int& args, std::string& flags, |
|
705 int& fw, int& prec, char& modifier, char& type, int& num_elts) |
2117
|
706 { |
|
707 args = 0; |
3640
|
708 flags = ""; |
|
709 fw = 0; |
|
710 prec = 0; |
2117
|
711 modifier = '\0'; |
|
712 type = '\0'; |
|
713 |
|
714 *buf << s[i++]; |
|
715 |
4587
|
716 bool nxt = false; |
2117
|
717 |
|
718 while (i < n) |
|
719 { |
|
720 switch (s[i]) |
|
721 { |
|
722 case '-': case '+': case ' ': case '0': case '#': |
3640
|
723 flags += s[i]; |
2117
|
724 *buf << s[i++]; |
|
725 break; |
|
726 |
|
727 default: |
4587
|
728 nxt = true; |
2117
|
729 break; |
|
730 } |
|
731 |
4587
|
732 if (nxt) |
2117
|
733 break; |
|
734 } |
|
735 |
|
736 if (i < n) |
|
737 { |
|
738 if (s[i] == '*') |
|
739 { |
3640
|
740 fw = -1; |
2117
|
741 args++; |
|
742 *buf << s[i++]; |
|
743 } |
|
744 else |
|
745 { |
3640
|
746 if (isdigit (s[i])) |
|
747 { |
4587
|
748 int nn = 0; |
3643
|
749 std::string tmp = s.substr (i); |
4587
|
750 sscanf (tmp.c_str (), "%d%n", &fw, &nn); |
3640
|
751 } |
|
752 |
2117
|
753 while (i < n && isdigit (s[i])) |
|
754 *buf << s[i++]; |
|
755 } |
|
756 } |
|
757 |
|
758 if (i < n && s[i] == '.') |
|
759 { |
|
760 *buf << s[i++]; |
|
761 |
|
762 if (i < n) |
|
763 { |
|
764 if (s[i] == '*') |
|
765 { |
3640
|
766 prec = -1; |
2117
|
767 args++; |
|
768 *buf << s[i++]; |
|
769 } |
|
770 else |
|
771 { |
3640
|
772 if (isdigit (s[i])) |
|
773 { |
4587
|
774 int nn = 0; |
3643
|
775 std::string tmp = s.substr (i); |
4587
|
776 sscanf (tmp.c_str (), "%d%n", &prec, &nn); |
3640
|
777 } |
|
778 |
2117
|
779 while (i < n && isdigit (s[i])) |
|
780 *buf << s[i++]; |
|
781 } |
|
782 } |
|
783 } |
|
784 |
|
785 if (i < n) |
|
786 { |
|
787 switch (s[i]) |
|
788 { |
|
789 case 'h': case 'l': case 'L': |
|
790 modifier = s[i]; |
|
791 *buf << s[i++]; |
|
792 break; |
|
793 |
|
794 default: |
|
795 break; |
|
796 } |
|
797 } |
|
798 |
|
799 if (i < n) |
3640
|
800 finish_conversion (s, i, args, flags, fw, prec, modifier, type, num_elts); |
2117
|
801 else |
|
802 nconv = -1; |
|
803 } |
|
804 |
|
805 void |
3640
|
806 printf_format_list::finish_conversion |
|
807 (const std::string& s, int& i, int args, const std::string& flags, |
|
808 int fw, int prec, char modifier, char& type, int& num_elts) |
2117
|
809 |
|
810 { |
|
811 switch (s[i]) |
|
812 { |
|
813 case 'd': case 'i': case 'o': case 'x': case 'X': |
|
814 case 'u': case 'c': |
|
815 if (modifier == 'L') |
|
816 { |
|
817 nconv = -1; |
|
818 break; |
|
819 } |
|
820 goto fini; |
|
821 |
|
822 case 'f': case 'e': case 'E': case 'g': case 'G': |
|
823 if (modifier == 'h' || modifier == 'l') |
|
824 { |
|
825 nconv = -1; |
|
826 break; |
|
827 } |
|
828 goto fini; |
|
829 |
|
830 case 's': case 'p': case '%': |
|
831 if (modifier != '\0') |
|
832 { |
|
833 nconv = -1; |
|
834 break; |
|
835 } |
|
836 goto fini; |
|
837 |
|
838 fini: |
|
839 |
3640
|
840 type = s[i]; |
|
841 |
|
842 *buf << s[i++]; |
|
843 |
|
844 if (type != '%' || args != 0) |
|
845 nconv++; |
|
846 |
|
847 if (type != '%') |
|
848 args++; |
|
849 |
|
850 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); |
|
851 |
2117
|
852 break; |
|
853 |
|
854 default: |
|
855 nconv = -1; |
|
856 break; |
|
857 } |
|
858 } |
|
859 |
|
860 void |
|
861 printf_format_list::printme (void) const |
|
862 { |
|
863 int n = list.length (); |
|
864 |
|
865 for (int i = 0; i < n; i++) |
|
866 { |
3340
|
867 printf_format_elt *elt = list(i); |
2117
|
868 |
3640
|
869 std::cerr |
|
870 << "args: " << elt->args << "\n" |
|
871 << "flags: `" << elt->flags << "'\n" |
|
872 << "width: " << elt->fw << "\n" |
|
873 << "prec: " << elt->prec << "\n" |
|
874 << "type: `" << elt->type << "'\n" |
|
875 << "modifier: `" << elt->modifier << "'\n" |
|
876 << "text: `" << undo_string_escapes (elt->text) << "'\n\n"; |
2117
|
877 } |
|
878 } |
|
879 |
3145
|
880 int |
3148
|
881 octave_base_stream::file_number (void) |
3145
|
882 { |
|
883 // Kluge alert! |
|
884 |
|
885 if (name () == "stdin") |
|
886 return 0; |
|
887 |
|
888 if (name () == "stdout") |
|
889 return 1; |
|
890 |
|
891 if (name () == "stderr") |
|
892 return 2; |
|
893 |
|
894 int retval = -1; |
|
895 |
3523
|
896 std::istream *is = input_stream (); |
|
897 std::ostream *os = output_stream (); |
3145
|
898 |
3775
|
899 // There is no standard way to get the underlying file descriptor from |
|
900 // std::filebuf (nor in the GNU libstdc++-v3 implementation). We cache |
|
901 // the descriptor in c_file_ptr_buf, and then extract it here. |
|
902 |
|
903 c_file_ptr_buf *ibuf = is ? |
|
904 dynamic_cast<c_file_ptr_buf *> (is->rdbuf ()) : 0; |
|
905 c_file_ptr_buf *obuf = os ? |
|
906 dynamic_cast<c_file_ptr_buf *> (os->rdbuf ()) : 0; |
|
907 |
|
908 int i_fid = ibuf ? ibuf->file_number () : -1; |
|
909 int o_fid = obuf ? obuf->file_number () : -1; |
3145
|
910 |
|
911 if (i_fid >= 0) |
|
912 { |
|
913 if (o_fid >= 0) |
|
914 retval = (i_fid == o_fid) ? i_fid : -1; |
|
915 else |
|
916 retval = i_fid; |
|
917 } |
|
918 else if (o_fid >= 0) |
|
919 retval = o_fid; |
|
920 |
|
921 return retval; |
|
922 } |
|
923 |
2117
|
924 void |
3523
|
925 octave_base_stream::error (const std::string& msg) |
2117
|
926 { |
|
927 fail = true; |
|
928 errmsg = msg; |
|
929 } |
|
930 |
|
931 void |
4468
|
932 octave_base_stream::error (const std::string& who, const std::string& msg) |
|
933 { |
|
934 fail = true; |
6296
|
935 errmsg = who + ": " + msg; |
4468
|
936 } |
|
937 |
|
938 void |
2117
|
939 octave_base_stream::clear (void) |
|
940 { |
4889
|
941 fail = false; |
|
942 errmsg = ""; |
|
943 } |
|
944 |
|
945 void |
|
946 octave_base_stream::clearerr (void) |
|
947 { |
4888
|
948 std::istream *is = input_stream (); |
|
949 std::ostream *os = output_stream (); |
|
950 |
|
951 if (is) |
|
952 is->clear (); |
|
953 |
|
954 if (os) |
|
955 os->clear (); |
2117
|
956 } |
|
957 |
|
958 // Functions that are defined for all input streams (input streams |
|
959 // are those that define is). |
|
960 |
3536
|
961 std::string |
5275
|
962 octave_base_stream::do_gets (octave_idx_type max_len, bool& err, |
4468
|
963 bool strip_newline, const std::string& who) |
2117
|
964 { |
3523
|
965 std::string retval; |
2117
|
966 |
|
967 err = false; |
|
968 |
3523
|
969 std::istream *isp = input_stream (); |
2117
|
970 |
|
971 if (isp) |
|
972 { |
3523
|
973 std::istream& is = *isp; |
2117
|
974 |
5765
|
975 std::ostringstream buf; |
2117
|
976 |
|
977 int c = 0; |
3553
|
978 int char_count = 0; |
6345
|
979 |
|
980 if (max_len != 0) |
2117
|
981 { |
6345
|
982 while (is && (c = is.get ()) != EOF) |
2117
|
983 { |
6345
|
984 char_count++; |
|
985 |
|
986 if (c == '\n') |
|
987 { |
|
988 if (! strip_newline) |
|
989 buf << static_cast<char> (c); |
|
990 |
|
991 break; |
|
992 } |
|
993 else |
5760
|
994 buf << static_cast<char> (c); |
6345
|
995 |
|
996 if (max_len > 0 && char_count == max_len) |
|
997 break; |
2117
|
998 } |
6345
|
999 } |
|
1000 |
|
1001 if (! is.eof () && char_count > 0) |
|
1002 { |
|
1003 // GAGME. Matlab seems to check for EOF even if the last |
|
1004 // character in a file is a newline character. This is NOT |
|
1005 // what the corresponding C-library functions do. |
|
1006 int disgusting_compatibility_hack = is.get (); |
|
1007 if (! is.eof ()) |
|
1008 is.putback (disgusting_compatibility_hack); |
2117
|
1009 } |
|
1010 |
4224
|
1011 if (is.good () || (is.eof () && char_count > 0)) |
5765
|
1012 retval = buf.str (); |
4224
|
1013 else |
|
1014 { |
|
1015 err = true; |
4468
|
1016 |
4224
|
1017 if (is.eof () && char_count == 0) |
4468
|
1018 error (who, "at end of file"); |
4224
|
1019 else |
4468
|
1020 error (who, "read error"); |
4224
|
1021 } |
2117
|
1022 } |
|
1023 else |
|
1024 { |
|
1025 err = true; |
4468
|
1026 invalid_operation (who, "reading"); |
2117
|
1027 } |
|
1028 |
|
1029 return retval; |
|
1030 } |
|
1031 |
3536
|
1032 std::string |
5275
|
1033 octave_base_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117
|
1034 { |
4468
|
1035 return do_gets (max_len, err, true, who); |
2117
|
1036 } |
|
1037 |
3536
|
1038 std::string |
5275
|
1039 octave_base_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117
|
1040 { |
4468
|
1041 return do_gets (max_len, err, false, who); |
2117
|
1042 } |
|
1043 |
3779
|
1044 #if defined (__GNUG__) && ! defined (CXX_ISO_COMPLIANT_LIBRARY) |
3636
|
1045 |
3640
|
1046 #define OCTAVE_SCAN(is, fmt, arg) is.scan ((fmt).text, arg) |
3636
|
1047 |
|
1048 #else |
|
1049 |
3640
|
1050 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) |
3636
|
1051 |
5775
|
1052 // FIXME -- this needs to be fixed to handle formats which |
3779
|
1053 // specify a maximum width. |
|
1054 |
3636
|
1055 template <class T> |
|
1056 std::istream& |
3779
|
1057 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) |
3636
|
1058 { |
3779
|
1059 T& ref = *valptr; |
|
1060 |
|
1061 switch (fmt.type) |
|
1062 { |
|
1063 case 'o': |
4926
|
1064 is >> std::oct >> ref >> std::dec; |
3779
|
1065 break; |
|
1066 |
|
1067 case 'x': |
4926
|
1068 is >> std::hex >> ref >> std::dec; |
|
1069 break; |
|
1070 |
|
1071 case 'i': |
|
1072 { |
|
1073 int c1 = is.get (); |
|
1074 |
|
1075 if (! is.eof ()) |
|
1076 { |
|
1077 if (c1 == '0') |
|
1078 { |
4927
|
1079 int c2 = is.get (); |
4926
|
1080 |
|
1081 if (c2 == 'x' || c2 == 'X') |
|
1082 is >> std::hex >> ref >> std::dec; |
|
1083 else |
4927
|
1084 { |
|
1085 is.putback (c2); |
|
1086 |
|
1087 if (c2 == '0' || c2 == '1' || c2 == '2' |
|
1088 || c2 == '3' || c2 == '4' || c2 == '5' |
|
1089 || c2 == '6' || c2 == '7') |
|
1090 is >> std::oct >> ref >> std::dec; |
|
1091 else |
|
1092 ref = 0; |
|
1093 } |
4926
|
1094 } |
|
1095 else |
|
1096 { |
|
1097 is.putback (c1); |
|
1098 |
|
1099 is >> ref; |
|
1100 } |
|
1101 } |
|
1102 } |
3779
|
1103 break; |
|
1104 |
|
1105 default: |
|
1106 is >> ref; |
|
1107 break; |
|
1108 } |
3639
|
1109 |
3638
|
1110 return is; |
3636
|
1111 } |
|
1112 |
3779
|
1113 // Note that this specialization is only used for reading characters, not |
|
1114 // character strings. See BEGIN_S_CONVERSION for details. |
|
1115 |
|
1116 template<> |
|
1117 std::istream& |
4661
|
1118 octave_scan<> (std::istream& is, const scanf_format_elt& /* fmt */, |
|
1119 char* valptr) |
3779
|
1120 { |
|
1121 return is >> valptr; |
|
1122 } |
3636
|
1123 |
4595
|
1124 template std::istream& |
|
1125 octave_scan (std::istream&, const scanf_format_elt&, int*); |
|
1126 |
|
1127 template std::istream& |
|
1128 octave_scan (std::istream&, const scanf_format_elt&, long int*); |
|
1129 |
|
1130 template std::istream& |
|
1131 octave_scan (std::istream&, const scanf_format_elt&, short int*); |
|
1132 |
|
1133 template std::istream& |
|
1134 octave_scan (std::istream&, const scanf_format_elt&, unsigned int*); |
|
1135 |
|
1136 template std::istream& |
|
1137 octave_scan (std::istream&, const scanf_format_elt&, unsigned long int*); |
|
1138 |
|
1139 template std::istream& |
|
1140 octave_scan (std::istream&, const scanf_format_elt&, unsigned short int*); |
|
1141 |
|
1142 #if 0 |
|
1143 template std::istream& |
|
1144 octave_scan (std::istream&, const scanf_format_elt&, float*); |
|
1145 #endif |
|
1146 |
5403
|
1147 template<> |
5176
|
1148 std::istream& |
5403
|
1149 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double* valptr) |
5176
|
1150 { |
|
1151 double& ref = *valptr; |
|
1152 |
|
1153 switch (fmt.type) |
|
1154 { |
|
1155 case 'e': |
|
1156 case 'f': |
|
1157 case 'g': |
|
1158 { |
5259
|
1159 int c1 = EOF; |
5176
|
1160 |
|
1161 while (is && (c1 = is.get ()) != EOF && isspace (c1)) |
|
1162 /* skip whitespace */; |
|
1163 |
|
1164 if (c1 != EOF) |
|
1165 { |
|
1166 if (c1 == 'N') |
|
1167 { |
|
1168 int c2 = is.get (); |
|
1169 |
|
1170 if (c2 != EOF) |
|
1171 { |
|
1172 if (c2 == 'A') |
|
1173 { |
|
1174 int c3 = is.get (); |
|
1175 |
|
1176 if (c3 != EOF) |
|
1177 { |
|
1178 is.putback (c3); |
|
1179 |
|
1180 if (isspace (c3) || ispunct (c3)) |
|
1181 ref = octave_NA; |
|
1182 else |
|
1183 { |
|
1184 is.putback (c2); |
|
1185 is.putback (c1); |
|
1186 |
|
1187 is >> ref; |
|
1188 } |
|
1189 } |
|
1190 else |
|
1191 { |
|
1192 is.clear (); |
|
1193 |
|
1194 ref = octave_NA; |
|
1195 } |
|
1196 } |
|
1197 else if (c2 == 'a') |
|
1198 { |
|
1199 int c3 = is.get (); |
|
1200 |
|
1201 if (c3 != EOF) |
|
1202 { |
|
1203 if (c3 == 'N') |
|
1204 { |
|
1205 int c4 = is.get (); |
|
1206 |
|
1207 if (c4 != EOF) |
|
1208 { |
|
1209 is.putback (c4); |
|
1210 |
|
1211 if (isspace (c4) || ispunct (c4)) |
|
1212 ref = octave_NaN; |
|
1213 else |
|
1214 { |
|
1215 is.putback (c3); |
|
1216 is.putback (c2); |
|
1217 is.putback (c1); |
|
1218 |
|
1219 is >> ref; |
|
1220 } |
|
1221 } |
|
1222 else |
|
1223 { |
|
1224 is.clear (); |
|
1225 |
|
1226 ref = octave_NaN; |
|
1227 } |
|
1228 } |
|
1229 else |
|
1230 { |
|
1231 is.putback (c3); |
|
1232 is.putback (c2); |
|
1233 is.putback (c1); |
|
1234 |
|
1235 is >> ref; |
|
1236 } |
|
1237 } |
|
1238 } |
|
1239 else |
|
1240 { |
|
1241 is.putback (c2); |
|
1242 is.putback (c1); |
|
1243 |
|
1244 is >> ref; |
|
1245 } |
|
1246 } |
|
1247 } |
|
1248 else if (c1 == 'I') |
|
1249 { |
|
1250 int c2 = is.get (); |
|
1251 |
|
1252 if (c2 != EOF) |
|
1253 { |
|
1254 if (c2 == 'n') |
|
1255 { |
|
1256 int c3 = is.get (); |
|
1257 |
|
1258 if (c3 != EOF) |
|
1259 |
|
1260 if (c3 == 'f') |
|
1261 { |
|
1262 int c4 = is.get (); |
|
1263 |
|
1264 if (c4 != EOF) |
|
1265 { |
|
1266 is.putback (c4); |
|
1267 |
|
1268 if (isspace (c4) || ispunct (c4)) |
|
1269 ref = octave_Inf; |
|
1270 else |
|
1271 { |
|
1272 is.putback (c3); |
|
1273 is.putback (c2); |
|
1274 is.putback (c1); |
|
1275 |
|
1276 is >> ref; |
|
1277 } |
|
1278 } |
|
1279 else |
|
1280 { |
|
1281 is.clear (); |
|
1282 |
|
1283 ref = octave_Inf; |
|
1284 } |
|
1285 } |
|
1286 else |
|
1287 { |
|
1288 is.putback (c3); |
|
1289 is.putback (c2); |
|
1290 is.putback (c1); |
|
1291 |
|
1292 is >> ref; |
|
1293 } |
|
1294 } |
|
1295 else |
|
1296 { |
|
1297 is.putback (c2); |
|
1298 is.putback (c1); |
|
1299 |
|
1300 is >> ref; |
|
1301 } |
|
1302 } |
|
1303 } |
|
1304 else |
|
1305 { |
|
1306 is.putback (c1); |
|
1307 |
|
1308 is >> ref; |
|
1309 } |
|
1310 } |
|
1311 } |
|
1312 break; |
|
1313 |
|
1314 default: |
|
1315 panic_impossible (); |
|
1316 break; |
|
1317 } |
|
1318 |
|
1319 return is; |
|
1320 } |
|
1321 |
3636
|
1322 #endif |
|
1323 |
2572
|
1324 template <class T> |
|
1325 void |
3636
|
1326 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt, |
5275
|
1327 T valptr, Matrix& mval, double *data, octave_idx_type& idx, |
|
1328 octave_idx_type& conversion_count, octave_idx_type nr, octave_idx_type max_size, |
3636
|
1329 bool discard) |
2572
|
1330 { |
3640
|
1331 OCTAVE_SCAN (is, fmt, valptr); |
2572
|
1332 |
|
1333 if (is) |
|
1334 { |
|
1335 if (idx == max_size && ! discard) |
|
1336 { |
|
1337 max_size *= 2; |
|
1338 |
|
1339 if (nr > 0) |
|
1340 mval.resize (nr, max_size / nr, 0.0); |
|
1341 else |
|
1342 mval.resize (max_size, 1, 0.0); |
|
1343 |
|
1344 data = mval.fortran_vec (); |
|
1345 } |
|
1346 |
|
1347 if (! discard) |
3268
|
1348 { |
3559
|
1349 conversion_count++; |
3268
|
1350 data[idx++] = *(valptr); |
|
1351 } |
2572
|
1352 } |
|
1353 } |
|
1354 |
|
1355 template void |
3878
|
1356 do_scanf_conv (std::istream&, const scanf_format_elt&, int*, |
5275
|
1357 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572
|
1358 |
3233
|
1359 template void |
3636
|
1360 do_scanf_conv (std::istream&, const scanf_format_elt&, long int*, |
5275
|
1361 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233
|
1362 |
|
1363 template void |
3636
|
1364 do_scanf_conv (std::istream&, const scanf_format_elt&, short int*, |
5275
|
1365 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233
|
1366 |
3878
|
1367 template void |
|
1368 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned int*, |
5275
|
1369 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878
|
1370 |
|
1371 template void |
|
1372 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned long int*, |
5275
|
1373 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878
|
1374 |
|
1375 template void |
|
1376 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned short int*, |
5275
|
1377 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878
|
1378 |
2600
|
1379 #if 0 |
2572
|
1380 template void |
3636
|
1381 do_scanf_conv (std::istream&, const scanf_format_elt&, float*, |
5275
|
1382 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2600
|
1383 #endif |
2572
|
1384 |
|
1385 template void |
3636
|
1386 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
5275
|
1387 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572
|
1388 |
3483
|
1389 #define DO_WHITESPACE_CONVERSION() \ |
|
1390 do \ |
|
1391 { \ |
|
1392 int c = EOF; \ |
|
1393 \ |
|
1394 while (is && (c = is.get ()) != EOF && isspace (c)) \ |
|
1395 /* skip whitespace */; \ |
|
1396 \ |
|
1397 if (c != EOF) \ |
|
1398 is.putback (c); \ |
|
1399 } \ |
|
1400 while (0) |
|
1401 |
|
1402 #define DO_LITERAL_CONVERSION() \ |
|
1403 do \ |
|
1404 { \ |
|
1405 int c = EOF; \ |
|
1406 \ |
|
1407 int n = strlen (fmt); \ |
|
1408 int i = 0; \ |
|
1409 \ |
|
1410 while (i < n && is && (c = is.get ()) != EOF) \ |
|
1411 { \ |
5326
|
1412 if (c == static_cast<unsigned char> (fmt[i])) \ |
3483
|
1413 { \ |
|
1414 i++; \ |
|
1415 continue; \ |
|
1416 } \ |
|
1417 else \ |
|
1418 { \ |
|
1419 is.putback (c); \ |
|
1420 break; \ |
|
1421 } \ |
|
1422 } \ |
|
1423 \ |
|
1424 if (i != n) \ |
3538
|
1425 is.setstate (std::ios::failbit); \ |
3483
|
1426 } \ |
|
1427 while (0) |
|
1428 |
3640
|
1429 #define DO_PCT_CONVERSION() \ |
|
1430 do \ |
|
1431 { \ |
|
1432 int c = is.get (); \ |
|
1433 \ |
|
1434 if (c != EOF) \ |
|
1435 { \ |
|
1436 if (c != '%') \ |
|
1437 { \ |
|
1438 is.putback (c); \ |
|
1439 is.setstate (std::ios::failbit); \ |
|
1440 } \ |
|
1441 } \ |
|
1442 else \ |
|
1443 is.setstate (std::ios::failbit); \ |
|
1444 } \ |
|
1445 while (0) |
|
1446 |
3483
|
1447 #define BEGIN_C_CONVERSION() \ |
3538
|
1448 is.unsetf (std::ios::skipws); \ |
3483
|
1449 \ |
|
1450 int width = elt->width ? elt->width : 1; \ |
|
1451 \ |
4051
|
1452 char *tbuf = new char[width + 1]; \ |
3483
|
1453 \ |
|
1454 int c = EOF; \ |
|
1455 int n = 0; \ |
|
1456 \ |
|
1457 while (is && n < width && (c = is.get ()) != EOF) \ |
5760
|
1458 tbuf[n++] = static_cast<char> (c); \ |
4051
|
1459 \ |
|
1460 tbuf[n] = '\0'; \ |
3483
|
1461 \ |
5266
|
1462 if (n > 0 && c == EOF) \ |
|
1463 is.clear (); \ |
|
1464 \ |
4051
|
1465 std::string tmp = tbuf; \ |
|
1466 \ |
|
1467 delete [] tbuf |
3483
|
1468 |
|
1469 // For a `%s' format, skip initial whitespace and then read until the |
5338
|
1470 // next whitespace character or until WIDTH characters have been read. |
3483
|
1471 #define BEGIN_S_CONVERSION() \ |
|
1472 int width = elt->width; \ |
|
1473 \ |
4051
|
1474 std::string tmp; \ |
3483
|
1475 \ |
|
1476 do \ |
|
1477 { \ |
|
1478 if (width) \ |
5338
|
1479 { \ |
|
1480 char *tbuf = new char [width+1]; \ |
|
1481 \ |
|
1482 int c = EOF; \ |
|
1483 \ |
|
1484 int n = 0; \ |
|
1485 \ |
|
1486 while (is && (c = is.get ()) != EOF) \ |
|
1487 { \ |
|
1488 if (! isspace (c)) \ |
|
1489 { \ |
|
1490 tbuf[n++] = static_cast<char> (c); \ |
|
1491 break; \ |
|
1492 } \ |
|
1493 } \ |
4051
|
1494 \ |
5338
|
1495 while (is && n < width && (c = is.get ()) != EOF) \ |
|
1496 { \ |
|
1497 if (isspace (c)) \ |
|
1498 { \ |
|
1499 is.putback (c); \ |
|
1500 break; \ |
|
1501 } \ |
|
1502 else \ |
|
1503 tbuf[n++] = static_cast<char> (c); \ |
|
1504 } \ |
3483
|
1505 \ |
5338
|
1506 tbuf[n] = '\0'; \ |
|
1507 \ |
|
1508 if (n > 0 && c == EOF) \ |
|
1509 is.clear (); \ |
|
1510 \ |
4051
|
1511 tmp = tbuf; \ |
5338
|
1512 \ |
4051
|
1513 delete [] tbuf; \ |
5338
|
1514 } \ |
3483
|
1515 else \ |
5338
|
1516 { \ |
|
1517 is >> std::ws >> tmp; \ |
|
1518 } \ |
3483
|
1519 } \ |
|
1520 while (0) |
|
1521 |
|
1522 // This format must match a nonempty sequence of characters. |
|
1523 #define BEGIN_CHAR_CLASS_CONVERSION() \ |
|
1524 int width = elt->width; \ |
|
1525 \ |
4051
|
1526 std::string tmp; \ |
3483
|
1527 \ |
|
1528 do \ |
|
1529 { \ |
|
1530 if (width) \ |
|
1531 { \ |
4051
|
1532 char *tbuf = new char[width+1]; \ |
|
1533 \ |
|
1534 OCTAVE_SCAN (is, *elt, tbuf); \ |
3483
|
1535 \ |
4051
|
1536 tbuf[width] = '\0'; \ |
|
1537 tmp = tbuf; \ |
|
1538 delete [] tbuf; \ |
3483
|
1539 } \ |
|
1540 else \ |
|
1541 { \ |
5765
|
1542 std::ostringstream buf; \ |
3483
|
1543 \ |
3523
|
1544 std::string char_class = elt->char_class; \ |
3483
|
1545 \ |
|
1546 int c = EOF; \ |
|
1547 \ |
|
1548 if (elt->type == '[') \ |
|
1549 { \ |
|
1550 while (is && (c = is.get ()) != EOF \ |
|
1551 && char_class.find (c) != NPOS) \ |
5760
|
1552 buf << static_cast<char> (c); \ |
3483
|
1553 } \ |
|
1554 else \ |
|
1555 { \ |
|
1556 while (is && (c = is.get ()) != EOF \ |
|
1557 && char_class.find (c) == NPOS) \ |
5760
|
1558 buf << static_cast<char> (c); \ |
3483
|
1559 } \ |
|
1560 \ |
|
1561 if (c != EOF) \ |
|
1562 is.putback (c); \ |
|
1563 \ |
5765
|
1564 tmp = buf.str (); \ |
3483
|
1565 \ |
4051
|
1566 if (tmp.empty ()) \ |
3538
|
1567 is.setstate (std::ios::failbit); \ |
3483
|
1568 } \ |
|
1569 } \ |
|
1570 while (0) |
|
1571 |
3410
|
1572 #define FINISH_CHARACTER_CONVERSION() \ |
|
1573 do \ |
|
1574 { \ |
4051
|
1575 width = tmp.length (); \ |
3410
|
1576 \ |
|
1577 if (is) \ |
|
1578 { \ |
|
1579 int i = 0; \ |
|
1580 \ |
|
1581 if (! discard) \ |
|
1582 { \ |
|
1583 conversion_count++; \ |
|
1584 \ |
|
1585 while (i < width && tmp[i] != '\0') \ |
|
1586 { \ |
|
1587 if (data_index == max_size) \ |
|
1588 { \ |
|
1589 max_size *= 2; \ |
|
1590 \ |
4420
|
1591 if (all_char_conv) \ |
3410
|
1592 { \ |
4420
|
1593 if (one_elt_size_spec) \ |
3410
|
1594 mval.resize (1, max_size, 0.0); \ |
4420
|
1595 else if (nr > 0) \ |
|
1596 mval.resize (nr, max_size / nr, 0.0); \ |
3410
|
1597 else \ |
4420
|
1598 panic_impossible (); \ |
3410
|
1599 } \ |
4420
|
1600 else if (nr > 0) \ |
|
1601 { \ |
|
1602 if (nc <= 0) \ |
|
1603 mval.resize (nr, max_size / nr, 0.0); \ |
|
1604 else \ |
|
1605 panic_impossible (); \ |
|
1606 } \ |
|
1607 else \ |
|
1608 mval.resize (max_size, 1, 0.0); \ |
3410
|
1609 \ |
|
1610 data = mval.fortran_vec (); \ |
|
1611 } \ |
|
1612 \ |
|
1613 data[data_index++] = tmp[i++]; \ |
|
1614 } \ |
|
1615 } \ |
|
1616 } \ |
|
1617 } \ |
|
1618 while (0) |
2117
|
1619 |
|
1620 octave_value |
|
1621 octave_base_stream::do_scanf (scanf_format_list& fmt_list, |
5275
|
1622 octave_idx_type nr, octave_idx_type nc, bool one_elt_size_spec, |
|
1623 octave_idx_type& conversion_count, const std::string& who) |
2117
|
1624 { |
3268
|
1625 conversion_count = 0; |
|
1626 |
3640
|
1627 int nconv = fmt_list.num_conversions (); |
|
1628 |
5275
|
1629 octave_idx_type data_index = 0; |
2121
|
1630 |
2117
|
1631 octave_value retval = Matrix (); |
|
1632 |
3268
|
1633 if (nr == 0 || nc == 0) |
|
1634 { |
|
1635 if (one_elt_size_spec) |
|
1636 nc = 0; |
|
1637 |
|
1638 return Matrix (nr, nc, 0.0); |
|
1639 } |
|
1640 |
3523
|
1641 std::istream *isp = input_stream (); |
2117
|
1642 |
|
1643 bool all_char_conv = fmt_list.all_character_conversions (); |
|
1644 |
|
1645 Matrix mval; |
|
1646 double *data = 0; |
5275
|
1647 octave_idx_type max_size = 0; |
|
1648 octave_idx_type max_conv = 0; |
|
1649 |
|
1650 octave_idx_type final_nr = 0; |
|
1651 octave_idx_type final_nc = 0; |
2117
|
1652 |
3268
|
1653 if (all_char_conv) |
|
1654 { |
4420
|
1655 // Any of these could be resized later (if we have %s |
|
1656 // conversions, we may read more than one element for each |
|
1657 // conversion). |
|
1658 |
3268
|
1659 if (one_elt_size_spec) |
|
1660 { |
3410
|
1661 max_size = 512; |
|
1662 mval.resize (1, max_size, 0.0); |
|
1663 |
3268
|
1664 if (nr > 0) |
|
1665 max_conv = nr; |
|
1666 } |
4420
|
1667 else if (nr > 0) |
3268
|
1668 { |
4420
|
1669 if (nc > 0) |
|
1670 { |
|
1671 mval.resize (nr, nc, 0.0); |
|
1672 max_size = max_conv = nr * nc; |
|
1673 } |
|
1674 else |
|
1675 { |
|
1676 mval.resize (nr, 32, 0.0); |
|
1677 max_size = nr * 32; |
|
1678 } |
3268
|
1679 } |
4420
|
1680 else |
|
1681 panic_impossible (); |
3268
|
1682 } |
|
1683 else if (nr > 0) |
2117
|
1684 { |
|
1685 if (nc > 0) |
|
1686 { |
4420
|
1687 // Will not resize later. |
2117
|
1688 mval.resize (nr, nc, 0.0); |
|
1689 max_size = nr * nc; |
3268
|
1690 max_conv = max_size; |
2117
|
1691 } |
|
1692 else |
|
1693 { |
4420
|
1694 // Maybe resize later. |
2117
|
1695 mval.resize (nr, 32, 0.0); |
2121
|
1696 max_size = nr * 32; |
2117
|
1697 } |
|
1698 } |
|
1699 else |
|
1700 { |
4420
|
1701 // Maybe resize later. |
2117
|
1702 mval.resize (32, 1, 0.0); |
|
1703 max_size = 32; |
|
1704 } |
|
1705 |
4420
|
1706 data = mval.fortran_vec (); |
|
1707 |
2117
|
1708 if (isp) |
|
1709 { |
3523
|
1710 std::istream& is = *isp; |
2117
|
1711 |
|
1712 const scanf_format_elt *elt = fmt_list.first (); |
|
1713 |
3538
|
1714 std::ios::fmtflags flags = is.flags (); |
2213
|
1715 |
2117
|
1716 for (;;) |
|
1717 { |
4153
|
1718 OCTAVE_QUIT; |
|
1719 |
2117
|
1720 if (elt) |
|
1721 { |
3268
|
1722 if (max_conv > 0 && conversion_count == max_conv) |
|
1723 { |
|
1724 if (all_char_conv && one_elt_size_spec) |
|
1725 { |
|
1726 final_nr = 1; |
|
1727 final_nc = data_index; |
|
1728 } |
|
1729 else |
|
1730 { |
|
1731 final_nr = nr; |
|
1732 final_nc = (data_index - 1) / nr + 1; |
|
1733 } |
|
1734 |
|
1735 break; |
|
1736 } |
|
1737 else if (data_index == max_size) |
2117
|
1738 { |
3410
|
1739 max_size *= 2; |
|
1740 |
4420
|
1741 if (all_char_conv) |
2687
|
1742 { |
4420
|
1743 if (one_elt_size_spec) |
3410
|
1744 mval.resize (1, max_size, 0.0); |
4420
|
1745 else if (nr > 0) |
|
1746 mval.resize (nr, max_size / nr, 0.0); |
3410
|
1747 else |
4420
|
1748 panic_impossible (); |
2687
|
1749 } |
4420
|
1750 else if (nr > 0) |
|
1751 { |
|
1752 if (nc <= 0) |
|
1753 mval.resize (nr, max_size / nr, 0.0); |
|
1754 else |
|
1755 panic_impossible (); |
|
1756 } |
|
1757 else |
|
1758 mval.resize (max_size, 1, 0.0); |
3410
|
1759 |
|
1760 data = mval.fortran_vec (); |
2117
|
1761 } |
|
1762 |
|
1763 const char *fmt = elt->text; |
|
1764 |
|
1765 bool discard = elt->discard; |
|
1766 |
|
1767 switch (elt->type) |
|
1768 { |
3483
|
1769 case scanf_format_elt::whitespace_conversion: |
|
1770 DO_WHITESPACE_CONVERSION (); |
|
1771 break; |
|
1772 |
|
1773 case scanf_format_elt::literal_conversion: |
|
1774 DO_LITERAL_CONVERSION (); |
|
1775 break; |
|
1776 |
2117
|
1777 case '%': |
3640
|
1778 DO_PCT_CONVERSION (); |
3483
|
1779 break; |
2117
|
1780 |
3878
|
1781 case 'd': case 'i': |
2117
|
1782 { |
3233
|
1783 switch (elt->modifier) |
|
1784 { |
|
1785 case 'h': |
|
1786 { |
|
1787 short int tmp; |
3779
|
1788 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1789 data_index, conversion_count, |
3233
|
1790 nr, max_size, discard); |
|
1791 } |
3483
|
1792 break; |
3233
|
1793 |
|
1794 case 'l': |
|
1795 { |
|
1796 long int tmp; |
3779
|
1797 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1798 data_index, conversion_count, |
3233
|
1799 nr, max_size, discard); |
|
1800 } |
3483
|
1801 break; |
3233
|
1802 |
|
1803 default: |
|
1804 { |
|
1805 int tmp; |
3779
|
1806 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1807 data_index, conversion_count, |
3233
|
1808 nr, max_size, discard); |
|
1809 } |
3483
|
1810 break; |
3233
|
1811 } |
2117
|
1812 } |
3483
|
1813 break; |
2117
|
1814 |
3878
|
1815 case 'o': case 'u': case 'x': |
|
1816 { |
|
1817 switch (elt->modifier) |
|
1818 { |
|
1819 case 'h': |
|
1820 { |
|
1821 unsigned short int tmp; |
|
1822 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1823 data_index, conversion_count, |
|
1824 nr, max_size, discard); |
|
1825 } |
|
1826 break; |
|
1827 |
|
1828 case 'l': |
|
1829 { |
|
1830 unsigned long int tmp; |
|
1831 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1832 data_index, conversion_count, |
|
1833 nr, max_size, discard); |
|
1834 } |
|
1835 break; |
|
1836 |
|
1837 default: |
|
1838 { |
|
1839 unsigned int tmp; |
|
1840 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1841 data_index, conversion_count, |
|
1842 nr, max_size, discard); |
|
1843 } |
|
1844 break; |
|
1845 } |
|
1846 } |
|
1847 break; |
|
1848 |
2117
|
1849 case 'e': case 'f': case 'g': |
|
1850 { |
2600
|
1851 double tmp; |
|
1852 |
3779
|
1853 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1854 data_index, conversion_count, |
2600
|
1855 nr, max_size, discard); |
2117
|
1856 } |
3483
|
1857 break; |
2117
|
1858 |
2213
|
1859 case 'c': |
3410
|
1860 { |
3483
|
1861 BEGIN_C_CONVERSION (); |
3410
|
1862 |
|
1863 FINISH_CHARACTER_CONVERSION (); |
3483
|
1864 |
|
1865 is.setf (flags); |
3410
|
1866 } |
|
1867 break; |
2213
|
1868 |
2117
|
1869 case 's': |
|
1870 { |
3483
|
1871 BEGIN_S_CONVERSION (); |
3268
|
1872 |
3410
|
1873 FINISH_CHARACTER_CONVERSION (); |
2117
|
1874 } |
3483
|
1875 break; |
|
1876 |
|
1877 case '[': case '^': |
|
1878 { |
|
1879 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1880 |
|
1881 FINISH_CHARACTER_CONVERSION (); |
|
1882 } |
|
1883 break; |
|
1884 |
|
1885 case 'p': |
4468
|
1886 error ("%s: unsupported format specifier", who.c_str ()); |
2117
|
1887 break; |
|
1888 |
|
1889 default: |
4468
|
1890 error ("%s: internal format error", who.c_str ()); |
2117
|
1891 break; |
|
1892 } |
|
1893 |
|
1894 if (! ok ()) |
|
1895 { |
|
1896 break; |
|
1897 } |
|
1898 else if (! is) |
|
1899 { |
3268
|
1900 if (all_char_conv) |
2117
|
1901 { |
3268
|
1902 if (one_elt_size_spec) |
|
1903 { |
|
1904 final_nr = 1; |
|
1905 final_nc = data_index; |
|
1906 } |
|
1907 else if (data_index > nr) |
2117
|
1908 { |
2759
|
1909 final_nr = nr; |
3268
|
1910 final_nc = (data_index - 1) / nr + 1; |
2117
|
1911 } |
|
1912 else |
|
1913 { |
3268
|
1914 final_nr = data_index; |
|
1915 final_nc = 1; |
|
1916 } |
|
1917 } |
|
1918 else if (nr > 0) |
|
1919 { |
|
1920 if (data_index > nr) |
|
1921 { |
|
1922 final_nr = nr; |
|
1923 final_nc = (data_index - 1) / nr + 1; |
|
1924 } |
|
1925 else |
|
1926 { |
|
1927 final_nr = data_index; |
2117
|
1928 final_nc = 1; |
|
1929 } |
|
1930 } |
|
1931 else |
|
1932 { |
3268
|
1933 final_nr = data_index; |
2759
|
1934 final_nc = 1; |
|
1935 } |
|
1936 |
3337
|
1937 // If it looks like we have a matching failure, then |
|
1938 // reset the failbit in the stream state. |
|
1939 |
3538
|
1940 if (is.rdstate () & std::ios::failbit) |
|
1941 is.clear (is.rdstate () & (~std::ios::failbit)); |
3337
|
1942 |
5775
|
1943 // FIXME -- is this the right thing to do? |
3342
|
1944 |
|
1945 if (interactive && name () == "stdin") |
2759
|
1946 { |
|
1947 is.clear (); |
|
1948 |
|
1949 // Skip to end of line. |
|
1950 |
|
1951 bool err; |
4468
|
1952 do_gets (-1, err, false, who); |
2117
|
1953 } |
|
1954 |
|
1955 break; |
|
1956 } |
|
1957 } |
|
1958 else |
|
1959 { |
4468
|
1960 error ("%s: internal format error", who.c_str ()); |
2117
|
1961 break; |
|
1962 } |
|
1963 |
3640
|
1964 elt = fmt_list.next (nconv > 0); |
2117
|
1965 } |
|
1966 } |
|
1967 |
|
1968 if (ok ()) |
|
1969 { |
2121
|
1970 mval.resize (final_nr, final_nc, 0.0); |
2117
|
1971 |
3268
|
1972 retval = mval; |
|
1973 |
2117
|
1974 if (all_char_conv) |
5279
|
1975 retval = retval.convert_to_str (false, true); |
2117
|
1976 } |
|
1977 |
|
1978 return retval; |
|
1979 } |
|
1980 |
|
1981 octave_value |
3810
|
1982 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
5275
|
1983 octave_idx_type& conversion_count, const std::string& who) |
2117
|
1984 { |
|
1985 octave_value retval = Matrix (); |
|
1986 |
3559
|
1987 conversion_count = 0; |
2117
|
1988 |
3523
|
1989 std::istream *isp = input_stream (); |
2117
|
1990 |
|
1991 if (isp) |
|
1992 { |
|
1993 scanf_format_list fmt_list (fmt); |
|
1994 |
3640
|
1995 if (fmt_list.num_conversions () == -1) |
4468
|
1996 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
1997 else |
2117
|
1998 { |
5275
|
1999 octave_idx_type nr = -1; |
|
2000 octave_idx_type nc = -1; |
3640
|
2001 |
|
2002 bool one_elt_size_spec; |
|
2003 |
4468
|
2004 get_size (size, nr, nc, one_elt_size_spec, who); |
3640
|
2005 |
|
2006 if (! error_state) |
|
2007 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, |
4468
|
2008 conversion_count, who); |
2215
|
2009 } |
|
2010 } |
|
2011 else |
4468
|
2012 invalid_operation (who, "reading"); |
2572
|
2013 |
|
2014 return retval; |
|
2015 } |
|
2016 |
2712
|
2017 bool |
|
2018 octave_base_stream::do_oscanf (const scanf_format_elt *elt, |
4468
|
2019 octave_value& retval, const std::string& who) |
2572
|
2020 { |
2712
|
2021 bool quit = false; |
2215
|
2022 |
3523
|
2023 std::istream *isp = input_stream (); |
2215
|
2024 |
|
2025 if (isp) |
|
2026 { |
3523
|
2027 std::istream& is = *isp; |
2215
|
2028 |
3538
|
2029 std::ios::fmtflags flags = is.flags (); |
2215
|
2030 |
|
2031 if (elt) |
|
2032 { |
|
2033 const char *fmt = elt->text; |
|
2034 |
|
2035 bool discard = elt->discard; |
|
2036 |
|
2037 switch (elt->type) |
|
2038 { |
3483
|
2039 case scanf_format_elt::whitespace_conversion: |
|
2040 DO_WHITESPACE_CONVERSION (); |
|
2041 break; |
|
2042 |
|
2043 case scanf_format_elt::literal_conversion: |
|
2044 DO_LITERAL_CONVERSION (); |
|
2045 break; |
|
2046 |
2215
|
2047 case '%': |
|
2048 { |
3640
|
2049 DO_PCT_CONVERSION (); |
|
2050 |
|
2051 if (! is) |
2712
|
2052 quit = true; |
3640
|
2053 |
2215
|
2054 } |
|
2055 break; |
|
2056 |
3878
|
2057 case 'd': case 'i': |
2215
|
2058 { |
|
2059 int tmp; |
|
2060 |
3640
|
2061 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
2062 { |
|
2063 if (! discard) |
4233
|
2064 retval = tmp; |
2712
|
2065 } |
|
2066 else |
|
2067 quit = true; |
2215
|
2068 } |
|
2069 break; |
|
2070 |
3878
|
2071 case 'o': case 'u': case 'x': |
|
2072 { |
|
2073 long int tmp; |
|
2074 |
|
2075 if (OCTAVE_SCAN (is, *elt, &tmp)) |
|
2076 { |
|
2077 if (! discard) |
4254
|
2078 retval = tmp; |
3878
|
2079 } |
|
2080 else |
|
2081 quit = true; |
|
2082 } |
|
2083 break; |
|
2084 |
2215
|
2085 case 'e': case 'f': case 'g': |
|
2086 { |
2600
|
2087 double tmp; |
|
2088 |
3640
|
2089 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
2090 { |
|
2091 if (! discard) |
|
2092 retval = tmp; |
|
2093 } |
|
2094 else |
|
2095 quit = true; |
2215
|
2096 } |
|
2097 break; |
|
2098 |
|
2099 case 'c': |
|
2100 { |
3483
|
2101 BEGIN_C_CONVERSION (); |
|
2102 |
|
2103 if (! discard) |
|
2104 retval = tmp; |
|
2105 |
|
2106 if (! is) |
2712
|
2107 quit = true; |
2215
|
2108 |
|
2109 is.setf (flags); |
|
2110 } |
|
2111 break; |
|
2112 |
|
2113 case 's': |
|
2114 { |
3483
|
2115 BEGIN_S_CONVERSION (); |
|
2116 |
|
2117 if (! discard) |
|
2118 retval = tmp; |
2572
|
2119 |
3268
|
2120 if (! is) |
|
2121 quit = true; |
2215
|
2122 } |
|
2123 break; |
|
2124 |
3483
|
2125 case '[': case '^': |
|
2126 { |
|
2127 BEGIN_CHAR_CLASS_CONVERSION (); |
|
2128 |
|
2129 if (! discard) |
|
2130 retval = tmp; |
|
2131 |
|
2132 if (! is) |
|
2133 quit = true; |
|
2134 } |
|
2135 break; |
|
2136 |
|
2137 case 'p': |
4468
|
2138 error ("%s: unsupported format specifier", who.c_str ()); |
2215
|
2139 break; |
|
2140 |
|
2141 default: |
4468
|
2142 error ("%s: internal format error", who.c_str ()); |
2215
|
2143 break; |
|
2144 } |
|
2145 } |
|
2146 |
|
2147 if (ok () && is.fail ()) |
|
2148 { |
4468
|
2149 error ("%s: read error", who.c_str ()); |
3483
|
2150 |
5775
|
2151 // FIXME -- is this the right thing to do? |
3342
|
2152 |
|
2153 if (interactive && name () == "stdin") |
2215
|
2154 { |
|
2155 // Skip to end of line. |
|
2156 |
|
2157 bool err; |
4468
|
2158 do_gets (-1, err, false, who); |
2215
|
2159 } |
|
2160 } |
|
2161 } |
|
2162 |
2712
|
2163 return quit; |
2215
|
2164 } |
|
2165 |
|
2166 octave_value_list |
4468
|
2167 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
2168 { |
|
2169 octave_value_list retval; |
|
2170 |
3523
|
2171 std::istream *isp = input_stream (); |
2215
|
2172 |
|
2173 if (isp) |
|
2174 { |
3523
|
2175 std::istream& is = *isp; |
2215
|
2176 |
|
2177 scanf_format_list fmt_list (fmt); |
|
2178 |
|
2179 int nconv = fmt_list.num_conversions (); |
|
2180 |
3640
|
2181 if (nconv == -1) |
4468
|
2182 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
2183 else |
2215
|
2184 { |
3640
|
2185 is.clear (); |
|
2186 |
|
2187 int len = fmt_list.length (); |
|
2188 |
|
2189 retval.resize (nconv+1, Matrix ()); |
|
2190 |
|
2191 const scanf_format_elt *elt = fmt_list.first (); |
|
2192 |
|
2193 int num_values = 0; |
|
2194 |
|
2195 bool quit = false; |
|
2196 |
5275
|
2197 for (octave_idx_type i = 0; i < len; i++) |
3640
|
2198 { |
|
2199 octave_value tmp; |
|
2200 |
4468
|
2201 quit = do_oscanf (elt, tmp, who); |
3640
|
2202 |
|
2203 if (quit) |
|
2204 break; |
|
2205 else |
|
2206 { |
|
2207 if (tmp.is_defined ()) |
|
2208 retval (num_values++) = tmp; |
|
2209 |
|
2210 if (! ok ()) |
|
2211 break; |
|
2212 |
|
2213 elt = fmt_list.next (nconv > 0); |
|
2214 } |
|
2215 } |
|
2216 |
4233
|
2217 retval(nconv) = num_values; |
3640
|
2218 |
|
2219 if (! quit) |
|
2220 { |
|
2221 // Pick up any trailing stuff. |
|
2222 if (ok () && len > nconv) |
|
2223 { |
|
2224 octave_value tmp; |
3704
|
2225 |
|
2226 elt = fmt_list.next (); |
|
2227 |
4468
|
2228 do_oscanf (elt, tmp, who); |
3640
|
2229 } |
|
2230 } |
2117
|
2231 } |
|
2232 } |
|
2233 else |
4468
|
2234 invalid_operation (who, "reading"); |
2117
|
2235 |
|
2236 return retval; |
|
2237 } |
|
2238 |
|
2239 // Functions that are defined for all output streams (output streams |
|
2240 // are those that define os). |
|
2241 |
|
2242 int |
|
2243 octave_base_stream::flush (void) |
|
2244 { |
|
2245 int retval = -1; |
|
2246 |
3523
|
2247 std::ostream *os = output_stream (); |
2117
|
2248 |
|
2249 if (os) |
|
2250 { |
|
2251 os->flush (); |
|
2252 |
|
2253 if (os->good ()) |
|
2254 retval = 0; |
|
2255 } |
|
2256 else |
|
2257 invalid_operation ("fflush", "writing"); |
|
2258 |
|
2259 return retval; |
|
2260 } |
|
2261 |
|
2262 class |
|
2263 printf_value_cache |
|
2264 { |
|
2265 public: |
|
2266 |
3653
|
2267 enum state { ok, conversion_error }; |
2117
|
2268 |
|
2269 printf_value_cache (const octave_value_list& args) |
|
2270 : values (args), val_idx (0), elt_idx (0), |
|
2271 n_vals (values.length ()), n_elts (0), data (0), |
|
2272 curr_state (ok) { } |
|
2273 |
|
2274 ~printf_value_cache (void) { } |
|
2275 |
|
2276 // Get the current value as a double and advance the internal pointer. |
|
2277 double double_value (void); |
|
2278 |
|
2279 // Get the current value as an int and advance the internal pointer. |
|
2280 int int_value (void); |
|
2281 |
|
2282 // Get the current value as a string and advance the internal pointer. |
3523
|
2283 std::string string_value (void); |
2117
|
2284 |
3145
|
2285 operator bool () const { return (curr_state == ok); } |
2117
|
2286 |
3653
|
2287 bool exhausted (void) { return (val_idx >= n_vals); } |
2117
|
2288 |
|
2289 private: |
|
2290 |
|
2291 const octave_value_list values; |
|
2292 int val_idx; |
|
2293 int elt_idx; |
|
2294 int n_vals; |
|
2295 int n_elts; |
|
2296 const double *data; |
4874
|
2297 NDArray curr_val; |
2117
|
2298 state curr_state; |
|
2299 |
|
2300 // Must create value cache with values! |
|
2301 |
|
2302 printf_value_cache (void); |
|
2303 |
|
2304 // No copying! |
|
2305 |
|
2306 printf_value_cache (const printf_value_cache&); |
|
2307 |
|
2308 printf_value_cache& operator = (const printf_value_cache&); |
|
2309 }; |
|
2310 |
|
2311 double |
|
2312 printf_value_cache::double_value (void) |
|
2313 { |
|
2314 double retval = 0.0; |
|
2315 |
3711
|
2316 if (exhausted ()) |
|
2317 curr_state = conversion_error; |
|
2318 |
|
2319 while (! exhausted ()) |
2117
|
2320 { |
|
2321 if (! data) |
|
2322 { |
|
2323 octave_value tmp_val = values (val_idx); |
|
2324 |
5476
|
2325 // Force string conversion here for compatibility. |
|
2326 |
|
2327 curr_val = tmp_val.array_value (true); |
2117
|
2328 |
|
2329 if (! error_state) |
|
2330 { |
|
2331 elt_idx = 0; |
|
2332 n_elts = curr_val.length (); |
|
2333 data = curr_val.data (); |
|
2334 } |
|
2335 else |
|
2336 { |
|
2337 curr_state = conversion_error; |
|
2338 break; |
|
2339 } |
|
2340 } |
|
2341 |
|
2342 if (elt_idx < n_elts) |
|
2343 { |
3653
|
2344 retval = data[elt_idx++]; |
|
2345 |
|
2346 if (elt_idx >= n_elts) |
|
2347 { |
|
2348 elt_idx = 0; |
|
2349 val_idx++; |
|
2350 data = 0; |
|
2351 } |
|
2352 |
2117
|
2353 break; |
|
2354 } |
|
2355 else |
|
2356 { |
|
2357 val_idx++; |
|
2358 data = 0; |
3969
|
2359 |
|
2360 if (n_elts == 0 && exhausted ()) |
|
2361 curr_state = conversion_error; |
|
2362 |
2117
|
2363 continue; |
|
2364 } |
|
2365 } |
|
2366 |
|
2367 return retval; |
|
2368 } |
|
2369 |
|
2370 int |
|
2371 printf_value_cache::int_value (void) |
|
2372 { |
|
2373 int retval = 0; |
|
2374 |
|
2375 double dval = double_value (); |
|
2376 |
|
2377 if (! error_state) |
|
2378 { |
|
2379 if (D_NINT (dval) == dval) |
|
2380 retval = NINT (dval); |
|
2381 else |
|
2382 curr_state = conversion_error; |
|
2383 } |
|
2384 |
|
2385 return retval; |
|
2386 } |
|
2387 |
3536
|
2388 std::string |
2117
|
2389 printf_value_cache::string_value (void) |
|
2390 { |
3523
|
2391 std::string retval; |
2117
|
2392 |
4425
|
2393 if (exhausted ()) |
|
2394 curr_state = conversion_error; |
4257
|
2395 else |
2117
|
2396 { |
4425
|
2397 octave_value tval = values (val_idx++); |
|
2398 |
|
2399 if (tval.rows () == 1) |
|
2400 retval = tval.string_value (); |
|
2401 else |
|
2402 { |
|
2403 // In the name of Matlab compatibility. |
|
2404 |
|
2405 charMatrix chm = tval.char_matrix_value (); |
|
2406 |
5275
|
2407 octave_idx_type nr = chm.rows (); |
|
2408 octave_idx_type nc = chm.columns (); |
4425
|
2409 |
|
2410 int k = 0; |
|
2411 |
|
2412 retval.resize (nr * nc, '\0'); |
|
2413 |
5275
|
2414 for (octave_idx_type j = 0; j < nc; j++) |
|
2415 for (octave_idx_type i = 0; i < nr; i++) |
4425
|
2416 retval[k++] = chm(i,j); |
|
2417 } |
|
2418 |
|
2419 if (error_state) |
|
2420 curr_state = conversion_error; |
2117
|
2421 } |
4257
|
2422 |
2117
|
2423 return retval; |
|
2424 } |
|
2425 |
|
2426 // Ugh again and again. |
|
2427 |
2572
|
2428 template <class T> |
3620
|
2429 int |
3523
|
2430 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
4468
|
2431 int sa_2, T arg, const std::string& who) |
2572
|
2432 { |
3620
|
2433 int retval = 0; |
|
2434 |
2572
|
2435 switch (nsa) |
|
2436 { |
|
2437 case 2: |
3640
|
2438 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572
|
2439 break; |
|
2440 |
|
2441 case 1: |
3640
|
2442 retval = octave_format (os, fmt, sa_1, arg); |
2572
|
2443 break; |
|
2444 |
|
2445 case 0: |
3640
|
2446 retval = octave_format (os, fmt, arg); |
2572
|
2447 break; |
|
2448 |
|
2449 default: |
4468
|
2450 ::error ("%s: internal error handling format", who.c_str ()); |
2572
|
2451 break; |
|
2452 } |
3620
|
2453 |
|
2454 return retval; |
2572
|
2455 } |
|
2456 |
3620
|
2457 template int |
4468
|
2458 do_printf_conv (std::ostream&, const char*, int, int, int, int, |
|
2459 const std::string&); |
|
2460 |
|
2461 template int |
|
2462 do_printf_conv (std::ostream&, const char*, int, int, int, long, |
|
2463 const std::string&); |
|
2464 |
3878
|
2465 template int |
4468
|
2466 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, |
|
2467 const std::string&); |
|
2468 |
|
2469 template int |
|
2470 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, |
|
2471 const std::string&); |
|
2472 |
|
2473 template int |
|
2474 do_printf_conv (std::ostream&, const char*, int, int, int, double, |
|
2475 const std::string&); |
|
2476 |
|
2477 template int |
|
2478 do_printf_conv (std::ostream&, const char*, int, int, int, const char*, |
|
2479 const std::string&); |
2117
|
2480 |
|
2481 int |
|
2482 octave_base_stream::do_printf (printf_format_list& fmt_list, |
4468
|
2483 const octave_value_list& args, |
|
2484 const std::string& who) |
2117
|
2485 { |
3620
|
2486 int retval = 0; |
2117
|
2487 |
3640
|
2488 int nconv = fmt_list.num_conversions (); |
|
2489 |
3523
|
2490 std::ostream *osp = output_stream (); |
2117
|
2491 |
|
2492 if (osp) |
|
2493 { |
3523
|
2494 std::ostream& os = *osp; |
2117
|
2495 |
|
2496 const printf_format_elt *elt = fmt_list.first (); |
|
2497 |
|
2498 printf_value_cache val_cache (args); |
|
2499 |
|
2500 for (;;) |
|
2501 { |
4153
|
2502 OCTAVE_QUIT; |
|
2503 |
2117
|
2504 if (elt) |
|
2505 { |
3640
|
2506 // NSA is the number of `star' args to convert. |
|
2507 |
|
2508 int nsa = (elt->fw < 0) + (elt->prec < 0); |
2117
|
2509 |
|
2510 int sa_1 = 0; |
|
2511 int sa_2 = 0; |
|
2512 |
|
2513 if (nsa > 0) |
|
2514 { |
|
2515 sa_1 = val_cache.int_value (); |
|
2516 |
|
2517 if (! val_cache) |
|
2518 break; |
|
2519 else |
|
2520 { |
|
2521 if (nsa > 1) |
|
2522 { |
|
2523 sa_2 = val_cache.int_value (); |
|
2524 |
|
2525 if (! val_cache) |
|
2526 break; |
|
2527 } |
|
2528 } |
|
2529 } |
|
2530 |
|
2531 const char *fmt = elt->text; |
|
2532 |
3640
|
2533 if (elt->type == '%') |
|
2534 { |
|
2535 os << "%"; |
|
2536 retval++; |
|
2537 } |
|
2538 else if (elt->args == 0 && elt->text) |
|
2539 { |
|
2540 os << elt->text; |
|
2541 retval += strlen (elt->text); |
|
2542 } |
4257
|
2543 else if (elt->type == 's') |
3640
|
2544 { |
|
2545 std::string val = val_cache.string_value (); |
|
2546 |
|
2547 if (val_cache) |
|
2548 retval += do_printf_conv (os, fmt, nsa, sa_1, |
4468
|
2549 sa_2, val.c_str (), who); |
3640
|
2550 else |
|
2551 break; |
|
2552 } |
2117
|
2553 else |
|
2554 { |
3640
|
2555 double val = val_cache.double_value (); |
|
2556 |
|
2557 if (val_cache) |
2117
|
2558 { |
5389
|
2559 if (lo_ieee_isnan (val) || xisinf (val) |
4693
|
2560 || ((val > INT_MAX || val < INT_MIN) |
|
2561 && (elt->type == 'd' |
|
2562 || elt->type == 'i' |
|
2563 || elt->type == 'c' |
|
2564 || elt->type == 'o' |
|
2565 || elt->type == 'x' |
|
2566 || elt->type == 'X' |
|
2567 || elt->type == 'u'))) |
4303
|
2568 { |
|
2569 std::string tfmt = fmt; |
|
2570 |
5389
|
2571 if (lo_ieee_isnan (val) || xisinf (val)) |
4305
|
2572 { |
|
2573 tfmt.replace (tfmt.rfind (elt->type), 1, 1, 's'); |
|
2574 |
|
2575 const char *tval = xisinf (val) |
4693
|
2576 ? (val < 0 ? "-Inf" : "Inf") |
|
2577 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); |
4305
|
2578 |
|
2579 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2580 nsa, sa_1, sa_2, |
|
2581 tval, who); |
4305
|
2582 } |
|
2583 else |
|
2584 { |
|
2585 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); |
|
2586 |
|
2587 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2588 nsa, sa_1, sa_2, |
|
2589 val, who); |
4305
|
2590 } |
4303
|
2591 } |
|
2592 else |
3640
|
2593 { |
4303
|
2594 switch (elt->type) |
|
2595 { |
|
2596 case 'd': case 'i': case 'c': |
|
2597 { |
|
2598 if (elt->modifier == 'l') |
|
2599 retval += do_printf_conv |
|
2600 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2601 static_cast<long int> (val), who); |
4303
|
2602 else |
|
2603 retval += do_printf_conv |
|
2604 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2605 static_cast<int> (val), who); |
4303
|
2606 } |
|
2607 break; |
|
2608 |
|
2609 case 'o': case 'x': case 'X': case 'u': |
|
2610 { |
|
2611 if (elt->modifier == 'l') |
|
2612 retval += do_printf_conv |
|
2613 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2614 static_cast<unsigned long int> (val), |
|
2615 who); |
4303
|
2616 else |
|
2617 retval += do_printf_conv |
|
2618 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2619 static_cast<unsigned int> (val), who); |
4303
|
2620 } |
|
2621 break; |
|
2622 |
|
2623 case 'f': case 'e': case 'E': |
|
2624 case 'g': case 'G': |
|
2625 retval |
|
2626 += do_printf_conv (os, fmt, nsa, sa_1, sa_2, |
4468
|
2627 val, who); |
4303
|
2628 break; |
|
2629 |
|
2630 default: |
4468
|
2631 error ("%s: invalid format specifier", |
|
2632 who.c_str ()); |
4303
|
2633 return -1; |
|
2634 break; |
|
2635 } |
3640
|
2636 } |
2117
|
2637 } |
|
2638 else |
3620
|
2639 break; |
2117
|
2640 } |
|
2641 |
3620
|
2642 if (! os) |
2117
|
2643 { |
4468
|
2644 error ("%s: write error", who.c_str ()); |
2117
|
2645 break; |
|
2646 } |
|
2647 } |
|
2648 else |
|
2649 { |
4468
|
2650 ::error ("%s: internal error handling format", who.c_str ()); |
2117
|
2651 retval = -1; |
|
2652 break; |
|
2653 } |
|
2654 |
3640
|
2655 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
|
2656 |
|
2657 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
|
2658 break; |
|
2659 } |
2117
|
2660 } |
3640
|
2661 else |
4468
|
2662 invalid_operation (who, "writing"); |
2117
|
2663 |
|
2664 return retval; |
|
2665 } |
|
2666 |
|
2667 int |
3640
|
2668 octave_base_stream::printf (const std::string& fmt, |
4468
|
2669 const octave_value_list& args, |
|
2670 const std::string& who) |
2117
|
2671 { |
3640
|
2672 int retval = 0; |
|
2673 |
|
2674 printf_format_list fmt_list (fmt); |
|
2675 |
|
2676 if (fmt_list.num_conversions () == -1) |
4468
|
2677 ::error ("%s: invalid format specified", who.c_str ()); |
2117
|
2678 else |
4468
|
2679 retval = do_printf (fmt_list, args, who); |
2117
|
2680 |
|
2681 return retval; |
|
2682 } |
|
2683 |
|
2684 int |
4468
|
2685 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117
|
2686 { |
|
2687 int retval = -1; |
|
2688 |
3523
|
2689 std::ostream *osp = output_stream (); |
2117
|
2690 |
|
2691 if (osp) |
|
2692 { |
3523
|
2693 std::ostream& os = *osp; |
2117
|
2694 |
|
2695 os << s; |
|
2696 |
|
2697 if (os) |
|
2698 { |
5775
|
2699 // FIXME -- why does this seem to be necessary? |
2117
|
2700 // Without it, output from a loop like |
|
2701 // |
|
2702 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
|
2703 // |
|
2704 // doesn't seem to go to the pager immediately. |
|
2705 |
|
2706 os.flush (); |
|
2707 |
|
2708 if (os) |
|
2709 retval = 0; |
|
2710 else |
4468
|
2711 error ("%s: write error", who.c_str ()); |
2117
|
2712 } |
|
2713 else |
4468
|
2714 error ("%s: write error", who.c_str ()); |
2117
|
2715 } |
|
2716 else |
4468
|
2717 invalid_operation (who, "writing"); |
2117
|
2718 |
|
2719 return retval; |
|
2720 } |
|
2721 |
|
2722 // Return current error message for this stream. |
|
2723 |
3536
|
2724 std::string |
2435
|
2725 octave_base_stream::error (bool clear_err, int& err_num) |
2117
|
2726 { |
2435
|
2727 err_num = fail ? -1 : 0; |
2117
|
2728 |
3523
|
2729 std::string tmp = errmsg; |
2117
|
2730 |
|
2731 if (clear_err) |
|
2732 clear (); |
|
2733 |
|
2734 return tmp; |
|
2735 } |
|
2736 |
|
2737 void |
4468
|
2738 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117
|
2739 { |
4468
|
2740 // Note that this is not ::error () ! |
|
2741 |
6297
|
2742 error (who, std::string ("stream not open for ") + rw); |
2117
|
2743 } |
|
2744 |
3552
|
2745 octave_stream::octave_stream (octave_base_stream *bs) |
3340
|
2746 : rep (bs) |
|
2747 { |
|
2748 if (rep) |
|
2749 rep->count = 1; |
|
2750 } |
|
2751 |
|
2752 octave_stream::~octave_stream (void) |
|
2753 { |
|
2754 if (rep && --rep->count == 0) |
|
2755 delete rep; |
|
2756 } |
|
2757 |
|
2758 octave_stream::octave_stream (const octave_stream& s) |
|
2759 : rep (s.rep) |
|
2760 { |
|
2761 if (rep) |
|
2762 rep->count++; |
|
2763 } |
|
2764 |
|
2765 octave_stream& |
|
2766 octave_stream::operator = (const octave_stream& s) |
|
2767 { |
|
2768 if (rep != s.rep) |
|
2769 { |
|
2770 if (rep && --rep->count == 0) |
|
2771 delete rep; |
|
2772 |
|
2773 rep = s.rep; |
|
2774 |
|
2775 if (rep) |
|
2776 rep->count++; |
|
2777 } |
|
2778 |
|
2779 return *this; |
|
2780 } |
|
2781 |
2117
|
2782 int |
|
2783 octave_stream::flush (void) |
|
2784 { |
|
2785 int retval = -1; |
|
2786 |
5659
|
2787 if (stream_ok ()) |
2117
|
2788 retval = rep->flush (); |
|
2789 |
|
2790 return retval; |
|
2791 } |
|
2792 |
3536
|
2793 std::string |
5275
|
2794 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117
|
2795 { |
3523
|
2796 std::string retval; |
2117
|
2797 |
5659
|
2798 if (stream_ok ()) |
4468
|
2799 retval = rep->getl (max_len, err, who); |
2117
|
2800 |
|
2801 return retval; |
|
2802 } |
|
2803 |
3536
|
2804 std::string |
4468
|
2805 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
|
2806 const std::string& who) |
2117
|
2807 { |
3523
|
2808 std::string retval; |
2117
|
2809 |
|
2810 err = false; |
|
2811 |
|
2812 int conv_err = 0; |
|
2813 |
6345
|
2814 int max_len = -1; |
|
2815 |
|
2816 if (tc_max_len.is_defined ()) |
2117
|
2817 { |
6345
|
2818 max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2819 |
|
2820 if (conv_err || max_len < 0) |
|
2821 { |
|
2822 err = true; |
|
2823 ::error ("%s: invalid maximum length specified", who.c_str ()); |
|
2824 } |
2117
|
2825 } |
6345
|
2826 |
|
2827 if (! error_state) |
4468
|
2828 retval = getl (max_len, err, who); |
2117
|
2829 |
|
2830 return retval; |
|
2831 } |
|
2832 |
3536
|
2833 std::string |
5275
|
2834 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117
|
2835 { |
3523
|
2836 std::string retval; |
2117
|
2837 |
5659
|
2838 if (stream_ok ()) |
4468
|
2839 retval = rep->gets (max_len, err, who); |
2117
|
2840 |
|
2841 return retval; |
|
2842 } |
|
2843 |
3536
|
2844 std::string |
4468
|
2845 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
|
2846 const std::string& who) |
2117
|
2847 { |
3523
|
2848 std::string retval; |
2117
|
2849 |
|
2850 err = false; |
|
2851 |
|
2852 int conv_err = 0; |
|
2853 |
6345
|
2854 int max_len = -1; |
|
2855 |
|
2856 if (tc_max_len.is_defined ()) |
2117
|
2857 { |
6345
|
2858 max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2859 |
|
2860 if (conv_err || max_len < 0) |
|
2861 { |
|
2862 err = true; |
|
2863 ::error ("%s: invalid maximum length specified", who.c_str ()); |
|
2864 } |
2117
|
2865 } |
6345
|
2866 |
|
2867 if (! error_state) |
4468
|
2868 retval = gets (max_len, err, who); |
2117
|
2869 |
|
2870 return retval; |
|
2871 } |
|
2872 |
|
2873 int |
4797
|
2874 octave_stream::seek (long offset, int origin) |
2117
|
2875 { |
5065
|
2876 int status = -1; |
2117
|
2877 |
5659
|
2878 if (stream_ok ()) |
4889
|
2879 { |
|
2880 clearerr (); |
|
2881 |
5065
|
2882 long orig_pos = rep->tell (); |
|
2883 |
|
2884 status = rep->seek (offset, origin); |
|
2885 |
|
2886 if (status == 0) |
|
2887 { |
|
2888 long save_pos = rep->tell (); |
|
2889 |
|
2890 rep->seek (0, SEEK_END); |
|
2891 |
|
2892 long pos_eof = rep->tell (); |
|
2893 |
|
2894 // I don't think save_pos can be less than zero, but we'll |
|
2895 // check anyway... |
|
2896 |
|
2897 if (save_pos > pos_eof || save_pos < 0) |
|
2898 { |
|
2899 // Seek outside bounds of file. Failure should leave |
|
2900 // position unchanged. |
|
2901 |
|
2902 rep->seek (orig_pos, SEEK_SET); |
|
2903 |
|
2904 status = -1; |
|
2905 } |
|
2906 else |
|
2907 { |
|
2908 // Is it possible for this to fail? We are just |
|
2909 // returning to a position after the first successful |
|
2910 // seek. |
|
2911 |
|
2912 rep->seek (save_pos, SEEK_SET); |
|
2913 } |
|
2914 } |
4889
|
2915 } |
2117
|
2916 |
5065
|
2917 return status; |
2117
|
2918 } |
|
2919 |
|
2920 int |
|
2921 octave_stream::seek (const octave_value& tc_offset, |
|
2922 const octave_value& tc_origin) |
|
2923 { |
|
2924 int retval = -1; |
|
2925 |
4797
|
2926 long xoffset = tc_offset.long_value (true); |
4645
|
2927 |
|
2928 if (! error_state) |
2117
|
2929 { |
4645
|
2930 int conv_err = 0; |
|
2931 |
4797
|
2932 int origin = SEEK_SET; |
2117
|
2933 |
2341
|
2934 if (tc_origin.is_string ()) |
2117
|
2935 { |
3523
|
2936 std::string xorigin = tc_origin.string_value (); |
2341
|
2937 |
|
2938 if (xorigin == "bof") |
4797
|
2939 origin = SEEK_SET; |
2341
|
2940 else if (xorigin == "cof") |
4797
|
2941 origin = SEEK_CUR; |
2341
|
2942 else if (xorigin == "eof") |
4797
|
2943 origin = SEEK_END; |
2117
|
2944 else |
|
2945 conv_err = -1; |
|
2946 } |
2341
|
2947 else |
|
2948 { |
|
2949 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
|
2950 |
|
2951 if (! conv_err) |
|
2952 { |
|
2953 if (xorigin == -1) |
4797
|
2954 origin = SEEK_SET; |
2341
|
2955 else if (xorigin == 0) |
4797
|
2956 origin = SEEK_CUR; |
2341
|
2957 else if (xorigin == 1) |
4797
|
2958 origin = SEEK_END; |
2341
|
2959 else |
|
2960 conv_err = -1; |
|
2961 } |
|
2962 } |
2117
|
2963 |
|
2964 if (! conv_err) |
5065
|
2965 { |
|
2966 retval = seek (xoffset, origin); |
|
2967 |
|
2968 if (retval != 0) |
|
2969 error ("fseek: failed to seek to requested position"); |
|
2970 } |
2117
|
2971 else |
|
2972 error ("fseek: invalid value for origin"); |
|
2973 } |
|
2974 else |
|
2975 error ("fseek: invalid value for offset"); |
|
2976 |
|
2977 return retval; |
|
2978 } |
|
2979 |
4797
|
2980 long |
|
2981 octave_stream::tell (void) |
2117
|
2982 { |
4797
|
2983 long retval = -1; |
2117
|
2984 |
5659
|
2985 if (stream_ok ()) |
2117
|
2986 retval = rep->tell (); |
|
2987 |
|
2988 return retval; |
|
2989 } |
|
2990 |
|
2991 int |
|
2992 octave_stream::rewind (void) |
|
2993 { |
6296
|
2994 return seek (0, SEEK_SET); |
2117
|
2995 } |
|
2996 |
3340
|
2997 bool |
|
2998 octave_stream::is_open (void) const |
|
2999 { |
|
3000 bool retval = false; |
|
3001 |
5659
|
3002 if (stream_ok ()) |
3340
|
3003 retval = rep->is_open (); |
|
3004 |
|
3005 return retval; |
|
3006 } |
|
3007 |
|
3008 void |
|
3009 octave_stream::close (void) |
|
3010 { |
5659
|
3011 if (stream_ok ()) |
3340
|
3012 rep->close (); |
|
3013 } |
|
3014 |
4944
|
3015 template <class RET_T, class READ_T> |
2117
|
3016 octave_value |
5275
|
3017 do_read (octave_stream& strm, octave_idx_type nr, octave_idx_type nc, octave_idx_type block_size, |
|
3018 octave_idx_type skip, bool do_float_fmt_conv, |
|
3019 oct_mach_info::float_format from_flt_fmt, octave_idx_type& count) |
2117
|
3020 { |
|
3021 octave_value retval; |
|
3022 |
4944
|
3023 RET_T nda; |
|
3024 |
|
3025 count = 0; |
|
3026 |
|
3027 typename octave_array_type_traits<RET_T>::element_type elt_zero |
|
3028 = typename octave_array_type_traits<RET_T>::element_type (); |
|
3029 |
|
3030 typename octave_array_type_traits<RET_T>::element_type *dat = 0; |
|
3031 |
5275
|
3032 octave_idx_type max_size = 0; |
|
3033 |
|
3034 octave_idx_type final_nr = 0; |
|
3035 octave_idx_type final_nc = 1; |
4944
|
3036 |
|
3037 if (nr > 0) |
|
3038 { |
|
3039 if (nc > 0) |
|
3040 { |
|
3041 nda.resize (dim_vector (nr, nc), elt_zero); |
|
3042 dat = nda.fortran_vec (); |
|
3043 max_size = nr * nc; |
|
3044 } |
|
3045 else |
|
3046 { |
|
3047 nda.resize (dim_vector (nr, 32), elt_zero); |
|
3048 dat = nda.fortran_vec (); |
|
3049 max_size = nr * 32; |
|
3050 } |
|
3051 } |
|
3052 else |
|
3053 { |
|
3054 nda.resize (dim_vector (32, 1), elt_zero); |
|
3055 dat = nda.fortran_vec (); |
|
3056 max_size = 32; |
|
3057 } |
|
3058 |
5775
|
3059 // FIXME -- byte order for Cray? |
4944
|
3060 |
|
3061 bool swap = false; |
|
3062 |
|
3063 if (oct_mach_info::words_big_endian ()) |
|
3064 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
3065 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
3066 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g); |
|
3067 else |
|
3068 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
|
3069 |
|
3070 union |
|
3071 { |
|
3072 char buf[sizeof (typename octave_type_traits<READ_T>::val_type)]; |
|
3073 typename octave_type_traits<READ_T>::val_type val; |
|
3074 } u; |
|
3075 |
|
3076 std::istream *isp = strm.input_stream (); |
|
3077 |
|
3078 if (isp) |
|
3079 { |
|
3080 std::istream& is = *isp; |
|
3081 |
5275
|
3082 octave_idx_type elts_read = 0; |
4944
|
3083 |
|
3084 for (;;) |
|
3085 { |
5775
|
3086 // FIXME -- maybe there should be a special case for |
4944
|
3087 // skip == 0. |
|
3088 |
|
3089 if (is) |
|
3090 { |
|
3091 if (nr > 0 && nc > 0 && count == max_size) |
|
3092 { |
|
3093 final_nr = nr; |
|
3094 final_nc = nc; |
|
3095 |
|
3096 break; |
|
3097 } |
|
3098 |
|
3099 is.read (u.buf, sizeof (typename octave_type_traits<READ_T>::val_type)); |
|
3100 |
|
3101 // We only swap bytes for integer types. For float |
|
3102 // types, the format conversion will also handle byte |
|
3103 // swapping. |
|
3104 |
|
3105 if (swap) |
|
3106 swap_bytes<sizeof (typename octave_type_traits<READ_T>::val_type)> (u.buf); |
|
3107 else if (do_float_fmt_conv) |
|
3108 do_float_format_conversion |
|
3109 (u.buf, |
|
3110 sizeof (typename octave_type_traits<READ_T>::val_type), |
|
3111 1, from_flt_fmt, oct_mach_info::float_format ()); |
|
3112 |
|
3113 typename octave_array_type_traits<RET_T>::element_type tmp |
|
3114 = static_cast <typename octave_array_type_traits<RET_T>::element_type> (u.val); |
|
3115 |
5030
|
3116 if (is) |
4944
|
3117 { |
5030
|
3118 if (count == max_size) |
4944
|
3119 { |
5030
|
3120 max_size *= 2; |
|
3121 |
|
3122 if (nr > 0) |
|
3123 nda.resize (dim_vector (nr, max_size / nr), |
|
3124 elt_zero); |
|
3125 else |
|
3126 nda.resize (dim_vector (max_size, 1), elt_zero); |
|
3127 |
|
3128 dat = nda.fortran_vec (); |
4944
|
3129 } |
|
3130 |
5030
|
3131 dat[count++] = tmp; |
|
3132 |
|
3133 elts_read++; |
|
3134 } |
|
3135 |
|
3136 if (skip != 0 && elts_read == block_size) |
|
3137 { |
|
3138 strm.seek (skip, SEEK_CUR); |
|
3139 elts_read = 0; |
|
3140 } |
|
3141 |
|
3142 if (is.eof ()) |
|
3143 { |
|
3144 if (nr > 0) |
4944
|
3145 { |
5030
|
3146 if (count > nr) |
4944
|
3147 { |
5030
|
3148 final_nr = nr; |
|
3149 final_nc = (count - 1) / nr + 1; |
4944
|
3150 } |
|
3151 else |
|
3152 { |
|
3153 final_nr = count; |
|
3154 final_nc = 1; |
|
3155 } |
|
3156 } |
5030
|
3157 else |
|
3158 { |
|
3159 final_nr = count; |
|
3160 final_nc = 1; |
|
3161 } |
|
3162 |
4944
|
3163 break; |
|
3164 } |
|
3165 } |
5030
|
3166 else if (is.eof ()) |
|
3167 break; |
4944
|
3168 } |
|
3169 } |
|
3170 |
5030
|
3171 nda.resize (dim_vector (final_nr, final_nc), elt_zero); |
|
3172 |
|
3173 retval = nda; |
4944
|
3174 |
|
3175 return retval; |
|
3176 } |
|
3177 |
|
3178 #define DO_READ_VAL_TEMPLATE(RET_T, READ_T) \ |
|
3179 template octave_value \ |
5275
|
3180 do_read<RET_T, READ_T> (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, \ |
|
3181 oct_mach_info::float_format, octave_idx_type&) |
4944
|
3182 |
5775
|
3183 // FIXME -- should we only have float if it is a different |
4944
|
3184 // size from double? |
|
3185 |
|
3186 #define INSTANTIATE_DO_READ(VAL_T) \ |
|
3187 DO_READ_VAL_TEMPLATE (VAL_T, octave_int8); \ |
|
3188 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint8); \ |
|
3189 DO_READ_VAL_TEMPLATE (VAL_T, octave_int16); \ |
|
3190 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint16); \ |
|
3191 DO_READ_VAL_TEMPLATE (VAL_T, octave_int32); \ |
|
3192 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint32); \ |
|
3193 DO_READ_VAL_TEMPLATE (VAL_T, octave_int64); \ |
|
3194 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint64); \ |
|
3195 DO_READ_VAL_TEMPLATE (VAL_T, float); \ |
|
3196 DO_READ_VAL_TEMPLATE (VAL_T, double); \ |
|
3197 DO_READ_VAL_TEMPLATE (VAL_T, char); \ |
|
3198 DO_READ_VAL_TEMPLATE (VAL_T, signed char); \ |
|
3199 DO_READ_VAL_TEMPLATE (VAL_T, unsigned char) |
|
3200 |
4970
|
3201 INSTANTIATE_DO_READ (int8NDArray); |
|
3202 INSTANTIATE_DO_READ (uint8NDArray); |
|
3203 INSTANTIATE_DO_READ (int16NDArray); |
|
3204 INSTANTIATE_DO_READ (uint16NDArray); |
|
3205 INSTANTIATE_DO_READ (int32NDArray); |
|
3206 INSTANTIATE_DO_READ (uint32NDArray); |
|
3207 INSTANTIATE_DO_READ (int64NDArray); |
|
3208 INSTANTIATE_DO_READ (uint64NDArray); |
|
3209 // INSTANTIATE_DO_READ (floatNDArray); |
|
3210 INSTANTIATE_DO_READ (NDArray); |
|
3211 INSTANTIATE_DO_READ (charNDArray); |
5780
|
3212 INSTANTIATE_DO_READ (boolNDArray); |
4944
|
3213 |
5275
|
3214 typedef octave_value (*read_fptr) (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, |
|
3215 oct_mach_info::float_format ffmt, octave_idx_type&); |
4944
|
3216 |
|
3217 INSTANTIATE_ARRAY (read_fptr); |
|
3218 template class Array2<read_fptr>; |
|
3219 |
|
3220 #define FILL_TABLE_ROW(R, VAL_T) \ |
|
3221 read_fptr_table(R,oct_data_conv::dt_int8) = do_read<VAL_T, octave_int8>; \ |
|
3222 read_fptr_table(R,oct_data_conv::dt_uint8) = do_read<VAL_T, octave_uint8>; \ |
|
3223 read_fptr_table(R,oct_data_conv::dt_int16) = do_read<VAL_T, octave_int16>; \ |
|
3224 read_fptr_table(R,oct_data_conv::dt_uint16) = do_read<VAL_T, octave_uint16>; \ |
|
3225 read_fptr_table(R,oct_data_conv::dt_int32) = do_read<VAL_T, octave_int32>; \ |
|
3226 read_fptr_table(R,oct_data_conv::dt_uint32) = do_read<VAL_T, octave_uint32>; \ |
|
3227 read_fptr_table(R,oct_data_conv::dt_int64) = do_read<VAL_T, octave_int64>; \ |
|
3228 read_fptr_table(R,oct_data_conv::dt_uint64) = do_read<VAL_T, octave_uint64>; \ |
|
3229 read_fptr_table(R,oct_data_conv::dt_single) = do_read<VAL_T, float>; \ |
|
3230 read_fptr_table(R,oct_data_conv::dt_double) = do_read<VAL_T, double>; \ |
|
3231 read_fptr_table(R,oct_data_conv::dt_char) = do_read<VAL_T, char>; \ |
|
3232 read_fptr_table(R,oct_data_conv::dt_schar) = do_read<VAL_T, signed char>; \ |
4970
|
3233 read_fptr_table(R,oct_data_conv::dt_uchar) = do_read<VAL_T, unsigned char>; \ |
|
3234 read_fptr_table(R,oct_data_conv::dt_logical) = do_read<VAL_T, unsigned char> |
4944
|
3235 |
|
3236 octave_value |
5275
|
3237 octave_stream::read (const Array<double>& size, octave_idx_type block_size, |
4944
|
3238 oct_data_conv::data_type input_type, |
|
3239 oct_data_conv::data_type output_type, |
5275
|
3240 octave_idx_type skip, oct_mach_info::float_format ffmt, |
|
3241 octave_idx_type& char_count) |
4944
|
3242 { |
|
3243 static bool initialized = false; |
|
3244 |
|
3245 // Table function pointers for return types x read types. |
|
3246 |
4970
|
3247 static Array2<read_fptr> read_fptr_table (oct_data_conv::dt_unknown, 14, 0); |
4944
|
3248 |
|
3249 if (! initialized) |
|
3250 { |
|
3251 FILL_TABLE_ROW (oct_data_conv::dt_int8, int8NDArray); |
|
3252 FILL_TABLE_ROW (oct_data_conv::dt_uint8, uint8NDArray); |
|
3253 FILL_TABLE_ROW (oct_data_conv::dt_int16, int16NDArray); |
|
3254 FILL_TABLE_ROW (oct_data_conv::dt_uint16, uint16NDArray); |
|
3255 FILL_TABLE_ROW (oct_data_conv::dt_int32, int32NDArray); |
|
3256 FILL_TABLE_ROW (oct_data_conv::dt_uint32, uint32NDArray); |
|
3257 FILL_TABLE_ROW (oct_data_conv::dt_int64, int64NDArray); |
|
3258 FILL_TABLE_ROW (oct_data_conv::dt_uint64, uint64NDArray); |
|
3259 FILL_TABLE_ROW (oct_data_conv::dt_double, NDArray); |
|
3260 FILL_TABLE_ROW (oct_data_conv::dt_char, charNDArray); |
|
3261 FILL_TABLE_ROW (oct_data_conv::dt_schar, charNDArray); |
|
3262 FILL_TABLE_ROW (oct_data_conv::dt_uchar, charNDArray); |
4970
|
3263 FILL_TABLE_ROW (oct_data_conv::dt_logical, boolNDArray); |
4944
|
3264 |
|
3265 initialized = true; |
|
3266 } |
|
3267 |
|
3268 octave_value retval; |
|
3269 |
5659
|
3270 if (stream_ok ()) |
4944
|
3271 { |
5775
|
3272 // FIXME -- we may eventually want to make this extensible. |
|
3273 |
|
3274 // FIXME -- we need a better way to ensure that this |
4944
|
3275 // numbering stays consistent with the order of the elements in the |
|
3276 // data_type enum in the oct_data_conv class. |
|
3277 |
|
3278 char_count = 0; |
|
3279 |
5275
|
3280 octave_idx_type nr = -1; |
|
3281 octave_idx_type nc = -1; |
4944
|
3282 |
|
3283 bool ignore; |
|
3284 |
|
3285 get_size (size, nr, nc, ignore, "fread"); |
|
3286 |
|
3287 if (! error_state) |
|
3288 { |
|
3289 if (nr == 0 || nc == 0) |
|
3290 retval = Matrix (nr, nc); |
|
3291 else |
|
3292 { |
|
3293 if (ffmt == oct_mach_info::flt_fmt_unknown) |
|
3294 ffmt = float_format (); |
|
3295 |
|
3296 read_fptr fcn = read_fptr_table (output_type, input_type); |
|
3297 |
|
3298 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double |
|
3299 || input_type == oct_data_conv::dt_single) |
|
3300 && ffmt != float_format ()); |
|
3301 |
|
3302 if (fcn) |
|
3303 { |
|
3304 retval = (*fcn) (*this, nr, nc, block_size, skip, |
|
3305 do_float_fmt_conv, ffmt, char_count); |
|
3306 |
5775
|
3307 // FIXME -- kluge! |
4944
|
3308 |
|
3309 if (! error_state |
|
3310 && (output_type == oct_data_conv::dt_char |
|
3311 || output_type == oct_data_conv::dt_schar |
|
3312 || output_type == oct_data_conv::dt_uchar)) |
|
3313 retval = octave_value (retval.char_matrix_value (), true); |
|
3314 } |
|
3315 else |
|
3316 error ("fread: unable to read and convert requested types"); |
|
3317 } |
|
3318 } |
|
3319 else |
|
3320 invalid_operation ("fread", "reading"); |
|
3321 } |
2117
|
3322 |
|
3323 return retval; |
|
3324 } |
|
3325 |
5275
|
3326 octave_idx_type |
|
3327 octave_stream::write (const octave_value& data, octave_idx_type block_size, |
|
3328 oct_data_conv::data_type output_type, octave_idx_type skip, |
2317
|
3329 oct_mach_info::float_format flt_fmt) |
2117
|
3330 { |
5275
|
3331 octave_idx_type retval = -1; |
2117
|
3332 |
5659
|
3333 if (stream_ok ()) |
4944
|
3334 { |
|
3335 if (! error_state) |
|
3336 { |
|
3337 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
|
3338 flt_fmt = float_format (); |
|
3339 |
5275
|
3340 octave_idx_type status = data.write (*this, block_size, output_type, |
4944
|
3341 skip, flt_fmt); |
|
3342 |
|
3343 if (status < 0) |
|
3344 error ("fwrite: write error"); |
|
3345 else |
|
3346 retval = status; |
|
3347 } |
|
3348 else |
|
3349 invalid_operation ("fwrite", "writing"); |
|
3350 } |
2117
|
3351 |
|
3352 return retval; |
|
3353 } |
|
3354 |
4944
|
3355 template <class T> |
|
3356 void |
|
3357 write_int (std::ostream& os, bool swap, const T& val) |
|
3358 { |
|
3359 typename octave_type_traits<T>::val_type tmp = val.value (); |
|
3360 |
|
3361 if (swap) |
|
3362 swap_bytes<sizeof (typename octave_type_traits<T>::val_type)> (&tmp); |
|
3363 |
|
3364 os.write (reinterpret_cast<const char *> (&tmp), |
|
3365 sizeof (typename octave_type_traits<T>::val_type)); |
|
3366 } |
|
3367 |
|
3368 template void write_int (std::ostream&, bool, const octave_int8&); |
|
3369 template void write_int (std::ostream&, bool, const octave_uint8&); |
|
3370 template void write_int (std::ostream&, bool, const octave_int16&); |
|
3371 template void write_int (std::ostream&, bool, const octave_uint16&); |
|
3372 template void write_int (std::ostream&, bool, const octave_int32&); |
|
3373 template void write_int (std::ostream&, bool, const octave_uint32&); |
|
3374 template void write_int (std::ostream&, bool, const octave_int64&); |
|
3375 template void write_int (std::ostream&, bool, const octave_uint64&); |
|
3376 |
|
3377 template <class T> |
|
3378 static inline bool |
|
3379 do_write (std::ostream& os, const T& val, oct_data_conv::data_type output_type, |
|
3380 oct_mach_info::float_format flt_fmt, bool swap, |
|
3381 bool do_float_conversion) |
|
3382 { |
|
3383 bool retval = true; |
|
3384 |
|
3385 // For compatibility, Octave converts to the output type, then |
|
3386 // writes. This means that truncation happens on the conversion. |
|
3387 // For example, the following program prints 0: |
|
3388 // |
|
3389 // x = int8 (-1) |
|
3390 // f = fopen ("foo.dat", "w"); |
|
3391 // fwrite (f, x, "unsigned char"); |
|
3392 // fclose (f); |
|
3393 // f = fopen ("foo.dat", "r"); |
|
3394 // y = fread (f, 1, "unsigned char"); |
|
3395 // printf ("%d\n", y); |
|
3396 |
|
3397 switch (output_type) |
|
3398 { |
|
3399 case oct_data_conv::dt_char: |
|
3400 case oct_data_conv::dt_schar: |
|
3401 case oct_data_conv::dt_int8: |
|
3402 write_int (os, swap, octave_int8 (val)); |
|
3403 break; |
|
3404 |
|
3405 case oct_data_conv::dt_uchar: |
|
3406 case oct_data_conv::dt_uint8: |
|
3407 write_int (os, swap, octave_uint8 (val)); |
|
3408 break; |
|
3409 |
|
3410 case oct_data_conv::dt_int16: |
|
3411 write_int (os, swap, octave_int16 (val)); |
|
3412 break; |
|
3413 |
|
3414 case oct_data_conv::dt_uint16: |
|
3415 write_int (os, swap, octave_uint16 (val)); |
|
3416 break; |
|
3417 |
|
3418 case oct_data_conv::dt_int32: |
|
3419 write_int (os, swap, octave_int32 (val)); |
|
3420 break; |
|
3421 |
|
3422 case oct_data_conv::dt_uint32: |
|
3423 write_int (os, swap, octave_uint32 (val)); |
|
3424 break; |
|
3425 |
|
3426 case oct_data_conv::dt_int64: |
|
3427 write_int (os, swap, octave_int64 (val)); |
|
3428 break; |
|
3429 |
|
3430 case oct_data_conv::dt_uint64: |
|
3431 write_int (os, swap, octave_uint64 (val)); |
|
3432 break; |
|
3433 |
|
3434 case oct_data_conv::dt_single: |
|
3435 { |
|
3436 float f = static_cast<float> (val); |
|
3437 |
|
3438 if (do_float_conversion) |
|
3439 do_float_format_conversion (&f, 1, flt_fmt); |
|
3440 |
|
3441 os.write (reinterpret_cast<const char *> (&f), sizeof (float)); |
|
3442 } |
|
3443 break; |
|
3444 |
|
3445 case oct_data_conv::dt_double: |
|
3446 { |
|
3447 double d = static_cast<double> (val); |
|
3448 if (do_float_conversion) |
|
3449 do_double_format_conversion (&d, 1, flt_fmt); |
|
3450 |
|
3451 os.write (reinterpret_cast<const char *> (&d), sizeof (double)); |
|
3452 } |
|
3453 break; |
|
3454 |
|
3455 default: |
|
3456 retval = false; |
|
3457 (*current_liboctave_error_handler) |
|
3458 ("write: invalid type specification"); |
|
3459 break; |
|
3460 } |
|
3461 |
|
3462 return retval; |
|
3463 } |
|
3464 |
5887
|
3465 template bool |
|
3466 do_write (std::ostream&, const octave_int8&, oct_data_conv::data_type, |
|
3467 oct_mach_info::float_format, bool, bool); |
|
3468 |
|
3469 template bool |
|
3470 do_write (std::ostream&, const octave_uint8&, oct_data_conv::data_type, |
|
3471 oct_mach_info::float_format, bool, bool); |
|
3472 |
|
3473 template bool |
|
3474 do_write (std::ostream&, const octave_int16&, oct_data_conv::data_type, |
|
3475 oct_mach_info::float_format, bool, bool); |
|
3476 |
|
3477 template bool |
|
3478 do_write (std::ostream&, const octave_uint16&, oct_data_conv::data_type, |
|
3479 oct_mach_info::float_format, bool, bool); |
|
3480 |
|
3481 template bool |
|
3482 do_write (std::ostream&, const octave_int32&, oct_data_conv::data_type, |
|
3483 oct_mach_info::float_format, bool, bool); |
|
3484 |
|
3485 template bool |
|
3486 do_write (std::ostream&, const octave_uint32&, oct_data_conv::data_type, |
|
3487 oct_mach_info::float_format, bool, bool); |
|
3488 |
|
3489 template bool |
|
3490 do_write (std::ostream&, const octave_int64&, oct_data_conv::data_type, |
|
3491 oct_mach_info::float_format, bool, bool); |
|
3492 |
|
3493 template bool |
|
3494 do_write (std::ostream&, const octave_uint64&, oct_data_conv::data_type, |
|
3495 oct_mach_info::float_format, bool, bool); |
|
3496 |
4944
|
3497 template <class T> |
5275
|
3498 octave_idx_type |
|
3499 octave_stream::write (const Array<T>& data, octave_idx_type block_size, |
4944
|
3500 oct_data_conv::data_type output_type, |
5275
|
3501 octave_idx_type skip, oct_mach_info::float_format flt_fmt) |
4944
|
3502 { |
5275
|
3503 octave_idx_type retval = -1; |
4944
|
3504 |
|
3505 bool status = true; |
|
3506 |
5275
|
3507 octave_idx_type count = 0; |
4944
|
3508 |
|
3509 const T *d = data.data (); |
|
3510 |
5275
|
3511 octave_idx_type n = data.length (); |
4944
|
3512 |
|
3513 oct_mach_info::float_format native_flt_fmt |
|
3514 = oct_mach_info::float_format (); |
|
3515 |
|
3516 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
3517 |
5775
|
3518 // FIXME -- byte order for Cray? |
4944
|
3519 |
|
3520 bool swap = false; |
|
3521 |
|
3522 if (oct_mach_info::words_big_endian ()) |
|
3523 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
3524 || flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
3525 || flt_fmt == oct_mach_info::flt_fmt_vax_g); |
|
3526 else |
|
3527 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
|
3528 |
5275
|
3529 for (octave_idx_type i = 0; i < n; i++) |
4944
|
3530 { |
|
3531 std::ostream *osp = output_stream (); |
|
3532 |
|
3533 if (osp) |
|
3534 { |
|
3535 std::ostream& os = *osp; |
|
3536 |
5254
|
3537 // It seems that Matlab writes zeros instead of actually |
|
3538 // seeking. Hmm... |
|
3539 |
4944
|
3540 if (skip != 0 && (i % block_size) == 0) |
5254
|
3541 { |
5775
|
3542 // FIXME -- probably should try to write larger |
5254
|
3543 // blocks... |
|
3544 |
|
3545 unsigned char zero = 0; |
|
3546 for (int j = 0; j < skip; j++) |
|
3547 os.write (reinterpret_cast<const char *> (&zero), 1); |
|
3548 } |
4944
|
3549 |
|
3550 if (os) |
|
3551 { |
|
3552 status = do_write (os, d[i], output_type, flt_fmt, swap, |
|
3553 do_float_conversion); |
|
3554 |
|
3555 if (os && status) |
|
3556 count++; |
|
3557 else |
|
3558 break; |
|
3559 } |
|
3560 else |
|
3561 { |
|
3562 status = false; |
|
3563 break; |
|
3564 } |
|
3565 } |
|
3566 else |
|
3567 { |
|
3568 status = false; |
|
3569 break; |
|
3570 } |
|
3571 } |
|
3572 |
|
3573 if (status) |
|
3574 retval = count; |
|
3575 |
|
3576 return retval; |
|
3577 } |
|
3578 |
5275
|
3579 template octave_idx_type |
|
3580 octave_stream::write (const Array<char>&, octave_idx_type, |
4944
|
3581 oct_data_conv::data_type, |
5275
|
3582 octave_idx_type, oct_mach_info::float_format); |
|
3583 |
|
3584 template octave_idx_type |
|
3585 octave_stream::write (const Array<bool>&, octave_idx_type, |
4970
|
3586 oct_data_conv::data_type, |
5275
|
3587 octave_idx_type, oct_mach_info::float_format); |
|
3588 |
|
3589 template octave_idx_type |
|
3590 octave_stream::write (const Array<double>&, octave_idx_type, |
4944
|
3591 oct_data_conv::data_type, |
5275
|
3592 octave_idx_type, oct_mach_info::float_format); |
|
3593 |
|
3594 template octave_idx_type |
|
3595 octave_stream::write (const Array<octave_int8>&, octave_idx_type, |
4944
|
3596 oct_data_conv::data_type, |
5275
|
3597 octave_idx_type, oct_mach_info::float_format); |
|
3598 |
|
3599 template octave_idx_type |
|
3600 octave_stream::write (const Array<octave_uint8>&, octave_idx_type, |
4944
|
3601 oct_data_conv::data_type, |
5275
|
3602 octave_idx_type, oct_mach_info::float_format); |
|
3603 |
|
3604 template octave_idx_type |
|
3605 octave_stream::write (const Array<octave_int16>&, octave_idx_type, |
4944
|
3606 oct_data_conv::data_type, |
5275
|
3607 octave_idx_type, oct_mach_info::float_format); |
|
3608 |
|
3609 template octave_idx_type |
|
3610 octave_stream::write (const Array<octave_uint16>&, octave_idx_type, |
4944
|
3611 oct_data_conv::data_type, |
5275
|
3612 octave_idx_type, oct_mach_info::float_format); |
|
3613 |
|
3614 template octave_idx_type |
|
3615 octave_stream::write (const Array<octave_int32>&, octave_idx_type, |
4944
|
3616 oct_data_conv::data_type, |
5275
|
3617 octave_idx_type, oct_mach_info::float_format); |
|
3618 |
|
3619 template octave_idx_type |
|
3620 octave_stream::write (const Array<octave_uint32>&, octave_idx_type, |
4944
|
3621 oct_data_conv::data_type, |
5275
|
3622 octave_idx_type, oct_mach_info::float_format); |
|
3623 |
|
3624 template octave_idx_type |
|
3625 octave_stream::write (const Array<octave_int64>&, octave_idx_type, |
4944
|
3626 oct_data_conv::data_type, |
5275
|
3627 octave_idx_type, oct_mach_info::float_format); |
|
3628 |
|
3629 template octave_idx_type |
|
3630 octave_stream::write (const Array<octave_uint64>&, octave_idx_type, |
4944
|
3631 oct_data_conv::data_type, |
5275
|
3632 octave_idx_type, oct_mach_info::float_format); |
4944
|
3633 |
2117
|
3634 octave_value |
3810
|
3635 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
5275
|
3636 octave_idx_type& count, const std::string& who) |
2117
|
3637 { |
|
3638 octave_value retval; |
|
3639 |
5659
|
3640 if (stream_ok ()) |
4468
|
3641 retval = rep->scanf (fmt, size, count, who); |
2117
|
3642 |
|
3643 return retval; |
|
3644 } |
|
3645 |
5279
|
3646 octave_value |
|
3647 octave_stream::scanf (const octave_value& fmt, const Array<double>& size, |
5299
|
3648 octave_idx_type& count, const std::string& who) |
5279
|
3649 { |
|
3650 octave_value retval = Matrix (); |
|
3651 |
|
3652 if (fmt.is_string ()) |
|
3653 { |
|
3654 std::string sfmt = fmt.string_value (); |
|
3655 |
|
3656 if (fmt.is_sq_string ()) |
|
3657 sfmt = do_string_escapes (sfmt); |
|
3658 |
|
3659 retval = scanf (sfmt, size, count, who); |
|
3660 } |
|
3661 else |
|
3662 { |
|
3663 // Note that this is not ::error () ! |
|
3664 |
|
3665 error (who + ": format must be a string"); |
|
3666 } |
|
3667 |
|
3668 return retval; |
|
3669 } |
|
3670 |
2215
|
3671 octave_value_list |
4468
|
3672 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
3673 { |
|
3674 octave_value_list retval; |
|
3675 |
5659
|
3676 if (stream_ok ()) |
4468
|
3677 retval = rep->oscanf (fmt, who); |
2215
|
3678 |
|
3679 return retval; |
|
3680 } |
|
3681 |
5279
|
3682 octave_value_list |
|
3683 octave_stream::oscanf (const octave_value& fmt, const std::string& who) |
|
3684 { |
|
3685 octave_value_list retval; |
|
3686 |
|
3687 if (fmt.is_string ()) |
|
3688 { |
|
3689 std::string sfmt = fmt.string_value (); |
|
3690 |
|
3691 if (fmt.is_sq_string ()) |
|
3692 sfmt = do_string_escapes (sfmt); |
|
3693 |
|
3694 retval = oscanf (sfmt, who); |
|
3695 } |
|
3696 else |
|
3697 { |
|
3698 // Note that this is not ::error () ! |
|
3699 |
|
3700 error (who + ": format must be a string"); |
|
3701 } |
|
3702 |
|
3703 return retval; |
|
3704 } |
|
3705 |
2117
|
3706 int |
4468
|
3707 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
|
3708 const std::string& who) |
2117
|
3709 { |
|
3710 int retval = -1; |
|
3711 |
5659
|
3712 if (stream_ok ()) |
4468
|
3713 retval = rep->printf (fmt, args, who); |
2117
|
3714 |
|
3715 return retval; |
|
3716 } |
|
3717 |
|
3718 int |
5279
|
3719 octave_stream::printf (const octave_value& fmt, const octave_value_list& args, |
|
3720 const std::string& who) |
|
3721 { |
|
3722 int retval = 0; |
|
3723 |
|
3724 if (fmt.is_string ()) |
|
3725 { |
|
3726 std::string sfmt = fmt.string_value (); |
|
3727 |
|
3728 if (fmt.is_sq_string ()) |
|
3729 sfmt = do_string_escapes (sfmt); |
|
3730 |
|
3731 retval = printf (sfmt, args, who); |
|
3732 } |
|
3733 else |
|
3734 { |
|
3735 // Note that this is not ::error () ! |
|
3736 |
|
3737 error (who + ": format must be a string"); |
|
3738 } |
|
3739 |
|
3740 return retval; |
|
3741 } |
|
3742 |
|
3743 int |
4468
|
3744 octave_stream::puts (const std::string& s, const std::string& who) |
2117
|
3745 { |
|
3746 int retval = -1; |
|
3747 |
5659
|
3748 if (stream_ok ()) |
4468
|
3749 retval = rep->puts (s, who); |
2117
|
3750 |
|
3751 return retval; |
|
3752 } |
|
3753 |
5775
|
3754 // FIXME -- maybe this should work for string arrays too. |
2117
|
3755 |
|
3756 int |
4468
|
3757 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117
|
3758 { |
|
3759 int retval = -1; |
|
3760 |
|
3761 if (tc_s.is_string ()) |
|
3762 { |
3523
|
3763 std::string s = tc_s.string_value (); |
5279
|
3764 retval = puts (s, who); |
2117
|
3765 } |
|
3766 else |
4468
|
3767 { |
|
3768 // Note that this is not ::error () ! |
|
3769 |
|
3770 error (who + ": argument must be a string"); |
|
3771 } |
2117
|
3772 |
|
3773 return retval; |
|
3774 } |
|
3775 |
|
3776 bool |
|
3777 octave_stream::eof (void) const |
|
3778 { |
|
3779 int retval = -1; |
|
3780 |
5659
|
3781 if (stream_ok ()) |
2117
|
3782 retval = rep->eof (); |
|
3783 |
|
3784 return retval; |
|
3785 } |
|
3786 |
3536
|
3787 std::string |
2435
|
3788 octave_stream::error (bool clear, int& err_num) |
2117
|
3789 { |
5649
|
3790 std::string retval = "invalid stream object"; |
|
3791 |
5659
|
3792 if (stream_ok (false)) |
2435
|
3793 retval = rep->error (clear, err_num); |
2117
|
3794 |
|
3795 return retval; |
|
3796 } |
|
3797 |
3536
|
3798 std::string |
3340
|
3799 octave_stream::name (void) const |
2117
|
3800 { |
3523
|
3801 std::string retval; |
2117
|
3802 |
5659
|
3803 if (stream_ok ()) |
2117
|
3804 retval = rep->name (); |
|
3805 |
|
3806 return retval; |
|
3807 } |
|
3808 |
|
3809 int |
3340
|
3810 octave_stream::mode (void) const |
2117
|
3811 { |
|
3812 int retval = 0; |
|
3813 |
5659
|
3814 if (stream_ok ()) |
2117
|
3815 retval = rep->mode (); |
|
3816 |
|
3817 return retval; |
|
3818 } |
|
3819 |
2317
|
3820 oct_mach_info::float_format |
3340
|
3821 octave_stream::float_format (void) const |
2117
|
3822 { |
4574
|
3823 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317
|
3824 |
5659
|
3825 if (stream_ok ()) |
2317
|
3826 retval = rep->float_format (); |
2117
|
3827 |
|
3828 return retval; |
|
3829 } |
|
3830 |
3536
|
3831 std::string |
2117
|
3832 octave_stream::mode_as_string (int mode) |
|
3833 { |
3523
|
3834 std::string retval = "???"; |
3775
|
3835 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
|
3836 |
|
3837 if (in_mode == std::ios::in) |
|
3838 retval = "r"; |
|
3839 else if (in_mode == std::ios::out |
4078
|
3840 || in_mode == (std::ios::out | std::ios::trunc)) |
3775
|
3841 retval = "w"; |
4078
|
3842 else if (in_mode == (std::ios::out | std::ios::app)) |
3775
|
3843 retval = "a"; |
4078
|
3844 else if (in_mode == (std::ios::in | std::ios::out)) |
3775
|
3845 retval = "r+"; |
4078
|
3846 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775
|
3847 retval = "w+"; |
4078
|
3848 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775
|
3849 retval = "a+"; |
4078
|
3850 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775
|
3851 retval = "rb"; |
4078
|
3852 else if (in_mode == (std::ios::out | std::ios::binary) |
|
3853 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775
|
3854 retval = "wb"; |
4078
|
3855 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775
|
3856 retval = "ab"; |
4078
|
3857 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775
|
3858 retval = "r+b"; |
4078
|
3859 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
|
3860 | std::ios::binary)) |
3775
|
3861 retval = "w+b"; |
4078
|
3862 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
|
3863 | std::ios::binary)) |
3775
|
3864 retval = "a+b"; |
2117
|
3865 |
|
3866 return retval; |
|
3867 } |
|
3868 |
|
3869 octave_stream_list *octave_stream_list::instance = 0; |
|
3870 |
2926
|
3871 bool |
|
3872 octave_stream_list::instance_ok (void) |
|
3873 { |
|
3874 bool retval = true; |
|
3875 |
|
3876 if (! instance) |
|
3877 instance = new octave_stream_list (); |
|
3878 |
|
3879 if (! instance) |
|
3880 { |
|
3881 ::error ("unable to create stream list object!"); |
|
3882 |
|
3883 retval = false; |
|
3884 } |
|
3885 |
|
3886 return retval; |
|
3887 } |
|
3888 |
5353
|
3889 int |
3340
|
3890 octave_stream_list::insert (const octave_stream& os) |
2926
|
3891 { |
5353
|
3892 return (instance_ok ()) ? instance->do_insert (os) : -1; |
2926
|
3893 } |
|
3894 |
3340
|
3895 octave_stream |
3523
|
3896 octave_stream_list::lookup (int fid, const std::string& who) |
2926
|
3897 { |
3341
|
3898 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3899 } |
|
3900 |
3340
|
3901 octave_stream |
3523
|
3902 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926
|
3903 { |
3341
|
3904 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3905 } |
|
3906 |
|
3907 int |
3523
|
3908 octave_stream_list::remove (int fid, const std::string& who) |
2926
|
3909 { |
3341
|
3910 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3911 } |
|
3912 |
|
3913 int |
3523
|
3914 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926
|
3915 { |
3341
|
3916 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3917 } |
|
3918 |
|
3919 void |
|
3920 octave_stream_list::clear (void) |
|
3921 { |
|
3922 if (instance) |
|
3923 instance->do_clear (); |
|
3924 } |
|
3925 |
|
3926 string_vector |
|
3927 octave_stream_list::get_info (int fid) |
|
3928 { |
|
3929 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3930 } |
|
3931 |
|
3932 string_vector |
|
3933 octave_stream_list::get_info (const octave_value& fid) |
|
3934 { |
|
3935 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3936 } |
|
3937 |
3536
|
3938 std::string |
2926
|
3939 octave_stream_list::list_open_files (void) |
|
3940 { |
3523
|
3941 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926
|
3942 } |
|
3943 |
|
3944 octave_value |
|
3945 octave_stream_list::open_file_numbers (void) |
|
3946 { |
|
3947 return (instance_ok ()) |
|
3948 ? instance->do_open_file_numbers () : octave_value (); |
|
3949 } |
|
3950 |
|
3951 int |
|
3952 octave_stream_list::get_file_number (const octave_value& fid) |
|
3953 { |
|
3954 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; |
|
3955 } |
|
3956 |
5353
|
3957 int |
3340
|
3958 octave_stream_list::do_insert (const octave_stream& os) |
2117
|
3959 { |
3340
|
3960 octave_value retval; |
|
3961 |
2902
|
3962 int stream_number = -1; |
|
3963 |
3340
|
3964 // Insert item in first open slot, increasing size of list if |
|
3965 // necessary. |
|
3966 |
|
3967 for (int i = 0; i < curr_len; i++) |
2117
|
3968 { |
3340
|
3969 octave_stream tmp = list(i); |
|
3970 |
5575
|
3971 if (! tmp.is_open ()) |
2117
|
3972 { |
3340
|
3973 list(i) = os; |
|
3974 stream_number = i; |
|
3975 break; |
2117
|
3976 } |
|
3977 } |
3340
|
3978 |
|
3979 if (stream_number < 0) |
|
3980 { |
|
3981 int total_len = list.length (); |
|
3982 |
|
3983 if (curr_len == total_len) |
|
3984 list.resize (total_len * 2); |
|
3985 |
|
3986 list(curr_len) = os; |
|
3987 stream_number = curr_len; |
|
3988 curr_len++; |
|
3989 } |
2117
|
3990 |
5353
|
3991 return stream_number; |
2117
|
3992 } |
|
3993 |
3341
|
3994 static void |
3523
|
3995 gripe_invalid_file_id (int fid, const std::string& who) |
3341
|
3996 { |
|
3997 if (who.empty ()) |
|
3998 ::error ("invalid stream number = %d", fid); |
|
3999 else |
|
4000 ::error ("%s: invalid stream number = %d", who.c_str (), fid); |
|
4001 } |
|
4002 |
3340
|
4003 octave_stream |
3523
|
4004 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117
|
4005 { |
3340
|
4006 octave_stream retval; |
2117
|
4007 |
|
4008 if (fid >= 0 && fid < curr_len) |
3340
|
4009 retval = list(fid); |
3341
|
4010 else |
|
4011 gripe_invalid_file_id (fid, who); |
2117
|
4012 |
|
4013 return retval; |
|
4014 } |
|
4015 |
3340
|
4016 octave_stream |
3341
|
4017 octave_stream_list::do_lookup (const octave_value& fid, |
3523
|
4018 const std::string& who) const |
2117
|
4019 { |
3340
|
4020 octave_stream retval; |
2117
|
4021 |
|
4022 int i = get_file_number (fid); |
|
4023 |
|
4024 if (! error_state) |
3341
|
4025 retval = do_lookup (i, who); |
2117
|
4026 |
|
4027 return retval; |
|
4028 } |
|
4029 |
|
4030 int |
3523
|
4031 octave_stream_list::do_remove (int fid, const std::string& who) |
2117
|
4032 { |
|
4033 int retval = -1; |
|
4034 |
3531
|
4035 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
|
4036 // (std::cerr). |
2117
|
4037 |
|
4038 if (fid > 2 && fid < curr_len) |
|
4039 { |
3340
|
4040 octave_stream os = list(fid); |
2117
|
4041 |
3341
|
4042 if (os.is_valid ()) |
2117
|
4043 { |
3340
|
4044 os.close (); |
|
4045 list(fid) = octave_stream (); |
2117
|
4046 retval = 0; |
|
4047 } |
3341
|
4048 else |
|
4049 gripe_invalid_file_id (fid, who); |
2117
|
4050 } |
3341
|
4051 else |
|
4052 gripe_invalid_file_id (fid, who); |
2117
|
4053 |
|
4054 return retval; |
|
4055 } |
|
4056 |
|
4057 int |
3523
|
4058 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117
|
4059 { |
|
4060 int retval = -1; |
|
4061 |
6054
|
4062 if (fid.is_string () && fid.string_value () == "all") |
|
4063 { |
|
4064 // Skip stdin, stdout, and stderr. |
|
4065 |
|
4066 for (int i = 3; i < curr_len; i++) |
|
4067 { |
|
4068 octave_stream os = list(i); |
|
4069 |
|
4070 if (os.is_valid ()) |
|
4071 do_remove (i, who); |
|
4072 } |
|
4073 |
|
4074 retval = 0; |
|
4075 } |
|
4076 else |
|
4077 { |
|
4078 int i = get_file_number (fid); |
|
4079 |
|
4080 if (! error_state) |
|
4081 retval = do_remove (i, who); |
|
4082 } |
2117
|
4083 |
|
4084 return retval; |
|
4085 } |
|
4086 |
|
4087 void |
|
4088 octave_stream_list::do_clear (void) |
|
4089 { |
|
4090 // Do flush stdout and stderr. |
|
4091 |
3340
|
4092 list(0) . flush (); |
|
4093 list(1) . flush (); |
2117
|
4094 |
|
4095 // But don't delete them or stdin. |
|
4096 |
|
4097 for (int i = 3; i < curr_len; i++) |
3340
|
4098 list(i) = octave_stream (); |
2117
|
4099 } |
|
4100 |
|
4101 string_vector |
|
4102 octave_stream_list::do_get_info (int fid) const |
|
4103 { |
|
4104 string_vector retval; |
|
4105 |
3340
|
4106 octave_stream os = do_lookup (fid); |
2117
|
4107 |
3341
|
4108 if (os.is_valid ()) |
2117
|
4109 { |
|
4110 retval.resize (3); |
|
4111 |
3340
|
4112 retval(0) = os.name (); |
|
4113 retval(1) = octave_stream::mode_as_string (os.mode ()); |
|
4114 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
2117
|
4115 } |
|
4116 else |
3341
|
4117 ::error ("invalid file id = %d", fid); |
2117
|
4118 |
|
4119 return retval; |
|
4120 } |
|
4121 |
|
4122 string_vector |
|
4123 octave_stream_list::do_get_info (const octave_value& fid) const |
|
4124 { |
|
4125 string_vector retval; |
|
4126 |
|
4127 int conv_err = 0; |
|
4128 |
|
4129 int int_fid = convert_to_valid_int (fid, conv_err); |
|
4130 |
|
4131 if (! conv_err) |
|
4132 retval = do_get_info (int_fid); |
|
4133 else |
2915
|
4134 ::error ("file id must be a file object or integer value"); |
2117
|
4135 |
|
4136 return retval; |
|
4137 } |
|
4138 |
3536
|
4139 std::string |
2117
|
4140 octave_stream_list::do_list_open_files (void) const |
|
4141 { |
3523
|
4142 std::string retval; |
2117
|
4143 |
5765
|
4144 std::ostringstream buf; |
2117
|
4145 |
|
4146 buf << "\n" |
|
4147 << " number mode arch name\n" |
|
4148 << " ------ ---- ---- ----\n"; |
|
4149 |
|
4150 for (int i = 0; i < curr_len; i++) |
|
4151 { |
3340
|
4152 octave_stream os = list(i); |
2117
|
4153 |
4326
|
4154 buf << " " |
|
4155 << std::setiosflags (std::ios::right) |
|
4156 << std::setw (4) << i << " " |
|
4157 << std::setiosflags (std::ios::left) |
|
4158 << std::setw (3) |
|
4159 << octave_stream::mode_as_string (os.mode ()) |
|
4160 << " " |
|
4161 << std::setw (9) |
|
4162 << oct_mach_info::float_format_as_string (os.float_format ()) |
|
4163 << " " |
|
4164 << os.name () << "\n"; |
2117
|
4165 } |
|
4166 |
5765
|
4167 buf << "\n"; |
|
4168 |
|
4169 retval = buf.str (); |
2117
|
4170 |
|
4171 return retval; |
|
4172 } |
|
4173 |
|
4174 octave_value |
|
4175 octave_stream_list::do_open_file_numbers (void) const |
|
4176 { |
|
4177 Matrix retval (1, curr_len, 0.0); |
|
4178 |
|
4179 int num_open = 0; |
|
4180 |
|
4181 // Skip stdin, stdout, and stderr. |
|
4182 |
|
4183 for (int i = 3; i < curr_len; i++) |
|
4184 { |
3340
|
4185 if (list(i)) |
2117
|
4186 retval (0, num_open++) = i; |
|
4187 } |
|
4188 |
|
4189 retval.resize ((num_open > 0), num_open); |
|
4190 |
|
4191 return retval; |
|
4192 } |
|
4193 |
|
4194 int |
2609
|
4195 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117
|
4196 { |
|
4197 int retval = -1; |
|
4198 |
|
4199 if (fid.is_string ()) |
|
4200 { |
3523
|
4201 std::string nm = fid.string_value (); |
2117
|
4202 |
3531
|
4203 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
|
4204 // are unnamed. |
2117
|
4205 |
|
4206 for (int i = 3; i < curr_len; i++) |
|
4207 { |
3340
|
4208 octave_stream os = list(i); |
|
4209 |
|
4210 if (os && os.name () == nm) |
2117
|
4211 { |
|
4212 retval = i; |
|
4213 break; |
|
4214 } |
|
4215 } |
|
4216 } |
|
4217 else |
|
4218 { |
|
4219 int conv_err = 0; |
|
4220 |
|
4221 int int_fid = convert_to_valid_int (fid, conv_err); |
|
4222 |
|
4223 if (conv_err) |
3523
|
4224 ::error ("file id must be a file object, std::string, or integer value"); |
2117
|
4225 else |
|
4226 retval = int_fid; |
|
4227 } |
|
4228 |
|
4229 return retval; |
|
4230 } |
|
4231 |
|
4232 /* |
|
4233 ;;; Local Variables: *** |
|
4234 ;;; mode: C++ *** |
|
4235 ;;; End: *** |
|
4236 */ |