2117
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2117
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3268
|
27 #include <cassert> |
2215
|
28 #include <cstring> |
|
29 |
3503
|
30 #include <iomanip> |
3559
|
31 #include <fstream> |
3535
|
32 #include <string> |
2117
|
33 |
|
34 #include "lo-ieee.h" |
|
35 #include "lo-mappers.h" |
4051
|
36 #include "lo-sstream.h" |
2117
|
37 #include "lo-utils.h" |
|
38 #include "str-vec.h" |
4153
|
39 #include "quit.h" |
2117
|
40 |
|
41 #include "error.h" |
3342
|
42 #include "input.h" |
3775
|
43 #include "oct-stdstrm.h" |
2117
|
44 #include "oct-stream.h" |
2877
|
45 #include "oct-obj.h" |
2117
|
46 #include "utils.h" |
|
47 |
|
48 // Possible values for conv_err: |
|
49 // |
|
50 // 1 : not a real scalar |
2902
|
51 // 2 : value is NaN |
|
52 // 3 : value is not an integer |
2117
|
53 |
|
54 static int |
|
55 convert_to_valid_int (const octave_value& tc, int& conv_err) |
|
56 { |
|
57 int retval = 0; |
|
58 |
|
59 conv_err = 0; |
|
60 |
2902
|
61 double dval = tc.double_value (); |
|
62 |
|
63 if (! error_state) |
2117
|
64 { |
4693
|
65 if (! lo_ieee_is_NaN_or_NA (dval)) |
2117
|
66 { |
2902
|
67 int ival = NINT (dval); |
|
68 |
|
69 if (ival == dval) |
|
70 retval = ival; |
2117
|
71 else |
|
72 conv_err = 3; |
|
73 } |
|
74 else |
|
75 conv_err = 2; |
|
76 } |
|
77 else |
|
78 conv_err = 1; |
|
79 |
|
80 return retval; |
|
81 } |
|
82 |
|
83 static int |
4468
|
84 get_size (double d, const std::string& who) |
2117
|
85 { |
|
86 int retval = -1; |
|
87 |
4693
|
88 if (! lo_ieee_is_NaN_or_NA (d)) |
2117
|
89 { |
|
90 if (! xisinf (d)) |
|
91 { |
3268
|
92 if (d >= 0.0) |
2117
|
93 retval = NINT (d); |
|
94 else |
|
95 ::error ("%s: negative value invalid as size specification", |
4468
|
96 who.c_str ()); |
2117
|
97 } |
|
98 else |
|
99 retval = -1; |
|
100 } |
|
101 else |
4468
|
102 ::error ("%s: NaN is invalid as size specification", who.c_str ()); |
2117
|
103 |
|
104 return retval; |
|
105 } |
|
106 |
|
107 static void |
3810
|
108 get_size (const Array<double>& size, int& nr, int& nc, bool& one_elt_size_spec, |
4468
|
109 const std::string& who) |
2117
|
110 { |
|
111 nr = -1; |
|
112 nc = -1; |
|
113 |
3268
|
114 one_elt_size_spec = false; |
|
115 |
2117
|
116 double dnr = -1.0; |
|
117 double dnc = -1.0; |
|
118 |
3810
|
119 int sz_len = size.length (); |
|
120 |
|
121 if (sz_len == 1) |
2601
|
122 { |
3268
|
123 one_elt_size_spec = true; |
|
124 |
3810
|
125 dnr = size (0); |
4293
|
126 |
|
127 dnc = (dnr == 0.0) ? 0.0 : 1.0; |
2601
|
128 } |
3810
|
129 else if (sz_len == 2) |
2117
|
130 { |
3810
|
131 dnr = size (0); |
|
132 |
|
133 if (! xisinf (dnr)) |
|
134 dnc = size (1); |
|
135 else |
4468
|
136 ::error ("%s: invalid size specification", who.c_str ()); |
2117
|
137 } |
|
138 else |
4468
|
139 ::error ("%s: invalid size specification", who.c_str ()); |
2117
|
140 |
|
141 if (! error_state) |
|
142 { |
4468
|
143 nr = get_size (dnr, who); |
2117
|
144 |
3268
|
145 if (! error_state && dnc >= 0.0) |
4468
|
146 nc = get_size (dnc, who); |
2117
|
147 } |
|
148 } |
|
149 |
3523
|
150 scanf_format_list::scanf_format_list (const std::string& s) |
2117
|
151 : nconv (0), curr_idx (0), list (16), buf (0) |
|
152 { |
|
153 int num_elts = 0; |
|
154 |
|
155 int n = s.length (); |
|
156 |
|
157 int i = 0; |
|
158 |
2215
|
159 int width = 0; |
2117
|
160 bool discard = false; |
|
161 char modifier = '\0'; |
|
162 char type = '\0'; |
|
163 |
|
164 bool have_more = true; |
|
165 |
|
166 while (i < n) |
|
167 { |
|
168 have_more = true; |
|
169 |
|
170 if (! buf) |
4051
|
171 buf = new OSSTREAM (); |
2117
|
172 |
|
173 if (s[i] == '%') |
|
174 { |
3483
|
175 // Process percent-escape conversion type. |
|
176 |
2215
|
177 process_conversion (s, i, n, width, discard, type, modifier, |
|
178 num_elts); |
2117
|
179 have_more = (buf != 0); |
|
180 } |
3483
|
181 else if (isspace (s[i])) |
2117
|
182 { |
3483
|
183 type = scanf_format_elt::whitespace_conversion; |
|
184 |
2215
|
185 width = 0; |
2117
|
186 discard = false; |
|
187 modifier = '\0'; |
3483
|
188 *buf << " "; |
|
189 |
|
190 while (++i < n && isspace (s[i])) |
|
191 /* skip whitespace */; |
|
192 |
|
193 add_elt_to_list (width, discard, type, modifier, num_elts); |
|
194 |
|
195 have_more = false; |
|
196 } |
|
197 else |
|
198 { |
|
199 type = scanf_format_elt::literal_conversion; |
|
200 |
|
201 width = 0; |
|
202 discard = false; |
|
203 modifier = '\0'; |
|
204 |
|
205 while (i < n && ! isspace (s[i]) && s[i] != '%') |
|
206 *buf << s[i++]; |
|
207 |
|
208 add_elt_to_list (width, discard, type, modifier, num_elts); |
|
209 |
|
210 have_more = false; |
2117
|
211 } |
|
212 |
|
213 if (nconv < 0) |
|
214 { |
|
215 have_more = false; |
|
216 break; |
|
217 } |
|
218 } |
|
219 |
|
220 if (have_more) |
2215
|
221 add_elt_to_list (width, discard, type, modifier, num_elts); |
2117
|
222 |
|
223 list.resize (num_elts); |
|
224 |
|
225 delete buf; |
|
226 } |
|
227 |
|
228 scanf_format_list::~scanf_format_list (void) |
|
229 { |
|
230 int n = list.length (); |
|
231 |
|
232 for (int i = 0; i < n; i++) |
|
233 { |
3340
|
234 scanf_format_elt *elt = list(i); |
2117
|
235 delete elt; |
|
236 } |
|
237 } |
|
238 |
|
239 void |
2215
|
240 scanf_format_list::add_elt_to_list (int width, bool discard, char type, |
3483
|
241 char modifier, int& num_elts, |
3523
|
242 const std::string& char_class) |
2117
|
243 { |
|
244 if (buf) |
|
245 { |
4051
|
246 *buf << OSSTREAM_ENDS; |
|
247 |
|
248 std::string text = OSSTREAM_STR (*buf); |
|
249 |
|
250 OSSTREAM_FREEZE (*buf); |
|
251 |
|
252 if (! text.empty ()) |
2117
|
253 { |
4051
|
254 scanf_format_elt *elt |
|
255 = new scanf_format_elt (text.c_str (), width, discard, type, |
|
256 modifier, char_class); |
|
257 |
|
258 if (num_elts == list.length ()) |
|
259 list.resize (2 * num_elts); |
|
260 |
|
261 list(num_elts++) = elt; |
2117
|
262 } |
|
263 |
|
264 delete buf; |
|
265 buf = 0; |
|
266 } |
|
267 } |
|
268 |
3535
|
269 static std::string |
3523
|
270 expand_char_class (const std::string& s) |
3483
|
271 { |
3523
|
272 std::string retval; |
3483
|
273 |
|
274 size_t len = s.length (); |
|
275 |
|
276 size_t i = 0; |
|
277 |
|
278 while (i < len) |
|
279 { |
|
280 unsigned char c = s[i++]; |
|
281 |
|
282 if (c == '-' && i > 1 && i < len |
|
283 && (unsigned char) s[i-2] <= (unsigned char) s[i]) |
|
284 { |
|
285 // Add all characters from the range except the first (we |
|
286 // already added it below). |
|
287 |
|
288 for (c = s[i-2]+1; c < s[i]; c++) |
|
289 retval += c; |
|
290 } |
|
291 else |
|
292 { |
|
293 // Add the character to the class. Only add '-' if it is |
|
294 // the last character in the class. |
|
295 |
|
296 if (c != '-' || i == len) |
|
297 retval += c; |
|
298 } |
|
299 } |
|
300 |
|
301 return retval; |
|
302 } |
|
303 |
2117
|
304 void |
3523
|
305 scanf_format_list::process_conversion (const std::string& s, int& i, int n, |
2215
|
306 int& width, bool& discard, char& type, |
2117
|
307 char& modifier, int& num_elts) |
|
308 { |
2215
|
309 width = 0; |
2117
|
310 discard = false; |
|
311 modifier = '\0'; |
|
312 type = '\0'; |
|
313 |
|
314 *buf << s[i++]; |
|
315 |
|
316 bool have_width = false; |
|
317 |
|
318 while (i < n) |
|
319 { |
|
320 switch (s[i]) |
|
321 { |
|
322 case '*': |
|
323 if (discard) |
|
324 nconv = -1; |
|
325 else |
|
326 { |
|
327 discard = true; |
|
328 *buf << s[i++]; |
|
329 } |
|
330 break; |
|
331 |
|
332 case '0': case '1': case '2': case '3': case '4': |
|
333 case '5': case '6': case '7': case '8': case '9': |
|
334 if (have_width) |
|
335 nconv = -1; |
|
336 else |
|
337 { |
2215
|
338 char c = s[i++]; |
|
339 width = width * 10 + c - '0'; |
2117
|
340 have_width = true; |
2215
|
341 *buf << c; |
2117
|
342 while (i < n && isdigit (s[i])) |
2215
|
343 { |
|
344 c = s[i++]; |
|
345 width = width * 10 + c - '0'; |
|
346 *buf << c; |
|
347 } |
2117
|
348 } |
|
349 break; |
|
350 |
|
351 case 'h': case 'l': case 'L': |
|
352 if (modifier != '\0') |
|
353 nconv = -1; |
|
354 else |
2663
|
355 modifier = s[i++]; |
2117
|
356 break; |
|
357 |
|
358 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
359 if (modifier == 'L') |
|
360 { |
|
361 nconv = -1; |
|
362 break; |
|
363 } |
|
364 goto fini; |
|
365 |
|
366 case 'e': case 'f': case 'g': |
|
367 if (modifier == 'h') |
|
368 { |
|
369 nconv = -1; |
|
370 break; |
|
371 } |
2663
|
372 |
|
373 // No float or long double conversions, thanks. |
|
374 *buf << 'l'; |
|
375 |
2117
|
376 goto fini; |
|
377 |
|
378 case 'c': case 's': case 'p': case '%': case '[': |
|
379 if (modifier != '\0') |
|
380 { |
|
381 nconv = -1; |
|
382 break; |
|
383 } |
|
384 goto fini; |
|
385 |
|
386 fini: |
|
387 { |
2215
|
388 if (finish_conversion (s, i, n, width, discard, type, |
2117
|
389 modifier, num_elts) == 0) |
|
390 return; |
|
391 } |
|
392 break; |
|
393 |
|
394 default: |
|
395 nconv = -1; |
|
396 break; |
|
397 } |
|
398 |
|
399 if (nconv < 0) |
|
400 break; |
|
401 } |
|
402 |
|
403 nconv = -1; |
|
404 } |
|
405 |
|
406 int |
3523
|
407 scanf_format_list::finish_conversion (const std::string& s, int& i, int n, |
2215
|
408 int& width, bool discard, char& type, |
2117
|
409 char modifier, int& num_elts) |
|
410 { |
|
411 int retval = 0; |
|
412 |
3523
|
413 std::string char_class; |
3483
|
414 |
3640
|
415 int beg_idx = -1; |
|
416 int end_idx = -1; |
|
417 |
2117
|
418 if (s[i] == '%') |
3640
|
419 { |
|
420 type = '%'; |
|
421 *buf << s[i++]; |
|
422 } |
2117
|
423 else |
|
424 { |
|
425 type = s[i]; |
|
426 |
|
427 if (s[i] == '[') |
|
428 { |
|
429 *buf << s[i++]; |
|
430 |
|
431 if (i < n) |
|
432 { |
3483
|
433 beg_idx = i; |
|
434 |
2117
|
435 if (s[i] == '^') |
|
436 { |
|
437 type = '^'; |
|
438 *buf << s[i++]; |
3483
|
439 |
|
440 if (i < n) |
|
441 { |
|
442 beg_idx = i; |
|
443 |
|
444 if (s[i] == ']') |
|
445 *buf << s[i++]; |
|
446 } |
2117
|
447 } |
|
448 else if (s[i] == ']') |
|
449 *buf << s[i++]; |
|
450 } |
|
451 |
|
452 while (i < n && s[i] != ']') |
|
453 *buf << s[i++]; |
|
454 |
|
455 if (i < n && s[i] == ']') |
3483
|
456 { |
|
457 end_idx = i-1; |
|
458 *buf << s[i++]; |
|
459 } |
2117
|
460 |
|
461 if (s[i-1] != ']') |
|
462 retval = nconv = -1; |
|
463 } |
|
464 else |
2215
|
465 *buf << s[i++]; |
3640
|
466 } |
|
467 |
|
468 nconv++; |
|
469 |
|
470 if (nconv > 0) |
|
471 { |
|
472 if (beg_idx >= 0 && end_idx >= 0) |
|
473 char_class = expand_char_class (s.substr (beg_idx, |
|
474 end_idx - beg_idx + 1)); |
|
475 |
|
476 add_elt_to_list (width, discard, type, modifier, num_elts, char_class); |
2117
|
477 } |
|
478 |
|
479 return retval; |
|
480 } |
|
481 |
|
482 void |
|
483 scanf_format_list::printme (void) const |
|
484 { |
|
485 int n = list.length (); |
|
486 |
|
487 for (int i = 0; i < n; i++) |
|
488 { |
3340
|
489 scanf_format_elt *elt = list(i); |
2117
|
490 |
3531
|
491 std::cerr |
|
492 << "width: " << elt->width << "\n" |
|
493 << "discard: " << elt->discard << "\n" |
|
494 << "type: "; |
3483
|
495 |
|
496 if (elt->type == scanf_format_elt::literal_conversion) |
3531
|
497 std::cerr << "literal text\n"; |
3483
|
498 else if (elt->type == scanf_format_elt::whitespace_conversion) |
3531
|
499 std::cerr << "whitespace\n"; |
3483
|
500 else |
3531
|
501 std::cerr << elt->type << "\n"; |
|
502 |
|
503 std::cerr |
|
504 << "modifier: " << elt->modifier << "\n" |
|
505 << "char_class: `" << undo_string_escapes (elt->char_class) << "'\n" |
|
506 << "text: `" << undo_string_escapes (elt->text) << "'\n\n"; |
2117
|
507 } |
|
508 } |
|
509 |
|
510 bool |
|
511 scanf_format_list::all_character_conversions (void) |
|
512 { |
|
513 int n = list.length (); |
|
514 |
|
515 if (n > 0) |
|
516 { |
|
517 for (int i = 0; i < n; i++) |
|
518 { |
3340
|
519 scanf_format_elt *elt = list(i); |
2117
|
520 |
|
521 switch (elt->type) |
|
522 { |
3483
|
523 case 'c': case 's': case '%': case '[': case '^': |
|
524 case scanf_format_elt::literal_conversion: |
|
525 case scanf_format_elt::whitespace_conversion: |
2117
|
526 break; |
|
527 |
|
528 default: |
|
529 return false; |
|
530 break; |
|
531 } |
|
532 } |
|
533 |
|
534 return true; |
|
535 } |
|
536 else |
|
537 return false; |
|
538 } |
|
539 |
|
540 bool |
|
541 scanf_format_list::all_numeric_conversions (void) |
|
542 { |
|
543 int n = list.length (); |
|
544 |
|
545 if (n > 0) |
|
546 { |
|
547 for (int i = 0; i < n; i++) |
|
548 { |
3340
|
549 scanf_format_elt *elt = list(i); |
2117
|
550 |
|
551 switch (elt->type) |
|
552 { |
|
553 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
554 case 'e': case 'f': case 'g': |
|
555 break; |
|
556 |
|
557 default: |
|
558 return false; |
|
559 break; |
|
560 } |
|
561 } |
|
562 |
|
563 return true; |
|
564 } |
|
565 else |
|
566 return false; |
|
567 } |
|
568 |
|
569 // Ugh again. |
|
570 |
3523
|
571 printf_format_list::printf_format_list (const std::string& s) |
2117
|
572 : nconv (0), curr_idx (0), list (16), buf (0) |
|
573 { |
|
574 int num_elts = 0; |
|
575 |
|
576 int n = s.length (); |
|
577 |
|
578 int i = 0; |
|
579 |
|
580 int args = 0; |
3643
|
581 std::string flags; |
3640
|
582 int fw = 0; |
|
583 int prec = 0; |
2117
|
584 char modifier = '\0'; |
|
585 char type = '\0'; |
|
586 |
|
587 bool have_more = true; |
3640
|
588 bool empty_buf = true; |
2117
|
589 |
4223
|
590 if (n == 0) |
2117
|
591 { |
4223
|
592 printf_format_elt *elt |
|
593 = new printf_format_elt ("", args, fw, prec, flags, type, modifier); |
|
594 |
|
595 list(num_elts++) = elt; |
|
596 |
|
597 list.resize (num_elts); |
|
598 } |
|
599 else |
|
600 { |
|
601 while (i < n) |
3640
|
602 { |
4223
|
603 have_more = true; |
|
604 |
|
605 if (! buf) |
|
606 { |
|
607 buf = new OSSTREAM (); |
|
608 empty_buf = true; |
|
609 } |
|
610 |
|
611 switch (s[i]) |
|
612 { |
|
613 case '%': |
3640
|
614 { |
4223
|
615 if (empty_buf) |
|
616 { |
|
617 process_conversion (s, i, n, args, flags, fw, prec, |
|
618 type, modifier, num_elts); |
|
619 |
|
620 have_more = (buf != 0); |
|
621 } |
|
622 else |
|
623 add_elt_to_list (args, flags, fw, prec, type, modifier, |
|
624 num_elts); |
3640
|
625 } |
4223
|
626 break; |
|
627 |
|
628 default: |
|
629 { |
|
630 args = 0; |
|
631 flags = ""; |
|
632 fw = 0; |
|
633 prec = 0; |
|
634 modifier = '\0'; |
|
635 type = '\0'; |
|
636 *buf << s[i++]; |
|
637 empty_buf = false; |
|
638 } |
|
639 break; |
|
640 } |
|
641 |
|
642 if (nconv < 0) |
|
643 { |
|
644 have_more = false; |
|
645 break; |
|
646 } |
2117
|
647 } |
|
648 |
4223
|
649 if (have_more) |
|
650 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); |
|
651 |
|
652 list.resize (num_elts); |
|
653 |
|
654 delete buf; |
2117
|
655 } |
|
656 } |
|
657 |
|
658 printf_format_list::~printf_format_list (void) |
|
659 { |
|
660 int n = list.length (); |
|
661 |
|
662 for (int i = 0; i < n; i++) |
|
663 { |
3340
|
664 printf_format_elt *elt = list(i); |
2117
|
665 delete elt; |
|
666 } |
|
667 } |
|
668 |
|
669 void |
3640
|
670 printf_format_list::add_elt_to_list (int args, const std::string& flags, |
|
671 int fw, int prec, char type, |
|
672 char modifier, int& num_elts) |
2117
|
673 { |
|
674 if (buf) |
|
675 { |
4051
|
676 *buf << OSSTREAM_ENDS; |
|
677 |
|
678 std::string text = OSSTREAM_STR (*buf); |
|
679 |
|
680 OSSTREAM_FREEZE (*buf); |
|
681 |
|
682 if (! text.empty ()) |
2117
|
683 { |
4051
|
684 printf_format_elt *elt |
|
685 = new printf_format_elt (text.c_str (), args, fw, prec, flags, |
|
686 type, modifier); |
|
687 |
|
688 if (num_elts == list.length ()) |
|
689 list.resize (2 * num_elts); |
|
690 |
|
691 list(num_elts++) = elt; |
2117
|
692 } |
|
693 |
|
694 delete buf; |
|
695 buf = 0; |
|
696 } |
|
697 } |
|
698 |
|
699 void |
3640
|
700 printf_format_list::process_conversion |
|
701 (const std::string& s, int& i, int n, int& args, std::string& flags, |
|
702 int& fw, int& prec, char& modifier, char& type, int& num_elts) |
2117
|
703 { |
|
704 args = 0; |
3640
|
705 flags = ""; |
|
706 fw = 0; |
|
707 prec = 0; |
2117
|
708 modifier = '\0'; |
|
709 type = '\0'; |
|
710 |
|
711 *buf << s[i++]; |
|
712 |
4587
|
713 bool nxt = false; |
2117
|
714 |
|
715 while (i < n) |
|
716 { |
|
717 switch (s[i]) |
|
718 { |
|
719 case '-': case '+': case ' ': case '0': case '#': |
3640
|
720 flags += s[i]; |
2117
|
721 *buf << s[i++]; |
|
722 break; |
|
723 |
|
724 default: |
4587
|
725 nxt = true; |
2117
|
726 break; |
|
727 } |
|
728 |
4587
|
729 if (nxt) |
2117
|
730 break; |
|
731 } |
|
732 |
|
733 if (i < n) |
|
734 { |
|
735 if (s[i] == '*') |
|
736 { |
3640
|
737 fw = -1; |
2117
|
738 args++; |
|
739 *buf << s[i++]; |
|
740 } |
|
741 else |
|
742 { |
3640
|
743 if (isdigit (s[i])) |
|
744 { |
4587
|
745 int nn = 0; |
3643
|
746 std::string tmp = s.substr (i); |
4587
|
747 sscanf (tmp.c_str (), "%d%n", &fw, &nn); |
3640
|
748 } |
|
749 |
2117
|
750 while (i < n && isdigit (s[i])) |
|
751 *buf << s[i++]; |
|
752 } |
|
753 } |
|
754 |
|
755 if (i < n && s[i] == '.') |
|
756 { |
|
757 *buf << s[i++]; |
|
758 |
|
759 if (i < n) |
|
760 { |
|
761 if (s[i] == '*') |
|
762 { |
3640
|
763 prec = -1; |
2117
|
764 args++; |
|
765 *buf << s[i++]; |
|
766 } |
|
767 else |
|
768 { |
3640
|
769 if (isdigit (s[i])) |
|
770 { |
4587
|
771 int nn = 0; |
3643
|
772 std::string tmp = s.substr (i); |
4587
|
773 sscanf (tmp.c_str (), "%d%n", &prec, &nn); |
3640
|
774 } |
|
775 |
2117
|
776 while (i < n && isdigit (s[i])) |
|
777 *buf << s[i++]; |
|
778 } |
|
779 } |
|
780 } |
|
781 |
|
782 if (i < n) |
|
783 { |
|
784 switch (s[i]) |
|
785 { |
|
786 case 'h': case 'l': case 'L': |
|
787 modifier = s[i]; |
|
788 *buf << s[i++]; |
|
789 break; |
|
790 |
|
791 default: |
|
792 break; |
|
793 } |
|
794 } |
|
795 |
|
796 if (i < n) |
3640
|
797 finish_conversion (s, i, args, flags, fw, prec, modifier, type, num_elts); |
2117
|
798 else |
|
799 nconv = -1; |
|
800 } |
|
801 |
|
802 void |
3640
|
803 printf_format_list::finish_conversion |
|
804 (const std::string& s, int& i, int args, const std::string& flags, |
|
805 int fw, int prec, char modifier, char& type, int& num_elts) |
2117
|
806 |
|
807 { |
|
808 switch (s[i]) |
|
809 { |
|
810 case 'd': case 'i': case 'o': case 'x': case 'X': |
|
811 case 'u': case 'c': |
|
812 if (modifier == 'L') |
|
813 { |
|
814 nconv = -1; |
|
815 break; |
|
816 } |
|
817 goto fini; |
|
818 |
|
819 case 'f': case 'e': case 'E': case 'g': case 'G': |
|
820 if (modifier == 'h' || modifier == 'l') |
|
821 { |
|
822 nconv = -1; |
|
823 break; |
|
824 } |
|
825 goto fini; |
|
826 |
|
827 case 's': case 'p': case '%': |
|
828 if (modifier != '\0') |
|
829 { |
|
830 nconv = -1; |
|
831 break; |
|
832 } |
|
833 goto fini; |
|
834 |
|
835 fini: |
|
836 |
3640
|
837 type = s[i]; |
|
838 |
|
839 *buf << s[i++]; |
|
840 |
|
841 if (type != '%' || args != 0) |
|
842 nconv++; |
|
843 |
|
844 if (type != '%') |
|
845 args++; |
|
846 |
|
847 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); |
|
848 |
2117
|
849 break; |
|
850 |
|
851 default: |
|
852 nconv = -1; |
|
853 break; |
|
854 } |
|
855 } |
|
856 |
|
857 void |
|
858 printf_format_list::printme (void) const |
|
859 { |
|
860 int n = list.length (); |
|
861 |
|
862 for (int i = 0; i < n; i++) |
|
863 { |
3340
|
864 printf_format_elt *elt = list(i); |
2117
|
865 |
3640
|
866 std::cerr |
|
867 << "args: " << elt->args << "\n" |
|
868 << "flags: `" << elt->flags << "'\n" |
|
869 << "width: " << elt->fw << "\n" |
|
870 << "prec: " << elt->prec << "\n" |
|
871 << "type: `" << elt->type << "'\n" |
|
872 << "modifier: `" << elt->modifier << "'\n" |
|
873 << "text: `" << undo_string_escapes (elt->text) << "'\n\n"; |
2117
|
874 } |
|
875 } |
|
876 |
3145
|
877 int |
3148
|
878 octave_base_stream::file_number (void) |
3145
|
879 { |
|
880 // Kluge alert! |
|
881 |
|
882 if (name () == "stdin") |
|
883 return 0; |
|
884 |
|
885 if (name () == "stdout") |
|
886 return 1; |
|
887 |
|
888 if (name () == "stderr") |
|
889 return 2; |
|
890 |
|
891 int retval = -1; |
|
892 |
3523
|
893 std::istream *is = input_stream (); |
|
894 std::ostream *os = output_stream (); |
3145
|
895 |
3775
|
896 // There is no standard way to get the underlying file descriptor from |
|
897 // std::filebuf (nor in the GNU libstdc++-v3 implementation). We cache |
|
898 // the descriptor in c_file_ptr_buf, and then extract it here. |
|
899 |
|
900 c_file_ptr_buf *ibuf = is ? |
|
901 dynamic_cast<c_file_ptr_buf *> (is->rdbuf ()) : 0; |
|
902 c_file_ptr_buf *obuf = os ? |
|
903 dynamic_cast<c_file_ptr_buf *> (os->rdbuf ()) : 0; |
|
904 |
|
905 int i_fid = ibuf ? ibuf->file_number () : -1; |
|
906 int o_fid = obuf ? obuf->file_number () : -1; |
3145
|
907 |
|
908 if (i_fid >= 0) |
|
909 { |
|
910 if (o_fid >= 0) |
|
911 retval = (i_fid == o_fid) ? i_fid : -1; |
|
912 else |
|
913 retval = i_fid; |
|
914 } |
|
915 else if (o_fid >= 0) |
|
916 retval = o_fid; |
|
917 |
|
918 return retval; |
|
919 } |
|
920 |
2117
|
921 void |
3523
|
922 octave_base_stream::error (const std::string& msg) |
2117
|
923 { |
|
924 fail = true; |
|
925 errmsg = msg; |
|
926 } |
|
927 |
|
928 void |
4468
|
929 octave_base_stream::error (const std::string& who, const std::string& msg) |
|
930 { |
|
931 fail = true; |
|
932 errmsg = who + msg; |
|
933 } |
|
934 |
|
935 void |
2117
|
936 octave_base_stream::clear (void) |
|
937 { |
|
938 fail = false; |
|
939 errmsg = ""; |
|
940 } |
|
941 |
|
942 // Functions that are defined for all input streams (input streams |
|
943 // are those that define is). |
|
944 |
3536
|
945 std::string |
2117
|
946 octave_base_stream::do_gets (int max_len, bool& err, |
4468
|
947 bool strip_newline, const std::string& who) |
2117
|
948 { |
3523
|
949 std::string retval; |
2117
|
950 |
|
951 err = false; |
|
952 |
3523
|
953 std::istream *isp = input_stream (); |
2117
|
954 |
|
955 if (isp) |
|
956 { |
3523
|
957 std::istream& is = *isp; |
2117
|
958 |
4051
|
959 OSSTREAM buf; |
2117
|
960 |
|
961 int c = 0; |
3553
|
962 int char_count = 0; |
2117
|
963 int newline_stripped = 0; |
|
964 |
|
965 while (is && (c = is.get ()) != EOF) |
|
966 { |
3553
|
967 char_count++; |
2117
|
968 |
|
969 if (c == '\n') |
|
970 { |
|
971 if (! strip_newline) |
|
972 buf << (char) c; |
|
973 else |
|
974 newline_stripped = 1; |
|
975 |
|
976 break; |
|
977 } |
|
978 else |
|
979 buf << (char) c; |
|
980 |
3553
|
981 if (max_len > 0 && char_count == max_len) |
2117
|
982 break; |
|
983 } |
|
984 |
4224
|
985 if (is.good () || (is.eof () && char_count > 0)) |
2117
|
986 { |
4051
|
987 buf << OSSTREAM_ENDS; |
|
988 retval = OSSTREAM_STR (buf); |
|
989 OSSTREAM_FREEZE (buf); |
2117
|
990 } |
4224
|
991 else |
|
992 { |
|
993 err = true; |
4468
|
994 |
4224
|
995 if (is.eof () && char_count == 0) |
4468
|
996 error (who, "at end of file"); |
4224
|
997 else |
4468
|
998 error (who, "read error"); |
4224
|
999 } |
2117
|
1000 } |
|
1001 else |
|
1002 { |
|
1003 err = true; |
4468
|
1004 invalid_operation (who, "reading"); |
2117
|
1005 } |
|
1006 |
|
1007 return retval; |
|
1008 } |
|
1009 |
3536
|
1010 std::string |
4468
|
1011 octave_base_stream::getl (int max_len, bool& err, const std::string& who) |
2117
|
1012 { |
4468
|
1013 return do_gets (max_len, err, true, who); |
2117
|
1014 } |
|
1015 |
3536
|
1016 std::string |
4468
|
1017 octave_base_stream::gets (int max_len, bool& err, const std::string& who) |
2117
|
1018 { |
4468
|
1019 return do_gets (max_len, err, false, who); |
2117
|
1020 } |
|
1021 |
|
1022 octave_value |
3810
|
1023 octave_base_stream::read (const Array<double>& size, |
2317
|
1024 oct_data_conv::data_type dt, int skip, |
3553
|
1025 oct_mach_info::float_format ffmt, |
|
1026 int& char_count) |
2117
|
1027 { |
2317
|
1028 Matrix retval; |
|
1029 |
3553
|
1030 char_count = 0; |
2117
|
1031 |
3523
|
1032 std::istream *isp = input_stream (); |
2117
|
1033 |
|
1034 if (isp) |
|
1035 { |
3523
|
1036 std::istream& is = *isp; |
2117
|
1037 |
|
1038 int nr = -1; |
|
1039 int nc = -1; |
|
1040 |
3268
|
1041 bool ignore; |
|
1042 |
|
1043 get_size (size, nr, nc, ignore, "fread"); |
2117
|
1044 |
|
1045 if (! error_state) |
2317
|
1046 { |
4574
|
1047 if (ffmt == oct_mach_info::flt_fmt_unknown) |
3553
|
1048 ffmt = float_format (); |
|
1049 |
|
1050 int tmp = retval.read (is, nr, nc, dt, skip, ffmt); |
2317
|
1051 |
|
1052 if (tmp < 0) |
|
1053 error ("fread: read error"); |
|
1054 else |
3553
|
1055 char_count = tmp; |
2317
|
1056 } |
2117
|
1057 } |
|
1058 else |
|
1059 invalid_operation ("fread", "reading"); |
|
1060 |
|
1061 return retval; |
|
1062 } |
|
1063 |
3779
|
1064 #if defined (__GNUG__) && ! defined (CXX_ISO_COMPLIANT_LIBRARY) |
3636
|
1065 |
3640
|
1066 #define OCTAVE_SCAN(is, fmt, arg) is.scan ((fmt).text, arg) |
3636
|
1067 |
|
1068 #else |
|
1069 |
3640
|
1070 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) |
3636
|
1071 |
3779
|
1072 // XXX FIXME XXX -- this needs to be fixed to handle formats which |
|
1073 // specify a maximum width. |
|
1074 |
3636
|
1075 template <class T> |
|
1076 std::istream& |
3779
|
1077 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) |
3636
|
1078 { |
3779
|
1079 T& ref = *valptr; |
|
1080 |
|
1081 switch (fmt.type) |
|
1082 { |
|
1083 case 'o': |
|
1084 is >> std::oct >> ref; |
|
1085 break; |
|
1086 |
|
1087 case 'x': |
|
1088 is >> std::hex >> ref; |
|
1089 break; |
|
1090 |
|
1091 default: |
|
1092 is >> ref; |
|
1093 break; |
|
1094 } |
3639
|
1095 |
3638
|
1096 return is; |
3636
|
1097 } |
|
1098 |
3779
|
1099 // Note that this specialization is only used for reading characters, not |
|
1100 // character strings. See BEGIN_S_CONVERSION for details. |
|
1101 |
|
1102 template<> |
|
1103 std::istream& |
4661
|
1104 octave_scan<> (std::istream& is, const scanf_format_elt& /* fmt */, |
|
1105 char* valptr) |
3779
|
1106 { |
|
1107 return is >> valptr; |
|
1108 } |
3636
|
1109 |
4595
|
1110 template std::istream& |
|
1111 octave_scan (std::istream&, const scanf_format_elt&, int*); |
|
1112 |
|
1113 template std::istream& |
|
1114 octave_scan (std::istream&, const scanf_format_elt&, long int*); |
|
1115 |
|
1116 template std::istream& |
|
1117 octave_scan (std::istream&, const scanf_format_elt&, short int*); |
|
1118 |
|
1119 template std::istream& |
|
1120 octave_scan (std::istream&, const scanf_format_elt&, unsigned int*); |
|
1121 |
|
1122 template std::istream& |
|
1123 octave_scan (std::istream&, const scanf_format_elt&, unsigned long int*); |
|
1124 |
|
1125 template std::istream& |
|
1126 octave_scan (std::istream&, const scanf_format_elt&, unsigned short int*); |
|
1127 |
|
1128 #if 0 |
|
1129 template std::istream& |
|
1130 octave_scan (std::istream&, const scanf_format_elt&, float*); |
|
1131 #endif |
|
1132 |
|
1133 template std::istream& |
|
1134 octave_scan (std::istream&, const scanf_format_elt&, double*); |
|
1135 |
3636
|
1136 #endif |
|
1137 |
2572
|
1138 template <class T> |
|
1139 void |
3636
|
1140 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt, |
|
1141 T valptr, Matrix& mval, double *data, int& idx, |
|
1142 int& conversion_count, int nr, int max_size, |
|
1143 bool discard) |
2572
|
1144 { |
3640
|
1145 OCTAVE_SCAN (is, fmt, valptr); |
2572
|
1146 |
|
1147 if (is) |
|
1148 { |
|
1149 if (idx == max_size && ! discard) |
|
1150 { |
|
1151 max_size *= 2; |
|
1152 |
|
1153 if (nr > 0) |
|
1154 mval.resize (nr, max_size / nr, 0.0); |
|
1155 else |
|
1156 mval.resize (max_size, 1, 0.0); |
|
1157 |
|
1158 data = mval.fortran_vec (); |
|
1159 } |
|
1160 |
|
1161 if (! discard) |
3268
|
1162 { |
3559
|
1163 conversion_count++; |
3268
|
1164 data[idx++] = *(valptr); |
|
1165 } |
2572
|
1166 } |
|
1167 } |
|
1168 |
|
1169 template void |
3878
|
1170 do_scanf_conv (std::istream&, const scanf_format_elt&, int*, |
|
1171 Matrix&, double*, int&, int&, int, int, bool); |
2572
|
1172 |
3233
|
1173 template void |
3636
|
1174 do_scanf_conv (std::istream&, const scanf_format_elt&, long int*, |
|
1175 Matrix&, double*, int&, int&, int, int, bool); |
3233
|
1176 |
|
1177 template void |
3636
|
1178 do_scanf_conv (std::istream&, const scanf_format_elt&, short int*, |
|
1179 Matrix&, double*, int&, int&, int, int, bool); |
3233
|
1180 |
3878
|
1181 template void |
|
1182 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned int*, |
|
1183 Matrix&, double*, int&, int&, int, int, bool); |
|
1184 |
|
1185 template void |
|
1186 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned long int*, |
|
1187 Matrix&, double*, int&, int&, int, int, bool); |
|
1188 |
|
1189 template void |
|
1190 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned short int*, |
|
1191 Matrix&, double*, int&, int&, int, int, bool); |
|
1192 |
2600
|
1193 #if 0 |
2572
|
1194 template void |
3636
|
1195 do_scanf_conv (std::istream&, const scanf_format_elt&, float*, |
|
1196 Matrix&, double*, int&, int&, int, int, bool); |
2600
|
1197 #endif |
2572
|
1198 |
|
1199 template void |
3636
|
1200 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
|
1201 Matrix&, double*, int&, int&, int, int, bool); |
2572
|
1202 |
3483
|
1203 #define DO_WHITESPACE_CONVERSION() \ |
|
1204 do \ |
|
1205 { \ |
|
1206 int c = EOF; \ |
|
1207 \ |
|
1208 while (is && (c = is.get ()) != EOF && isspace (c)) \ |
|
1209 /* skip whitespace */; \ |
|
1210 \ |
|
1211 if (c != EOF) \ |
|
1212 is.putback (c); \ |
|
1213 } \ |
|
1214 while (0) |
|
1215 |
|
1216 #define DO_LITERAL_CONVERSION() \ |
|
1217 do \ |
|
1218 { \ |
|
1219 int c = EOF; \ |
|
1220 \ |
|
1221 int n = strlen (fmt); \ |
|
1222 int i = 0; \ |
|
1223 \ |
|
1224 while (i < n && is && (c = is.get ()) != EOF) \ |
|
1225 { \ |
|
1226 if (c == fmt[i]) \ |
|
1227 { \ |
|
1228 i++; \ |
|
1229 continue; \ |
|
1230 } \ |
|
1231 else \ |
|
1232 { \ |
|
1233 is.putback (c); \ |
|
1234 break; \ |
|
1235 } \ |
|
1236 } \ |
|
1237 \ |
|
1238 if (i != n) \ |
3538
|
1239 is.setstate (std::ios::failbit); \ |
3483
|
1240 } \ |
|
1241 while (0) |
|
1242 |
3640
|
1243 #define DO_PCT_CONVERSION() \ |
|
1244 do \ |
|
1245 { \ |
|
1246 int c = is.get (); \ |
|
1247 \ |
|
1248 if (c != EOF) \ |
|
1249 { \ |
|
1250 if (c != '%') \ |
|
1251 { \ |
|
1252 is.putback (c); \ |
|
1253 is.setstate (std::ios::failbit); \ |
|
1254 } \ |
|
1255 } \ |
|
1256 else \ |
|
1257 is.setstate (std::ios::failbit); \ |
|
1258 } \ |
|
1259 while (0) |
|
1260 |
3483
|
1261 #define BEGIN_C_CONVERSION() \ |
3538
|
1262 is.unsetf (std::ios::skipws); \ |
3483
|
1263 \ |
|
1264 int width = elt->width ? elt->width : 1; \ |
|
1265 \ |
4051
|
1266 char *tbuf = new char[width + 1]; \ |
3483
|
1267 \ |
|
1268 int c = EOF; \ |
|
1269 int n = 0; \ |
|
1270 \ |
|
1271 while (is && n < width && (c = is.get ()) != EOF) \ |
4051
|
1272 tbuf[n++] = (char) c; \ |
|
1273 \ |
|
1274 tbuf[n] = '\0'; \ |
3483
|
1275 \ |
4051
|
1276 std::string tmp = tbuf; \ |
|
1277 \ |
|
1278 delete [] tbuf |
3483
|
1279 |
|
1280 // For a `%s' format, skip initial whitespace and then read until the |
|
1281 // next whitespace character. |
|
1282 #define BEGIN_S_CONVERSION() \ |
|
1283 int width = elt->width; \ |
|
1284 \ |
4051
|
1285 std::string tmp; \ |
3483
|
1286 \ |
|
1287 do \ |
|
1288 { \ |
|
1289 if (width) \ |
|
1290 { \ |
4051
|
1291 char *tbuf = new char [width+1]; \ |
|
1292 \ |
|
1293 OCTAVE_SCAN (is, *elt, tbuf); \ |
3483
|
1294 \ |
4051
|
1295 tbuf[width] = '\0'; \ |
|
1296 tmp = tbuf; \ |
|
1297 delete [] tbuf; \ |
3483
|
1298 } \ |
|
1299 else \ |
|
1300 { \ |
4051
|
1301 is >> std::ws >> tmp; \ |
3483
|
1302 } \ |
|
1303 } \ |
|
1304 while (0) |
|
1305 |
|
1306 // This format must match a nonempty sequence of characters. |
|
1307 #define BEGIN_CHAR_CLASS_CONVERSION() \ |
|
1308 int width = elt->width; \ |
|
1309 \ |
4051
|
1310 std::string tmp; \ |
3483
|
1311 \ |
|
1312 do \ |
|
1313 { \ |
|
1314 if (width) \ |
|
1315 { \ |
4051
|
1316 char *tbuf = new char[width+1]; \ |
|
1317 \ |
|
1318 OCTAVE_SCAN (is, *elt, tbuf); \ |
3483
|
1319 \ |
4051
|
1320 tbuf[width] = '\0'; \ |
|
1321 tmp = tbuf; \ |
|
1322 delete [] tbuf; \ |
3483
|
1323 } \ |
|
1324 else \ |
|
1325 { \ |
4051
|
1326 OSSTREAM buf; \ |
3483
|
1327 \ |
3523
|
1328 std::string char_class = elt->char_class; \ |
3483
|
1329 \ |
|
1330 int c = EOF; \ |
|
1331 \ |
|
1332 if (elt->type == '[') \ |
|
1333 { \ |
|
1334 while (is && (c = is.get ()) != EOF \ |
|
1335 && char_class.find (c) != NPOS) \ |
|
1336 buf << (char) c; \ |
|
1337 } \ |
|
1338 else \ |
|
1339 { \ |
|
1340 while (is && (c = is.get ()) != EOF \ |
|
1341 && char_class.find (c) == NPOS) \ |
|
1342 buf << (char) c; \ |
|
1343 } \ |
|
1344 \ |
|
1345 if (c != EOF) \ |
|
1346 is.putback (c); \ |
|
1347 \ |
4051
|
1348 buf << OSSTREAM_ENDS; \ |
|
1349 tmp = OSSTREAM_STR (buf); \ |
|
1350 OSSTREAM_FREEZE (buf); \ |
3483
|
1351 \ |
4051
|
1352 if (tmp.empty ()) \ |
3538
|
1353 is.setstate (std::ios::failbit); \ |
3483
|
1354 } \ |
|
1355 } \ |
|
1356 while (0) |
|
1357 |
3410
|
1358 #define FINISH_CHARACTER_CONVERSION() \ |
|
1359 do \ |
|
1360 { \ |
4051
|
1361 width = tmp.length (); \ |
3410
|
1362 \ |
|
1363 if (is) \ |
|
1364 { \ |
|
1365 int i = 0; \ |
|
1366 \ |
|
1367 if (! discard) \ |
|
1368 { \ |
|
1369 conversion_count++; \ |
|
1370 \ |
|
1371 while (i < width && tmp[i] != '\0') \ |
|
1372 { \ |
|
1373 if (data_index == max_size) \ |
|
1374 { \ |
|
1375 max_size *= 2; \ |
|
1376 \ |
4420
|
1377 if (all_char_conv) \ |
3410
|
1378 { \ |
4420
|
1379 if (one_elt_size_spec) \ |
3410
|
1380 mval.resize (1, max_size, 0.0); \ |
4420
|
1381 else if (nr > 0) \ |
|
1382 mval.resize (nr, max_size / nr, 0.0); \ |
3410
|
1383 else \ |
4420
|
1384 panic_impossible (); \ |
3410
|
1385 } \ |
4420
|
1386 else if (nr > 0) \ |
|
1387 { \ |
|
1388 if (nc <= 0) \ |
|
1389 mval.resize (nr, max_size / nr, 0.0); \ |
|
1390 else \ |
|
1391 panic_impossible (); \ |
|
1392 } \ |
|
1393 else \ |
|
1394 mval.resize (max_size, 1, 0.0); \ |
3410
|
1395 \ |
|
1396 data = mval.fortran_vec (); \ |
|
1397 } \ |
|
1398 \ |
|
1399 data[data_index++] = tmp[i++]; \ |
|
1400 } \ |
|
1401 } \ |
|
1402 } \ |
|
1403 } \ |
|
1404 while (0) |
2117
|
1405 |
|
1406 octave_value |
|
1407 octave_base_stream::do_scanf (scanf_format_list& fmt_list, |
3268
|
1408 int nr, int nc, bool one_elt_size_spec, |
4468
|
1409 int& conversion_count, const std::string& who) |
2117
|
1410 { |
3268
|
1411 conversion_count = 0; |
|
1412 |
3640
|
1413 int nconv = fmt_list.num_conversions (); |
|
1414 |
3268
|
1415 int data_index = 0; |
2121
|
1416 |
2117
|
1417 octave_value retval = Matrix (); |
|
1418 |
3268
|
1419 if (nr == 0 || nc == 0) |
|
1420 { |
|
1421 if (one_elt_size_spec) |
|
1422 nc = 0; |
|
1423 |
|
1424 return Matrix (nr, nc, 0.0); |
|
1425 } |
|
1426 |
3523
|
1427 std::istream *isp = input_stream (); |
2117
|
1428 |
|
1429 bool all_char_conv = fmt_list.all_character_conversions (); |
|
1430 |
|
1431 Matrix mval; |
|
1432 double *data = 0; |
|
1433 int max_size = 0; |
3268
|
1434 int max_conv = 0; |
2117
|
1435 |
|
1436 int final_nr = 0; |
|
1437 int final_nc = 0; |
|
1438 |
3268
|
1439 if (all_char_conv) |
|
1440 { |
4420
|
1441 // Any of these could be resized later (if we have %s |
|
1442 // conversions, we may read more than one element for each |
|
1443 // conversion). |
|
1444 |
3268
|
1445 if (one_elt_size_spec) |
|
1446 { |
3410
|
1447 max_size = 512; |
|
1448 mval.resize (1, max_size, 0.0); |
|
1449 |
3268
|
1450 if (nr > 0) |
|
1451 max_conv = nr; |
|
1452 } |
4420
|
1453 else if (nr > 0) |
3268
|
1454 { |
4420
|
1455 if (nc > 0) |
|
1456 { |
|
1457 mval.resize (nr, nc, 0.0); |
|
1458 max_size = max_conv = nr * nc; |
|
1459 } |
|
1460 else |
|
1461 { |
|
1462 mval.resize (nr, 32, 0.0); |
|
1463 max_size = nr * 32; |
|
1464 } |
3268
|
1465 } |
4420
|
1466 else |
|
1467 panic_impossible (); |
3268
|
1468 } |
|
1469 else if (nr > 0) |
2117
|
1470 { |
|
1471 if (nc > 0) |
|
1472 { |
4420
|
1473 // Will not resize later. |
2117
|
1474 mval.resize (nr, nc, 0.0); |
|
1475 max_size = nr * nc; |
3268
|
1476 max_conv = max_size; |
2117
|
1477 } |
|
1478 else |
|
1479 { |
4420
|
1480 // Maybe resize later. |
2117
|
1481 mval.resize (nr, 32, 0.0); |
2121
|
1482 max_size = nr * 32; |
2117
|
1483 } |
|
1484 } |
|
1485 else |
|
1486 { |
4420
|
1487 // Maybe resize later. |
2117
|
1488 mval.resize (32, 1, 0.0); |
|
1489 max_size = 32; |
|
1490 } |
|
1491 |
4420
|
1492 data = mval.fortran_vec (); |
|
1493 |
2117
|
1494 if (isp) |
|
1495 { |
3523
|
1496 std::istream& is = *isp; |
2117
|
1497 |
|
1498 const scanf_format_elt *elt = fmt_list.first (); |
|
1499 |
3538
|
1500 std::ios::fmtflags flags = is.flags (); |
2213
|
1501 |
2117
|
1502 for (;;) |
|
1503 { |
4153
|
1504 OCTAVE_QUIT; |
|
1505 |
2117
|
1506 if (elt) |
|
1507 { |
3268
|
1508 if (max_conv > 0 && conversion_count == max_conv) |
|
1509 { |
|
1510 if (all_char_conv && one_elt_size_spec) |
|
1511 { |
|
1512 final_nr = 1; |
|
1513 final_nc = data_index; |
|
1514 } |
|
1515 else |
|
1516 { |
|
1517 final_nr = nr; |
|
1518 final_nc = (data_index - 1) / nr + 1; |
|
1519 } |
|
1520 |
|
1521 break; |
|
1522 } |
|
1523 else if (data_index == max_size) |
2117
|
1524 { |
3410
|
1525 max_size *= 2; |
|
1526 |
4420
|
1527 if (all_char_conv) |
2687
|
1528 { |
4420
|
1529 if (one_elt_size_spec) |
3410
|
1530 mval.resize (1, max_size, 0.0); |
4420
|
1531 else if (nr > 0) |
|
1532 mval.resize (nr, max_size / nr, 0.0); |
3410
|
1533 else |
4420
|
1534 panic_impossible (); |
2687
|
1535 } |
4420
|
1536 else if (nr > 0) |
|
1537 { |
|
1538 if (nc <= 0) |
|
1539 mval.resize (nr, max_size / nr, 0.0); |
|
1540 else |
|
1541 panic_impossible (); |
|
1542 } |
|
1543 else |
|
1544 mval.resize (max_size, 1, 0.0); |
3410
|
1545 |
|
1546 data = mval.fortran_vec (); |
2117
|
1547 } |
|
1548 |
|
1549 const char *fmt = elt->text; |
|
1550 |
|
1551 bool discard = elt->discard; |
|
1552 |
|
1553 switch (elt->type) |
|
1554 { |
3483
|
1555 case scanf_format_elt::whitespace_conversion: |
|
1556 DO_WHITESPACE_CONVERSION (); |
|
1557 break; |
|
1558 |
|
1559 case scanf_format_elt::literal_conversion: |
|
1560 DO_LITERAL_CONVERSION (); |
|
1561 break; |
|
1562 |
2117
|
1563 case '%': |
3640
|
1564 DO_PCT_CONVERSION (); |
3483
|
1565 break; |
2117
|
1566 |
3878
|
1567 case 'd': case 'i': |
2117
|
1568 { |
3233
|
1569 switch (elt->modifier) |
|
1570 { |
|
1571 case 'h': |
|
1572 { |
|
1573 short int tmp; |
3779
|
1574 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1575 data_index, conversion_count, |
3233
|
1576 nr, max_size, discard); |
|
1577 } |
3483
|
1578 break; |
3233
|
1579 |
|
1580 case 'l': |
|
1581 { |
|
1582 long int tmp; |
3779
|
1583 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1584 data_index, conversion_count, |
3233
|
1585 nr, max_size, discard); |
|
1586 } |
3483
|
1587 break; |
3233
|
1588 |
|
1589 default: |
|
1590 { |
|
1591 int tmp; |
3779
|
1592 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1593 data_index, conversion_count, |
3233
|
1594 nr, max_size, discard); |
|
1595 } |
3483
|
1596 break; |
3233
|
1597 } |
2117
|
1598 } |
3483
|
1599 break; |
2117
|
1600 |
3878
|
1601 case 'o': case 'u': case 'x': |
|
1602 { |
|
1603 switch (elt->modifier) |
|
1604 { |
|
1605 case 'h': |
|
1606 { |
|
1607 unsigned short int tmp; |
|
1608 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1609 data_index, conversion_count, |
|
1610 nr, max_size, discard); |
|
1611 } |
|
1612 break; |
|
1613 |
|
1614 case 'l': |
|
1615 { |
|
1616 unsigned long int tmp; |
|
1617 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1618 data_index, conversion_count, |
|
1619 nr, max_size, discard); |
|
1620 } |
|
1621 break; |
|
1622 |
|
1623 default: |
|
1624 { |
|
1625 unsigned int tmp; |
|
1626 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1627 data_index, conversion_count, |
|
1628 nr, max_size, discard); |
|
1629 } |
|
1630 break; |
|
1631 } |
|
1632 } |
|
1633 break; |
|
1634 |
2117
|
1635 case 'e': case 'f': case 'g': |
|
1636 { |
2600
|
1637 double tmp; |
|
1638 |
3779
|
1639 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1640 data_index, conversion_count, |
2600
|
1641 nr, max_size, discard); |
2117
|
1642 } |
3483
|
1643 break; |
2117
|
1644 |
2213
|
1645 case 'c': |
3410
|
1646 { |
3483
|
1647 BEGIN_C_CONVERSION (); |
3410
|
1648 |
|
1649 FINISH_CHARACTER_CONVERSION (); |
3483
|
1650 |
|
1651 is.setf (flags); |
3410
|
1652 } |
|
1653 break; |
2213
|
1654 |
2117
|
1655 case 's': |
|
1656 { |
3483
|
1657 BEGIN_S_CONVERSION (); |
3268
|
1658 |
3410
|
1659 FINISH_CHARACTER_CONVERSION (); |
2117
|
1660 } |
3483
|
1661 break; |
|
1662 |
|
1663 case '[': case '^': |
|
1664 { |
|
1665 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1666 |
|
1667 FINISH_CHARACTER_CONVERSION (); |
|
1668 } |
|
1669 break; |
|
1670 |
|
1671 case 'p': |
4468
|
1672 error ("%s: unsupported format specifier", who.c_str ()); |
2117
|
1673 break; |
|
1674 |
|
1675 default: |
4468
|
1676 error ("%s: internal format error", who.c_str ()); |
2117
|
1677 break; |
|
1678 } |
|
1679 |
|
1680 if (! ok ()) |
|
1681 { |
|
1682 break; |
|
1683 } |
|
1684 else if (! is) |
|
1685 { |
3268
|
1686 if (all_char_conv) |
2117
|
1687 { |
3268
|
1688 if (one_elt_size_spec) |
|
1689 { |
|
1690 final_nr = 1; |
|
1691 final_nc = data_index; |
|
1692 } |
|
1693 else if (data_index > nr) |
2117
|
1694 { |
2759
|
1695 final_nr = nr; |
3268
|
1696 final_nc = (data_index - 1) / nr + 1; |
2117
|
1697 } |
|
1698 else |
|
1699 { |
3268
|
1700 final_nr = data_index; |
|
1701 final_nc = 1; |
|
1702 } |
|
1703 } |
|
1704 else if (nr > 0) |
|
1705 { |
|
1706 if (data_index > nr) |
|
1707 { |
|
1708 final_nr = nr; |
|
1709 final_nc = (data_index - 1) / nr + 1; |
|
1710 } |
|
1711 else |
|
1712 { |
|
1713 final_nr = data_index; |
2117
|
1714 final_nc = 1; |
|
1715 } |
|
1716 } |
|
1717 else |
|
1718 { |
3268
|
1719 final_nr = data_index; |
2759
|
1720 final_nc = 1; |
|
1721 } |
|
1722 |
3337
|
1723 // If it looks like we have a matching failure, then |
|
1724 // reset the failbit in the stream state. |
|
1725 |
3538
|
1726 if (is.rdstate () & std::ios::failbit) |
|
1727 is.clear (is.rdstate () & (~std::ios::failbit)); |
3337
|
1728 |
2759
|
1729 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1730 |
|
1731 if (interactive && name () == "stdin") |
2759
|
1732 { |
|
1733 is.clear (); |
|
1734 |
|
1735 // Skip to end of line. |
|
1736 |
|
1737 bool err; |
4468
|
1738 do_gets (-1, err, false, who); |
2117
|
1739 } |
|
1740 |
|
1741 break; |
|
1742 } |
|
1743 } |
|
1744 else |
|
1745 { |
4468
|
1746 error ("%s: internal format error", who.c_str ()); |
2117
|
1747 break; |
|
1748 } |
|
1749 |
3640
|
1750 elt = fmt_list.next (nconv > 0); |
2117
|
1751 } |
|
1752 } |
|
1753 |
|
1754 if (ok ()) |
|
1755 { |
2121
|
1756 mval.resize (final_nr, final_nc, 0.0); |
2117
|
1757 |
3268
|
1758 retval = mval; |
|
1759 |
2117
|
1760 if (all_char_conv) |
3268
|
1761 retval = retval.convert_to_str (); |
2117
|
1762 } |
|
1763 |
|
1764 return retval; |
|
1765 } |
|
1766 |
|
1767 octave_value |
3810
|
1768 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
4468
|
1769 int& conversion_count, const std::string& who) |
2117
|
1770 { |
|
1771 octave_value retval = Matrix (); |
|
1772 |
3559
|
1773 conversion_count = 0; |
2117
|
1774 |
3523
|
1775 std::istream *isp = input_stream (); |
2117
|
1776 |
|
1777 if (isp) |
|
1778 { |
|
1779 scanf_format_list fmt_list (fmt); |
|
1780 |
3640
|
1781 if (fmt_list.num_conversions () == -1) |
4468
|
1782 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
1783 else |
2117
|
1784 { |
3640
|
1785 int nr = -1; |
|
1786 int nc = -1; |
|
1787 |
|
1788 bool one_elt_size_spec; |
|
1789 |
4468
|
1790 get_size (size, nr, nc, one_elt_size_spec, who); |
3640
|
1791 |
|
1792 if (! error_state) |
|
1793 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, |
4468
|
1794 conversion_count, who); |
2215
|
1795 } |
|
1796 } |
|
1797 else |
4468
|
1798 invalid_operation (who, "reading"); |
2572
|
1799 |
|
1800 return retval; |
|
1801 } |
|
1802 |
2712
|
1803 bool |
|
1804 octave_base_stream::do_oscanf (const scanf_format_elt *elt, |
4468
|
1805 octave_value& retval, const std::string& who) |
2572
|
1806 { |
2712
|
1807 bool quit = false; |
2215
|
1808 |
3523
|
1809 std::istream *isp = input_stream (); |
2215
|
1810 |
|
1811 if (isp) |
|
1812 { |
3523
|
1813 std::istream& is = *isp; |
2215
|
1814 |
3538
|
1815 std::ios::fmtflags flags = is.flags (); |
2215
|
1816 |
|
1817 if (elt) |
|
1818 { |
|
1819 const char *fmt = elt->text; |
|
1820 |
|
1821 bool discard = elt->discard; |
|
1822 |
|
1823 switch (elt->type) |
|
1824 { |
3483
|
1825 case scanf_format_elt::whitespace_conversion: |
|
1826 DO_WHITESPACE_CONVERSION (); |
|
1827 break; |
|
1828 |
|
1829 case scanf_format_elt::literal_conversion: |
|
1830 DO_LITERAL_CONVERSION (); |
|
1831 break; |
|
1832 |
2215
|
1833 case '%': |
|
1834 { |
3640
|
1835 DO_PCT_CONVERSION (); |
|
1836 |
|
1837 if (! is) |
2712
|
1838 quit = true; |
3640
|
1839 |
2215
|
1840 } |
|
1841 break; |
|
1842 |
3878
|
1843 case 'd': case 'i': |
2215
|
1844 { |
|
1845 int tmp; |
|
1846 |
3640
|
1847 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
1848 { |
|
1849 if (! discard) |
4233
|
1850 retval = tmp; |
2712
|
1851 } |
|
1852 else |
|
1853 quit = true; |
2215
|
1854 } |
|
1855 break; |
|
1856 |
3878
|
1857 case 'o': case 'u': case 'x': |
|
1858 { |
|
1859 long int tmp; |
|
1860 |
|
1861 if (OCTAVE_SCAN (is, *elt, &tmp)) |
|
1862 { |
|
1863 if (! discard) |
4254
|
1864 retval = tmp; |
3878
|
1865 } |
|
1866 else |
|
1867 quit = true; |
|
1868 } |
|
1869 break; |
|
1870 |
2215
|
1871 case 'e': case 'f': case 'g': |
|
1872 { |
2600
|
1873 double tmp; |
|
1874 |
3640
|
1875 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
1876 { |
|
1877 if (! discard) |
|
1878 retval = tmp; |
|
1879 } |
|
1880 else |
|
1881 quit = true; |
2215
|
1882 } |
|
1883 break; |
|
1884 |
|
1885 case 'c': |
|
1886 { |
3483
|
1887 BEGIN_C_CONVERSION (); |
|
1888 |
|
1889 if (! discard) |
|
1890 retval = tmp; |
|
1891 |
|
1892 if (! is) |
2712
|
1893 quit = true; |
2215
|
1894 |
|
1895 is.setf (flags); |
|
1896 } |
|
1897 break; |
|
1898 |
|
1899 case 's': |
|
1900 { |
3483
|
1901 BEGIN_S_CONVERSION (); |
|
1902 |
|
1903 if (! discard) |
|
1904 retval = tmp; |
2572
|
1905 |
3268
|
1906 if (! is) |
|
1907 quit = true; |
2215
|
1908 } |
|
1909 break; |
|
1910 |
3483
|
1911 case '[': case '^': |
|
1912 { |
|
1913 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1914 |
|
1915 if (! discard) |
|
1916 retval = tmp; |
|
1917 |
|
1918 if (! is) |
|
1919 quit = true; |
|
1920 } |
|
1921 break; |
|
1922 |
|
1923 case 'p': |
4468
|
1924 error ("%s: unsupported format specifier", who.c_str ()); |
2215
|
1925 break; |
|
1926 |
|
1927 default: |
4468
|
1928 error ("%s: internal format error", who.c_str ()); |
2215
|
1929 break; |
|
1930 } |
|
1931 } |
|
1932 |
|
1933 if (ok () && is.fail ()) |
|
1934 { |
4468
|
1935 error ("%s: read error", who.c_str ()); |
3483
|
1936 |
2215
|
1937 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1938 |
|
1939 if (interactive && name () == "stdin") |
2215
|
1940 { |
|
1941 // Skip to end of line. |
|
1942 |
|
1943 bool err; |
4468
|
1944 do_gets (-1, err, false, who); |
2215
|
1945 } |
|
1946 } |
|
1947 } |
|
1948 |
2712
|
1949 return quit; |
2215
|
1950 } |
|
1951 |
|
1952 octave_value_list |
4468
|
1953 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
1954 { |
|
1955 octave_value_list retval; |
|
1956 |
3523
|
1957 std::istream *isp = input_stream (); |
2215
|
1958 |
|
1959 if (isp) |
|
1960 { |
3523
|
1961 std::istream& is = *isp; |
2215
|
1962 |
|
1963 scanf_format_list fmt_list (fmt); |
|
1964 |
|
1965 int nconv = fmt_list.num_conversions (); |
|
1966 |
3640
|
1967 if (nconv == -1) |
4468
|
1968 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
1969 else |
2215
|
1970 { |
3640
|
1971 is.clear (); |
|
1972 |
|
1973 int len = fmt_list.length (); |
|
1974 |
|
1975 retval.resize (nconv+1, Matrix ()); |
|
1976 |
|
1977 const scanf_format_elt *elt = fmt_list.first (); |
|
1978 |
|
1979 int num_values = 0; |
|
1980 |
|
1981 bool quit = false; |
|
1982 |
|
1983 for (int i = 0; i < len; i++) |
|
1984 { |
|
1985 octave_value tmp; |
|
1986 |
4468
|
1987 quit = do_oscanf (elt, tmp, who); |
3640
|
1988 |
|
1989 if (quit) |
|
1990 break; |
|
1991 else |
|
1992 { |
|
1993 if (tmp.is_defined ()) |
|
1994 retval (num_values++) = tmp; |
|
1995 |
|
1996 if (! ok ()) |
|
1997 break; |
|
1998 |
|
1999 elt = fmt_list.next (nconv > 0); |
|
2000 } |
|
2001 } |
|
2002 |
4233
|
2003 retval(nconv) = num_values; |
3640
|
2004 |
|
2005 if (! quit) |
|
2006 { |
|
2007 // Pick up any trailing stuff. |
|
2008 if (ok () && len > nconv) |
|
2009 { |
|
2010 octave_value tmp; |
3704
|
2011 |
|
2012 elt = fmt_list.next (); |
|
2013 |
4468
|
2014 do_oscanf (elt, tmp, who); |
3640
|
2015 } |
|
2016 } |
2117
|
2017 } |
|
2018 } |
|
2019 else |
4468
|
2020 invalid_operation (who, "reading"); |
2117
|
2021 |
|
2022 return retval; |
|
2023 } |
|
2024 |
|
2025 // Functions that are defined for all output streams (output streams |
|
2026 // are those that define os). |
|
2027 |
|
2028 int |
|
2029 octave_base_stream::flush (void) |
|
2030 { |
|
2031 int retval = -1; |
|
2032 |
3523
|
2033 std::ostream *os = output_stream (); |
2117
|
2034 |
|
2035 if (os) |
|
2036 { |
|
2037 os->flush (); |
|
2038 |
|
2039 if (os->good ()) |
|
2040 retval = 0; |
|
2041 } |
|
2042 else |
|
2043 invalid_operation ("fflush", "writing"); |
|
2044 |
|
2045 return retval; |
|
2046 } |
|
2047 |
|
2048 int |
2317
|
2049 octave_base_stream::write (const octave_value& data, |
|
2050 oct_data_conv::data_type dt, int skip, |
3553
|
2051 oct_mach_info::float_format ffmt) |
2117
|
2052 { |
|
2053 int retval = -1; |
|
2054 |
3523
|
2055 std::ostream *osp = output_stream (); |
2117
|
2056 |
|
2057 if (osp) |
|
2058 { |
3523
|
2059 std::ostream& os = *osp; |
2117
|
2060 |
4691
|
2061 int status = 0; |
|
2062 |
|
2063 // XXX FIXME XXX -- the octave_value class should probably have |
|
2064 // a write method that would handle the dispatch for us? |
|
2065 // |
|
2066 // If DATA is a character matrix, then it is a bit of a kluge to |
|
2067 // force it to be a double matrix and then write it out as uchar |
|
2068 // data, but this is the quick fix... |
|
2069 |
|
2070 Matrix mval = data.matrix_value (true); |
2117
|
2071 |
|
2072 if (! error_state) |
|
2073 { |
4574
|
2074 if (ffmt == oct_mach_info::flt_fmt_unknown) |
3553
|
2075 ffmt = float_format (); |
|
2076 |
4691
|
2077 status = mval.write (os, dt, skip, ffmt); |
|
2078 |
|
2079 if (status < 0) |
2317
|
2080 error ("fwrite: write error"); |
|
2081 else |
4691
|
2082 retval = status; |
2117
|
2083 } |
|
2084 } |
|
2085 else |
|
2086 invalid_operation ("fwrite", "writing"); |
|
2087 |
|
2088 return retval; |
|
2089 } |
|
2090 |
|
2091 class |
|
2092 printf_value_cache |
|
2093 { |
|
2094 public: |
|
2095 |
3653
|
2096 enum state { ok, conversion_error }; |
2117
|
2097 |
|
2098 printf_value_cache (const octave_value_list& args) |
|
2099 : values (args), val_idx (0), elt_idx (0), |
|
2100 n_vals (values.length ()), n_elts (0), data (0), |
|
2101 curr_state (ok) { } |
|
2102 |
|
2103 ~printf_value_cache (void) { } |
|
2104 |
|
2105 // Get the current value as a double and advance the internal pointer. |
|
2106 double double_value (void); |
|
2107 |
|
2108 // Get the current value as an int and advance the internal pointer. |
|
2109 int int_value (void); |
|
2110 |
|
2111 // Get the current value as a string and advance the internal pointer. |
3523
|
2112 std::string string_value (void); |
2117
|
2113 |
3145
|
2114 operator bool () const { return (curr_state == ok); } |
2117
|
2115 |
3653
|
2116 bool exhausted (void) { return (val_idx >= n_vals); } |
2117
|
2117 |
|
2118 private: |
|
2119 |
|
2120 const octave_value_list values; |
|
2121 int val_idx; |
|
2122 int elt_idx; |
|
2123 int n_vals; |
|
2124 int n_elts; |
|
2125 const double *data; |
4874
|
2126 NDArray curr_val; |
2117
|
2127 state curr_state; |
|
2128 |
|
2129 // Must create value cache with values! |
|
2130 |
|
2131 printf_value_cache (void); |
|
2132 |
|
2133 // No copying! |
|
2134 |
|
2135 printf_value_cache (const printf_value_cache&); |
|
2136 |
|
2137 printf_value_cache& operator = (const printf_value_cache&); |
|
2138 }; |
|
2139 |
|
2140 double |
|
2141 printf_value_cache::double_value (void) |
|
2142 { |
|
2143 double retval = 0.0; |
|
2144 |
3711
|
2145 if (exhausted ()) |
|
2146 curr_state = conversion_error; |
|
2147 |
|
2148 while (! exhausted ()) |
2117
|
2149 { |
|
2150 if (! data) |
|
2151 { |
|
2152 octave_value tmp_val = values (val_idx); |
|
2153 |
4874
|
2154 curr_val = tmp_val.array_value (); |
2117
|
2155 |
|
2156 if (! error_state) |
|
2157 { |
|
2158 elt_idx = 0; |
|
2159 n_elts = curr_val.length (); |
|
2160 data = curr_val.data (); |
|
2161 } |
|
2162 else |
|
2163 { |
|
2164 curr_state = conversion_error; |
|
2165 break; |
|
2166 } |
|
2167 } |
|
2168 |
|
2169 if (elt_idx < n_elts) |
|
2170 { |
3653
|
2171 retval = data[elt_idx++]; |
|
2172 |
|
2173 if (elt_idx >= n_elts) |
|
2174 { |
|
2175 elt_idx = 0; |
|
2176 val_idx++; |
|
2177 data = 0; |
|
2178 } |
|
2179 |
2117
|
2180 break; |
|
2181 } |
|
2182 else |
|
2183 { |
|
2184 val_idx++; |
|
2185 data = 0; |
3969
|
2186 |
|
2187 if (n_elts == 0 && exhausted ()) |
|
2188 curr_state = conversion_error; |
|
2189 |
2117
|
2190 continue; |
|
2191 } |
|
2192 } |
|
2193 |
|
2194 return retval; |
|
2195 } |
|
2196 |
|
2197 int |
|
2198 printf_value_cache::int_value (void) |
|
2199 { |
|
2200 int retval = 0; |
|
2201 |
|
2202 double dval = double_value (); |
|
2203 |
|
2204 if (! error_state) |
|
2205 { |
|
2206 if (D_NINT (dval) == dval) |
|
2207 retval = NINT (dval); |
|
2208 else |
|
2209 curr_state = conversion_error; |
|
2210 } |
|
2211 |
|
2212 return retval; |
|
2213 } |
|
2214 |
3536
|
2215 std::string |
2117
|
2216 printf_value_cache::string_value (void) |
|
2217 { |
3523
|
2218 std::string retval; |
2117
|
2219 |
4425
|
2220 if (exhausted ()) |
|
2221 curr_state = conversion_error; |
4257
|
2222 else |
2117
|
2223 { |
4425
|
2224 octave_value tval = values (val_idx++); |
|
2225 |
|
2226 if (tval.rows () == 1) |
|
2227 retval = tval.string_value (); |
|
2228 else |
|
2229 { |
|
2230 // In the name of Matlab compatibility. |
|
2231 |
|
2232 charMatrix chm = tval.char_matrix_value (); |
|
2233 |
|
2234 int nr = chm.rows (); |
|
2235 int nc = chm.columns (); |
|
2236 |
|
2237 int k = 0; |
|
2238 |
|
2239 retval.resize (nr * nc, '\0'); |
|
2240 |
|
2241 for (int j = 0; j < nc; j++) |
|
2242 for (int i = 0; i < nr; i++) |
|
2243 retval[k++] = chm(i,j); |
|
2244 } |
|
2245 |
|
2246 if (error_state) |
|
2247 curr_state = conversion_error; |
2117
|
2248 } |
4257
|
2249 |
2117
|
2250 return retval; |
|
2251 } |
|
2252 |
|
2253 // Ugh again and again. |
|
2254 |
2572
|
2255 template <class T> |
3620
|
2256 int |
3523
|
2257 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
4468
|
2258 int sa_2, T arg, const std::string& who) |
2572
|
2259 { |
3620
|
2260 int retval = 0; |
|
2261 |
2572
|
2262 switch (nsa) |
|
2263 { |
|
2264 case 2: |
3640
|
2265 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572
|
2266 break; |
|
2267 |
|
2268 case 1: |
3640
|
2269 retval = octave_format (os, fmt, sa_1, arg); |
2572
|
2270 break; |
|
2271 |
|
2272 case 0: |
3640
|
2273 retval = octave_format (os, fmt, arg); |
2572
|
2274 break; |
|
2275 |
|
2276 default: |
4468
|
2277 ::error ("%s: internal error handling format", who.c_str ()); |
2572
|
2278 break; |
|
2279 } |
3620
|
2280 |
|
2281 return retval; |
2572
|
2282 } |
|
2283 |
3620
|
2284 template int |
4468
|
2285 do_printf_conv (std::ostream&, const char*, int, int, int, int, |
|
2286 const std::string&); |
|
2287 |
|
2288 template int |
|
2289 do_printf_conv (std::ostream&, const char*, int, int, int, long, |
|
2290 const std::string&); |
|
2291 |
3878
|
2292 template int |
4468
|
2293 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, |
|
2294 const std::string&); |
|
2295 |
|
2296 template int |
|
2297 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, |
|
2298 const std::string&); |
|
2299 |
|
2300 template int |
|
2301 do_printf_conv (std::ostream&, const char*, int, int, int, double, |
|
2302 const std::string&); |
|
2303 |
|
2304 template int |
|
2305 do_printf_conv (std::ostream&, const char*, int, int, int, const char*, |
|
2306 const std::string&); |
2117
|
2307 |
|
2308 int |
|
2309 octave_base_stream::do_printf (printf_format_list& fmt_list, |
4468
|
2310 const octave_value_list& args, |
|
2311 const std::string& who) |
2117
|
2312 { |
3620
|
2313 int retval = 0; |
2117
|
2314 |
3640
|
2315 int nconv = fmt_list.num_conversions (); |
|
2316 |
3523
|
2317 std::ostream *osp = output_stream (); |
2117
|
2318 |
|
2319 if (osp) |
|
2320 { |
3523
|
2321 std::ostream& os = *osp; |
2117
|
2322 |
|
2323 const printf_format_elt *elt = fmt_list.first (); |
|
2324 |
|
2325 printf_value_cache val_cache (args); |
|
2326 |
|
2327 for (;;) |
|
2328 { |
4153
|
2329 OCTAVE_QUIT; |
|
2330 |
2117
|
2331 if (elt) |
|
2332 { |
3640
|
2333 // NSA is the number of `star' args to convert. |
|
2334 |
|
2335 int nsa = (elt->fw < 0) + (elt->prec < 0); |
2117
|
2336 |
|
2337 int sa_1 = 0; |
|
2338 int sa_2 = 0; |
|
2339 |
|
2340 if (nsa > 0) |
|
2341 { |
|
2342 sa_1 = val_cache.int_value (); |
|
2343 |
|
2344 if (! val_cache) |
|
2345 break; |
|
2346 else |
|
2347 { |
|
2348 if (nsa > 1) |
|
2349 { |
|
2350 sa_2 = val_cache.int_value (); |
|
2351 |
|
2352 if (! val_cache) |
|
2353 break; |
|
2354 } |
|
2355 } |
|
2356 } |
|
2357 |
|
2358 const char *fmt = elt->text; |
|
2359 |
3640
|
2360 if (elt->type == '%') |
|
2361 { |
|
2362 os << "%"; |
|
2363 retval++; |
|
2364 } |
|
2365 else if (elt->args == 0 && elt->text) |
|
2366 { |
|
2367 os << elt->text; |
|
2368 retval += strlen (elt->text); |
|
2369 } |
4257
|
2370 else if (elt->type == 's') |
3640
|
2371 { |
|
2372 std::string val = val_cache.string_value (); |
|
2373 |
|
2374 if (val_cache) |
|
2375 retval += do_printf_conv (os, fmt, nsa, sa_1, |
4468
|
2376 sa_2, val.c_str (), who); |
3640
|
2377 else |
|
2378 break; |
|
2379 } |
2117
|
2380 else |
|
2381 { |
3640
|
2382 double val = val_cache.double_value (); |
|
2383 |
|
2384 if (val_cache) |
2117
|
2385 { |
4693
|
2386 if (lo_ieee_is_NaN_or_NA (val) || xisinf (val) |
|
2387 || ((val > INT_MAX || val < INT_MIN) |
|
2388 && (elt->type == 'd' |
|
2389 || elt->type == 'i' |
|
2390 || elt->type == 'c' |
|
2391 || elt->type == 'o' |
|
2392 || elt->type == 'x' |
|
2393 || elt->type == 'X' |
|
2394 || elt->type == 'u'))) |
4303
|
2395 { |
|
2396 std::string tfmt = fmt; |
|
2397 |
4693
|
2398 if (lo_ieee_is_NaN_or_NA (val) || xisinf (val)) |
4305
|
2399 { |
|
2400 tfmt.replace (tfmt.rfind (elt->type), 1, 1, 's'); |
|
2401 |
|
2402 const char *tval = xisinf (val) |
4693
|
2403 ? (val < 0 ? "-Inf" : "Inf") |
|
2404 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); |
4305
|
2405 |
|
2406 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2407 nsa, sa_1, sa_2, |
|
2408 tval, who); |
4305
|
2409 } |
|
2410 else |
|
2411 { |
|
2412 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); |
|
2413 |
|
2414 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2415 nsa, sa_1, sa_2, |
|
2416 val, who); |
4305
|
2417 } |
4303
|
2418 } |
|
2419 else |
3640
|
2420 { |
4303
|
2421 switch (elt->type) |
|
2422 { |
|
2423 case 'd': case 'i': case 'c': |
|
2424 { |
|
2425 if (elt->modifier == 'l') |
|
2426 retval += do_printf_conv |
|
2427 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2428 static_cast<long int> (val), who); |
4303
|
2429 else |
|
2430 retval += do_printf_conv |
|
2431 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2432 static_cast<int> (val), who); |
4303
|
2433 } |
|
2434 break; |
|
2435 |
|
2436 case 'o': case 'x': case 'X': case 'u': |
|
2437 { |
|
2438 if (elt->modifier == 'l') |
|
2439 retval += do_printf_conv |
|
2440 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2441 static_cast<unsigned long int> (val), |
|
2442 who); |
4303
|
2443 else |
|
2444 retval += do_printf_conv |
|
2445 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2446 static_cast<unsigned int> (val), who); |
4303
|
2447 } |
|
2448 break; |
|
2449 |
|
2450 case 'f': case 'e': case 'E': |
|
2451 case 'g': case 'G': |
|
2452 retval |
|
2453 += do_printf_conv (os, fmt, nsa, sa_1, sa_2, |
4468
|
2454 val, who); |
4303
|
2455 break; |
|
2456 |
|
2457 default: |
4468
|
2458 error ("%s: invalid format specifier", |
|
2459 who.c_str ()); |
4303
|
2460 return -1; |
|
2461 break; |
|
2462 } |
3640
|
2463 } |
2117
|
2464 } |
|
2465 else |
3620
|
2466 break; |
2117
|
2467 } |
|
2468 |
3620
|
2469 if (! os) |
2117
|
2470 { |
4468
|
2471 error ("%s: write error", who.c_str ()); |
2117
|
2472 break; |
|
2473 } |
|
2474 } |
|
2475 else |
|
2476 { |
4468
|
2477 ::error ("%s: internal error handling format", who.c_str ()); |
2117
|
2478 retval = -1; |
|
2479 break; |
|
2480 } |
|
2481 |
3640
|
2482 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
|
2483 |
|
2484 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
|
2485 break; |
|
2486 } |
2117
|
2487 } |
3640
|
2488 else |
4468
|
2489 invalid_operation (who, "writing"); |
2117
|
2490 |
|
2491 return retval; |
|
2492 } |
|
2493 |
|
2494 int |
3640
|
2495 octave_base_stream::printf (const std::string& fmt, |
4468
|
2496 const octave_value_list& args, |
|
2497 const std::string& who) |
2117
|
2498 { |
3640
|
2499 int retval = 0; |
|
2500 |
|
2501 printf_format_list fmt_list (fmt); |
|
2502 |
|
2503 if (fmt_list.num_conversions () == -1) |
4468
|
2504 ::error ("%s: invalid format specified", who.c_str ()); |
2117
|
2505 else |
4468
|
2506 retval = do_printf (fmt_list, args, who); |
2117
|
2507 |
|
2508 return retval; |
|
2509 } |
|
2510 |
|
2511 int |
4468
|
2512 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117
|
2513 { |
|
2514 int retval = -1; |
|
2515 |
3523
|
2516 std::ostream *osp = output_stream (); |
2117
|
2517 |
|
2518 if (osp) |
|
2519 { |
3523
|
2520 std::ostream& os = *osp; |
2117
|
2521 |
|
2522 os << s; |
|
2523 |
|
2524 if (os) |
|
2525 { |
|
2526 // XXX FIXME XXX -- why does this seem to be necessary? |
|
2527 // Without it, output from a loop like |
|
2528 // |
|
2529 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
|
2530 // |
|
2531 // doesn't seem to go to the pager immediately. |
|
2532 |
|
2533 os.flush (); |
|
2534 |
|
2535 if (os) |
|
2536 retval = 0; |
|
2537 else |
4468
|
2538 error ("%s: write error", who.c_str ()); |
2117
|
2539 } |
|
2540 else |
4468
|
2541 error ("%s: write error", who.c_str ()); |
2117
|
2542 } |
|
2543 else |
4468
|
2544 invalid_operation (who, "writing"); |
2117
|
2545 |
|
2546 return retval; |
|
2547 } |
|
2548 |
|
2549 int |
|
2550 octave_base_stream::rewind (void) |
|
2551 { |
3538
|
2552 return seek (0, std::ios::beg); |
2117
|
2553 } |
|
2554 |
|
2555 // Return current error message for this stream. |
|
2556 |
3536
|
2557 std::string |
2435
|
2558 octave_base_stream::error (bool clear_err, int& err_num) |
2117
|
2559 { |
2435
|
2560 err_num = fail ? -1 : 0; |
2117
|
2561 |
3523
|
2562 std::string tmp = errmsg; |
2117
|
2563 |
|
2564 if (clear_err) |
|
2565 clear (); |
|
2566 |
|
2567 return tmp; |
|
2568 } |
|
2569 |
|
2570 void |
4468
|
2571 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117
|
2572 { |
4468
|
2573 // Note that this is not ::error () ! |
|
2574 |
|
2575 error (who + ": stream not open for " + rw); |
2117
|
2576 } |
|
2577 |
3552
|
2578 octave_stream::octave_stream (octave_base_stream *bs) |
3340
|
2579 : rep (bs) |
|
2580 { |
|
2581 if (rep) |
|
2582 rep->count = 1; |
|
2583 } |
|
2584 |
|
2585 octave_stream::~octave_stream (void) |
|
2586 { |
|
2587 if (rep && --rep->count == 0) |
|
2588 delete rep; |
|
2589 } |
|
2590 |
|
2591 octave_stream::octave_stream (const octave_stream& s) |
|
2592 : rep (s.rep) |
|
2593 { |
|
2594 if (rep) |
|
2595 rep->count++; |
|
2596 } |
|
2597 |
|
2598 octave_stream& |
|
2599 octave_stream::operator = (const octave_stream& s) |
|
2600 { |
|
2601 if (rep != s.rep) |
|
2602 { |
|
2603 if (rep && --rep->count == 0) |
|
2604 delete rep; |
|
2605 |
|
2606 rep = s.rep; |
|
2607 |
|
2608 if (rep) |
|
2609 rep->count++; |
|
2610 } |
|
2611 |
|
2612 return *this; |
|
2613 } |
|
2614 |
2117
|
2615 int |
|
2616 octave_stream::flush (void) |
|
2617 { |
|
2618 int retval = -1; |
|
2619 |
|
2620 if (stream_ok ("fflush")) |
|
2621 retval = rep->flush (); |
|
2622 |
|
2623 return retval; |
|
2624 } |
|
2625 |
3536
|
2626 std::string |
4468
|
2627 octave_stream::getl (int max_len, bool& err, const std::string& who) |
2117
|
2628 { |
3523
|
2629 std::string retval; |
2117
|
2630 |
4468
|
2631 if (stream_ok (who)) |
|
2632 retval = rep->getl (max_len, err, who); |
2117
|
2633 |
|
2634 return retval; |
|
2635 } |
|
2636 |
3536
|
2637 std::string |
4468
|
2638 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
|
2639 const std::string& who) |
2117
|
2640 { |
3523
|
2641 std::string retval; |
2117
|
2642 |
|
2643 err = false; |
|
2644 |
|
2645 int conv_err = 0; |
|
2646 |
|
2647 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2648 |
|
2649 if (conv_err || max_len < 0) |
|
2650 { |
|
2651 err = true; |
4468
|
2652 ::error ("%s: invalid maximum length specified", who.c_str ()); |
2117
|
2653 } |
|
2654 else |
4468
|
2655 retval = getl (max_len, err, who); |
2117
|
2656 |
|
2657 return retval; |
|
2658 } |
|
2659 |
3536
|
2660 std::string |
4468
|
2661 octave_stream::gets (int max_len, bool& err, const std::string& who) |
2117
|
2662 { |
3523
|
2663 std::string retval; |
2117
|
2664 |
4468
|
2665 if (stream_ok (who)) |
|
2666 retval = rep->gets (max_len, err, who); |
2117
|
2667 |
|
2668 return retval; |
|
2669 } |
|
2670 |
3536
|
2671 std::string |
4468
|
2672 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
|
2673 const std::string& who) |
2117
|
2674 { |
3523
|
2675 std::string retval; |
2117
|
2676 |
|
2677 err = false; |
|
2678 |
|
2679 int conv_err = 0; |
|
2680 |
|
2681 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2682 |
|
2683 if (conv_err || max_len < 0) |
|
2684 { |
|
2685 err = true; |
4468
|
2686 ::error ("%s: invalid maximum length specified", who.c_str ()); |
2117
|
2687 } |
|
2688 else |
4468
|
2689 retval = gets (max_len, err, who); |
2117
|
2690 |
|
2691 return retval; |
|
2692 } |
|
2693 |
|
2694 int |
4797
|
2695 octave_stream::seek (long offset, int origin) |
2117
|
2696 { |
|
2697 int retval = -1; |
|
2698 |
|
2699 if (stream_ok ("fseek")) |
|
2700 retval = rep->seek (offset, origin); |
|
2701 |
|
2702 return retval; |
|
2703 } |
|
2704 |
|
2705 int |
|
2706 octave_stream::seek (const octave_value& tc_offset, |
|
2707 const octave_value& tc_origin) |
|
2708 { |
|
2709 int retval = -1; |
|
2710 |
4797
|
2711 long xoffset = tc_offset.long_value (true); |
4645
|
2712 |
|
2713 if (! error_state) |
2117
|
2714 { |
4645
|
2715 int conv_err = 0; |
|
2716 |
4797
|
2717 int origin = SEEK_SET; |
2117
|
2718 |
2341
|
2719 if (tc_origin.is_string ()) |
2117
|
2720 { |
3523
|
2721 std::string xorigin = tc_origin.string_value (); |
2341
|
2722 |
|
2723 if (xorigin == "bof") |
4797
|
2724 origin = SEEK_SET; |
2341
|
2725 else if (xorigin == "cof") |
4797
|
2726 origin = SEEK_CUR; |
2341
|
2727 else if (xorigin == "eof") |
4797
|
2728 origin = SEEK_END; |
2117
|
2729 else |
|
2730 conv_err = -1; |
|
2731 } |
2341
|
2732 else |
|
2733 { |
|
2734 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
|
2735 |
|
2736 if (! conv_err) |
|
2737 { |
|
2738 if (xorigin == -1) |
4797
|
2739 origin = SEEK_SET; |
2341
|
2740 else if (xorigin == 0) |
4797
|
2741 origin = SEEK_CUR; |
2341
|
2742 else if (xorigin == 1) |
4797
|
2743 origin = SEEK_END; |
2341
|
2744 else |
|
2745 conv_err = -1; |
|
2746 } |
|
2747 } |
2117
|
2748 |
|
2749 if (! conv_err) |
|
2750 retval = seek (xoffset, origin); |
|
2751 else |
|
2752 error ("fseek: invalid value for origin"); |
|
2753 } |
|
2754 else |
|
2755 error ("fseek: invalid value for offset"); |
|
2756 |
|
2757 return retval; |
|
2758 } |
|
2759 |
4797
|
2760 long |
|
2761 octave_stream::tell (void) |
2117
|
2762 { |
4797
|
2763 long retval = -1; |
2117
|
2764 |
|
2765 if (stream_ok ("tell")) |
|
2766 retval = rep->tell (); |
|
2767 |
|
2768 return retval; |
|
2769 } |
|
2770 |
|
2771 int |
|
2772 octave_stream::rewind (void) |
|
2773 { |
|
2774 int retval = -1; |
|
2775 |
|
2776 if (stream_ok ("frewind")) |
|
2777 retval = rep->rewind (); |
|
2778 |
|
2779 return retval; |
|
2780 } |
|
2781 |
3340
|
2782 bool |
|
2783 octave_stream::is_open (void) const |
|
2784 { |
|
2785 bool retval = false; |
|
2786 |
|
2787 if (stream_ok ("is_open")) |
|
2788 retval = rep->is_open (); |
|
2789 |
|
2790 return retval; |
|
2791 } |
|
2792 |
|
2793 void |
|
2794 octave_stream::close (void) |
|
2795 { |
|
2796 if (stream_ok ("close")) |
|
2797 rep->close (); |
|
2798 } |
|
2799 |
2117
|
2800 octave_value |
3810
|
2801 octave_stream::read (const Array<double>& size, |
2317
|
2802 oct_data_conv::data_type dt, int skip, |
|
2803 oct_mach_info::float_format flt_fmt, int& count) |
2117
|
2804 { |
|
2805 octave_value retval; |
|
2806 |
|
2807 if (stream_ok ("fread")) |
2317
|
2808 retval = rep->read (size, dt, skip, flt_fmt, count); |
2117
|
2809 |
|
2810 return retval; |
|
2811 } |
|
2812 |
|
2813 int |
|
2814 octave_stream::write (const octave_value& data, |
2317
|
2815 oct_data_conv::data_type dt, int skip, |
|
2816 oct_mach_info::float_format flt_fmt) |
2117
|
2817 { |
|
2818 int retval = -1; |
|
2819 |
|
2820 if (stream_ok ("fwrite")) |
2317
|
2821 retval = rep->write (data, dt, skip, flt_fmt); |
2117
|
2822 |
|
2823 return retval; |
|
2824 } |
|
2825 |
|
2826 octave_value |
3810
|
2827 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
4468
|
2828 int& count, const std::string& who) |
2117
|
2829 { |
|
2830 octave_value retval; |
|
2831 |
4468
|
2832 if (stream_ok (who)) |
|
2833 retval = rep->scanf (fmt, size, count, who); |
2117
|
2834 |
|
2835 return retval; |
|
2836 } |
|
2837 |
2215
|
2838 octave_value_list |
4468
|
2839 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
2840 { |
|
2841 octave_value_list retval; |
|
2842 |
4468
|
2843 if (stream_ok (who)) |
|
2844 retval = rep->oscanf (fmt, who); |
2215
|
2845 |
|
2846 return retval; |
|
2847 } |
|
2848 |
2117
|
2849 int |
4468
|
2850 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
|
2851 const std::string& who) |
2117
|
2852 { |
|
2853 int retval = -1; |
|
2854 |
4468
|
2855 if (stream_ok (who)) |
|
2856 retval = rep->printf (fmt, args, who); |
2117
|
2857 |
|
2858 return retval; |
|
2859 } |
|
2860 |
|
2861 int |
4468
|
2862 octave_stream::puts (const std::string& s, const std::string& who) |
2117
|
2863 { |
|
2864 int retval = -1; |
|
2865 |
4468
|
2866 if (stream_ok (who)) |
|
2867 retval = rep->puts (s, who); |
2117
|
2868 |
|
2869 return retval; |
|
2870 } |
|
2871 |
|
2872 // XXX FIXME XXX -- maybe this should work for string arrays too. |
|
2873 |
|
2874 int |
4468
|
2875 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117
|
2876 { |
|
2877 int retval = -1; |
|
2878 |
|
2879 if (tc_s.is_string ()) |
|
2880 { |
3523
|
2881 std::string s = tc_s.string_value (); |
4468
|
2882 retval = rep->puts (s, who); |
2117
|
2883 } |
|
2884 else |
4468
|
2885 { |
|
2886 // Note that this is not ::error () ! |
|
2887 |
|
2888 error (who + ": argument must be a string"); |
|
2889 } |
2117
|
2890 |
|
2891 return retval; |
|
2892 } |
|
2893 |
|
2894 bool |
|
2895 octave_stream::eof (void) const |
|
2896 { |
|
2897 int retval = -1; |
|
2898 |
|
2899 if (stream_ok ("feof")) |
|
2900 retval = rep->eof (); |
|
2901 |
|
2902 return retval; |
|
2903 } |
|
2904 |
3536
|
2905 std::string |
2435
|
2906 octave_stream::error (bool clear, int& err_num) |
2117
|
2907 { |
3523
|
2908 std::string retval; |
2117
|
2909 |
|
2910 if (stream_ok ("ferror", false)) |
2435
|
2911 retval = rep->error (clear, err_num); |
2117
|
2912 |
|
2913 return retval; |
|
2914 } |
|
2915 |
3536
|
2916 std::string |
3340
|
2917 octave_stream::name (void) const |
2117
|
2918 { |
3523
|
2919 std::string retval; |
2117
|
2920 |
|
2921 if (stream_ok ("name")) |
|
2922 retval = rep->name (); |
|
2923 |
|
2924 return retval; |
|
2925 } |
|
2926 |
|
2927 int |
3340
|
2928 octave_stream::mode (void) const |
2117
|
2929 { |
|
2930 int retval = 0; |
|
2931 |
|
2932 if (stream_ok ("mode")) |
|
2933 retval = rep->mode (); |
|
2934 |
|
2935 return retval; |
|
2936 } |
|
2937 |
2317
|
2938 oct_mach_info::float_format |
3340
|
2939 octave_stream::float_format (void) const |
2117
|
2940 { |
4574
|
2941 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317
|
2942 |
|
2943 if (stream_ok ("float_format")) |
|
2944 retval = rep->float_format (); |
2117
|
2945 |
|
2946 return retval; |
|
2947 } |
|
2948 |
3536
|
2949 std::string |
2117
|
2950 octave_stream::mode_as_string (int mode) |
|
2951 { |
3523
|
2952 std::string retval = "???"; |
3775
|
2953 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
|
2954 |
|
2955 if (in_mode == std::ios::in) |
|
2956 retval = "r"; |
|
2957 else if (in_mode == std::ios::out |
4078
|
2958 || in_mode == (std::ios::out | std::ios::trunc)) |
3775
|
2959 retval = "w"; |
4078
|
2960 else if (in_mode == (std::ios::out | std::ios::app)) |
3775
|
2961 retval = "a"; |
4078
|
2962 else if (in_mode == (std::ios::in | std::ios::out)) |
3775
|
2963 retval = "r+"; |
4078
|
2964 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775
|
2965 retval = "w+"; |
4078
|
2966 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775
|
2967 retval = "a+"; |
4078
|
2968 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775
|
2969 retval = "rb"; |
4078
|
2970 else if (in_mode == (std::ios::out | std::ios::binary) |
|
2971 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775
|
2972 retval = "wb"; |
4078
|
2973 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775
|
2974 retval = "ab"; |
4078
|
2975 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775
|
2976 retval = "r+b"; |
4078
|
2977 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
|
2978 | std::ios::binary)) |
3775
|
2979 retval = "w+b"; |
4078
|
2980 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
|
2981 | std::ios::binary)) |
3775
|
2982 retval = "a+b"; |
2117
|
2983 |
|
2984 return retval; |
|
2985 } |
|
2986 |
|
2987 void |
4468
|
2988 octave_stream::invalid_stream_error (const std::string& who) const |
2117
|
2989 { |
4468
|
2990 ::error ("%s: attempt to use invalid I/O stream", who.c_str ()); |
2117
|
2991 } |
|
2992 |
|
2993 octave_stream_list *octave_stream_list::instance = 0; |
|
2994 |
2926
|
2995 bool |
|
2996 octave_stream_list::instance_ok (void) |
|
2997 { |
|
2998 bool retval = true; |
|
2999 |
|
3000 if (! instance) |
|
3001 instance = new octave_stream_list (); |
|
3002 |
|
3003 if (! instance) |
|
3004 { |
|
3005 ::error ("unable to create stream list object!"); |
|
3006 |
|
3007 retval = false; |
|
3008 } |
|
3009 |
|
3010 return retval; |
|
3011 } |
|
3012 |
|
3013 octave_value |
3340
|
3014 octave_stream_list::insert (const octave_stream& os) |
2926
|
3015 { |
3340
|
3016 return (instance_ok ()) ? instance->do_insert (os) : octave_value (-1.0); |
2926
|
3017 } |
|
3018 |
3340
|
3019 octave_stream |
3523
|
3020 octave_stream_list::lookup (int fid, const std::string& who) |
2926
|
3021 { |
3341
|
3022 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3023 } |
|
3024 |
3340
|
3025 octave_stream |
3523
|
3026 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926
|
3027 { |
3341
|
3028 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3029 } |
|
3030 |
|
3031 int |
3523
|
3032 octave_stream_list::remove (int fid, const std::string& who) |
2926
|
3033 { |
3341
|
3034 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3035 } |
|
3036 |
|
3037 int |
3523
|
3038 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926
|
3039 { |
3341
|
3040 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3041 } |
|
3042 |
|
3043 void |
|
3044 octave_stream_list::clear (void) |
|
3045 { |
|
3046 if (instance) |
|
3047 instance->do_clear (); |
|
3048 } |
|
3049 |
|
3050 string_vector |
|
3051 octave_stream_list::get_info (int fid) |
|
3052 { |
|
3053 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3054 } |
|
3055 |
|
3056 string_vector |
|
3057 octave_stream_list::get_info (const octave_value& fid) |
|
3058 { |
|
3059 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3060 } |
|
3061 |
3536
|
3062 std::string |
2926
|
3063 octave_stream_list::list_open_files (void) |
|
3064 { |
3523
|
3065 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926
|
3066 } |
|
3067 |
|
3068 octave_value |
|
3069 octave_stream_list::open_file_numbers (void) |
|
3070 { |
|
3071 return (instance_ok ()) |
|
3072 ? instance->do_open_file_numbers () : octave_value (); |
|
3073 } |
|
3074 |
|
3075 int |
|
3076 octave_stream_list::get_file_number (const octave_value& fid) |
|
3077 { |
|
3078 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; |
|
3079 } |
|
3080 |
2902
|
3081 octave_value |
3340
|
3082 octave_stream_list::do_insert (const octave_stream& os) |
2117
|
3083 { |
3340
|
3084 octave_value retval; |
|
3085 |
2902
|
3086 int stream_number = -1; |
|
3087 |
3340
|
3088 // Insert item in first open slot, increasing size of list if |
|
3089 // necessary. |
|
3090 |
|
3091 for (int i = 0; i < curr_len; i++) |
2117
|
3092 { |
3340
|
3093 octave_stream tmp = list(i); |
|
3094 |
|
3095 if (! tmp) |
2117
|
3096 { |
3340
|
3097 list(i) = os; |
|
3098 stream_number = i; |
|
3099 break; |
2117
|
3100 } |
|
3101 } |
3340
|
3102 |
|
3103 if (stream_number < 0) |
|
3104 { |
|
3105 int total_len = list.length (); |
|
3106 |
|
3107 if (curr_len == total_len) |
|
3108 list.resize (total_len * 2); |
|
3109 |
|
3110 list(curr_len) = os; |
|
3111 stream_number = curr_len; |
|
3112 curr_len++; |
|
3113 } |
2117
|
3114 |
2902
|
3115 return octave_value (os, stream_number); |
2117
|
3116 } |
|
3117 |
3341
|
3118 static void |
3523
|
3119 gripe_invalid_file_id (int fid, const std::string& who) |
3341
|
3120 { |
|
3121 if (who.empty ()) |
|
3122 ::error ("invalid stream number = %d", fid); |
|
3123 else |
|
3124 ::error ("%s: invalid stream number = %d", who.c_str (), fid); |
|
3125 } |
|
3126 |
3340
|
3127 octave_stream |
3523
|
3128 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117
|
3129 { |
3340
|
3130 octave_stream retval; |
2117
|
3131 |
|
3132 if (fid >= 0 && fid < curr_len) |
3340
|
3133 retval = list(fid); |
3341
|
3134 else |
|
3135 gripe_invalid_file_id (fid, who); |
2117
|
3136 |
|
3137 return retval; |
|
3138 } |
|
3139 |
3340
|
3140 octave_stream |
3341
|
3141 octave_stream_list::do_lookup (const octave_value& fid, |
3523
|
3142 const std::string& who) const |
2117
|
3143 { |
3340
|
3144 octave_stream retval; |
2117
|
3145 |
|
3146 int i = get_file_number (fid); |
|
3147 |
|
3148 if (! error_state) |
3341
|
3149 retval = do_lookup (i, who); |
2117
|
3150 |
|
3151 return retval; |
|
3152 } |
|
3153 |
|
3154 int |
3523
|
3155 octave_stream_list::do_remove (int fid, const std::string& who) |
2117
|
3156 { |
|
3157 int retval = -1; |
|
3158 |
3531
|
3159 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
|
3160 // (std::cerr). |
2117
|
3161 |
|
3162 if (fid > 2 && fid < curr_len) |
|
3163 { |
3340
|
3164 octave_stream os = list(fid); |
2117
|
3165 |
3341
|
3166 if (os.is_valid ()) |
2117
|
3167 { |
3340
|
3168 os.close (); |
|
3169 list(fid) = octave_stream (); |
2117
|
3170 retval = 0; |
|
3171 } |
3341
|
3172 else |
|
3173 gripe_invalid_file_id (fid, who); |
2117
|
3174 } |
3341
|
3175 else |
|
3176 gripe_invalid_file_id (fid, who); |
2117
|
3177 |
|
3178 return retval; |
|
3179 } |
|
3180 |
|
3181 int |
3523
|
3182 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117
|
3183 { |
|
3184 int retval = -1; |
|
3185 |
|
3186 int i = get_file_number (fid); |
|
3187 |
|
3188 if (! error_state) |
3341
|
3189 retval = do_remove (i, who); |
2117
|
3190 |
|
3191 return retval; |
|
3192 } |
|
3193 |
|
3194 void |
|
3195 octave_stream_list::do_clear (void) |
|
3196 { |
|
3197 // Do flush stdout and stderr. |
|
3198 |
3340
|
3199 list(0) . flush (); |
|
3200 list(1) . flush (); |
2117
|
3201 |
|
3202 // But don't delete them or stdin. |
|
3203 |
|
3204 for (int i = 3; i < curr_len; i++) |
3340
|
3205 list(i) = octave_stream (); |
2117
|
3206 } |
|
3207 |
|
3208 string_vector |
|
3209 octave_stream_list::do_get_info (int fid) const |
|
3210 { |
|
3211 string_vector retval; |
|
3212 |
3340
|
3213 octave_stream os = do_lookup (fid); |
2117
|
3214 |
3341
|
3215 if (os.is_valid ()) |
2117
|
3216 { |
|
3217 retval.resize (3); |
|
3218 |
3340
|
3219 retval(0) = os.name (); |
|
3220 retval(1) = octave_stream::mode_as_string (os.mode ()); |
|
3221 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
2117
|
3222 } |
|
3223 else |
3341
|
3224 ::error ("invalid file id = %d", fid); |
2117
|
3225 |
|
3226 return retval; |
|
3227 } |
|
3228 |
|
3229 string_vector |
|
3230 octave_stream_list::do_get_info (const octave_value& fid) const |
|
3231 { |
|
3232 string_vector retval; |
|
3233 |
|
3234 int conv_err = 0; |
|
3235 |
|
3236 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3237 |
|
3238 if (! conv_err) |
|
3239 retval = do_get_info (int_fid); |
|
3240 else |
2915
|
3241 ::error ("file id must be a file object or integer value"); |
2117
|
3242 |
|
3243 return retval; |
|
3244 } |
|
3245 |
3536
|
3246 std::string |
2117
|
3247 octave_stream_list::do_list_open_files (void) const |
|
3248 { |
3523
|
3249 std::string retval; |
2117
|
3250 |
4051
|
3251 OSSTREAM buf; |
2117
|
3252 |
|
3253 buf << "\n" |
|
3254 << " number mode arch name\n" |
|
3255 << " ------ ---- ---- ----\n"; |
|
3256 |
|
3257 for (int i = 0; i < curr_len; i++) |
|
3258 { |
3340
|
3259 octave_stream os = list(i); |
2117
|
3260 |
4326
|
3261 buf << " " |
|
3262 << std::setiosflags (std::ios::right) |
|
3263 << std::setw (4) << i << " " |
|
3264 << std::setiosflags (std::ios::left) |
|
3265 << std::setw (3) |
|
3266 << octave_stream::mode_as_string (os.mode ()) |
|
3267 << " " |
|
3268 << std::setw (9) |
|
3269 << oct_mach_info::float_format_as_string (os.float_format ()) |
|
3270 << " " |
|
3271 << os.name () << "\n"; |
2117
|
3272 } |
|
3273 |
4051
|
3274 buf << "\n" << OSSTREAM_ENDS; |
|
3275 |
|
3276 retval = OSSTREAM_STR (buf); |
|
3277 |
|
3278 OSSTREAM_FREEZE (buf); |
2117
|
3279 |
|
3280 return retval; |
|
3281 } |
|
3282 |
|
3283 octave_value |
|
3284 octave_stream_list::do_open_file_numbers (void) const |
|
3285 { |
|
3286 Matrix retval (1, curr_len, 0.0); |
|
3287 |
|
3288 int num_open = 0; |
|
3289 |
|
3290 // Skip stdin, stdout, and stderr. |
|
3291 |
|
3292 for (int i = 3; i < curr_len; i++) |
|
3293 { |
3340
|
3294 if (list(i)) |
2117
|
3295 retval (0, num_open++) = i; |
|
3296 } |
|
3297 |
|
3298 retval.resize ((num_open > 0), num_open); |
|
3299 |
|
3300 return retval; |
|
3301 } |
|
3302 |
|
3303 int |
2609
|
3304 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117
|
3305 { |
|
3306 int retval = -1; |
|
3307 |
|
3308 if (fid.is_string ()) |
|
3309 { |
3523
|
3310 std::string nm = fid.string_value (); |
2117
|
3311 |
3531
|
3312 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
|
3313 // are unnamed. |
2117
|
3314 |
|
3315 for (int i = 3; i < curr_len; i++) |
|
3316 { |
3340
|
3317 octave_stream os = list(i); |
|
3318 |
|
3319 if (os && os.name () == nm) |
2117
|
3320 { |
|
3321 retval = i; |
|
3322 break; |
|
3323 } |
|
3324 } |
|
3325 } |
|
3326 else |
|
3327 { |
|
3328 int conv_err = 0; |
|
3329 |
|
3330 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3331 |
|
3332 if (conv_err) |
3523
|
3333 ::error ("file id must be a file object, std::string, or integer value"); |
2117
|
3334 else |
|
3335 retval = int_fid; |
|
3336 } |
|
3337 |
|
3338 return retval; |
|
3339 } |
|
3340 |
|
3341 /* |
|
3342 ;;; Local Variables: *** |
|
3343 ;;; mode: C++ *** |
|
3344 ;;; End: *** |
|
3345 */ |