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