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