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