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 |
|
1145 template std::istream& |
|
1146 octave_scan (std::istream&, const scanf_format_elt&, double*); |
|
1147 |
3636
|
1148 #endif |
|
1149 |
2572
|
1150 template <class T> |
|
1151 void |
3636
|
1152 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt, |
|
1153 T valptr, Matrix& mval, double *data, int& idx, |
|
1154 int& conversion_count, int nr, int max_size, |
|
1155 bool discard) |
2572
|
1156 { |
3640
|
1157 OCTAVE_SCAN (is, fmt, valptr); |
2572
|
1158 |
|
1159 if (is) |
|
1160 { |
|
1161 if (idx == max_size && ! discard) |
|
1162 { |
|
1163 max_size *= 2; |
|
1164 |
|
1165 if (nr > 0) |
|
1166 mval.resize (nr, max_size / nr, 0.0); |
|
1167 else |
|
1168 mval.resize (max_size, 1, 0.0); |
|
1169 |
|
1170 data = mval.fortran_vec (); |
|
1171 } |
|
1172 |
|
1173 if (! discard) |
3268
|
1174 { |
3559
|
1175 conversion_count++; |
3268
|
1176 data[idx++] = *(valptr); |
|
1177 } |
2572
|
1178 } |
|
1179 } |
|
1180 |
|
1181 template void |
3878
|
1182 do_scanf_conv (std::istream&, const scanf_format_elt&, int*, |
|
1183 Matrix&, double*, int&, int&, int, int, bool); |
2572
|
1184 |
3233
|
1185 template void |
3636
|
1186 do_scanf_conv (std::istream&, const scanf_format_elt&, long int*, |
|
1187 Matrix&, double*, int&, int&, int, int, bool); |
3233
|
1188 |
|
1189 template void |
3636
|
1190 do_scanf_conv (std::istream&, const scanf_format_elt&, short int*, |
|
1191 Matrix&, double*, int&, int&, int, int, bool); |
3233
|
1192 |
3878
|
1193 template void |
|
1194 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned int*, |
|
1195 Matrix&, double*, int&, int&, int, int, bool); |
|
1196 |
|
1197 template void |
|
1198 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned long int*, |
|
1199 Matrix&, double*, int&, int&, int, int, bool); |
|
1200 |
|
1201 template void |
|
1202 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned short int*, |
|
1203 Matrix&, double*, int&, int&, int, int, bool); |
|
1204 |
2600
|
1205 #if 0 |
2572
|
1206 template void |
3636
|
1207 do_scanf_conv (std::istream&, const scanf_format_elt&, float*, |
|
1208 Matrix&, double*, int&, int&, int, int, bool); |
2600
|
1209 #endif |
2572
|
1210 |
|
1211 template void |
3636
|
1212 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
|
1213 Matrix&, double*, int&, int&, int, int, bool); |
2572
|
1214 |
3483
|
1215 #define DO_WHITESPACE_CONVERSION() \ |
|
1216 do \ |
|
1217 { \ |
|
1218 int c = EOF; \ |
|
1219 \ |
|
1220 while (is && (c = is.get ()) != EOF && isspace (c)) \ |
|
1221 /* skip whitespace */; \ |
|
1222 \ |
|
1223 if (c != EOF) \ |
|
1224 is.putback (c); \ |
|
1225 } \ |
|
1226 while (0) |
|
1227 |
|
1228 #define DO_LITERAL_CONVERSION() \ |
|
1229 do \ |
|
1230 { \ |
|
1231 int c = EOF; \ |
|
1232 \ |
|
1233 int n = strlen (fmt); \ |
|
1234 int i = 0; \ |
|
1235 \ |
|
1236 while (i < n && is && (c = is.get ()) != EOF) \ |
|
1237 { \ |
|
1238 if (c == fmt[i]) \ |
|
1239 { \ |
|
1240 i++; \ |
|
1241 continue; \ |
|
1242 } \ |
|
1243 else \ |
|
1244 { \ |
|
1245 is.putback (c); \ |
|
1246 break; \ |
|
1247 } \ |
|
1248 } \ |
|
1249 \ |
|
1250 if (i != n) \ |
3538
|
1251 is.setstate (std::ios::failbit); \ |
3483
|
1252 } \ |
|
1253 while (0) |
|
1254 |
3640
|
1255 #define DO_PCT_CONVERSION() \ |
|
1256 do \ |
|
1257 { \ |
|
1258 int c = is.get (); \ |
|
1259 \ |
|
1260 if (c != EOF) \ |
|
1261 { \ |
|
1262 if (c != '%') \ |
|
1263 { \ |
|
1264 is.putback (c); \ |
|
1265 is.setstate (std::ios::failbit); \ |
|
1266 } \ |
|
1267 } \ |
|
1268 else \ |
|
1269 is.setstate (std::ios::failbit); \ |
|
1270 } \ |
|
1271 while (0) |
|
1272 |
3483
|
1273 #define BEGIN_C_CONVERSION() \ |
3538
|
1274 is.unsetf (std::ios::skipws); \ |
3483
|
1275 \ |
|
1276 int width = elt->width ? elt->width : 1; \ |
|
1277 \ |
4051
|
1278 char *tbuf = new char[width + 1]; \ |
3483
|
1279 \ |
|
1280 int c = EOF; \ |
|
1281 int n = 0; \ |
|
1282 \ |
|
1283 while (is && n < width && (c = is.get ()) != EOF) \ |
4051
|
1284 tbuf[n++] = (char) c; \ |
|
1285 \ |
|
1286 tbuf[n] = '\0'; \ |
3483
|
1287 \ |
4051
|
1288 std::string tmp = tbuf; \ |
|
1289 \ |
|
1290 delete [] tbuf |
3483
|
1291 |
|
1292 // For a `%s' format, skip initial whitespace and then read until the |
|
1293 // next whitespace character. |
|
1294 #define BEGIN_S_CONVERSION() \ |
|
1295 int width = elt->width; \ |
|
1296 \ |
4051
|
1297 std::string tmp; \ |
3483
|
1298 \ |
|
1299 do \ |
|
1300 { \ |
|
1301 if (width) \ |
|
1302 { \ |
4051
|
1303 char *tbuf = new char [width+1]; \ |
|
1304 \ |
|
1305 OCTAVE_SCAN (is, *elt, tbuf); \ |
3483
|
1306 \ |
4051
|
1307 tbuf[width] = '\0'; \ |
|
1308 tmp = tbuf; \ |
|
1309 delete [] tbuf; \ |
3483
|
1310 } \ |
|
1311 else \ |
|
1312 { \ |
4051
|
1313 is >> std::ws >> tmp; \ |
3483
|
1314 } \ |
|
1315 } \ |
|
1316 while (0) |
|
1317 |
|
1318 // This format must match a nonempty sequence of characters. |
|
1319 #define BEGIN_CHAR_CLASS_CONVERSION() \ |
|
1320 int width = elt->width; \ |
|
1321 \ |
4051
|
1322 std::string tmp; \ |
3483
|
1323 \ |
|
1324 do \ |
|
1325 { \ |
|
1326 if (width) \ |
|
1327 { \ |
4051
|
1328 char *tbuf = new char[width+1]; \ |
|
1329 \ |
|
1330 OCTAVE_SCAN (is, *elt, tbuf); \ |
3483
|
1331 \ |
4051
|
1332 tbuf[width] = '\0'; \ |
|
1333 tmp = tbuf; \ |
|
1334 delete [] tbuf; \ |
3483
|
1335 } \ |
|
1336 else \ |
|
1337 { \ |
4051
|
1338 OSSTREAM buf; \ |
3483
|
1339 \ |
3523
|
1340 std::string char_class = elt->char_class; \ |
3483
|
1341 \ |
|
1342 int c = EOF; \ |
|
1343 \ |
|
1344 if (elt->type == '[') \ |
|
1345 { \ |
|
1346 while (is && (c = is.get ()) != EOF \ |
|
1347 && char_class.find (c) != NPOS) \ |
|
1348 buf << (char) c; \ |
|
1349 } \ |
|
1350 else \ |
|
1351 { \ |
|
1352 while (is && (c = is.get ()) != EOF \ |
|
1353 && char_class.find (c) == NPOS) \ |
|
1354 buf << (char) c; \ |
|
1355 } \ |
|
1356 \ |
|
1357 if (c != EOF) \ |
|
1358 is.putback (c); \ |
|
1359 \ |
4051
|
1360 buf << OSSTREAM_ENDS; \ |
|
1361 tmp = OSSTREAM_STR (buf); \ |
|
1362 OSSTREAM_FREEZE (buf); \ |
3483
|
1363 \ |
4051
|
1364 if (tmp.empty ()) \ |
3538
|
1365 is.setstate (std::ios::failbit); \ |
3483
|
1366 } \ |
|
1367 } \ |
|
1368 while (0) |
|
1369 |
3410
|
1370 #define FINISH_CHARACTER_CONVERSION() \ |
|
1371 do \ |
|
1372 { \ |
4051
|
1373 width = tmp.length (); \ |
3410
|
1374 \ |
|
1375 if (is) \ |
|
1376 { \ |
|
1377 int i = 0; \ |
|
1378 \ |
|
1379 if (! discard) \ |
|
1380 { \ |
|
1381 conversion_count++; \ |
|
1382 \ |
|
1383 while (i < width && tmp[i] != '\0') \ |
|
1384 { \ |
|
1385 if (data_index == max_size) \ |
|
1386 { \ |
|
1387 max_size *= 2; \ |
|
1388 \ |
4420
|
1389 if (all_char_conv) \ |
3410
|
1390 { \ |
4420
|
1391 if (one_elt_size_spec) \ |
3410
|
1392 mval.resize (1, max_size, 0.0); \ |
4420
|
1393 else if (nr > 0) \ |
|
1394 mval.resize (nr, max_size / nr, 0.0); \ |
3410
|
1395 else \ |
4420
|
1396 panic_impossible (); \ |
3410
|
1397 } \ |
4420
|
1398 else if (nr > 0) \ |
|
1399 { \ |
|
1400 if (nc <= 0) \ |
|
1401 mval.resize (nr, max_size / nr, 0.0); \ |
|
1402 else \ |
|
1403 panic_impossible (); \ |
|
1404 } \ |
|
1405 else \ |
|
1406 mval.resize (max_size, 1, 0.0); \ |
3410
|
1407 \ |
|
1408 data = mval.fortran_vec (); \ |
|
1409 } \ |
|
1410 \ |
|
1411 data[data_index++] = tmp[i++]; \ |
|
1412 } \ |
|
1413 } \ |
|
1414 } \ |
|
1415 } \ |
|
1416 while (0) |
2117
|
1417 |
|
1418 octave_value |
|
1419 octave_base_stream::do_scanf (scanf_format_list& fmt_list, |
3268
|
1420 int nr, int nc, bool one_elt_size_spec, |
4468
|
1421 int& conversion_count, const std::string& who) |
2117
|
1422 { |
3268
|
1423 conversion_count = 0; |
|
1424 |
3640
|
1425 int nconv = fmt_list.num_conversions (); |
|
1426 |
3268
|
1427 int data_index = 0; |
2121
|
1428 |
2117
|
1429 octave_value retval = Matrix (); |
|
1430 |
3268
|
1431 if (nr == 0 || nc == 0) |
|
1432 { |
|
1433 if (one_elt_size_spec) |
|
1434 nc = 0; |
|
1435 |
|
1436 return Matrix (nr, nc, 0.0); |
|
1437 } |
|
1438 |
3523
|
1439 std::istream *isp = input_stream (); |
2117
|
1440 |
|
1441 bool all_char_conv = fmt_list.all_character_conversions (); |
|
1442 |
|
1443 Matrix mval; |
|
1444 double *data = 0; |
|
1445 int max_size = 0; |
3268
|
1446 int max_conv = 0; |
2117
|
1447 |
|
1448 int final_nr = 0; |
|
1449 int final_nc = 0; |
|
1450 |
3268
|
1451 if (all_char_conv) |
|
1452 { |
4420
|
1453 // Any of these could be resized later (if we have %s |
|
1454 // conversions, we may read more than one element for each |
|
1455 // conversion). |
|
1456 |
3268
|
1457 if (one_elt_size_spec) |
|
1458 { |
3410
|
1459 max_size = 512; |
|
1460 mval.resize (1, max_size, 0.0); |
|
1461 |
3268
|
1462 if (nr > 0) |
|
1463 max_conv = nr; |
|
1464 } |
4420
|
1465 else if (nr > 0) |
3268
|
1466 { |
4420
|
1467 if (nc > 0) |
|
1468 { |
|
1469 mval.resize (nr, nc, 0.0); |
|
1470 max_size = max_conv = nr * nc; |
|
1471 } |
|
1472 else |
|
1473 { |
|
1474 mval.resize (nr, 32, 0.0); |
|
1475 max_size = nr * 32; |
|
1476 } |
3268
|
1477 } |
4420
|
1478 else |
|
1479 panic_impossible (); |
3268
|
1480 } |
|
1481 else if (nr > 0) |
2117
|
1482 { |
|
1483 if (nc > 0) |
|
1484 { |
4420
|
1485 // Will not resize later. |
2117
|
1486 mval.resize (nr, nc, 0.0); |
|
1487 max_size = nr * nc; |
3268
|
1488 max_conv = max_size; |
2117
|
1489 } |
|
1490 else |
|
1491 { |
4420
|
1492 // Maybe resize later. |
2117
|
1493 mval.resize (nr, 32, 0.0); |
2121
|
1494 max_size = nr * 32; |
2117
|
1495 } |
|
1496 } |
|
1497 else |
|
1498 { |
4420
|
1499 // Maybe resize later. |
2117
|
1500 mval.resize (32, 1, 0.0); |
|
1501 max_size = 32; |
|
1502 } |
|
1503 |
4420
|
1504 data = mval.fortran_vec (); |
|
1505 |
2117
|
1506 if (isp) |
|
1507 { |
3523
|
1508 std::istream& is = *isp; |
2117
|
1509 |
|
1510 const scanf_format_elt *elt = fmt_list.first (); |
|
1511 |
3538
|
1512 std::ios::fmtflags flags = is.flags (); |
2213
|
1513 |
2117
|
1514 for (;;) |
|
1515 { |
4153
|
1516 OCTAVE_QUIT; |
|
1517 |
2117
|
1518 if (elt) |
|
1519 { |
3268
|
1520 if (max_conv > 0 && conversion_count == max_conv) |
|
1521 { |
|
1522 if (all_char_conv && one_elt_size_spec) |
|
1523 { |
|
1524 final_nr = 1; |
|
1525 final_nc = data_index; |
|
1526 } |
|
1527 else |
|
1528 { |
|
1529 final_nr = nr; |
|
1530 final_nc = (data_index - 1) / nr + 1; |
|
1531 } |
|
1532 |
|
1533 break; |
|
1534 } |
|
1535 else if (data_index == max_size) |
2117
|
1536 { |
3410
|
1537 max_size *= 2; |
|
1538 |
4420
|
1539 if (all_char_conv) |
2687
|
1540 { |
4420
|
1541 if (one_elt_size_spec) |
3410
|
1542 mval.resize (1, max_size, 0.0); |
4420
|
1543 else if (nr > 0) |
|
1544 mval.resize (nr, max_size / nr, 0.0); |
3410
|
1545 else |
4420
|
1546 panic_impossible (); |
2687
|
1547 } |
4420
|
1548 else if (nr > 0) |
|
1549 { |
|
1550 if (nc <= 0) |
|
1551 mval.resize (nr, max_size / nr, 0.0); |
|
1552 else |
|
1553 panic_impossible (); |
|
1554 } |
|
1555 else |
|
1556 mval.resize (max_size, 1, 0.0); |
3410
|
1557 |
|
1558 data = mval.fortran_vec (); |
2117
|
1559 } |
|
1560 |
|
1561 const char *fmt = elt->text; |
|
1562 |
|
1563 bool discard = elt->discard; |
|
1564 |
|
1565 switch (elt->type) |
|
1566 { |
3483
|
1567 case scanf_format_elt::whitespace_conversion: |
|
1568 DO_WHITESPACE_CONVERSION (); |
|
1569 break; |
|
1570 |
|
1571 case scanf_format_elt::literal_conversion: |
|
1572 DO_LITERAL_CONVERSION (); |
|
1573 break; |
|
1574 |
2117
|
1575 case '%': |
3640
|
1576 DO_PCT_CONVERSION (); |
3483
|
1577 break; |
2117
|
1578 |
3878
|
1579 case 'd': case 'i': |
2117
|
1580 { |
3233
|
1581 switch (elt->modifier) |
|
1582 { |
|
1583 case 'h': |
|
1584 { |
|
1585 short int tmp; |
3779
|
1586 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1587 data_index, conversion_count, |
3233
|
1588 nr, max_size, discard); |
|
1589 } |
3483
|
1590 break; |
3233
|
1591 |
|
1592 case 'l': |
|
1593 { |
|
1594 long int tmp; |
3779
|
1595 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1596 data_index, conversion_count, |
3233
|
1597 nr, max_size, discard); |
|
1598 } |
3483
|
1599 break; |
3233
|
1600 |
|
1601 default: |
|
1602 { |
|
1603 int tmp; |
3779
|
1604 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1605 data_index, conversion_count, |
3233
|
1606 nr, max_size, discard); |
|
1607 } |
3483
|
1608 break; |
3233
|
1609 } |
2117
|
1610 } |
3483
|
1611 break; |
2117
|
1612 |
3878
|
1613 case 'o': case 'u': case 'x': |
|
1614 { |
|
1615 switch (elt->modifier) |
|
1616 { |
|
1617 case 'h': |
|
1618 { |
|
1619 unsigned short int tmp; |
|
1620 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1621 data_index, conversion_count, |
|
1622 nr, max_size, discard); |
|
1623 } |
|
1624 break; |
|
1625 |
|
1626 case 'l': |
|
1627 { |
|
1628 unsigned long int tmp; |
|
1629 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1630 data_index, conversion_count, |
|
1631 nr, max_size, discard); |
|
1632 } |
|
1633 break; |
|
1634 |
|
1635 default: |
|
1636 { |
|
1637 unsigned int tmp; |
|
1638 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1639 data_index, conversion_count, |
|
1640 nr, max_size, discard); |
|
1641 } |
|
1642 break; |
|
1643 } |
|
1644 } |
|
1645 break; |
|
1646 |
2117
|
1647 case 'e': case 'f': case 'g': |
|
1648 { |
2600
|
1649 double tmp; |
|
1650 |
3779
|
1651 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1652 data_index, conversion_count, |
2600
|
1653 nr, max_size, discard); |
2117
|
1654 } |
3483
|
1655 break; |
2117
|
1656 |
2213
|
1657 case 'c': |
3410
|
1658 { |
3483
|
1659 BEGIN_C_CONVERSION (); |
3410
|
1660 |
|
1661 FINISH_CHARACTER_CONVERSION (); |
3483
|
1662 |
|
1663 is.setf (flags); |
3410
|
1664 } |
|
1665 break; |
2213
|
1666 |
2117
|
1667 case 's': |
|
1668 { |
3483
|
1669 BEGIN_S_CONVERSION (); |
3268
|
1670 |
3410
|
1671 FINISH_CHARACTER_CONVERSION (); |
2117
|
1672 } |
3483
|
1673 break; |
|
1674 |
|
1675 case '[': case '^': |
|
1676 { |
|
1677 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1678 |
|
1679 FINISH_CHARACTER_CONVERSION (); |
|
1680 } |
|
1681 break; |
|
1682 |
|
1683 case 'p': |
4468
|
1684 error ("%s: unsupported format specifier", who.c_str ()); |
2117
|
1685 break; |
|
1686 |
|
1687 default: |
4468
|
1688 error ("%s: internal format error", who.c_str ()); |
2117
|
1689 break; |
|
1690 } |
|
1691 |
|
1692 if (! ok ()) |
|
1693 { |
|
1694 break; |
|
1695 } |
|
1696 else if (! is) |
|
1697 { |
3268
|
1698 if (all_char_conv) |
2117
|
1699 { |
3268
|
1700 if (one_elt_size_spec) |
|
1701 { |
|
1702 final_nr = 1; |
|
1703 final_nc = data_index; |
|
1704 } |
|
1705 else if (data_index > nr) |
2117
|
1706 { |
2759
|
1707 final_nr = nr; |
3268
|
1708 final_nc = (data_index - 1) / nr + 1; |
2117
|
1709 } |
|
1710 else |
|
1711 { |
3268
|
1712 final_nr = data_index; |
|
1713 final_nc = 1; |
|
1714 } |
|
1715 } |
|
1716 else if (nr > 0) |
|
1717 { |
|
1718 if (data_index > nr) |
|
1719 { |
|
1720 final_nr = nr; |
|
1721 final_nc = (data_index - 1) / nr + 1; |
|
1722 } |
|
1723 else |
|
1724 { |
|
1725 final_nr = data_index; |
2117
|
1726 final_nc = 1; |
|
1727 } |
|
1728 } |
|
1729 else |
|
1730 { |
3268
|
1731 final_nr = data_index; |
2759
|
1732 final_nc = 1; |
|
1733 } |
|
1734 |
3337
|
1735 // If it looks like we have a matching failure, then |
|
1736 // reset the failbit in the stream state. |
|
1737 |
3538
|
1738 if (is.rdstate () & std::ios::failbit) |
|
1739 is.clear (is.rdstate () & (~std::ios::failbit)); |
3337
|
1740 |
2759
|
1741 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1742 |
|
1743 if (interactive && name () == "stdin") |
2759
|
1744 { |
|
1745 is.clear (); |
|
1746 |
|
1747 // Skip to end of line. |
|
1748 |
|
1749 bool err; |
4468
|
1750 do_gets (-1, err, false, who); |
2117
|
1751 } |
|
1752 |
|
1753 break; |
|
1754 } |
|
1755 } |
|
1756 else |
|
1757 { |
4468
|
1758 error ("%s: internal format error", who.c_str ()); |
2117
|
1759 break; |
|
1760 } |
|
1761 |
3640
|
1762 elt = fmt_list.next (nconv > 0); |
2117
|
1763 } |
|
1764 } |
|
1765 |
|
1766 if (ok ()) |
|
1767 { |
2121
|
1768 mval.resize (final_nr, final_nc, 0.0); |
2117
|
1769 |
3268
|
1770 retval = mval; |
|
1771 |
2117
|
1772 if (all_char_conv) |
3268
|
1773 retval = retval.convert_to_str (); |
2117
|
1774 } |
|
1775 |
|
1776 return retval; |
|
1777 } |
|
1778 |
|
1779 octave_value |
3810
|
1780 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
4468
|
1781 int& conversion_count, const std::string& who) |
2117
|
1782 { |
|
1783 octave_value retval = Matrix (); |
|
1784 |
3559
|
1785 conversion_count = 0; |
2117
|
1786 |
3523
|
1787 std::istream *isp = input_stream (); |
2117
|
1788 |
|
1789 if (isp) |
|
1790 { |
|
1791 scanf_format_list fmt_list (fmt); |
|
1792 |
3640
|
1793 if (fmt_list.num_conversions () == -1) |
4468
|
1794 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
1795 else |
2117
|
1796 { |
3640
|
1797 int nr = -1; |
|
1798 int nc = -1; |
|
1799 |
|
1800 bool one_elt_size_spec; |
|
1801 |
4468
|
1802 get_size (size, nr, nc, one_elt_size_spec, who); |
3640
|
1803 |
|
1804 if (! error_state) |
|
1805 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, |
4468
|
1806 conversion_count, who); |
2215
|
1807 } |
|
1808 } |
|
1809 else |
4468
|
1810 invalid_operation (who, "reading"); |
2572
|
1811 |
|
1812 return retval; |
|
1813 } |
|
1814 |
2712
|
1815 bool |
|
1816 octave_base_stream::do_oscanf (const scanf_format_elt *elt, |
4468
|
1817 octave_value& retval, const std::string& who) |
2572
|
1818 { |
2712
|
1819 bool quit = false; |
2215
|
1820 |
3523
|
1821 std::istream *isp = input_stream (); |
2215
|
1822 |
|
1823 if (isp) |
|
1824 { |
3523
|
1825 std::istream& is = *isp; |
2215
|
1826 |
3538
|
1827 std::ios::fmtflags flags = is.flags (); |
2215
|
1828 |
|
1829 if (elt) |
|
1830 { |
|
1831 const char *fmt = elt->text; |
|
1832 |
|
1833 bool discard = elt->discard; |
|
1834 |
|
1835 switch (elt->type) |
|
1836 { |
3483
|
1837 case scanf_format_elt::whitespace_conversion: |
|
1838 DO_WHITESPACE_CONVERSION (); |
|
1839 break; |
|
1840 |
|
1841 case scanf_format_elt::literal_conversion: |
|
1842 DO_LITERAL_CONVERSION (); |
|
1843 break; |
|
1844 |
2215
|
1845 case '%': |
|
1846 { |
3640
|
1847 DO_PCT_CONVERSION (); |
|
1848 |
|
1849 if (! is) |
2712
|
1850 quit = true; |
3640
|
1851 |
2215
|
1852 } |
|
1853 break; |
|
1854 |
3878
|
1855 case 'd': case 'i': |
2215
|
1856 { |
|
1857 int tmp; |
|
1858 |
3640
|
1859 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
1860 { |
|
1861 if (! discard) |
4233
|
1862 retval = tmp; |
2712
|
1863 } |
|
1864 else |
|
1865 quit = true; |
2215
|
1866 } |
|
1867 break; |
|
1868 |
3878
|
1869 case 'o': case 'u': case 'x': |
|
1870 { |
|
1871 long int tmp; |
|
1872 |
|
1873 if (OCTAVE_SCAN (is, *elt, &tmp)) |
|
1874 { |
|
1875 if (! discard) |
4254
|
1876 retval = tmp; |
3878
|
1877 } |
|
1878 else |
|
1879 quit = true; |
|
1880 } |
|
1881 break; |
|
1882 |
2215
|
1883 case 'e': case 'f': case 'g': |
|
1884 { |
2600
|
1885 double tmp; |
|
1886 |
3640
|
1887 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
1888 { |
|
1889 if (! discard) |
|
1890 retval = tmp; |
|
1891 } |
|
1892 else |
|
1893 quit = true; |
2215
|
1894 } |
|
1895 break; |
|
1896 |
|
1897 case 'c': |
|
1898 { |
3483
|
1899 BEGIN_C_CONVERSION (); |
|
1900 |
|
1901 if (! discard) |
|
1902 retval = tmp; |
|
1903 |
|
1904 if (! is) |
2712
|
1905 quit = true; |
2215
|
1906 |
|
1907 is.setf (flags); |
|
1908 } |
|
1909 break; |
|
1910 |
|
1911 case 's': |
|
1912 { |
3483
|
1913 BEGIN_S_CONVERSION (); |
|
1914 |
|
1915 if (! discard) |
|
1916 retval = tmp; |
2572
|
1917 |
3268
|
1918 if (! is) |
|
1919 quit = true; |
2215
|
1920 } |
|
1921 break; |
|
1922 |
3483
|
1923 case '[': case '^': |
|
1924 { |
|
1925 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1926 |
|
1927 if (! discard) |
|
1928 retval = tmp; |
|
1929 |
|
1930 if (! is) |
|
1931 quit = true; |
|
1932 } |
|
1933 break; |
|
1934 |
|
1935 case 'p': |
4468
|
1936 error ("%s: unsupported format specifier", who.c_str ()); |
2215
|
1937 break; |
|
1938 |
|
1939 default: |
4468
|
1940 error ("%s: internal format error", who.c_str ()); |
2215
|
1941 break; |
|
1942 } |
|
1943 } |
|
1944 |
|
1945 if (ok () && is.fail ()) |
|
1946 { |
4468
|
1947 error ("%s: read error", who.c_str ()); |
3483
|
1948 |
2215
|
1949 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1950 |
|
1951 if (interactive && name () == "stdin") |
2215
|
1952 { |
|
1953 // Skip to end of line. |
|
1954 |
|
1955 bool err; |
4468
|
1956 do_gets (-1, err, false, who); |
2215
|
1957 } |
|
1958 } |
|
1959 } |
|
1960 |
2712
|
1961 return quit; |
2215
|
1962 } |
|
1963 |
|
1964 octave_value_list |
4468
|
1965 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
1966 { |
|
1967 octave_value_list retval; |
|
1968 |
3523
|
1969 std::istream *isp = input_stream (); |
2215
|
1970 |
|
1971 if (isp) |
|
1972 { |
3523
|
1973 std::istream& is = *isp; |
2215
|
1974 |
|
1975 scanf_format_list fmt_list (fmt); |
|
1976 |
|
1977 int nconv = fmt_list.num_conversions (); |
|
1978 |
3640
|
1979 if (nconv == -1) |
4468
|
1980 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
1981 else |
2215
|
1982 { |
3640
|
1983 is.clear (); |
|
1984 |
|
1985 int len = fmt_list.length (); |
|
1986 |
|
1987 retval.resize (nconv+1, Matrix ()); |
|
1988 |
|
1989 const scanf_format_elt *elt = fmt_list.first (); |
|
1990 |
|
1991 int num_values = 0; |
|
1992 |
|
1993 bool quit = false; |
|
1994 |
|
1995 for (int i = 0; i < len; i++) |
|
1996 { |
|
1997 octave_value tmp; |
|
1998 |
4468
|
1999 quit = do_oscanf (elt, tmp, who); |
3640
|
2000 |
|
2001 if (quit) |
|
2002 break; |
|
2003 else |
|
2004 { |
|
2005 if (tmp.is_defined ()) |
|
2006 retval (num_values++) = tmp; |
|
2007 |
|
2008 if (! ok ()) |
|
2009 break; |
|
2010 |
|
2011 elt = fmt_list.next (nconv > 0); |
|
2012 } |
|
2013 } |
|
2014 |
4233
|
2015 retval(nconv) = num_values; |
3640
|
2016 |
|
2017 if (! quit) |
|
2018 { |
|
2019 // Pick up any trailing stuff. |
|
2020 if (ok () && len > nconv) |
|
2021 { |
|
2022 octave_value tmp; |
3704
|
2023 |
|
2024 elt = fmt_list.next (); |
|
2025 |
4468
|
2026 do_oscanf (elt, tmp, who); |
3640
|
2027 } |
|
2028 } |
2117
|
2029 } |
|
2030 } |
|
2031 else |
4468
|
2032 invalid_operation (who, "reading"); |
2117
|
2033 |
|
2034 return retval; |
|
2035 } |
|
2036 |
|
2037 // Functions that are defined for all output streams (output streams |
|
2038 // are those that define os). |
|
2039 |
|
2040 int |
|
2041 octave_base_stream::flush (void) |
|
2042 { |
|
2043 int retval = -1; |
|
2044 |
3523
|
2045 std::ostream *os = output_stream (); |
2117
|
2046 |
|
2047 if (os) |
|
2048 { |
|
2049 os->flush (); |
|
2050 |
|
2051 if (os->good ()) |
|
2052 retval = 0; |
|
2053 } |
|
2054 else |
|
2055 invalid_operation ("fflush", "writing"); |
|
2056 |
|
2057 return retval; |
|
2058 } |
|
2059 |
|
2060 class |
|
2061 printf_value_cache |
|
2062 { |
|
2063 public: |
|
2064 |
3653
|
2065 enum state { ok, conversion_error }; |
2117
|
2066 |
|
2067 printf_value_cache (const octave_value_list& args) |
|
2068 : values (args), val_idx (0), elt_idx (0), |
|
2069 n_vals (values.length ()), n_elts (0), data (0), |
|
2070 curr_state (ok) { } |
|
2071 |
|
2072 ~printf_value_cache (void) { } |
|
2073 |
|
2074 // Get the current value as a double and advance the internal pointer. |
|
2075 double double_value (void); |
|
2076 |
|
2077 // Get the current value as an int and advance the internal pointer. |
|
2078 int int_value (void); |
|
2079 |
|
2080 // Get the current value as a string and advance the internal pointer. |
3523
|
2081 std::string string_value (void); |
2117
|
2082 |
3145
|
2083 operator bool () const { return (curr_state == ok); } |
2117
|
2084 |
3653
|
2085 bool exhausted (void) { return (val_idx >= n_vals); } |
2117
|
2086 |
|
2087 private: |
|
2088 |
|
2089 const octave_value_list values; |
|
2090 int val_idx; |
|
2091 int elt_idx; |
|
2092 int n_vals; |
|
2093 int n_elts; |
|
2094 const double *data; |
4874
|
2095 NDArray curr_val; |
2117
|
2096 state curr_state; |
|
2097 |
|
2098 // Must create value cache with values! |
|
2099 |
|
2100 printf_value_cache (void); |
|
2101 |
|
2102 // No copying! |
|
2103 |
|
2104 printf_value_cache (const printf_value_cache&); |
|
2105 |
|
2106 printf_value_cache& operator = (const printf_value_cache&); |
|
2107 }; |
|
2108 |
|
2109 double |
|
2110 printf_value_cache::double_value (void) |
|
2111 { |
|
2112 double retval = 0.0; |
|
2113 |
3711
|
2114 if (exhausted ()) |
|
2115 curr_state = conversion_error; |
|
2116 |
|
2117 while (! exhausted ()) |
2117
|
2118 { |
|
2119 if (! data) |
|
2120 { |
|
2121 octave_value tmp_val = values (val_idx); |
|
2122 |
4874
|
2123 curr_val = tmp_val.array_value (); |
2117
|
2124 |
|
2125 if (! error_state) |
|
2126 { |
|
2127 elt_idx = 0; |
|
2128 n_elts = curr_val.length (); |
|
2129 data = curr_val.data (); |
|
2130 } |
|
2131 else |
|
2132 { |
|
2133 curr_state = conversion_error; |
|
2134 break; |
|
2135 } |
|
2136 } |
|
2137 |
|
2138 if (elt_idx < n_elts) |
|
2139 { |
3653
|
2140 retval = data[elt_idx++]; |
|
2141 |
|
2142 if (elt_idx >= n_elts) |
|
2143 { |
|
2144 elt_idx = 0; |
|
2145 val_idx++; |
|
2146 data = 0; |
|
2147 } |
|
2148 |
2117
|
2149 break; |
|
2150 } |
|
2151 else |
|
2152 { |
|
2153 val_idx++; |
|
2154 data = 0; |
3969
|
2155 |
|
2156 if (n_elts == 0 && exhausted ()) |
|
2157 curr_state = conversion_error; |
|
2158 |
2117
|
2159 continue; |
|
2160 } |
|
2161 } |
|
2162 |
|
2163 return retval; |
|
2164 } |
|
2165 |
|
2166 int |
|
2167 printf_value_cache::int_value (void) |
|
2168 { |
|
2169 int retval = 0; |
|
2170 |
|
2171 double dval = double_value (); |
|
2172 |
|
2173 if (! error_state) |
|
2174 { |
|
2175 if (D_NINT (dval) == dval) |
|
2176 retval = NINT (dval); |
|
2177 else |
|
2178 curr_state = conversion_error; |
|
2179 } |
|
2180 |
|
2181 return retval; |
|
2182 } |
|
2183 |
3536
|
2184 std::string |
2117
|
2185 printf_value_cache::string_value (void) |
|
2186 { |
3523
|
2187 std::string retval; |
2117
|
2188 |
4425
|
2189 if (exhausted ()) |
|
2190 curr_state = conversion_error; |
4257
|
2191 else |
2117
|
2192 { |
4425
|
2193 octave_value tval = values (val_idx++); |
|
2194 |
|
2195 if (tval.rows () == 1) |
|
2196 retval = tval.string_value (); |
|
2197 else |
|
2198 { |
|
2199 // In the name of Matlab compatibility. |
|
2200 |
|
2201 charMatrix chm = tval.char_matrix_value (); |
|
2202 |
|
2203 int nr = chm.rows (); |
|
2204 int nc = chm.columns (); |
|
2205 |
|
2206 int k = 0; |
|
2207 |
|
2208 retval.resize (nr * nc, '\0'); |
|
2209 |
|
2210 for (int j = 0; j < nc; j++) |
|
2211 for (int i = 0; i < nr; i++) |
|
2212 retval[k++] = chm(i,j); |
|
2213 } |
|
2214 |
|
2215 if (error_state) |
|
2216 curr_state = conversion_error; |
2117
|
2217 } |
4257
|
2218 |
2117
|
2219 return retval; |
|
2220 } |
|
2221 |
|
2222 // Ugh again and again. |
|
2223 |
2572
|
2224 template <class T> |
3620
|
2225 int |
3523
|
2226 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
4468
|
2227 int sa_2, T arg, const std::string& who) |
2572
|
2228 { |
3620
|
2229 int retval = 0; |
|
2230 |
2572
|
2231 switch (nsa) |
|
2232 { |
|
2233 case 2: |
3640
|
2234 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572
|
2235 break; |
|
2236 |
|
2237 case 1: |
3640
|
2238 retval = octave_format (os, fmt, sa_1, arg); |
2572
|
2239 break; |
|
2240 |
|
2241 case 0: |
3640
|
2242 retval = octave_format (os, fmt, arg); |
2572
|
2243 break; |
|
2244 |
|
2245 default: |
4468
|
2246 ::error ("%s: internal error handling format", who.c_str ()); |
2572
|
2247 break; |
|
2248 } |
3620
|
2249 |
|
2250 return retval; |
2572
|
2251 } |
|
2252 |
3620
|
2253 template int |
4468
|
2254 do_printf_conv (std::ostream&, const char*, int, int, int, int, |
|
2255 const std::string&); |
|
2256 |
|
2257 template int |
|
2258 do_printf_conv (std::ostream&, const char*, int, int, int, long, |
|
2259 const std::string&); |
|
2260 |
3878
|
2261 template int |
4468
|
2262 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, |
|
2263 const std::string&); |
|
2264 |
|
2265 template int |
|
2266 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, |
|
2267 const std::string&); |
|
2268 |
|
2269 template int |
|
2270 do_printf_conv (std::ostream&, const char*, int, int, int, double, |
|
2271 const std::string&); |
|
2272 |
|
2273 template int |
|
2274 do_printf_conv (std::ostream&, const char*, int, int, int, const char*, |
|
2275 const std::string&); |
2117
|
2276 |
|
2277 int |
|
2278 octave_base_stream::do_printf (printf_format_list& fmt_list, |
4468
|
2279 const octave_value_list& args, |
|
2280 const std::string& who) |
2117
|
2281 { |
3620
|
2282 int retval = 0; |
2117
|
2283 |
3640
|
2284 int nconv = fmt_list.num_conversions (); |
|
2285 |
3523
|
2286 std::ostream *osp = output_stream (); |
2117
|
2287 |
|
2288 if (osp) |
|
2289 { |
3523
|
2290 std::ostream& os = *osp; |
2117
|
2291 |
|
2292 const printf_format_elt *elt = fmt_list.first (); |
|
2293 |
|
2294 printf_value_cache val_cache (args); |
|
2295 |
|
2296 for (;;) |
|
2297 { |
4153
|
2298 OCTAVE_QUIT; |
|
2299 |
2117
|
2300 if (elt) |
|
2301 { |
3640
|
2302 // NSA is the number of `star' args to convert. |
|
2303 |
|
2304 int nsa = (elt->fw < 0) + (elt->prec < 0); |
2117
|
2305 |
|
2306 int sa_1 = 0; |
|
2307 int sa_2 = 0; |
|
2308 |
|
2309 if (nsa > 0) |
|
2310 { |
|
2311 sa_1 = val_cache.int_value (); |
|
2312 |
|
2313 if (! val_cache) |
|
2314 break; |
|
2315 else |
|
2316 { |
|
2317 if (nsa > 1) |
|
2318 { |
|
2319 sa_2 = val_cache.int_value (); |
|
2320 |
|
2321 if (! val_cache) |
|
2322 break; |
|
2323 } |
|
2324 } |
|
2325 } |
|
2326 |
|
2327 const char *fmt = elt->text; |
|
2328 |
3640
|
2329 if (elt->type == '%') |
|
2330 { |
|
2331 os << "%"; |
|
2332 retval++; |
|
2333 } |
|
2334 else if (elt->args == 0 && elt->text) |
|
2335 { |
|
2336 os << elt->text; |
|
2337 retval += strlen (elt->text); |
|
2338 } |
4257
|
2339 else if (elt->type == 's') |
3640
|
2340 { |
|
2341 std::string val = val_cache.string_value (); |
|
2342 |
|
2343 if (val_cache) |
|
2344 retval += do_printf_conv (os, fmt, nsa, sa_1, |
4468
|
2345 sa_2, val.c_str (), who); |
3640
|
2346 else |
|
2347 break; |
|
2348 } |
2117
|
2349 else |
|
2350 { |
3640
|
2351 double val = val_cache.double_value (); |
|
2352 |
|
2353 if (val_cache) |
2117
|
2354 { |
4693
|
2355 if (lo_ieee_is_NaN_or_NA (val) || xisinf (val) |
|
2356 || ((val > INT_MAX || val < INT_MIN) |
|
2357 && (elt->type == 'd' |
|
2358 || elt->type == 'i' |
|
2359 || elt->type == 'c' |
|
2360 || elt->type == 'o' |
|
2361 || elt->type == 'x' |
|
2362 || elt->type == 'X' |
|
2363 || elt->type == 'u'))) |
4303
|
2364 { |
|
2365 std::string tfmt = fmt; |
|
2366 |
4693
|
2367 if (lo_ieee_is_NaN_or_NA (val) || xisinf (val)) |
4305
|
2368 { |
|
2369 tfmt.replace (tfmt.rfind (elt->type), 1, 1, 's'); |
|
2370 |
|
2371 const char *tval = xisinf (val) |
4693
|
2372 ? (val < 0 ? "-Inf" : "Inf") |
|
2373 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); |
4305
|
2374 |
|
2375 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2376 nsa, sa_1, sa_2, |
|
2377 tval, who); |
4305
|
2378 } |
|
2379 else |
|
2380 { |
|
2381 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); |
|
2382 |
|
2383 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2384 nsa, sa_1, sa_2, |
|
2385 val, who); |
4305
|
2386 } |
4303
|
2387 } |
|
2388 else |
3640
|
2389 { |
4303
|
2390 switch (elt->type) |
|
2391 { |
|
2392 case 'd': case 'i': case 'c': |
|
2393 { |
|
2394 if (elt->modifier == 'l') |
|
2395 retval += do_printf_conv |
|
2396 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2397 static_cast<long int> (val), who); |
4303
|
2398 else |
|
2399 retval += do_printf_conv |
|
2400 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2401 static_cast<int> (val), who); |
4303
|
2402 } |
|
2403 break; |
|
2404 |
|
2405 case 'o': case 'x': case 'X': case 'u': |
|
2406 { |
|
2407 if (elt->modifier == 'l') |
|
2408 retval += do_printf_conv |
|
2409 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2410 static_cast<unsigned long int> (val), |
|
2411 who); |
4303
|
2412 else |
|
2413 retval += do_printf_conv |
|
2414 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2415 static_cast<unsigned int> (val), who); |
4303
|
2416 } |
|
2417 break; |
|
2418 |
|
2419 case 'f': case 'e': case 'E': |
|
2420 case 'g': case 'G': |
|
2421 retval |
|
2422 += do_printf_conv (os, fmt, nsa, sa_1, sa_2, |
4468
|
2423 val, who); |
4303
|
2424 break; |
|
2425 |
|
2426 default: |
4468
|
2427 error ("%s: invalid format specifier", |
|
2428 who.c_str ()); |
4303
|
2429 return -1; |
|
2430 break; |
|
2431 } |
3640
|
2432 } |
2117
|
2433 } |
|
2434 else |
3620
|
2435 break; |
2117
|
2436 } |
|
2437 |
3620
|
2438 if (! os) |
2117
|
2439 { |
4468
|
2440 error ("%s: write error", who.c_str ()); |
2117
|
2441 break; |
|
2442 } |
|
2443 } |
|
2444 else |
|
2445 { |
4468
|
2446 ::error ("%s: internal error handling format", who.c_str ()); |
2117
|
2447 retval = -1; |
|
2448 break; |
|
2449 } |
|
2450 |
3640
|
2451 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
|
2452 |
|
2453 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
|
2454 break; |
|
2455 } |
2117
|
2456 } |
3640
|
2457 else |
4468
|
2458 invalid_operation (who, "writing"); |
2117
|
2459 |
|
2460 return retval; |
|
2461 } |
|
2462 |
|
2463 int |
3640
|
2464 octave_base_stream::printf (const std::string& fmt, |
4468
|
2465 const octave_value_list& args, |
|
2466 const std::string& who) |
2117
|
2467 { |
3640
|
2468 int retval = 0; |
|
2469 |
|
2470 printf_format_list fmt_list (fmt); |
|
2471 |
|
2472 if (fmt_list.num_conversions () == -1) |
4468
|
2473 ::error ("%s: invalid format specified", who.c_str ()); |
2117
|
2474 else |
4468
|
2475 retval = do_printf (fmt_list, args, who); |
2117
|
2476 |
|
2477 return retval; |
|
2478 } |
|
2479 |
|
2480 int |
4468
|
2481 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117
|
2482 { |
|
2483 int retval = -1; |
|
2484 |
3523
|
2485 std::ostream *osp = output_stream (); |
2117
|
2486 |
|
2487 if (osp) |
|
2488 { |
3523
|
2489 std::ostream& os = *osp; |
2117
|
2490 |
|
2491 os << s; |
|
2492 |
|
2493 if (os) |
|
2494 { |
|
2495 // XXX FIXME XXX -- why does this seem to be necessary? |
|
2496 // Without it, output from a loop like |
|
2497 // |
|
2498 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
|
2499 // |
|
2500 // doesn't seem to go to the pager immediately. |
|
2501 |
|
2502 os.flush (); |
|
2503 |
|
2504 if (os) |
|
2505 retval = 0; |
|
2506 else |
4468
|
2507 error ("%s: write error", who.c_str ()); |
2117
|
2508 } |
|
2509 else |
4468
|
2510 error ("%s: write error", who.c_str ()); |
2117
|
2511 } |
|
2512 else |
4468
|
2513 invalid_operation (who, "writing"); |
2117
|
2514 |
|
2515 return retval; |
|
2516 } |
|
2517 |
|
2518 int |
|
2519 octave_base_stream::rewind (void) |
|
2520 { |
3538
|
2521 return seek (0, std::ios::beg); |
2117
|
2522 } |
|
2523 |
|
2524 // Return current error message for this stream. |
|
2525 |
3536
|
2526 std::string |
2435
|
2527 octave_base_stream::error (bool clear_err, int& err_num) |
2117
|
2528 { |
2435
|
2529 err_num = fail ? -1 : 0; |
2117
|
2530 |
3523
|
2531 std::string tmp = errmsg; |
2117
|
2532 |
|
2533 if (clear_err) |
|
2534 clear (); |
|
2535 |
|
2536 return tmp; |
|
2537 } |
|
2538 |
|
2539 void |
4468
|
2540 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117
|
2541 { |
4468
|
2542 // Note that this is not ::error () ! |
|
2543 |
|
2544 error (who + ": stream not open for " + rw); |
2117
|
2545 } |
|
2546 |
3552
|
2547 octave_stream::octave_stream (octave_base_stream *bs) |
3340
|
2548 : rep (bs) |
|
2549 { |
|
2550 if (rep) |
|
2551 rep->count = 1; |
|
2552 } |
|
2553 |
|
2554 octave_stream::~octave_stream (void) |
|
2555 { |
|
2556 if (rep && --rep->count == 0) |
|
2557 delete rep; |
|
2558 } |
|
2559 |
|
2560 octave_stream::octave_stream (const octave_stream& s) |
|
2561 : rep (s.rep) |
|
2562 { |
|
2563 if (rep) |
|
2564 rep->count++; |
|
2565 } |
|
2566 |
|
2567 octave_stream& |
|
2568 octave_stream::operator = (const octave_stream& s) |
|
2569 { |
|
2570 if (rep != s.rep) |
|
2571 { |
|
2572 if (rep && --rep->count == 0) |
|
2573 delete rep; |
|
2574 |
|
2575 rep = s.rep; |
|
2576 |
|
2577 if (rep) |
|
2578 rep->count++; |
|
2579 } |
|
2580 |
|
2581 return *this; |
|
2582 } |
|
2583 |
2117
|
2584 int |
|
2585 octave_stream::flush (void) |
|
2586 { |
|
2587 int retval = -1; |
|
2588 |
|
2589 if (stream_ok ("fflush")) |
|
2590 retval = rep->flush (); |
|
2591 |
|
2592 return retval; |
|
2593 } |
|
2594 |
3536
|
2595 std::string |
4468
|
2596 octave_stream::getl (int max_len, bool& err, const std::string& who) |
2117
|
2597 { |
3523
|
2598 std::string retval; |
2117
|
2599 |
4468
|
2600 if (stream_ok (who)) |
|
2601 retval = rep->getl (max_len, err, who); |
2117
|
2602 |
|
2603 return retval; |
|
2604 } |
|
2605 |
3536
|
2606 std::string |
4468
|
2607 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
|
2608 const std::string& who) |
2117
|
2609 { |
3523
|
2610 std::string retval; |
2117
|
2611 |
|
2612 err = false; |
|
2613 |
|
2614 int conv_err = 0; |
|
2615 |
|
2616 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2617 |
|
2618 if (conv_err || max_len < 0) |
|
2619 { |
|
2620 err = true; |
4468
|
2621 ::error ("%s: invalid maximum length specified", who.c_str ()); |
2117
|
2622 } |
|
2623 else |
4468
|
2624 retval = getl (max_len, err, who); |
2117
|
2625 |
|
2626 return retval; |
|
2627 } |
|
2628 |
3536
|
2629 std::string |
4468
|
2630 octave_stream::gets (int max_len, bool& err, const std::string& who) |
2117
|
2631 { |
3523
|
2632 std::string retval; |
2117
|
2633 |
4468
|
2634 if (stream_ok (who)) |
|
2635 retval = rep->gets (max_len, err, who); |
2117
|
2636 |
|
2637 return retval; |
|
2638 } |
|
2639 |
3536
|
2640 std::string |
4468
|
2641 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
|
2642 const std::string& who) |
2117
|
2643 { |
3523
|
2644 std::string retval; |
2117
|
2645 |
|
2646 err = false; |
|
2647 |
|
2648 int conv_err = 0; |
|
2649 |
|
2650 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2651 |
|
2652 if (conv_err || max_len < 0) |
|
2653 { |
|
2654 err = true; |
4468
|
2655 ::error ("%s: invalid maximum length specified", who.c_str ()); |
2117
|
2656 } |
|
2657 else |
4468
|
2658 retval = gets (max_len, err, who); |
2117
|
2659 |
|
2660 return retval; |
|
2661 } |
|
2662 |
|
2663 int |
4797
|
2664 octave_stream::seek (long offset, int origin) |
2117
|
2665 { |
5065
|
2666 int status = -1; |
2117
|
2667 |
|
2668 if (stream_ok ("fseek")) |
4889
|
2669 { |
|
2670 clearerr (); |
|
2671 |
5065
|
2672 long orig_pos = rep->tell (); |
|
2673 |
|
2674 status = rep->seek (offset, origin); |
|
2675 |
|
2676 if (status == 0) |
|
2677 { |
|
2678 long save_pos = rep->tell (); |
|
2679 |
|
2680 rep->seek (0, SEEK_END); |
|
2681 |
|
2682 long pos_eof = rep->tell (); |
|
2683 |
|
2684 // I don't think save_pos can be less than zero, but we'll |
|
2685 // check anyway... |
|
2686 |
|
2687 if (save_pos > pos_eof || save_pos < 0) |
|
2688 { |
|
2689 // Seek outside bounds of file. Failure should leave |
|
2690 // position unchanged. |
|
2691 |
|
2692 rep->seek (orig_pos, SEEK_SET); |
|
2693 |
|
2694 status = -1; |
|
2695 } |
|
2696 else |
|
2697 { |
|
2698 // Is it possible for this to fail? We are just |
|
2699 // returning to a position after the first successful |
|
2700 // seek. |
|
2701 |
|
2702 rep->seek (save_pos, SEEK_SET); |
|
2703 } |
|
2704 } |
4889
|
2705 } |
2117
|
2706 |
5065
|
2707 return status; |
2117
|
2708 } |
|
2709 |
|
2710 int |
|
2711 octave_stream::seek (const octave_value& tc_offset, |
|
2712 const octave_value& tc_origin) |
|
2713 { |
|
2714 int retval = -1; |
|
2715 |
4797
|
2716 long xoffset = tc_offset.long_value (true); |
4645
|
2717 |
|
2718 if (! error_state) |
2117
|
2719 { |
4645
|
2720 int conv_err = 0; |
|
2721 |
4797
|
2722 int origin = SEEK_SET; |
2117
|
2723 |
2341
|
2724 if (tc_origin.is_string ()) |
2117
|
2725 { |
3523
|
2726 std::string xorigin = tc_origin.string_value (); |
2341
|
2727 |
|
2728 if (xorigin == "bof") |
4797
|
2729 origin = SEEK_SET; |
2341
|
2730 else if (xorigin == "cof") |
4797
|
2731 origin = SEEK_CUR; |
2341
|
2732 else if (xorigin == "eof") |
4797
|
2733 origin = SEEK_END; |
2117
|
2734 else |
|
2735 conv_err = -1; |
|
2736 } |
2341
|
2737 else |
|
2738 { |
|
2739 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
|
2740 |
|
2741 if (! conv_err) |
|
2742 { |
|
2743 if (xorigin == -1) |
4797
|
2744 origin = SEEK_SET; |
2341
|
2745 else if (xorigin == 0) |
4797
|
2746 origin = SEEK_CUR; |
2341
|
2747 else if (xorigin == 1) |
4797
|
2748 origin = SEEK_END; |
2341
|
2749 else |
|
2750 conv_err = -1; |
|
2751 } |
|
2752 } |
2117
|
2753 |
|
2754 if (! conv_err) |
5065
|
2755 { |
|
2756 retval = seek (xoffset, origin); |
|
2757 |
|
2758 if (retval != 0) |
|
2759 error ("fseek: failed to seek to requested position"); |
|
2760 } |
2117
|
2761 else |
|
2762 error ("fseek: invalid value for origin"); |
|
2763 } |
|
2764 else |
|
2765 error ("fseek: invalid value for offset"); |
|
2766 |
|
2767 return retval; |
|
2768 } |
|
2769 |
4797
|
2770 long |
|
2771 octave_stream::tell (void) |
2117
|
2772 { |
4797
|
2773 long retval = -1; |
2117
|
2774 |
|
2775 if (stream_ok ("tell")) |
|
2776 retval = rep->tell (); |
|
2777 |
|
2778 return retval; |
|
2779 } |
|
2780 |
|
2781 int |
|
2782 octave_stream::rewind (void) |
|
2783 { |
|
2784 int retval = -1; |
|
2785 |
|
2786 if (stream_ok ("frewind")) |
|
2787 retval = rep->rewind (); |
|
2788 |
|
2789 return retval; |
|
2790 } |
|
2791 |
3340
|
2792 bool |
|
2793 octave_stream::is_open (void) const |
|
2794 { |
|
2795 bool retval = false; |
|
2796 |
|
2797 if (stream_ok ("is_open")) |
|
2798 retval = rep->is_open (); |
|
2799 |
|
2800 return retval; |
|
2801 } |
|
2802 |
|
2803 void |
|
2804 octave_stream::close (void) |
|
2805 { |
|
2806 if (stream_ok ("close")) |
|
2807 rep->close (); |
|
2808 } |
|
2809 |
4944
|
2810 template <class RET_T, class READ_T> |
2117
|
2811 octave_value |
4944
|
2812 do_read (octave_stream& strm, int nr, int nc, int block_size, |
|
2813 int skip, bool do_float_fmt_conv, |
|
2814 oct_mach_info::float_format from_flt_fmt, int& count) |
2117
|
2815 { |
|
2816 octave_value retval; |
|
2817 |
4944
|
2818 RET_T nda; |
|
2819 |
|
2820 count = 0; |
|
2821 |
|
2822 typename octave_array_type_traits<RET_T>::element_type elt_zero |
|
2823 = typename octave_array_type_traits<RET_T>::element_type (); |
|
2824 |
|
2825 typename octave_array_type_traits<RET_T>::element_type *dat = 0; |
|
2826 |
|
2827 int max_size = 0; |
|
2828 |
|
2829 int final_nr = 0; |
5030
|
2830 int final_nc = 1; |
4944
|
2831 |
|
2832 if (nr > 0) |
|
2833 { |
|
2834 if (nc > 0) |
|
2835 { |
|
2836 nda.resize (dim_vector (nr, nc), elt_zero); |
|
2837 dat = nda.fortran_vec (); |
|
2838 max_size = nr * nc; |
|
2839 } |
|
2840 else |
|
2841 { |
|
2842 nda.resize (dim_vector (nr, 32), elt_zero); |
|
2843 dat = nda.fortran_vec (); |
|
2844 max_size = nr * 32; |
|
2845 } |
|
2846 } |
|
2847 else |
|
2848 { |
|
2849 nda.resize (dim_vector (32, 1), elt_zero); |
|
2850 dat = nda.fortran_vec (); |
|
2851 max_size = 32; |
|
2852 } |
|
2853 |
|
2854 // XXX FIXME XXX -- byte order for Cray? |
|
2855 |
|
2856 bool swap = false; |
|
2857 |
|
2858 if (oct_mach_info::words_big_endian ()) |
|
2859 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
2860 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
2861 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g); |
|
2862 else |
|
2863 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
|
2864 |
|
2865 union |
|
2866 { |
|
2867 char buf[sizeof (typename octave_type_traits<READ_T>::val_type)]; |
|
2868 typename octave_type_traits<READ_T>::val_type val; |
|
2869 } u; |
|
2870 |
|
2871 std::istream *isp = strm.input_stream (); |
|
2872 |
|
2873 if (isp) |
|
2874 { |
|
2875 std::istream& is = *isp; |
|
2876 |
|
2877 int elts_read = 0; |
|
2878 |
|
2879 for (;;) |
|
2880 { |
|
2881 // XXX FIXME XXX -- maybe there should be a special case for |
|
2882 // skip == 0. |
|
2883 |
|
2884 if (is) |
|
2885 { |
|
2886 if (nr > 0 && nc > 0 && count == max_size) |
|
2887 { |
|
2888 final_nr = nr; |
|
2889 final_nc = nc; |
|
2890 |
|
2891 break; |
|
2892 } |
|
2893 |
|
2894 is.read (u.buf, sizeof (typename octave_type_traits<READ_T>::val_type)); |
|
2895 |
|
2896 // We only swap bytes for integer types. For float |
|
2897 // types, the format conversion will also handle byte |
|
2898 // swapping. |
|
2899 |
|
2900 if (swap) |
|
2901 swap_bytes<sizeof (typename octave_type_traits<READ_T>::val_type)> (u.buf); |
|
2902 else if (do_float_fmt_conv) |
|
2903 do_float_format_conversion |
|
2904 (u.buf, |
|
2905 sizeof (typename octave_type_traits<READ_T>::val_type), |
|
2906 1, from_flt_fmt, oct_mach_info::float_format ()); |
|
2907 |
|
2908 typename octave_array_type_traits<RET_T>::element_type tmp |
|
2909 = static_cast <typename octave_array_type_traits<RET_T>::element_type> (u.val); |
|
2910 |
5030
|
2911 if (is) |
4944
|
2912 { |
5030
|
2913 if (count == max_size) |
4944
|
2914 { |
5030
|
2915 max_size *= 2; |
|
2916 |
|
2917 if (nr > 0) |
|
2918 nda.resize (dim_vector (nr, max_size / nr), |
|
2919 elt_zero); |
|
2920 else |
|
2921 nda.resize (dim_vector (max_size, 1), elt_zero); |
|
2922 |
|
2923 dat = nda.fortran_vec (); |
4944
|
2924 } |
|
2925 |
5030
|
2926 dat[count++] = tmp; |
|
2927 |
|
2928 elts_read++; |
|
2929 } |
|
2930 |
|
2931 if (skip != 0 && elts_read == block_size) |
|
2932 { |
|
2933 strm.seek (skip, SEEK_CUR); |
|
2934 elts_read = 0; |
|
2935 } |
|
2936 |
|
2937 if (is.eof ()) |
|
2938 { |
|
2939 if (nr > 0) |
4944
|
2940 { |
5030
|
2941 if (count > nr) |
4944
|
2942 { |
5030
|
2943 final_nr = nr; |
|
2944 final_nc = (count - 1) / nr + 1; |
4944
|
2945 } |
|
2946 else |
|
2947 { |
|
2948 final_nr = count; |
|
2949 final_nc = 1; |
|
2950 } |
|
2951 } |
5030
|
2952 else |
|
2953 { |
|
2954 final_nr = count; |
|
2955 final_nc = 1; |
|
2956 } |
|
2957 |
4944
|
2958 break; |
|
2959 } |
|
2960 } |
5030
|
2961 else if (is.eof ()) |
|
2962 break; |
4944
|
2963 } |
|
2964 } |
|
2965 |
5030
|
2966 nda.resize (dim_vector (final_nr, final_nc), elt_zero); |
|
2967 |
|
2968 retval = nda; |
4944
|
2969 |
|
2970 return retval; |
|
2971 } |
|
2972 |
|
2973 #define DO_READ_VAL_TEMPLATE(RET_T, READ_T) \ |
|
2974 template octave_value \ |
|
2975 do_read<RET_T, READ_T> (octave_stream&, int, int, int, int, bool, \ |
|
2976 oct_mach_info::float_format, int&) |
|
2977 |
|
2978 // XXX FIXME XXX -- should we only have float if it is a different |
|
2979 // size from double? |
|
2980 |
|
2981 #define INSTANTIATE_DO_READ(VAL_T) \ |
|
2982 DO_READ_VAL_TEMPLATE (VAL_T, octave_int8); \ |
|
2983 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint8); \ |
|
2984 DO_READ_VAL_TEMPLATE (VAL_T, octave_int16); \ |
|
2985 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint16); \ |
|
2986 DO_READ_VAL_TEMPLATE (VAL_T, octave_int32); \ |
|
2987 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint32); \ |
|
2988 DO_READ_VAL_TEMPLATE (VAL_T, octave_int64); \ |
|
2989 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint64); \ |
|
2990 DO_READ_VAL_TEMPLATE (VAL_T, float); \ |
|
2991 DO_READ_VAL_TEMPLATE (VAL_T, double); \ |
|
2992 DO_READ_VAL_TEMPLATE (VAL_T, char); \ |
|
2993 DO_READ_VAL_TEMPLATE (VAL_T, signed char); \ |
|
2994 DO_READ_VAL_TEMPLATE (VAL_T, unsigned char) |
|
2995 |
4970
|
2996 INSTANTIATE_DO_READ (int8NDArray); |
|
2997 INSTANTIATE_DO_READ (uint8NDArray); |
|
2998 INSTANTIATE_DO_READ (int16NDArray); |
|
2999 INSTANTIATE_DO_READ (uint16NDArray); |
|
3000 INSTANTIATE_DO_READ (int32NDArray); |
|
3001 INSTANTIATE_DO_READ (uint32NDArray); |
|
3002 INSTANTIATE_DO_READ (int64NDArray); |
|
3003 INSTANTIATE_DO_READ (uint64NDArray); |
|
3004 // INSTANTIATE_DO_READ (floatNDArray); |
|
3005 INSTANTIATE_DO_READ (NDArray); |
|
3006 INSTANTIATE_DO_READ (charNDArray); |
4944
|
3007 |
|
3008 typedef octave_value (*read_fptr) (octave_stream&, int, int, int, int, bool, |
|
3009 oct_mach_info::float_format ffmt, int&); |
|
3010 |
|
3011 INSTANTIATE_ARRAY (read_fptr); |
|
3012 template class Array2<read_fptr>; |
|
3013 |
|
3014 #define FILL_TABLE_ROW(R, VAL_T) \ |
|
3015 read_fptr_table(R,oct_data_conv::dt_int8) = do_read<VAL_T, octave_int8>; \ |
|
3016 read_fptr_table(R,oct_data_conv::dt_uint8) = do_read<VAL_T, octave_uint8>; \ |
|
3017 read_fptr_table(R,oct_data_conv::dt_int16) = do_read<VAL_T, octave_int16>; \ |
|
3018 read_fptr_table(R,oct_data_conv::dt_uint16) = do_read<VAL_T, octave_uint16>; \ |
|
3019 read_fptr_table(R,oct_data_conv::dt_int32) = do_read<VAL_T, octave_int32>; \ |
|
3020 read_fptr_table(R,oct_data_conv::dt_uint32) = do_read<VAL_T, octave_uint32>; \ |
|
3021 read_fptr_table(R,oct_data_conv::dt_int64) = do_read<VAL_T, octave_int64>; \ |
|
3022 read_fptr_table(R,oct_data_conv::dt_uint64) = do_read<VAL_T, octave_uint64>; \ |
|
3023 read_fptr_table(R,oct_data_conv::dt_single) = do_read<VAL_T, float>; \ |
|
3024 read_fptr_table(R,oct_data_conv::dt_double) = do_read<VAL_T, double>; \ |
|
3025 read_fptr_table(R,oct_data_conv::dt_char) = do_read<VAL_T, char>; \ |
|
3026 read_fptr_table(R,oct_data_conv::dt_schar) = do_read<VAL_T, signed char>; \ |
4970
|
3027 read_fptr_table(R,oct_data_conv::dt_uchar) = do_read<VAL_T, unsigned char>; \ |
|
3028 read_fptr_table(R,oct_data_conv::dt_logical) = do_read<VAL_T, unsigned char> |
4944
|
3029 |
|
3030 octave_value |
|
3031 octave_stream::read (const Array<double>& size, int block_size, |
|
3032 oct_data_conv::data_type input_type, |
|
3033 oct_data_conv::data_type output_type, |
|
3034 int skip, oct_mach_info::float_format ffmt, |
|
3035 int& char_count) |
|
3036 { |
|
3037 static bool initialized = false; |
|
3038 |
|
3039 // Table function pointers for return types x read types. |
|
3040 |
4970
|
3041 static Array2<read_fptr> read_fptr_table (oct_data_conv::dt_unknown, 14, 0); |
4944
|
3042 |
|
3043 if (! initialized) |
|
3044 { |
|
3045 FILL_TABLE_ROW (oct_data_conv::dt_int8, int8NDArray); |
|
3046 FILL_TABLE_ROW (oct_data_conv::dt_uint8, uint8NDArray); |
|
3047 FILL_TABLE_ROW (oct_data_conv::dt_int16, int16NDArray); |
|
3048 FILL_TABLE_ROW (oct_data_conv::dt_uint16, uint16NDArray); |
|
3049 FILL_TABLE_ROW (oct_data_conv::dt_int32, int32NDArray); |
|
3050 FILL_TABLE_ROW (oct_data_conv::dt_uint32, uint32NDArray); |
|
3051 FILL_TABLE_ROW (oct_data_conv::dt_int64, int64NDArray); |
|
3052 FILL_TABLE_ROW (oct_data_conv::dt_uint64, uint64NDArray); |
|
3053 FILL_TABLE_ROW (oct_data_conv::dt_double, NDArray); |
|
3054 FILL_TABLE_ROW (oct_data_conv::dt_char, charNDArray); |
|
3055 FILL_TABLE_ROW (oct_data_conv::dt_schar, charNDArray); |
|
3056 FILL_TABLE_ROW (oct_data_conv::dt_uchar, charNDArray); |
4970
|
3057 FILL_TABLE_ROW (oct_data_conv::dt_logical, boolNDArray); |
4944
|
3058 |
|
3059 initialized = true; |
|
3060 } |
|
3061 |
|
3062 octave_value retval; |
|
3063 |
2117
|
3064 if (stream_ok ("fread")) |
4944
|
3065 { |
|
3066 // XXX FIXME XXX -- we may eventually want to make this extensible. |
|
3067 |
|
3068 // XXX FIXME XXX -- we need a better way to ensure that this |
|
3069 // numbering stays consistent with the order of the elements in the |
|
3070 // data_type enum in the oct_data_conv class. |
|
3071 |
|
3072 char_count = 0; |
|
3073 |
|
3074 int nr = -1; |
|
3075 int nc = -1; |
|
3076 |
|
3077 bool ignore; |
|
3078 |
|
3079 get_size (size, nr, nc, ignore, "fread"); |
|
3080 |
|
3081 if (! error_state) |
|
3082 { |
|
3083 if (nr == 0 || nc == 0) |
|
3084 retval = Matrix (nr, nc); |
|
3085 else |
|
3086 { |
|
3087 if (ffmt == oct_mach_info::flt_fmt_unknown) |
|
3088 ffmt = float_format (); |
|
3089 |
|
3090 read_fptr fcn = read_fptr_table (output_type, input_type); |
|
3091 |
|
3092 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double |
|
3093 || input_type == oct_data_conv::dt_single) |
|
3094 && ffmt != float_format ()); |
|
3095 |
|
3096 if (fcn) |
|
3097 { |
|
3098 retval = (*fcn) (*this, nr, nc, block_size, skip, |
|
3099 do_float_fmt_conv, ffmt, char_count); |
|
3100 |
|
3101 // XXX FIXME XXX -- kluge! |
|
3102 |
|
3103 if (! error_state |
|
3104 && (output_type == oct_data_conv::dt_char |
|
3105 || output_type == oct_data_conv::dt_schar |
|
3106 || output_type == oct_data_conv::dt_uchar)) |
|
3107 retval = octave_value (retval.char_matrix_value (), true); |
|
3108 } |
|
3109 else |
|
3110 error ("fread: unable to read and convert requested types"); |
|
3111 } |
|
3112 } |
|
3113 else |
|
3114 invalid_operation ("fread", "reading"); |
|
3115 } |
2117
|
3116 |
|
3117 return retval; |
|
3118 } |
|
3119 |
|
3120 int |
4944
|
3121 octave_stream::write (const octave_value& data, int block_size, |
|
3122 oct_data_conv::data_type output_type, int skip, |
2317
|
3123 oct_mach_info::float_format flt_fmt) |
2117
|
3124 { |
|
3125 int retval = -1; |
|
3126 |
|
3127 if (stream_ok ("fwrite")) |
4944
|
3128 { |
|
3129 if (! error_state) |
|
3130 { |
|
3131 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
|
3132 flt_fmt = float_format (); |
|
3133 |
|
3134 int status = data.write (*this, block_size, output_type, |
|
3135 skip, flt_fmt); |
|
3136 |
|
3137 if (status < 0) |
|
3138 error ("fwrite: write error"); |
|
3139 else |
|
3140 retval = status; |
|
3141 } |
|
3142 else |
|
3143 invalid_operation ("fwrite", "writing"); |
|
3144 } |
2117
|
3145 |
|
3146 return retval; |
|
3147 } |
|
3148 |
4944
|
3149 template <class T> |
|
3150 void |
|
3151 write_int (std::ostream& os, bool swap, const T& val) |
|
3152 { |
|
3153 typename octave_type_traits<T>::val_type tmp = val.value (); |
|
3154 |
|
3155 if (swap) |
|
3156 swap_bytes<sizeof (typename octave_type_traits<T>::val_type)> (&tmp); |
|
3157 |
|
3158 os.write (reinterpret_cast<const char *> (&tmp), |
|
3159 sizeof (typename octave_type_traits<T>::val_type)); |
|
3160 } |
|
3161 |
|
3162 template void write_int (std::ostream&, bool, const octave_int8&); |
|
3163 template void write_int (std::ostream&, bool, const octave_uint8&); |
|
3164 template void write_int (std::ostream&, bool, const octave_int16&); |
|
3165 template void write_int (std::ostream&, bool, const octave_uint16&); |
|
3166 template void write_int (std::ostream&, bool, const octave_int32&); |
|
3167 template void write_int (std::ostream&, bool, const octave_uint32&); |
|
3168 template void write_int (std::ostream&, bool, const octave_int64&); |
|
3169 template void write_int (std::ostream&, bool, const octave_uint64&); |
|
3170 |
|
3171 template <class T> |
|
3172 static inline bool |
|
3173 do_write (std::ostream& os, const T& val, oct_data_conv::data_type output_type, |
|
3174 oct_mach_info::float_format flt_fmt, bool swap, |
|
3175 bool do_float_conversion) |
|
3176 { |
|
3177 bool retval = true; |
|
3178 |
|
3179 // For compatibility, Octave converts to the output type, then |
|
3180 // writes. This means that truncation happens on the conversion. |
|
3181 // For example, the following program prints 0: |
|
3182 // |
|
3183 // x = int8 (-1) |
|
3184 // f = fopen ("foo.dat", "w"); |
|
3185 // fwrite (f, x, "unsigned char"); |
|
3186 // fclose (f); |
|
3187 // f = fopen ("foo.dat", "r"); |
|
3188 // y = fread (f, 1, "unsigned char"); |
|
3189 // printf ("%d\n", y); |
|
3190 |
|
3191 switch (output_type) |
|
3192 { |
|
3193 case oct_data_conv::dt_char: |
|
3194 case oct_data_conv::dt_schar: |
|
3195 case oct_data_conv::dt_int8: |
|
3196 write_int (os, swap, octave_int8 (val)); |
|
3197 break; |
|
3198 |
|
3199 case oct_data_conv::dt_uchar: |
|
3200 case oct_data_conv::dt_uint8: |
|
3201 write_int (os, swap, octave_uint8 (val)); |
|
3202 break; |
|
3203 |
|
3204 case oct_data_conv::dt_int16: |
|
3205 write_int (os, swap, octave_int16 (val)); |
|
3206 break; |
|
3207 |
|
3208 case oct_data_conv::dt_uint16: |
|
3209 write_int (os, swap, octave_uint16 (val)); |
|
3210 break; |
|
3211 |
|
3212 case oct_data_conv::dt_int32: |
|
3213 write_int (os, swap, octave_int32 (val)); |
|
3214 break; |
|
3215 |
|
3216 case oct_data_conv::dt_uint32: |
|
3217 write_int (os, swap, octave_uint32 (val)); |
|
3218 break; |
|
3219 |
|
3220 case oct_data_conv::dt_int64: |
|
3221 write_int (os, swap, octave_int64 (val)); |
|
3222 break; |
|
3223 |
|
3224 case oct_data_conv::dt_uint64: |
|
3225 write_int (os, swap, octave_uint64 (val)); |
|
3226 break; |
|
3227 |
|
3228 case oct_data_conv::dt_single: |
|
3229 { |
|
3230 float f = static_cast<float> (val); |
|
3231 |
|
3232 if (do_float_conversion) |
|
3233 do_float_format_conversion (&f, 1, flt_fmt); |
|
3234 |
|
3235 os.write (reinterpret_cast<const char *> (&f), sizeof (float)); |
|
3236 } |
|
3237 break; |
|
3238 |
|
3239 case oct_data_conv::dt_double: |
|
3240 { |
|
3241 double d = static_cast<double> (val); |
|
3242 if (do_float_conversion) |
|
3243 do_double_format_conversion (&d, 1, flt_fmt); |
|
3244 |
|
3245 os.write (reinterpret_cast<const char *> (&d), sizeof (double)); |
|
3246 } |
|
3247 break; |
|
3248 |
|
3249 default: |
|
3250 retval = false; |
|
3251 (*current_liboctave_error_handler) |
|
3252 ("write: invalid type specification"); |
|
3253 break; |
|
3254 } |
|
3255 |
|
3256 return retval; |
|
3257 } |
|
3258 |
|
3259 template <class T> |
|
3260 int |
|
3261 octave_stream::write (const Array<T>& data, int block_size, |
|
3262 oct_data_conv::data_type output_type, |
|
3263 int skip, oct_mach_info::float_format flt_fmt) |
|
3264 { |
|
3265 int retval = -1; |
|
3266 |
|
3267 bool status = true; |
|
3268 |
|
3269 int count = 0; |
|
3270 |
|
3271 const T *d = data.data (); |
|
3272 |
|
3273 int n = data.length (); |
|
3274 |
|
3275 oct_mach_info::float_format native_flt_fmt |
|
3276 = oct_mach_info::float_format (); |
|
3277 |
|
3278 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
3279 |
|
3280 // XXX FIXME XXX -- byte order for Cray? |
|
3281 |
|
3282 bool swap = false; |
|
3283 |
|
3284 if (oct_mach_info::words_big_endian ()) |
|
3285 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
3286 || flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
3287 || flt_fmt == oct_mach_info::flt_fmt_vax_g); |
|
3288 else |
|
3289 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
|
3290 |
|
3291 for (int i = 0; i < n; i++) |
|
3292 { |
|
3293 std::ostream *osp = output_stream (); |
|
3294 |
|
3295 if (osp) |
|
3296 { |
|
3297 std::ostream& os = *osp; |
|
3298 |
|
3299 if (skip != 0 && (i % block_size) == 0) |
|
3300 seek (skip, SEEK_CUR); |
|
3301 |
|
3302 if (os) |
|
3303 { |
|
3304 status = do_write (os, d[i], output_type, flt_fmt, swap, |
|
3305 do_float_conversion); |
|
3306 |
|
3307 if (os && status) |
|
3308 count++; |
|
3309 else |
|
3310 break; |
|
3311 } |
|
3312 else |
|
3313 { |
|
3314 status = false; |
|
3315 break; |
|
3316 } |
|
3317 } |
|
3318 else |
|
3319 { |
|
3320 status = false; |
|
3321 break; |
|
3322 } |
|
3323 } |
|
3324 |
|
3325 if (status) |
|
3326 retval = count; |
|
3327 |
|
3328 return retval; |
|
3329 } |
|
3330 |
|
3331 template int |
|
3332 octave_stream::write (const Array<char>&, int, |
|
3333 oct_data_conv::data_type, |
|
3334 int, oct_mach_info::float_format); |
|
3335 |
|
3336 template int |
4970
|
3337 octave_stream::write (const Array<bool>&, int, |
|
3338 oct_data_conv::data_type, |
|
3339 int, oct_mach_info::float_format); |
|
3340 |
|
3341 template int |
4944
|
3342 octave_stream::write (const Array<double>&, int, |
|
3343 oct_data_conv::data_type, |
|
3344 int, oct_mach_info::float_format); |
|
3345 |
|
3346 template int |
|
3347 octave_stream::write (const Array<octave_int8>&, int, |
|
3348 oct_data_conv::data_type, |
|
3349 int, oct_mach_info::float_format); |
|
3350 |
|
3351 template int |
|
3352 octave_stream::write (const Array<octave_uint8>&, int, |
|
3353 oct_data_conv::data_type, |
|
3354 int, oct_mach_info::float_format); |
|
3355 |
|
3356 template int |
|
3357 octave_stream::write (const Array<octave_int16>&, int, |
|
3358 oct_data_conv::data_type, |
|
3359 int, oct_mach_info::float_format); |
|
3360 |
|
3361 template int |
|
3362 octave_stream::write (const Array<octave_uint16>&, int, |
|
3363 oct_data_conv::data_type, |
|
3364 int, oct_mach_info::float_format); |
|
3365 |
|
3366 template int |
|
3367 octave_stream::write (const Array<octave_int32>&, int, |
|
3368 oct_data_conv::data_type, |
|
3369 int, oct_mach_info::float_format); |
|
3370 |
|
3371 template int |
|
3372 octave_stream::write (const Array<octave_uint32>&, int, |
|
3373 oct_data_conv::data_type, |
|
3374 int, oct_mach_info::float_format); |
|
3375 |
|
3376 template int |
|
3377 octave_stream::write (const Array<octave_int64>&, int, |
|
3378 oct_data_conv::data_type, |
|
3379 int, oct_mach_info::float_format); |
|
3380 |
|
3381 template int |
|
3382 octave_stream::write (const Array<octave_uint64>&, int, |
|
3383 oct_data_conv::data_type, |
|
3384 int, oct_mach_info::float_format); |
|
3385 |
2117
|
3386 octave_value |
3810
|
3387 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
4468
|
3388 int& count, const std::string& who) |
2117
|
3389 { |
|
3390 octave_value retval; |
|
3391 |
4468
|
3392 if (stream_ok (who)) |
|
3393 retval = rep->scanf (fmt, size, count, who); |
2117
|
3394 |
|
3395 return retval; |
|
3396 } |
|
3397 |
2215
|
3398 octave_value_list |
4468
|
3399 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
3400 { |
|
3401 octave_value_list retval; |
|
3402 |
4468
|
3403 if (stream_ok (who)) |
|
3404 retval = rep->oscanf (fmt, who); |
2215
|
3405 |
|
3406 return retval; |
|
3407 } |
|
3408 |
2117
|
3409 int |
4468
|
3410 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
|
3411 const std::string& who) |
2117
|
3412 { |
|
3413 int retval = -1; |
|
3414 |
4468
|
3415 if (stream_ok (who)) |
|
3416 retval = rep->printf (fmt, args, who); |
2117
|
3417 |
|
3418 return retval; |
|
3419 } |
|
3420 |
|
3421 int |
4468
|
3422 octave_stream::puts (const std::string& s, const std::string& who) |
2117
|
3423 { |
|
3424 int retval = -1; |
|
3425 |
4468
|
3426 if (stream_ok (who)) |
|
3427 retval = rep->puts (s, who); |
2117
|
3428 |
|
3429 return retval; |
|
3430 } |
|
3431 |
|
3432 // XXX FIXME XXX -- maybe this should work for string arrays too. |
|
3433 |
|
3434 int |
4468
|
3435 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117
|
3436 { |
|
3437 int retval = -1; |
|
3438 |
|
3439 if (tc_s.is_string ()) |
|
3440 { |
3523
|
3441 std::string s = tc_s.string_value (); |
4468
|
3442 retval = rep->puts (s, who); |
2117
|
3443 } |
|
3444 else |
4468
|
3445 { |
|
3446 // Note that this is not ::error () ! |
|
3447 |
|
3448 error (who + ": argument must be a string"); |
|
3449 } |
2117
|
3450 |
|
3451 return retval; |
|
3452 } |
|
3453 |
|
3454 bool |
|
3455 octave_stream::eof (void) const |
|
3456 { |
|
3457 int retval = -1; |
|
3458 |
|
3459 if (stream_ok ("feof")) |
|
3460 retval = rep->eof (); |
|
3461 |
|
3462 return retval; |
|
3463 } |
|
3464 |
3536
|
3465 std::string |
2435
|
3466 octave_stream::error (bool clear, int& err_num) |
2117
|
3467 { |
3523
|
3468 std::string retval; |
2117
|
3469 |
|
3470 if (stream_ok ("ferror", false)) |
2435
|
3471 retval = rep->error (clear, err_num); |
2117
|
3472 |
|
3473 return retval; |
|
3474 } |
|
3475 |
3536
|
3476 std::string |
3340
|
3477 octave_stream::name (void) const |
2117
|
3478 { |
3523
|
3479 std::string retval; |
2117
|
3480 |
|
3481 if (stream_ok ("name")) |
|
3482 retval = rep->name (); |
|
3483 |
|
3484 return retval; |
|
3485 } |
|
3486 |
|
3487 int |
3340
|
3488 octave_stream::mode (void) const |
2117
|
3489 { |
|
3490 int retval = 0; |
|
3491 |
|
3492 if (stream_ok ("mode")) |
|
3493 retval = rep->mode (); |
|
3494 |
|
3495 return retval; |
|
3496 } |
|
3497 |
2317
|
3498 oct_mach_info::float_format |
3340
|
3499 octave_stream::float_format (void) const |
2117
|
3500 { |
4574
|
3501 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317
|
3502 |
|
3503 if (stream_ok ("float_format")) |
|
3504 retval = rep->float_format (); |
2117
|
3505 |
|
3506 return retval; |
|
3507 } |
|
3508 |
3536
|
3509 std::string |
2117
|
3510 octave_stream::mode_as_string (int mode) |
|
3511 { |
3523
|
3512 std::string retval = "???"; |
3775
|
3513 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
|
3514 |
|
3515 if (in_mode == std::ios::in) |
|
3516 retval = "r"; |
|
3517 else if (in_mode == std::ios::out |
4078
|
3518 || in_mode == (std::ios::out | std::ios::trunc)) |
3775
|
3519 retval = "w"; |
4078
|
3520 else if (in_mode == (std::ios::out | std::ios::app)) |
3775
|
3521 retval = "a"; |
4078
|
3522 else if (in_mode == (std::ios::in | std::ios::out)) |
3775
|
3523 retval = "r+"; |
4078
|
3524 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775
|
3525 retval = "w+"; |
4078
|
3526 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775
|
3527 retval = "a+"; |
4078
|
3528 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775
|
3529 retval = "rb"; |
4078
|
3530 else if (in_mode == (std::ios::out | std::ios::binary) |
|
3531 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775
|
3532 retval = "wb"; |
4078
|
3533 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775
|
3534 retval = "ab"; |
4078
|
3535 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775
|
3536 retval = "r+b"; |
4078
|
3537 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
|
3538 | std::ios::binary)) |
3775
|
3539 retval = "w+b"; |
4078
|
3540 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
|
3541 | std::ios::binary)) |
3775
|
3542 retval = "a+b"; |
2117
|
3543 |
|
3544 return retval; |
|
3545 } |
|
3546 |
|
3547 void |
4468
|
3548 octave_stream::invalid_stream_error (const std::string& who) const |
2117
|
3549 { |
4468
|
3550 ::error ("%s: attempt to use invalid I/O stream", who.c_str ()); |
2117
|
3551 } |
|
3552 |
|
3553 octave_stream_list *octave_stream_list::instance = 0; |
|
3554 |
2926
|
3555 bool |
|
3556 octave_stream_list::instance_ok (void) |
|
3557 { |
|
3558 bool retval = true; |
|
3559 |
|
3560 if (! instance) |
|
3561 instance = new octave_stream_list (); |
|
3562 |
|
3563 if (! instance) |
|
3564 { |
|
3565 ::error ("unable to create stream list object!"); |
|
3566 |
|
3567 retval = false; |
|
3568 } |
|
3569 |
|
3570 return retval; |
|
3571 } |
|
3572 |
|
3573 octave_value |
3340
|
3574 octave_stream_list::insert (const octave_stream& os) |
2926
|
3575 { |
3340
|
3576 return (instance_ok ()) ? instance->do_insert (os) : octave_value (-1.0); |
2926
|
3577 } |
|
3578 |
3340
|
3579 octave_stream |
3523
|
3580 octave_stream_list::lookup (int fid, const std::string& who) |
2926
|
3581 { |
3341
|
3582 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3583 } |
|
3584 |
3340
|
3585 octave_stream |
3523
|
3586 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926
|
3587 { |
3341
|
3588 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3589 } |
|
3590 |
|
3591 int |
3523
|
3592 octave_stream_list::remove (int fid, const std::string& who) |
2926
|
3593 { |
3341
|
3594 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3595 } |
|
3596 |
|
3597 int |
3523
|
3598 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926
|
3599 { |
3341
|
3600 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3601 } |
|
3602 |
|
3603 void |
|
3604 octave_stream_list::clear (void) |
|
3605 { |
|
3606 if (instance) |
|
3607 instance->do_clear (); |
|
3608 } |
|
3609 |
|
3610 string_vector |
|
3611 octave_stream_list::get_info (int fid) |
|
3612 { |
|
3613 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3614 } |
|
3615 |
|
3616 string_vector |
|
3617 octave_stream_list::get_info (const octave_value& fid) |
|
3618 { |
|
3619 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3620 } |
|
3621 |
3536
|
3622 std::string |
2926
|
3623 octave_stream_list::list_open_files (void) |
|
3624 { |
3523
|
3625 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926
|
3626 } |
|
3627 |
|
3628 octave_value |
|
3629 octave_stream_list::open_file_numbers (void) |
|
3630 { |
|
3631 return (instance_ok ()) |
|
3632 ? instance->do_open_file_numbers () : octave_value (); |
|
3633 } |
|
3634 |
|
3635 int |
|
3636 octave_stream_list::get_file_number (const octave_value& fid) |
|
3637 { |
|
3638 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; |
|
3639 } |
|
3640 |
2902
|
3641 octave_value |
3340
|
3642 octave_stream_list::do_insert (const octave_stream& os) |
2117
|
3643 { |
3340
|
3644 octave_value retval; |
|
3645 |
2902
|
3646 int stream_number = -1; |
|
3647 |
3340
|
3648 // Insert item in first open slot, increasing size of list if |
|
3649 // necessary. |
|
3650 |
|
3651 for (int i = 0; i < curr_len; i++) |
2117
|
3652 { |
3340
|
3653 octave_stream tmp = list(i); |
|
3654 |
|
3655 if (! tmp) |
2117
|
3656 { |
3340
|
3657 list(i) = os; |
|
3658 stream_number = i; |
|
3659 break; |
2117
|
3660 } |
|
3661 } |
3340
|
3662 |
|
3663 if (stream_number < 0) |
|
3664 { |
|
3665 int total_len = list.length (); |
|
3666 |
|
3667 if (curr_len == total_len) |
|
3668 list.resize (total_len * 2); |
|
3669 |
|
3670 list(curr_len) = os; |
|
3671 stream_number = curr_len; |
|
3672 curr_len++; |
|
3673 } |
2117
|
3674 |
2902
|
3675 return octave_value (os, stream_number); |
2117
|
3676 } |
|
3677 |
3341
|
3678 static void |
3523
|
3679 gripe_invalid_file_id (int fid, const std::string& who) |
3341
|
3680 { |
|
3681 if (who.empty ()) |
|
3682 ::error ("invalid stream number = %d", fid); |
|
3683 else |
|
3684 ::error ("%s: invalid stream number = %d", who.c_str (), fid); |
|
3685 } |
|
3686 |
3340
|
3687 octave_stream |
3523
|
3688 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117
|
3689 { |
3340
|
3690 octave_stream retval; |
2117
|
3691 |
|
3692 if (fid >= 0 && fid < curr_len) |
3340
|
3693 retval = list(fid); |
3341
|
3694 else |
|
3695 gripe_invalid_file_id (fid, who); |
2117
|
3696 |
|
3697 return retval; |
|
3698 } |
|
3699 |
3340
|
3700 octave_stream |
3341
|
3701 octave_stream_list::do_lookup (const octave_value& fid, |
3523
|
3702 const std::string& who) const |
2117
|
3703 { |
3340
|
3704 octave_stream retval; |
2117
|
3705 |
|
3706 int i = get_file_number (fid); |
|
3707 |
|
3708 if (! error_state) |
3341
|
3709 retval = do_lookup (i, who); |
2117
|
3710 |
|
3711 return retval; |
|
3712 } |
|
3713 |
|
3714 int |
3523
|
3715 octave_stream_list::do_remove (int fid, const std::string& who) |
2117
|
3716 { |
|
3717 int retval = -1; |
|
3718 |
3531
|
3719 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
|
3720 // (std::cerr). |
2117
|
3721 |
|
3722 if (fid > 2 && fid < curr_len) |
|
3723 { |
3340
|
3724 octave_stream os = list(fid); |
2117
|
3725 |
3341
|
3726 if (os.is_valid ()) |
2117
|
3727 { |
3340
|
3728 os.close (); |
|
3729 list(fid) = octave_stream (); |
2117
|
3730 retval = 0; |
|
3731 } |
3341
|
3732 else |
|
3733 gripe_invalid_file_id (fid, who); |
2117
|
3734 } |
3341
|
3735 else |
|
3736 gripe_invalid_file_id (fid, who); |
2117
|
3737 |
|
3738 return retval; |
|
3739 } |
|
3740 |
|
3741 int |
3523
|
3742 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117
|
3743 { |
|
3744 int retval = -1; |
|
3745 |
|
3746 int i = get_file_number (fid); |
|
3747 |
|
3748 if (! error_state) |
3341
|
3749 retval = do_remove (i, who); |
2117
|
3750 |
|
3751 return retval; |
|
3752 } |
|
3753 |
|
3754 void |
|
3755 octave_stream_list::do_clear (void) |
|
3756 { |
|
3757 // Do flush stdout and stderr. |
|
3758 |
3340
|
3759 list(0) . flush (); |
|
3760 list(1) . flush (); |
2117
|
3761 |
|
3762 // But don't delete them or stdin. |
|
3763 |
|
3764 for (int i = 3; i < curr_len; i++) |
3340
|
3765 list(i) = octave_stream (); |
2117
|
3766 } |
|
3767 |
|
3768 string_vector |
|
3769 octave_stream_list::do_get_info (int fid) const |
|
3770 { |
|
3771 string_vector retval; |
|
3772 |
3340
|
3773 octave_stream os = do_lookup (fid); |
2117
|
3774 |
3341
|
3775 if (os.is_valid ()) |
2117
|
3776 { |
|
3777 retval.resize (3); |
|
3778 |
3340
|
3779 retval(0) = os.name (); |
|
3780 retval(1) = octave_stream::mode_as_string (os.mode ()); |
|
3781 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
2117
|
3782 } |
|
3783 else |
3341
|
3784 ::error ("invalid file id = %d", fid); |
2117
|
3785 |
|
3786 return retval; |
|
3787 } |
|
3788 |
|
3789 string_vector |
|
3790 octave_stream_list::do_get_info (const octave_value& fid) const |
|
3791 { |
|
3792 string_vector retval; |
|
3793 |
|
3794 int conv_err = 0; |
|
3795 |
|
3796 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3797 |
|
3798 if (! conv_err) |
|
3799 retval = do_get_info (int_fid); |
|
3800 else |
2915
|
3801 ::error ("file id must be a file object or integer value"); |
2117
|
3802 |
|
3803 return retval; |
|
3804 } |
|
3805 |
3536
|
3806 std::string |
2117
|
3807 octave_stream_list::do_list_open_files (void) const |
|
3808 { |
3523
|
3809 std::string retval; |
2117
|
3810 |
4051
|
3811 OSSTREAM buf; |
2117
|
3812 |
|
3813 buf << "\n" |
|
3814 << " number mode arch name\n" |
|
3815 << " ------ ---- ---- ----\n"; |
|
3816 |
|
3817 for (int i = 0; i < curr_len; i++) |
|
3818 { |
3340
|
3819 octave_stream os = list(i); |
2117
|
3820 |
4326
|
3821 buf << " " |
|
3822 << std::setiosflags (std::ios::right) |
|
3823 << std::setw (4) << i << " " |
|
3824 << std::setiosflags (std::ios::left) |
|
3825 << std::setw (3) |
|
3826 << octave_stream::mode_as_string (os.mode ()) |
|
3827 << " " |
|
3828 << std::setw (9) |
|
3829 << oct_mach_info::float_format_as_string (os.float_format ()) |
|
3830 << " " |
|
3831 << os.name () << "\n"; |
2117
|
3832 } |
|
3833 |
4051
|
3834 buf << "\n" << OSSTREAM_ENDS; |
|
3835 |
|
3836 retval = OSSTREAM_STR (buf); |
|
3837 |
|
3838 OSSTREAM_FREEZE (buf); |
2117
|
3839 |
|
3840 return retval; |
|
3841 } |
|
3842 |
|
3843 octave_value |
|
3844 octave_stream_list::do_open_file_numbers (void) const |
|
3845 { |
|
3846 Matrix retval (1, curr_len, 0.0); |
|
3847 |
|
3848 int num_open = 0; |
|
3849 |
|
3850 // Skip stdin, stdout, and stderr. |
|
3851 |
|
3852 for (int i = 3; i < curr_len; i++) |
|
3853 { |
3340
|
3854 if (list(i)) |
2117
|
3855 retval (0, num_open++) = i; |
|
3856 } |
|
3857 |
|
3858 retval.resize ((num_open > 0), num_open); |
|
3859 |
|
3860 return retval; |
|
3861 } |
|
3862 |
|
3863 int |
2609
|
3864 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117
|
3865 { |
|
3866 int retval = -1; |
|
3867 |
|
3868 if (fid.is_string ()) |
|
3869 { |
3523
|
3870 std::string nm = fid.string_value (); |
2117
|
3871 |
3531
|
3872 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
|
3873 // are unnamed. |
2117
|
3874 |
|
3875 for (int i = 3; i < curr_len; i++) |
|
3876 { |
3340
|
3877 octave_stream os = list(i); |
|
3878 |
|
3879 if (os && os.name () == nm) |
2117
|
3880 { |
|
3881 retval = i; |
|
3882 break; |
|
3883 } |
|
3884 } |
|
3885 } |
|
3886 else |
|
3887 { |
|
3888 int conv_err = 0; |
|
3889 |
|
3890 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3891 |
|
3892 if (conv_err) |
3523
|
3893 ::error ("file id must be a file object, std::string, or integer value"); |
2117
|
3894 else |
|
3895 retval = int_fid; |
|
3896 } |
|
3897 |
|
3898 return retval; |
|
3899 } |
|
3900 |
|
3901 /* |
|
3902 ;;; Local Variables: *** |
|
3903 ;;; mode: C++ *** |
|
3904 ;;; End: *** |
|
3905 */ |