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