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