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