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