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