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