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 { \ |
7199
|
2494 if (val > std::numeric_limits<TQUAL long>::max () \ |
|
2495 || val < std::numeric_limits<TQUAL long>::min ()) \ |
6492
|
2496 { \ |
7199
|
2497 std::string tfmt = fmt; \ |
6492
|
2498 \ |
7199
|
2499 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); \ |
6492
|
2500 \ |
7199
|
2501 if (elt->modifier == 'l') \ |
|
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); \ |
6492
|
2506 } \ |
|
2507 else \ |
7199
|
2508 retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, \ |
|
2509 static_cast<TQUAL long> (val), who); \ |
6492
|
2510 } \ |
|
2511 while (0) |
|
2512 |
2117
|
2513 int |
|
2514 octave_base_stream::do_printf (printf_format_list& fmt_list, |
4468
|
2515 const octave_value_list& args, |
|
2516 const std::string& who) |
2117
|
2517 { |
3620
|
2518 int retval = 0; |
2117
|
2519 |
3640
|
2520 int nconv = fmt_list.num_conversions (); |
|
2521 |
3523
|
2522 std::ostream *osp = output_stream (); |
2117
|
2523 |
|
2524 if (osp) |
|
2525 { |
3523
|
2526 std::ostream& os = *osp; |
2117
|
2527 |
|
2528 const printf_format_elt *elt = fmt_list.first (); |
|
2529 |
|
2530 printf_value_cache val_cache (args); |
|
2531 |
|
2532 for (;;) |
|
2533 { |
4153
|
2534 OCTAVE_QUIT; |
|
2535 |
2117
|
2536 if (elt) |
|
2537 { |
3640
|
2538 // NSA is the number of `star' args to convert. |
|
2539 |
|
2540 int nsa = (elt->fw < 0) + (elt->prec < 0); |
2117
|
2541 |
|
2542 int sa_1 = 0; |
|
2543 int sa_2 = 0; |
|
2544 |
|
2545 if (nsa > 0) |
|
2546 { |
|
2547 sa_1 = val_cache.int_value (); |
|
2548 |
|
2549 if (! val_cache) |
|
2550 break; |
|
2551 else |
|
2552 { |
|
2553 if (nsa > 1) |
|
2554 { |
|
2555 sa_2 = val_cache.int_value (); |
|
2556 |
|
2557 if (! val_cache) |
|
2558 break; |
|
2559 } |
|
2560 } |
|
2561 } |
|
2562 |
|
2563 const char *fmt = elt->text; |
|
2564 |
3640
|
2565 if (elt->type == '%') |
|
2566 { |
|
2567 os << "%"; |
|
2568 retval++; |
|
2569 } |
|
2570 else if (elt->args == 0 && elt->text) |
|
2571 { |
|
2572 os << elt->text; |
|
2573 retval += strlen (elt->text); |
|
2574 } |
4257
|
2575 else if (elt->type == 's') |
3640
|
2576 { |
|
2577 std::string val = val_cache.string_value (); |
|
2578 |
|
2579 if (val_cache) |
|
2580 retval += do_printf_conv (os, fmt, nsa, sa_1, |
4468
|
2581 sa_2, val.c_str (), who); |
3640
|
2582 else |
|
2583 break; |
|
2584 } |
2117
|
2585 else |
|
2586 { |
3640
|
2587 double val = val_cache.double_value (); |
|
2588 |
|
2589 if (val_cache) |
2117
|
2590 { |
6492
|
2591 if (lo_ieee_isnan (val) || xisinf (val)) |
4303
|
2592 { |
|
2593 std::string tfmt = fmt; |
6865
|
2594 std::string::size_type i1, i2; |
|
2595 |
|
2596 tfmt.replace ((i1 = tfmt.rfind (elt->type)), |
|
2597 1, 1, 's'); |
|
2598 |
|
2599 if ((i2 = tfmt.rfind ('.')) != NPOS && i2 < i1) |
|
2600 { |
|
2601 tfmt.erase (i2, i1-i2); |
|
2602 if (elt->prec < 0) |
|
2603 nsa--; |
|
2604 } |
6492
|
2605 |
|
2606 const char *tval = xisinf (val) |
|
2607 ? (val < 0 ? "-Inf" : "Inf") |
|
2608 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); |
|
2609 |
|
2610 retval += do_printf_conv (os, tfmt.c_str (), |
|
2611 nsa, sa_1, sa_2, |
|
2612 tval, who); |
4303
|
2613 } |
|
2614 else |
3640
|
2615 { |
6492
|
2616 char type = elt->type; |
|
2617 |
|
2618 switch (type) |
4303
|
2619 { |
|
2620 case 'd': case 'i': case 'c': |
7199
|
2621 DO_DOUBLE_CONV (); |
4303
|
2622 break; |
|
2623 |
|
2624 case 'o': case 'x': case 'X': case 'u': |
6492
|
2625 DO_DOUBLE_CONV (unsigned); |
4303
|
2626 break; |
|
2627 |
|
2628 case 'f': case 'e': case 'E': |
|
2629 case 'g': case 'G': |
|
2630 retval |
|
2631 += do_printf_conv (os, fmt, nsa, sa_1, sa_2, |
4468
|
2632 val, who); |
4303
|
2633 break; |
|
2634 |
|
2635 default: |
4468
|
2636 error ("%s: invalid format specifier", |
|
2637 who.c_str ()); |
4303
|
2638 return -1; |
|
2639 break; |
|
2640 } |
3640
|
2641 } |
2117
|
2642 } |
|
2643 else |
3620
|
2644 break; |
2117
|
2645 } |
|
2646 |
3620
|
2647 if (! os) |
2117
|
2648 { |
4468
|
2649 error ("%s: write error", who.c_str ()); |
2117
|
2650 break; |
|
2651 } |
|
2652 } |
|
2653 else |
|
2654 { |
4468
|
2655 ::error ("%s: internal error handling format", who.c_str ()); |
2117
|
2656 retval = -1; |
|
2657 break; |
|
2658 } |
|
2659 |
3640
|
2660 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
|
2661 |
|
2662 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
|
2663 break; |
|
2664 } |
2117
|
2665 } |
3640
|
2666 else |
4468
|
2667 invalid_operation (who, "writing"); |
2117
|
2668 |
|
2669 return retval; |
|
2670 } |
|
2671 |
|
2672 int |
3640
|
2673 octave_base_stream::printf (const std::string& fmt, |
4468
|
2674 const octave_value_list& args, |
|
2675 const std::string& who) |
2117
|
2676 { |
3640
|
2677 int retval = 0; |
|
2678 |
|
2679 printf_format_list fmt_list (fmt); |
|
2680 |
|
2681 if (fmt_list.num_conversions () == -1) |
4468
|
2682 ::error ("%s: invalid format specified", who.c_str ()); |
2117
|
2683 else |
4468
|
2684 retval = do_printf (fmt_list, args, who); |
2117
|
2685 |
|
2686 return retval; |
|
2687 } |
|
2688 |
|
2689 int |
4468
|
2690 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117
|
2691 { |
|
2692 int retval = -1; |
|
2693 |
3523
|
2694 std::ostream *osp = output_stream (); |
2117
|
2695 |
|
2696 if (osp) |
|
2697 { |
3523
|
2698 std::ostream& os = *osp; |
2117
|
2699 |
|
2700 os << s; |
|
2701 |
|
2702 if (os) |
|
2703 { |
5775
|
2704 // FIXME -- why does this seem to be necessary? |
2117
|
2705 // Without it, output from a loop like |
|
2706 // |
|
2707 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
|
2708 // |
|
2709 // doesn't seem to go to the pager immediately. |
|
2710 |
|
2711 os.flush (); |
|
2712 |
|
2713 if (os) |
|
2714 retval = 0; |
|
2715 else |
4468
|
2716 error ("%s: write error", who.c_str ()); |
2117
|
2717 } |
|
2718 else |
4468
|
2719 error ("%s: write error", who.c_str ()); |
2117
|
2720 } |
|
2721 else |
4468
|
2722 invalid_operation (who, "writing"); |
2117
|
2723 |
|
2724 return retval; |
|
2725 } |
|
2726 |
|
2727 // Return current error message for this stream. |
|
2728 |
3536
|
2729 std::string |
2435
|
2730 octave_base_stream::error (bool clear_err, int& err_num) |
2117
|
2731 { |
2435
|
2732 err_num = fail ? -1 : 0; |
2117
|
2733 |
3523
|
2734 std::string tmp = errmsg; |
2117
|
2735 |
|
2736 if (clear_err) |
|
2737 clear (); |
|
2738 |
|
2739 return tmp; |
|
2740 } |
|
2741 |
|
2742 void |
4468
|
2743 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117
|
2744 { |
4468
|
2745 // Note that this is not ::error () ! |
|
2746 |
6297
|
2747 error (who, std::string ("stream not open for ") + rw); |
2117
|
2748 } |
|
2749 |
3552
|
2750 octave_stream::octave_stream (octave_base_stream *bs) |
3340
|
2751 : rep (bs) |
|
2752 { |
|
2753 if (rep) |
|
2754 rep->count = 1; |
|
2755 } |
|
2756 |
|
2757 octave_stream::~octave_stream (void) |
|
2758 { |
|
2759 if (rep && --rep->count == 0) |
|
2760 delete rep; |
|
2761 } |
|
2762 |
|
2763 octave_stream::octave_stream (const octave_stream& s) |
|
2764 : rep (s.rep) |
|
2765 { |
|
2766 if (rep) |
|
2767 rep->count++; |
|
2768 } |
|
2769 |
|
2770 octave_stream& |
|
2771 octave_stream::operator = (const octave_stream& s) |
|
2772 { |
|
2773 if (rep != s.rep) |
|
2774 { |
|
2775 if (rep && --rep->count == 0) |
|
2776 delete rep; |
|
2777 |
|
2778 rep = s.rep; |
|
2779 |
|
2780 if (rep) |
|
2781 rep->count++; |
|
2782 } |
|
2783 |
|
2784 return *this; |
|
2785 } |
|
2786 |
2117
|
2787 int |
|
2788 octave_stream::flush (void) |
|
2789 { |
|
2790 int retval = -1; |
|
2791 |
5659
|
2792 if (stream_ok ()) |
2117
|
2793 retval = rep->flush (); |
|
2794 |
|
2795 return retval; |
|
2796 } |
|
2797 |
3536
|
2798 std::string |
5275
|
2799 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117
|
2800 { |
3523
|
2801 std::string retval; |
2117
|
2802 |
5659
|
2803 if (stream_ok ()) |
4468
|
2804 retval = rep->getl (max_len, err, who); |
2117
|
2805 |
|
2806 return retval; |
|
2807 } |
|
2808 |
3536
|
2809 std::string |
4468
|
2810 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
|
2811 const std::string& who) |
2117
|
2812 { |
3523
|
2813 std::string retval; |
2117
|
2814 |
|
2815 err = false; |
|
2816 |
|
2817 int conv_err = 0; |
|
2818 |
6345
|
2819 int max_len = -1; |
|
2820 |
|
2821 if (tc_max_len.is_defined ()) |
2117
|
2822 { |
6345
|
2823 max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2824 |
|
2825 if (conv_err || max_len < 0) |
|
2826 { |
|
2827 err = true; |
|
2828 ::error ("%s: invalid maximum length specified", who.c_str ()); |
|
2829 } |
2117
|
2830 } |
6345
|
2831 |
|
2832 if (! error_state) |
4468
|
2833 retval = getl (max_len, err, who); |
2117
|
2834 |
|
2835 return retval; |
|
2836 } |
|
2837 |
3536
|
2838 std::string |
5275
|
2839 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117
|
2840 { |
3523
|
2841 std::string retval; |
2117
|
2842 |
5659
|
2843 if (stream_ok ()) |
4468
|
2844 retval = rep->gets (max_len, err, who); |
2117
|
2845 |
|
2846 return retval; |
|
2847 } |
|
2848 |
3536
|
2849 std::string |
4468
|
2850 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
|
2851 const std::string& who) |
2117
|
2852 { |
3523
|
2853 std::string retval; |
2117
|
2854 |
|
2855 err = false; |
|
2856 |
|
2857 int conv_err = 0; |
|
2858 |
6345
|
2859 int max_len = -1; |
|
2860 |
|
2861 if (tc_max_len.is_defined ()) |
2117
|
2862 { |
6345
|
2863 max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2864 |
|
2865 if (conv_err || max_len < 0) |
|
2866 { |
|
2867 err = true; |
|
2868 ::error ("%s: invalid maximum length specified", who.c_str ()); |
|
2869 } |
2117
|
2870 } |
6345
|
2871 |
|
2872 if (! error_state) |
4468
|
2873 retval = gets (max_len, err, who); |
2117
|
2874 |
|
2875 return retval; |
|
2876 } |
|
2877 |
|
2878 int |
4797
|
2879 octave_stream::seek (long offset, int origin) |
2117
|
2880 { |
5065
|
2881 int status = -1; |
2117
|
2882 |
5659
|
2883 if (stream_ok ()) |
4889
|
2884 { |
|
2885 clearerr (); |
|
2886 |
5065
|
2887 long orig_pos = rep->tell (); |
|
2888 |
|
2889 status = rep->seek (offset, origin); |
|
2890 |
|
2891 if (status == 0) |
|
2892 { |
|
2893 long save_pos = rep->tell (); |
|
2894 |
|
2895 rep->seek (0, SEEK_END); |
|
2896 |
|
2897 long pos_eof = rep->tell (); |
|
2898 |
|
2899 // I don't think save_pos can be less than zero, but we'll |
|
2900 // check anyway... |
|
2901 |
|
2902 if (save_pos > pos_eof || save_pos < 0) |
|
2903 { |
|
2904 // Seek outside bounds of file. Failure should leave |
|
2905 // position unchanged. |
|
2906 |
|
2907 rep->seek (orig_pos, SEEK_SET); |
|
2908 |
|
2909 status = -1; |
|
2910 } |
|
2911 else |
|
2912 { |
|
2913 // Is it possible for this to fail? We are just |
|
2914 // returning to a position after the first successful |
|
2915 // seek. |
|
2916 |
|
2917 rep->seek (save_pos, SEEK_SET); |
|
2918 } |
|
2919 } |
4889
|
2920 } |
2117
|
2921 |
5065
|
2922 return status; |
2117
|
2923 } |
|
2924 |
|
2925 int |
|
2926 octave_stream::seek (const octave_value& tc_offset, |
|
2927 const octave_value& tc_origin) |
|
2928 { |
|
2929 int retval = -1; |
|
2930 |
4797
|
2931 long xoffset = tc_offset.long_value (true); |
4645
|
2932 |
|
2933 if (! error_state) |
2117
|
2934 { |
4645
|
2935 int conv_err = 0; |
|
2936 |
4797
|
2937 int origin = SEEK_SET; |
2117
|
2938 |
2341
|
2939 if (tc_origin.is_string ()) |
2117
|
2940 { |
3523
|
2941 std::string xorigin = tc_origin.string_value (); |
2341
|
2942 |
|
2943 if (xorigin == "bof") |
4797
|
2944 origin = SEEK_SET; |
2341
|
2945 else if (xorigin == "cof") |
4797
|
2946 origin = SEEK_CUR; |
2341
|
2947 else if (xorigin == "eof") |
4797
|
2948 origin = SEEK_END; |
2117
|
2949 else |
|
2950 conv_err = -1; |
|
2951 } |
2341
|
2952 else |
|
2953 { |
|
2954 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
|
2955 |
|
2956 if (! conv_err) |
|
2957 { |
|
2958 if (xorigin == -1) |
4797
|
2959 origin = SEEK_SET; |
2341
|
2960 else if (xorigin == 0) |
4797
|
2961 origin = SEEK_CUR; |
2341
|
2962 else if (xorigin == 1) |
4797
|
2963 origin = SEEK_END; |
2341
|
2964 else |
|
2965 conv_err = -1; |
|
2966 } |
|
2967 } |
2117
|
2968 |
|
2969 if (! conv_err) |
5065
|
2970 { |
|
2971 retval = seek (xoffset, origin); |
|
2972 |
|
2973 if (retval != 0) |
|
2974 error ("fseek: failed to seek to requested position"); |
|
2975 } |
2117
|
2976 else |
|
2977 error ("fseek: invalid value for origin"); |
|
2978 } |
|
2979 else |
|
2980 error ("fseek: invalid value for offset"); |
|
2981 |
|
2982 return retval; |
|
2983 } |
|
2984 |
4797
|
2985 long |
|
2986 octave_stream::tell (void) |
2117
|
2987 { |
4797
|
2988 long retval = -1; |
2117
|
2989 |
5659
|
2990 if (stream_ok ()) |
2117
|
2991 retval = rep->tell (); |
|
2992 |
|
2993 return retval; |
|
2994 } |
|
2995 |
|
2996 int |
|
2997 octave_stream::rewind (void) |
|
2998 { |
6296
|
2999 return seek (0, SEEK_SET); |
2117
|
3000 } |
|
3001 |
3340
|
3002 bool |
|
3003 octave_stream::is_open (void) const |
|
3004 { |
|
3005 bool retval = false; |
|
3006 |
5659
|
3007 if (stream_ok ()) |
3340
|
3008 retval = rep->is_open (); |
|
3009 |
|
3010 return retval; |
|
3011 } |
|
3012 |
|
3013 void |
|
3014 octave_stream::close (void) |
|
3015 { |
5659
|
3016 if (stream_ok ()) |
3340
|
3017 rep->close (); |
|
3018 } |
|
3019 |
4944
|
3020 template <class RET_T, class READ_T> |
2117
|
3021 octave_value |
5275
|
3022 do_read (octave_stream& strm, octave_idx_type nr, octave_idx_type nc, octave_idx_type block_size, |
|
3023 octave_idx_type skip, bool do_float_fmt_conv, |
|
3024 oct_mach_info::float_format from_flt_fmt, octave_idx_type& count) |
2117
|
3025 { |
|
3026 octave_value retval; |
|
3027 |
4944
|
3028 RET_T nda; |
|
3029 |
|
3030 count = 0; |
|
3031 |
|
3032 typename octave_array_type_traits<RET_T>::element_type elt_zero |
|
3033 = typename octave_array_type_traits<RET_T>::element_type (); |
|
3034 |
|
3035 typename octave_array_type_traits<RET_T>::element_type *dat = 0; |
|
3036 |
5275
|
3037 octave_idx_type max_size = 0; |
|
3038 |
|
3039 octave_idx_type final_nr = 0; |
|
3040 octave_idx_type final_nc = 1; |
4944
|
3041 |
|
3042 if (nr > 0) |
|
3043 { |
|
3044 if (nc > 0) |
|
3045 { |
|
3046 nda.resize (dim_vector (nr, nc), elt_zero); |
|
3047 dat = nda.fortran_vec (); |
|
3048 max_size = nr * nc; |
|
3049 } |
|
3050 else |
|
3051 { |
|
3052 nda.resize (dim_vector (nr, 32), elt_zero); |
|
3053 dat = nda.fortran_vec (); |
|
3054 max_size = nr * 32; |
|
3055 } |
|
3056 } |
|
3057 else |
|
3058 { |
|
3059 nda.resize (dim_vector (32, 1), elt_zero); |
|
3060 dat = nda.fortran_vec (); |
|
3061 max_size = 32; |
|
3062 } |
|
3063 |
5775
|
3064 // FIXME -- byte order for Cray? |
4944
|
3065 |
|
3066 bool swap = false; |
|
3067 |
|
3068 if (oct_mach_info::words_big_endian ()) |
|
3069 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
3070 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
3071 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g); |
|
3072 else |
|
3073 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
|
3074 |
|
3075 union |
|
3076 { |
|
3077 char buf[sizeof (typename octave_type_traits<READ_T>::val_type)]; |
|
3078 typename octave_type_traits<READ_T>::val_type val; |
|
3079 } u; |
|
3080 |
|
3081 std::istream *isp = strm.input_stream (); |
|
3082 |
|
3083 if (isp) |
|
3084 { |
|
3085 std::istream& is = *isp; |
|
3086 |
5275
|
3087 octave_idx_type elts_read = 0; |
4944
|
3088 |
|
3089 for (;;) |
|
3090 { |
5775
|
3091 // FIXME -- maybe there should be a special case for |
4944
|
3092 // skip == 0. |
|
3093 |
|
3094 if (is) |
|
3095 { |
|
3096 if (nr > 0 && nc > 0 && count == max_size) |
|
3097 { |
|
3098 final_nr = nr; |
|
3099 final_nc = nc; |
|
3100 |
|
3101 break; |
|
3102 } |
|
3103 |
|
3104 is.read (u.buf, sizeof (typename octave_type_traits<READ_T>::val_type)); |
|
3105 |
|
3106 // We only swap bytes for integer types. For float |
|
3107 // types, the format conversion will also handle byte |
|
3108 // swapping. |
|
3109 |
|
3110 if (swap) |
|
3111 swap_bytes<sizeof (typename octave_type_traits<READ_T>::val_type)> (u.buf); |
|
3112 else if (do_float_fmt_conv) |
|
3113 do_float_format_conversion |
|
3114 (u.buf, |
|
3115 sizeof (typename octave_type_traits<READ_T>::val_type), |
|
3116 1, from_flt_fmt, oct_mach_info::float_format ()); |
|
3117 |
|
3118 typename octave_array_type_traits<RET_T>::element_type tmp |
|
3119 = static_cast <typename octave_array_type_traits<RET_T>::element_type> (u.val); |
|
3120 |
5030
|
3121 if (is) |
4944
|
3122 { |
5030
|
3123 if (count == max_size) |
4944
|
3124 { |
5030
|
3125 max_size *= 2; |
|
3126 |
|
3127 if (nr > 0) |
|
3128 nda.resize (dim_vector (nr, max_size / nr), |
|
3129 elt_zero); |
|
3130 else |
|
3131 nda.resize (dim_vector (max_size, 1), elt_zero); |
|
3132 |
|
3133 dat = nda.fortran_vec (); |
4944
|
3134 } |
|
3135 |
5030
|
3136 dat[count++] = tmp; |
|
3137 |
|
3138 elts_read++; |
|
3139 } |
|
3140 |
|
3141 if (skip != 0 && elts_read == block_size) |
|
3142 { |
|
3143 strm.seek (skip, SEEK_CUR); |
|
3144 elts_read = 0; |
|
3145 } |
|
3146 |
|
3147 if (is.eof ()) |
|
3148 { |
|
3149 if (nr > 0) |
4944
|
3150 { |
5030
|
3151 if (count > nr) |
4944
|
3152 { |
5030
|
3153 final_nr = nr; |
|
3154 final_nc = (count - 1) / nr + 1; |
4944
|
3155 } |
|
3156 else |
|
3157 { |
|
3158 final_nr = count; |
|
3159 final_nc = 1; |
|
3160 } |
|
3161 } |
5030
|
3162 else |
|
3163 { |
|
3164 final_nr = count; |
|
3165 final_nc = 1; |
|
3166 } |
|
3167 |
4944
|
3168 break; |
|
3169 } |
|
3170 } |
5030
|
3171 else if (is.eof ()) |
|
3172 break; |
4944
|
3173 } |
|
3174 } |
|
3175 |
5030
|
3176 nda.resize (dim_vector (final_nr, final_nc), elt_zero); |
|
3177 |
|
3178 retval = nda; |
4944
|
3179 |
|
3180 return retval; |
|
3181 } |
|
3182 |
|
3183 #define DO_READ_VAL_TEMPLATE(RET_T, READ_T) \ |
|
3184 template octave_value \ |
5275
|
3185 do_read<RET_T, READ_T> (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, \ |
|
3186 oct_mach_info::float_format, octave_idx_type&) |
4944
|
3187 |
5775
|
3188 // FIXME -- should we only have float if it is a different |
4944
|
3189 // size from double? |
|
3190 |
|
3191 #define INSTANTIATE_DO_READ(VAL_T) \ |
|
3192 DO_READ_VAL_TEMPLATE (VAL_T, octave_int8); \ |
|
3193 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint8); \ |
|
3194 DO_READ_VAL_TEMPLATE (VAL_T, octave_int16); \ |
|
3195 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint16); \ |
|
3196 DO_READ_VAL_TEMPLATE (VAL_T, octave_int32); \ |
|
3197 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint32); \ |
|
3198 DO_READ_VAL_TEMPLATE (VAL_T, octave_int64); \ |
|
3199 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint64); \ |
|
3200 DO_READ_VAL_TEMPLATE (VAL_T, float); \ |
|
3201 DO_READ_VAL_TEMPLATE (VAL_T, double); \ |
|
3202 DO_READ_VAL_TEMPLATE (VAL_T, char); \ |
|
3203 DO_READ_VAL_TEMPLATE (VAL_T, signed char); \ |
|
3204 DO_READ_VAL_TEMPLATE (VAL_T, unsigned char) |
|
3205 |
4970
|
3206 INSTANTIATE_DO_READ (int8NDArray); |
|
3207 INSTANTIATE_DO_READ (uint8NDArray); |
|
3208 INSTANTIATE_DO_READ (int16NDArray); |
|
3209 INSTANTIATE_DO_READ (uint16NDArray); |
|
3210 INSTANTIATE_DO_READ (int32NDArray); |
|
3211 INSTANTIATE_DO_READ (uint32NDArray); |
|
3212 INSTANTIATE_DO_READ (int64NDArray); |
|
3213 INSTANTIATE_DO_READ (uint64NDArray); |
|
3214 // INSTANTIATE_DO_READ (floatNDArray); |
|
3215 INSTANTIATE_DO_READ (NDArray); |
|
3216 INSTANTIATE_DO_READ (charNDArray); |
5780
|
3217 INSTANTIATE_DO_READ (boolNDArray); |
4944
|
3218 |
5275
|
3219 typedef octave_value (*read_fptr) (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, |
|
3220 oct_mach_info::float_format ffmt, octave_idx_type&); |
4944
|
3221 |
6708
|
3222 INSTANTIATE_ARRAY (read_fptr,); |
4944
|
3223 template class Array2<read_fptr>; |
|
3224 |
|
3225 #define FILL_TABLE_ROW(R, VAL_T) \ |
|
3226 read_fptr_table(R,oct_data_conv::dt_int8) = do_read<VAL_T, octave_int8>; \ |
|
3227 read_fptr_table(R,oct_data_conv::dt_uint8) = do_read<VAL_T, octave_uint8>; \ |
|
3228 read_fptr_table(R,oct_data_conv::dt_int16) = do_read<VAL_T, octave_int16>; \ |
|
3229 read_fptr_table(R,oct_data_conv::dt_uint16) = do_read<VAL_T, octave_uint16>; \ |
|
3230 read_fptr_table(R,oct_data_conv::dt_int32) = do_read<VAL_T, octave_int32>; \ |
|
3231 read_fptr_table(R,oct_data_conv::dt_uint32) = do_read<VAL_T, octave_uint32>; \ |
|
3232 read_fptr_table(R,oct_data_conv::dt_int64) = do_read<VAL_T, octave_int64>; \ |
|
3233 read_fptr_table(R,oct_data_conv::dt_uint64) = do_read<VAL_T, octave_uint64>; \ |
|
3234 read_fptr_table(R,oct_data_conv::dt_single) = do_read<VAL_T, float>; \ |
|
3235 read_fptr_table(R,oct_data_conv::dt_double) = do_read<VAL_T, double>; \ |
|
3236 read_fptr_table(R,oct_data_conv::dt_char) = do_read<VAL_T, char>; \ |
|
3237 read_fptr_table(R,oct_data_conv::dt_schar) = do_read<VAL_T, signed char>; \ |
4970
|
3238 read_fptr_table(R,oct_data_conv::dt_uchar) = do_read<VAL_T, unsigned char>; \ |
|
3239 read_fptr_table(R,oct_data_conv::dt_logical) = do_read<VAL_T, unsigned char> |
4944
|
3240 |
|
3241 octave_value |
5275
|
3242 octave_stream::read (const Array<double>& size, octave_idx_type block_size, |
4944
|
3243 oct_data_conv::data_type input_type, |
|
3244 oct_data_conv::data_type output_type, |
5275
|
3245 octave_idx_type skip, oct_mach_info::float_format ffmt, |
|
3246 octave_idx_type& char_count) |
4944
|
3247 { |
|
3248 static bool initialized = false; |
|
3249 |
|
3250 // Table function pointers for return types x read types. |
|
3251 |
4970
|
3252 static Array2<read_fptr> read_fptr_table (oct_data_conv::dt_unknown, 14, 0); |
4944
|
3253 |
|
3254 if (! initialized) |
|
3255 { |
|
3256 FILL_TABLE_ROW (oct_data_conv::dt_int8, int8NDArray); |
|
3257 FILL_TABLE_ROW (oct_data_conv::dt_uint8, uint8NDArray); |
|
3258 FILL_TABLE_ROW (oct_data_conv::dt_int16, int16NDArray); |
|
3259 FILL_TABLE_ROW (oct_data_conv::dt_uint16, uint16NDArray); |
|
3260 FILL_TABLE_ROW (oct_data_conv::dt_int32, int32NDArray); |
|
3261 FILL_TABLE_ROW (oct_data_conv::dt_uint32, uint32NDArray); |
|
3262 FILL_TABLE_ROW (oct_data_conv::dt_int64, int64NDArray); |
|
3263 FILL_TABLE_ROW (oct_data_conv::dt_uint64, uint64NDArray); |
|
3264 FILL_TABLE_ROW (oct_data_conv::dt_double, NDArray); |
|
3265 FILL_TABLE_ROW (oct_data_conv::dt_char, charNDArray); |
|
3266 FILL_TABLE_ROW (oct_data_conv::dt_schar, charNDArray); |
|
3267 FILL_TABLE_ROW (oct_data_conv::dt_uchar, charNDArray); |
4970
|
3268 FILL_TABLE_ROW (oct_data_conv::dt_logical, boolNDArray); |
4944
|
3269 |
|
3270 initialized = true; |
|
3271 } |
|
3272 |
|
3273 octave_value retval; |
|
3274 |
5659
|
3275 if (stream_ok ()) |
4944
|
3276 { |
5775
|
3277 // FIXME -- we may eventually want to make this extensible. |
|
3278 |
|
3279 // FIXME -- we need a better way to ensure that this |
4944
|
3280 // numbering stays consistent with the order of the elements in the |
|
3281 // data_type enum in the oct_data_conv class. |
|
3282 |
|
3283 char_count = 0; |
|
3284 |
5275
|
3285 octave_idx_type nr = -1; |
|
3286 octave_idx_type nc = -1; |
4944
|
3287 |
|
3288 bool ignore; |
|
3289 |
|
3290 get_size (size, nr, nc, ignore, "fread"); |
|
3291 |
|
3292 if (! error_state) |
|
3293 { |
|
3294 if (nr == 0 || nc == 0) |
|
3295 retval = Matrix (nr, nc); |
|
3296 else |
|
3297 { |
|
3298 if (ffmt == oct_mach_info::flt_fmt_unknown) |
|
3299 ffmt = float_format (); |
|
3300 |
|
3301 read_fptr fcn = read_fptr_table (output_type, input_type); |
|
3302 |
|
3303 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double |
|
3304 || input_type == oct_data_conv::dt_single) |
|
3305 && ffmt != float_format ()); |
|
3306 |
|
3307 if (fcn) |
|
3308 { |
|
3309 retval = (*fcn) (*this, nr, nc, block_size, skip, |
|
3310 do_float_fmt_conv, ffmt, char_count); |
|
3311 |
5775
|
3312 // FIXME -- kluge! |
4944
|
3313 |
|
3314 if (! error_state |
|
3315 && (output_type == oct_data_conv::dt_char |
|
3316 || output_type == oct_data_conv::dt_schar |
|
3317 || output_type == oct_data_conv::dt_uchar)) |
|
3318 retval = octave_value (retval.char_matrix_value (), true); |
|
3319 } |
|
3320 else |
|
3321 error ("fread: unable to read and convert requested types"); |
|
3322 } |
|
3323 } |
|
3324 else |
|
3325 invalid_operation ("fread", "reading"); |
|
3326 } |
2117
|
3327 |
|
3328 return retval; |
|
3329 } |
|
3330 |
5275
|
3331 octave_idx_type |
|
3332 octave_stream::write (const octave_value& data, octave_idx_type block_size, |
|
3333 oct_data_conv::data_type output_type, octave_idx_type skip, |
2317
|
3334 oct_mach_info::float_format flt_fmt) |
2117
|
3335 { |
5275
|
3336 octave_idx_type retval = -1; |
2117
|
3337 |
5659
|
3338 if (stream_ok ()) |
4944
|
3339 { |
|
3340 if (! error_state) |
|
3341 { |
|
3342 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
|
3343 flt_fmt = float_format (); |
|
3344 |
5275
|
3345 octave_idx_type status = data.write (*this, block_size, output_type, |
4944
|
3346 skip, flt_fmt); |
|
3347 |
|
3348 if (status < 0) |
|
3349 error ("fwrite: write error"); |
|
3350 else |
|
3351 retval = status; |
|
3352 } |
|
3353 else |
|
3354 invalid_operation ("fwrite", "writing"); |
|
3355 } |
2117
|
3356 |
|
3357 return retval; |
|
3358 } |
|
3359 |
4944
|
3360 template <class T> |
|
3361 void |
|
3362 write_int (std::ostream& os, bool swap, const T& val) |
|
3363 { |
|
3364 typename octave_type_traits<T>::val_type tmp = val.value (); |
|
3365 |
|
3366 if (swap) |
|
3367 swap_bytes<sizeof (typename octave_type_traits<T>::val_type)> (&tmp); |
|
3368 |
|
3369 os.write (reinterpret_cast<const char *> (&tmp), |
|
3370 sizeof (typename octave_type_traits<T>::val_type)); |
|
3371 } |
|
3372 |
|
3373 template void write_int (std::ostream&, bool, const octave_int8&); |
|
3374 template void write_int (std::ostream&, bool, const octave_uint8&); |
|
3375 template void write_int (std::ostream&, bool, const octave_int16&); |
|
3376 template void write_int (std::ostream&, bool, const octave_uint16&); |
|
3377 template void write_int (std::ostream&, bool, const octave_int32&); |
|
3378 template void write_int (std::ostream&, bool, const octave_uint32&); |
|
3379 template void write_int (std::ostream&, bool, const octave_int64&); |
|
3380 template void write_int (std::ostream&, bool, const octave_uint64&); |
|
3381 |
|
3382 template <class T> |
|
3383 static inline bool |
|
3384 do_write (std::ostream& os, const T& val, oct_data_conv::data_type output_type, |
|
3385 oct_mach_info::float_format flt_fmt, bool swap, |
|
3386 bool do_float_conversion) |
|
3387 { |
|
3388 bool retval = true; |
|
3389 |
|
3390 // For compatibility, Octave converts to the output type, then |
|
3391 // writes. This means that truncation happens on the conversion. |
|
3392 // For example, the following program prints 0: |
|
3393 // |
|
3394 // x = int8 (-1) |
|
3395 // f = fopen ("foo.dat", "w"); |
|
3396 // fwrite (f, x, "unsigned char"); |
|
3397 // fclose (f); |
|
3398 // f = fopen ("foo.dat", "r"); |
|
3399 // y = fread (f, 1, "unsigned char"); |
|
3400 // printf ("%d\n", y); |
|
3401 |
|
3402 switch (output_type) |
|
3403 { |
|
3404 case oct_data_conv::dt_char: |
|
3405 case oct_data_conv::dt_schar: |
|
3406 case oct_data_conv::dt_int8: |
|
3407 write_int (os, swap, octave_int8 (val)); |
|
3408 break; |
|
3409 |
|
3410 case oct_data_conv::dt_uchar: |
|
3411 case oct_data_conv::dt_uint8: |
|
3412 write_int (os, swap, octave_uint8 (val)); |
|
3413 break; |
|
3414 |
|
3415 case oct_data_conv::dt_int16: |
|
3416 write_int (os, swap, octave_int16 (val)); |
|
3417 break; |
|
3418 |
|
3419 case oct_data_conv::dt_uint16: |
|
3420 write_int (os, swap, octave_uint16 (val)); |
|
3421 break; |
|
3422 |
|
3423 case oct_data_conv::dt_int32: |
|
3424 write_int (os, swap, octave_int32 (val)); |
|
3425 break; |
|
3426 |
|
3427 case oct_data_conv::dt_uint32: |
|
3428 write_int (os, swap, octave_uint32 (val)); |
|
3429 break; |
|
3430 |
|
3431 case oct_data_conv::dt_int64: |
|
3432 write_int (os, swap, octave_int64 (val)); |
|
3433 break; |
|
3434 |
|
3435 case oct_data_conv::dt_uint64: |
|
3436 write_int (os, swap, octave_uint64 (val)); |
|
3437 break; |
|
3438 |
|
3439 case oct_data_conv::dt_single: |
|
3440 { |
|
3441 float f = static_cast<float> (val); |
|
3442 |
|
3443 if (do_float_conversion) |
|
3444 do_float_format_conversion (&f, 1, flt_fmt); |
|
3445 |
|
3446 os.write (reinterpret_cast<const char *> (&f), sizeof (float)); |
|
3447 } |
|
3448 break; |
|
3449 |
|
3450 case oct_data_conv::dt_double: |
|
3451 { |
|
3452 double d = static_cast<double> (val); |
|
3453 if (do_float_conversion) |
|
3454 do_double_format_conversion (&d, 1, flt_fmt); |
|
3455 |
|
3456 os.write (reinterpret_cast<const char *> (&d), sizeof (double)); |
|
3457 } |
|
3458 break; |
|
3459 |
|
3460 default: |
|
3461 retval = false; |
|
3462 (*current_liboctave_error_handler) |
|
3463 ("write: invalid type specification"); |
|
3464 break; |
|
3465 } |
|
3466 |
|
3467 return retval; |
|
3468 } |
|
3469 |
5887
|
3470 template bool |
|
3471 do_write (std::ostream&, const octave_int8&, oct_data_conv::data_type, |
|
3472 oct_mach_info::float_format, bool, bool); |
|
3473 |
|
3474 template bool |
|
3475 do_write (std::ostream&, const octave_uint8&, oct_data_conv::data_type, |
|
3476 oct_mach_info::float_format, bool, bool); |
|
3477 |
|
3478 template bool |
|
3479 do_write (std::ostream&, const octave_int16&, oct_data_conv::data_type, |
|
3480 oct_mach_info::float_format, bool, bool); |
|
3481 |
|
3482 template bool |
|
3483 do_write (std::ostream&, const octave_uint16&, oct_data_conv::data_type, |
|
3484 oct_mach_info::float_format, bool, bool); |
|
3485 |
|
3486 template bool |
|
3487 do_write (std::ostream&, const octave_int32&, oct_data_conv::data_type, |
|
3488 oct_mach_info::float_format, bool, bool); |
|
3489 |
|
3490 template bool |
|
3491 do_write (std::ostream&, const octave_uint32&, oct_data_conv::data_type, |
|
3492 oct_mach_info::float_format, bool, bool); |
|
3493 |
|
3494 template bool |
|
3495 do_write (std::ostream&, const octave_int64&, oct_data_conv::data_type, |
|
3496 oct_mach_info::float_format, bool, bool); |
|
3497 |
|
3498 template bool |
|
3499 do_write (std::ostream&, const octave_uint64&, oct_data_conv::data_type, |
|
3500 oct_mach_info::float_format, bool, bool); |
|
3501 |
4944
|
3502 template <class T> |
5275
|
3503 octave_idx_type |
|
3504 octave_stream::write (const Array<T>& data, octave_idx_type block_size, |
4944
|
3505 oct_data_conv::data_type output_type, |
5275
|
3506 octave_idx_type skip, oct_mach_info::float_format flt_fmt) |
4944
|
3507 { |
5275
|
3508 octave_idx_type retval = -1; |
4944
|
3509 |
|
3510 bool status = true; |
|
3511 |
5275
|
3512 octave_idx_type count = 0; |
4944
|
3513 |
|
3514 const T *d = data.data (); |
|
3515 |
5275
|
3516 octave_idx_type n = data.length (); |
4944
|
3517 |
|
3518 oct_mach_info::float_format native_flt_fmt |
|
3519 = oct_mach_info::float_format (); |
|
3520 |
|
3521 bool do_float_conversion = (flt_fmt != native_flt_fmt); |
|
3522 |
5775
|
3523 // FIXME -- byte order for Cray? |
4944
|
3524 |
|
3525 bool swap = false; |
|
3526 |
|
3527 if (oct_mach_info::words_big_endian ()) |
|
3528 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
3529 || flt_fmt == oct_mach_info::flt_fmt_vax_g |
|
3530 || flt_fmt == oct_mach_info::flt_fmt_vax_g); |
|
3531 else |
|
3532 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
|
3533 |
5275
|
3534 for (octave_idx_type i = 0; i < n; i++) |
4944
|
3535 { |
|
3536 std::ostream *osp = output_stream (); |
|
3537 |
|
3538 if (osp) |
|
3539 { |
|
3540 std::ostream& os = *osp; |
|
3541 |
5254
|
3542 // It seems that Matlab writes zeros instead of actually |
|
3543 // seeking. Hmm... |
|
3544 |
4944
|
3545 if (skip != 0 && (i % block_size) == 0) |
5254
|
3546 { |
5775
|
3547 // FIXME -- probably should try to write larger |
5254
|
3548 // blocks... |
|
3549 |
|
3550 unsigned char zero = 0; |
|
3551 for (int j = 0; j < skip; j++) |
|
3552 os.write (reinterpret_cast<const char *> (&zero), 1); |
|
3553 } |
4944
|
3554 |
|
3555 if (os) |
|
3556 { |
|
3557 status = do_write (os, d[i], output_type, flt_fmt, swap, |
|
3558 do_float_conversion); |
|
3559 |
|
3560 if (os && status) |
|
3561 count++; |
|
3562 else |
|
3563 break; |
|
3564 } |
|
3565 else |
|
3566 { |
|
3567 status = false; |
|
3568 break; |
|
3569 } |
|
3570 } |
|
3571 else |
|
3572 { |
|
3573 status = false; |
|
3574 break; |
|
3575 } |
|
3576 } |
|
3577 |
|
3578 if (status) |
|
3579 retval = count; |
|
3580 |
|
3581 return retval; |
|
3582 } |
|
3583 |
5275
|
3584 template octave_idx_type |
|
3585 octave_stream::write (const Array<char>&, octave_idx_type, |
4944
|
3586 oct_data_conv::data_type, |
5275
|
3587 octave_idx_type, oct_mach_info::float_format); |
|
3588 |
|
3589 template octave_idx_type |
|
3590 octave_stream::write (const Array<bool>&, octave_idx_type, |
4970
|
3591 oct_data_conv::data_type, |
5275
|
3592 octave_idx_type, oct_mach_info::float_format); |
|
3593 |
|
3594 template octave_idx_type |
|
3595 octave_stream::write (const Array<double>&, octave_idx_type, |
4944
|
3596 oct_data_conv::data_type, |
5275
|
3597 octave_idx_type, oct_mach_info::float_format); |
|
3598 |
|
3599 template octave_idx_type |
|
3600 octave_stream::write (const Array<octave_int8>&, octave_idx_type, |
4944
|
3601 oct_data_conv::data_type, |
5275
|
3602 octave_idx_type, oct_mach_info::float_format); |
|
3603 |
|
3604 template octave_idx_type |
|
3605 octave_stream::write (const Array<octave_uint8>&, octave_idx_type, |
4944
|
3606 oct_data_conv::data_type, |
5275
|
3607 octave_idx_type, oct_mach_info::float_format); |
|
3608 |
|
3609 template octave_idx_type |
|
3610 octave_stream::write (const Array<octave_int16>&, octave_idx_type, |
4944
|
3611 oct_data_conv::data_type, |
5275
|
3612 octave_idx_type, oct_mach_info::float_format); |
|
3613 |
|
3614 template octave_idx_type |
|
3615 octave_stream::write (const Array<octave_uint16>&, octave_idx_type, |
4944
|
3616 oct_data_conv::data_type, |
5275
|
3617 octave_idx_type, oct_mach_info::float_format); |
|
3618 |
|
3619 template octave_idx_type |
|
3620 octave_stream::write (const Array<octave_int32>&, octave_idx_type, |
4944
|
3621 oct_data_conv::data_type, |
5275
|
3622 octave_idx_type, oct_mach_info::float_format); |
|
3623 |
|
3624 template octave_idx_type |
|
3625 octave_stream::write (const Array<octave_uint32>&, octave_idx_type, |
4944
|
3626 oct_data_conv::data_type, |
5275
|
3627 octave_idx_type, oct_mach_info::float_format); |
|
3628 |
|
3629 template octave_idx_type |
|
3630 octave_stream::write (const Array<octave_int64>&, octave_idx_type, |
4944
|
3631 oct_data_conv::data_type, |
5275
|
3632 octave_idx_type, oct_mach_info::float_format); |
|
3633 |
|
3634 template octave_idx_type |
|
3635 octave_stream::write (const Array<octave_uint64>&, octave_idx_type, |
4944
|
3636 oct_data_conv::data_type, |
5275
|
3637 octave_idx_type, oct_mach_info::float_format); |
4944
|
3638 |
2117
|
3639 octave_value |
3810
|
3640 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
5275
|
3641 octave_idx_type& count, const std::string& who) |
2117
|
3642 { |
|
3643 octave_value retval; |
|
3644 |
5659
|
3645 if (stream_ok ()) |
4468
|
3646 retval = rep->scanf (fmt, size, count, who); |
2117
|
3647 |
|
3648 return retval; |
|
3649 } |
|
3650 |
5279
|
3651 octave_value |
|
3652 octave_stream::scanf (const octave_value& fmt, const Array<double>& size, |
5299
|
3653 octave_idx_type& count, const std::string& who) |
5279
|
3654 { |
|
3655 octave_value retval = Matrix (); |
|
3656 |
|
3657 if (fmt.is_string ()) |
|
3658 { |
|
3659 std::string sfmt = fmt.string_value (); |
|
3660 |
|
3661 if (fmt.is_sq_string ()) |
|
3662 sfmt = do_string_escapes (sfmt); |
|
3663 |
|
3664 retval = scanf (sfmt, size, count, who); |
|
3665 } |
|
3666 else |
|
3667 { |
|
3668 // Note that this is not ::error () ! |
|
3669 |
|
3670 error (who + ": format must be a string"); |
|
3671 } |
|
3672 |
|
3673 return retval; |
|
3674 } |
|
3675 |
2215
|
3676 octave_value_list |
4468
|
3677 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
3678 { |
|
3679 octave_value_list retval; |
|
3680 |
5659
|
3681 if (stream_ok ()) |
4468
|
3682 retval = rep->oscanf (fmt, who); |
2215
|
3683 |
|
3684 return retval; |
|
3685 } |
|
3686 |
5279
|
3687 octave_value_list |
|
3688 octave_stream::oscanf (const octave_value& fmt, const std::string& who) |
|
3689 { |
|
3690 octave_value_list retval; |
|
3691 |
|
3692 if (fmt.is_string ()) |
|
3693 { |
|
3694 std::string sfmt = fmt.string_value (); |
|
3695 |
|
3696 if (fmt.is_sq_string ()) |
|
3697 sfmt = do_string_escapes (sfmt); |
|
3698 |
|
3699 retval = oscanf (sfmt, who); |
|
3700 } |
|
3701 else |
|
3702 { |
|
3703 // Note that this is not ::error () ! |
|
3704 |
|
3705 error (who + ": format must be a string"); |
|
3706 } |
|
3707 |
|
3708 return retval; |
|
3709 } |
|
3710 |
2117
|
3711 int |
4468
|
3712 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
|
3713 const std::string& who) |
2117
|
3714 { |
|
3715 int retval = -1; |
|
3716 |
5659
|
3717 if (stream_ok ()) |
4468
|
3718 retval = rep->printf (fmt, args, who); |
2117
|
3719 |
|
3720 return retval; |
|
3721 } |
|
3722 |
|
3723 int |
5279
|
3724 octave_stream::printf (const octave_value& fmt, const octave_value_list& args, |
|
3725 const std::string& who) |
|
3726 { |
|
3727 int retval = 0; |
|
3728 |
|
3729 if (fmt.is_string ()) |
|
3730 { |
|
3731 std::string sfmt = fmt.string_value (); |
|
3732 |
|
3733 if (fmt.is_sq_string ()) |
|
3734 sfmt = do_string_escapes (sfmt); |
|
3735 |
|
3736 retval = printf (sfmt, args, who); |
|
3737 } |
|
3738 else |
|
3739 { |
|
3740 // Note that this is not ::error () ! |
|
3741 |
|
3742 error (who + ": format must be a string"); |
|
3743 } |
|
3744 |
|
3745 return retval; |
|
3746 } |
|
3747 |
|
3748 int |
4468
|
3749 octave_stream::puts (const std::string& s, const std::string& who) |
2117
|
3750 { |
|
3751 int retval = -1; |
|
3752 |
5659
|
3753 if (stream_ok ()) |
4468
|
3754 retval = rep->puts (s, who); |
2117
|
3755 |
|
3756 return retval; |
|
3757 } |
|
3758 |
5775
|
3759 // FIXME -- maybe this should work for string arrays too. |
2117
|
3760 |
|
3761 int |
4468
|
3762 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117
|
3763 { |
|
3764 int retval = -1; |
|
3765 |
|
3766 if (tc_s.is_string ()) |
|
3767 { |
3523
|
3768 std::string s = tc_s.string_value (); |
5279
|
3769 retval = puts (s, who); |
2117
|
3770 } |
|
3771 else |
4468
|
3772 { |
|
3773 // Note that this is not ::error () ! |
|
3774 |
|
3775 error (who + ": argument must be a string"); |
|
3776 } |
2117
|
3777 |
|
3778 return retval; |
|
3779 } |
|
3780 |
|
3781 bool |
|
3782 octave_stream::eof (void) const |
|
3783 { |
|
3784 int retval = -1; |
|
3785 |
5659
|
3786 if (stream_ok ()) |
2117
|
3787 retval = rep->eof (); |
|
3788 |
|
3789 return retval; |
|
3790 } |
|
3791 |
3536
|
3792 std::string |
2435
|
3793 octave_stream::error (bool clear, int& err_num) |
2117
|
3794 { |
5649
|
3795 std::string retval = "invalid stream object"; |
|
3796 |
5659
|
3797 if (stream_ok (false)) |
2435
|
3798 retval = rep->error (clear, err_num); |
2117
|
3799 |
|
3800 return retval; |
|
3801 } |
|
3802 |
3536
|
3803 std::string |
3340
|
3804 octave_stream::name (void) const |
2117
|
3805 { |
3523
|
3806 std::string retval; |
2117
|
3807 |
5659
|
3808 if (stream_ok ()) |
2117
|
3809 retval = rep->name (); |
|
3810 |
|
3811 return retval; |
|
3812 } |
|
3813 |
|
3814 int |
3340
|
3815 octave_stream::mode (void) const |
2117
|
3816 { |
|
3817 int retval = 0; |
|
3818 |
5659
|
3819 if (stream_ok ()) |
2117
|
3820 retval = rep->mode (); |
|
3821 |
|
3822 return retval; |
|
3823 } |
|
3824 |
2317
|
3825 oct_mach_info::float_format |
3340
|
3826 octave_stream::float_format (void) const |
2117
|
3827 { |
4574
|
3828 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317
|
3829 |
5659
|
3830 if (stream_ok ()) |
2317
|
3831 retval = rep->float_format (); |
2117
|
3832 |
|
3833 return retval; |
|
3834 } |
|
3835 |
3536
|
3836 std::string |
2117
|
3837 octave_stream::mode_as_string (int mode) |
|
3838 { |
3523
|
3839 std::string retval = "???"; |
3775
|
3840 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
|
3841 |
|
3842 if (in_mode == std::ios::in) |
|
3843 retval = "r"; |
|
3844 else if (in_mode == std::ios::out |
4078
|
3845 || in_mode == (std::ios::out | std::ios::trunc)) |
3775
|
3846 retval = "w"; |
4078
|
3847 else if (in_mode == (std::ios::out | std::ios::app)) |
3775
|
3848 retval = "a"; |
4078
|
3849 else if (in_mode == (std::ios::in | std::ios::out)) |
3775
|
3850 retval = "r+"; |
4078
|
3851 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775
|
3852 retval = "w+"; |
4078
|
3853 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775
|
3854 retval = "a+"; |
4078
|
3855 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775
|
3856 retval = "rb"; |
4078
|
3857 else if (in_mode == (std::ios::out | std::ios::binary) |
|
3858 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775
|
3859 retval = "wb"; |
4078
|
3860 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775
|
3861 retval = "ab"; |
4078
|
3862 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775
|
3863 retval = "r+b"; |
4078
|
3864 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
|
3865 | std::ios::binary)) |
3775
|
3866 retval = "w+b"; |
4078
|
3867 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
|
3868 | std::ios::binary)) |
3775
|
3869 retval = "a+b"; |
2117
|
3870 |
|
3871 return retval; |
|
3872 } |
|
3873 |
|
3874 octave_stream_list *octave_stream_list::instance = 0; |
|
3875 |
2926
|
3876 bool |
|
3877 octave_stream_list::instance_ok (void) |
|
3878 { |
|
3879 bool retval = true; |
|
3880 |
|
3881 if (! instance) |
|
3882 instance = new octave_stream_list (); |
|
3883 |
|
3884 if (! instance) |
|
3885 { |
|
3886 ::error ("unable to create stream list object!"); |
|
3887 |
|
3888 retval = false; |
|
3889 } |
|
3890 |
|
3891 return retval; |
|
3892 } |
|
3893 |
5353
|
3894 int |
6757
|
3895 octave_stream_list::insert (octave_stream& os) |
2926
|
3896 { |
5353
|
3897 return (instance_ok ()) ? instance->do_insert (os) : -1; |
2926
|
3898 } |
|
3899 |
3340
|
3900 octave_stream |
3523
|
3901 octave_stream_list::lookup (int fid, const std::string& who) |
2926
|
3902 { |
3341
|
3903 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3904 } |
|
3905 |
3340
|
3906 octave_stream |
3523
|
3907 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926
|
3908 { |
3341
|
3909 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3910 } |
|
3911 |
|
3912 int |
3523
|
3913 octave_stream_list::remove (int fid, const std::string& who) |
2926
|
3914 { |
3341
|
3915 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3916 } |
|
3917 |
|
3918 int |
3523
|
3919 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926
|
3920 { |
3341
|
3921 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3922 } |
|
3923 |
|
3924 void |
|
3925 octave_stream_list::clear (void) |
|
3926 { |
|
3927 if (instance) |
|
3928 instance->do_clear (); |
|
3929 } |
|
3930 |
|
3931 string_vector |
|
3932 octave_stream_list::get_info (int fid) |
|
3933 { |
|
3934 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3935 } |
|
3936 |
|
3937 string_vector |
|
3938 octave_stream_list::get_info (const octave_value& fid) |
|
3939 { |
|
3940 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3941 } |
|
3942 |
3536
|
3943 std::string |
2926
|
3944 octave_stream_list::list_open_files (void) |
|
3945 { |
3523
|
3946 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926
|
3947 } |
|
3948 |
|
3949 octave_value |
|
3950 octave_stream_list::open_file_numbers (void) |
|
3951 { |
|
3952 return (instance_ok ()) |
|
3953 ? instance->do_open_file_numbers () : octave_value (); |
|
3954 } |
|
3955 |
|
3956 int |
|
3957 octave_stream_list::get_file_number (const octave_value& fid) |
|
3958 { |
|
3959 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; |
|
3960 } |
|
3961 |
5353
|
3962 int |
6757
|
3963 octave_stream_list::do_insert (octave_stream& os) |
2117
|
3964 { |
6757
|
3965 // Insert item with key corresponding to file-descriptor. |
|
3966 |
|
3967 int stream_number; |
|
3968 |
|
3969 if ((stream_number = os.file_number ()) == -1) |
|
3970 return stream_number; |
|
3971 |
|
3972 // Should we test for "(list.find (stream_number) != list.end ()) && |
|
3973 // list[stream_number].is_open ()" and respond with "error |
|
3974 // ("internal error: ...")"? It should not happen except for some |
|
3975 // bug or if the user has opened a stream with an interpreted |
|
3976 // command, but closed it directly with a system call in an |
|
3977 // oct-file; then the kernel knows the fd is free, but Octave does |
|
3978 // not know. If it happens, it should not do harm here to simply |
|
3979 // overwrite this entry, although the wrong entry might have done |
|
3980 // harm before. |
|
3981 |
|
3982 if (list.size () < list.max_size ()) |
|
3983 list[stream_number] = os; |
|
3984 else |
2117
|
3985 { |
6757
|
3986 stream_number = -1; |
|
3987 error ("could not create file id"); |
3340
|
3988 } |
2117
|
3989 |
5353
|
3990 return stream_number; |
6757
|
3991 |
2117
|
3992 } |
|
3993 |
3341
|
3994 static void |
3523
|
3995 gripe_invalid_file_id (int fid, const std::string& who) |
3341
|
3996 { |
|
3997 if (who.empty ()) |
|
3998 ::error ("invalid stream number = %d", fid); |
|
3999 else |
|
4000 ::error ("%s: invalid stream number = %d", who.c_str (), fid); |
|
4001 } |
|
4002 |
3340
|
4003 octave_stream |
3523
|
4004 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117
|
4005 { |
3340
|
4006 octave_stream retval; |
2117
|
4007 |
6757
|
4008 if (fid >= 0) |
|
4009 { |
|
4010 ostrl_map::const_iterator iter = list.find (fid); |
|
4011 |
|
4012 if (iter != list.end ()) |
|
4013 retval = iter->second; |
|
4014 else |
|
4015 gripe_invalid_file_id (fid, who); |
|
4016 } |
3341
|
4017 else |
|
4018 gripe_invalid_file_id (fid, who); |
2117
|
4019 |
|
4020 return retval; |
|
4021 } |
|
4022 |
3340
|
4023 octave_stream |
3341
|
4024 octave_stream_list::do_lookup (const octave_value& fid, |
3523
|
4025 const std::string& who) const |
2117
|
4026 { |
3340
|
4027 octave_stream retval; |
2117
|
4028 |
|
4029 int i = get_file_number (fid); |
|
4030 |
|
4031 if (! error_state) |
3341
|
4032 retval = do_lookup (i, who); |
2117
|
4033 |
|
4034 return retval; |
|
4035 } |
|
4036 |
|
4037 int |
3523
|
4038 octave_stream_list::do_remove (int fid, const std::string& who) |
2117
|
4039 { |
|
4040 int retval = -1; |
|
4041 |
3531
|
4042 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
|
4043 // (std::cerr). |
2117
|
4044 |
6757
|
4045 if (fid > 2) |
2117
|
4046 { |
6757
|
4047 ostrl_map::iterator iter = list.find (fid); |
|
4048 |
|
4049 if (iter != list.end ()) |
2117
|
4050 { |
6757
|
4051 octave_stream os = iter->second; |
|
4052 |
|
4053 if (os.is_valid ()) |
|
4054 { |
|
4055 os.close (); |
|
4056 iter->second = octave_stream (); |
|
4057 retval = 0; |
|
4058 } |
|
4059 else |
|
4060 gripe_invalid_file_id (fid, who); |
2117
|
4061 } |
3341
|
4062 else |
|
4063 gripe_invalid_file_id (fid, who); |
2117
|
4064 } |
3341
|
4065 else |
|
4066 gripe_invalid_file_id (fid, who); |
2117
|
4067 |
|
4068 return retval; |
|
4069 } |
|
4070 |
|
4071 int |
3523
|
4072 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117
|
4073 { |
|
4074 int retval = -1; |
|
4075 |
6054
|
4076 if (fid.is_string () && fid.string_value () == "all") |
|
4077 { |
6757
|
4078 for (ostrl_map::iterator p = list.begin (); p != list.end (); p++) |
6054
|
4079 { |
6757
|
4080 // Skip stdin, stdout, and stderr. |
|
4081 |
|
4082 if (p->first > 2) |
|
4083 { |
|
4084 octave_stream os = p->second; |
|
4085 |
|
4086 if (os.is_valid ()) |
|
4087 do_remove (p->first, who); |
|
4088 } |
6054
|
4089 } |
|
4090 |
|
4091 retval = 0; |
|
4092 } |
|
4093 else |
|
4094 { |
|
4095 int i = get_file_number (fid); |
|
4096 |
|
4097 if (! error_state) |
|
4098 retval = do_remove (i, who); |
|
4099 } |
2117
|
4100 |
|
4101 return retval; |
|
4102 } |
|
4103 |
|
4104 void |
|
4105 octave_stream_list::do_clear (void) |
|
4106 { |
|
4107 // Do flush stdout and stderr. |
|
4108 |
6757
|
4109 list[0].flush (); |
|
4110 list[1].flush (); |
2117
|
4111 |
|
4112 // But don't delete them or stdin. |
|
4113 |
6757
|
4114 for (ostrl_map::iterator p = list.begin (); p != list.end (); p++) |
|
4115 { |
|
4116 // Skip stdin, stdout, and stderr. |
|
4117 |
|
4118 if (p->first > 2) |
|
4119 p->second = octave_stream (); |
|
4120 } |
2117
|
4121 } |
|
4122 |
|
4123 string_vector |
|
4124 octave_stream_list::do_get_info (int fid) const |
|
4125 { |
|
4126 string_vector retval; |
|
4127 |
3340
|
4128 octave_stream os = do_lookup (fid); |
2117
|
4129 |
3341
|
4130 if (os.is_valid ()) |
2117
|
4131 { |
|
4132 retval.resize (3); |
|
4133 |
3340
|
4134 retval(0) = os.name (); |
|
4135 retval(1) = octave_stream::mode_as_string (os.mode ()); |
|
4136 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
2117
|
4137 } |
|
4138 else |
3341
|
4139 ::error ("invalid file id = %d", fid); |
2117
|
4140 |
|
4141 return retval; |
|
4142 } |
|
4143 |
|
4144 string_vector |
|
4145 octave_stream_list::do_get_info (const octave_value& fid) const |
|
4146 { |
|
4147 string_vector retval; |
|
4148 |
|
4149 int conv_err = 0; |
|
4150 |
|
4151 int int_fid = convert_to_valid_int (fid, conv_err); |
|
4152 |
|
4153 if (! conv_err) |
|
4154 retval = do_get_info (int_fid); |
|
4155 else |
2915
|
4156 ::error ("file id must be a file object or integer value"); |
2117
|
4157 |
|
4158 return retval; |
|
4159 } |
|
4160 |
3536
|
4161 std::string |
2117
|
4162 octave_stream_list::do_list_open_files (void) const |
|
4163 { |
3523
|
4164 std::string retval; |
2117
|
4165 |
5765
|
4166 std::ostringstream buf; |
2117
|
4167 |
|
4168 buf << "\n" |
|
4169 << " number mode arch name\n" |
|
4170 << " ------ ---- ---- ----\n"; |
|
4171 |
6757
|
4172 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117
|
4173 { |
6757
|
4174 octave_stream os = p->second; |
2117
|
4175 |
4326
|
4176 buf << " " |
|
4177 << std::setiosflags (std::ios::right) |
6757
|
4178 << std::setw (4) << p->first << " " |
4326
|
4179 << std::setiosflags (std::ios::left) |
|
4180 << std::setw (3) |
|
4181 << octave_stream::mode_as_string (os.mode ()) |
|
4182 << " " |
|
4183 << std::setw (9) |
|
4184 << oct_mach_info::float_format_as_string (os.float_format ()) |
|
4185 << " " |
|
4186 << os.name () << "\n"; |
2117
|
4187 } |
|
4188 |
5765
|
4189 buf << "\n"; |
|
4190 |
|
4191 retval = buf.str (); |
2117
|
4192 |
|
4193 return retval; |
|
4194 } |
|
4195 |
|
4196 octave_value |
|
4197 octave_stream_list::do_open_file_numbers (void) const |
|
4198 { |
6757
|
4199 Matrix retval (1, list.size (), 0.0); |
2117
|
4200 |
|
4201 int num_open = 0; |
|
4202 |
6757
|
4203 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117
|
4204 { |
6757
|
4205 // Skip stdin, stdout, and stderr. |
|
4206 |
|
4207 if (p->first > 2 && p->second) |
|
4208 retval(0,num_open++) = p->first; |
2117
|
4209 } |
|
4210 |
|
4211 retval.resize ((num_open > 0), num_open); |
|
4212 |
|
4213 return retval; |
|
4214 } |
|
4215 |
|
4216 int |
2609
|
4217 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117
|
4218 { |
|
4219 int retval = -1; |
|
4220 |
|
4221 if (fid.is_string ()) |
|
4222 { |
3523
|
4223 std::string nm = fid.string_value (); |
2117
|
4224 |
6757
|
4225 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117
|
4226 { |
6757
|
4227 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
|
4228 // are unnamed. |
|
4229 |
|
4230 if (p->first > 2) |
2117
|
4231 { |
6757
|
4232 octave_stream os = p->second; |
|
4233 |
|
4234 if (os && os.name () == nm) |
|
4235 { |
|
4236 retval = p->first; |
|
4237 break; |
|
4238 } |
2117
|
4239 } |
|
4240 } |
|
4241 } |
|
4242 else |
|
4243 { |
|
4244 int conv_err = 0; |
|
4245 |
|
4246 int int_fid = convert_to_valid_int (fid, conv_err); |
|
4247 |
|
4248 if (conv_err) |
3523
|
4249 ::error ("file id must be a file object, std::string, or integer value"); |
2117
|
4250 else |
|
4251 retval = int_fid; |
|
4252 } |
|
4253 |
|
4254 return retval; |
|
4255 } |
|
4256 |
|
4257 /* |
|
4258 ;;; Local Variables: *** |
|
4259 ;;; mode: C++ *** |
|
4260 ;;; End: *** |
|
4261 */ |