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