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