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 { |
4888
|
938 std::istream *is = input_stream (); |
|
939 std::ostream *os = output_stream (); |
|
940 |
|
941 if (is) |
|
942 is->clear (); |
|
943 |
|
944 if (os) |
|
945 os->clear (); |
|
946 |
2117
|
947 fail = false; |
|
948 errmsg = ""; |
|
949 } |
|
950 |
|
951 // Functions that are defined for all input streams (input streams |
|
952 // are those that define is). |
|
953 |
3536
|
954 std::string |
2117
|
955 octave_base_stream::do_gets (int max_len, bool& err, |
4468
|
956 bool strip_newline, const std::string& who) |
2117
|
957 { |
3523
|
958 std::string retval; |
2117
|
959 |
|
960 err = false; |
|
961 |
3523
|
962 std::istream *isp = input_stream (); |
2117
|
963 |
|
964 if (isp) |
|
965 { |
3523
|
966 std::istream& is = *isp; |
2117
|
967 |
4051
|
968 OSSTREAM buf; |
2117
|
969 |
|
970 int c = 0; |
3553
|
971 int char_count = 0; |
2117
|
972 int newline_stripped = 0; |
|
973 |
|
974 while (is && (c = is.get ()) != EOF) |
|
975 { |
3553
|
976 char_count++; |
2117
|
977 |
|
978 if (c == '\n') |
|
979 { |
|
980 if (! strip_newline) |
|
981 buf << (char) c; |
|
982 else |
|
983 newline_stripped = 1; |
|
984 |
|
985 break; |
|
986 } |
|
987 else |
|
988 buf << (char) c; |
|
989 |
3553
|
990 if (max_len > 0 && char_count == max_len) |
2117
|
991 break; |
|
992 } |
|
993 |
4224
|
994 if (is.good () || (is.eof () && char_count > 0)) |
2117
|
995 { |
4051
|
996 buf << OSSTREAM_ENDS; |
|
997 retval = OSSTREAM_STR (buf); |
|
998 OSSTREAM_FREEZE (buf); |
2117
|
999 } |
4224
|
1000 else |
|
1001 { |
|
1002 err = true; |
4468
|
1003 |
4224
|
1004 if (is.eof () && char_count == 0) |
4468
|
1005 error (who, "at end of file"); |
4224
|
1006 else |
4468
|
1007 error (who, "read error"); |
4224
|
1008 } |
2117
|
1009 } |
|
1010 else |
|
1011 { |
|
1012 err = true; |
4468
|
1013 invalid_operation (who, "reading"); |
2117
|
1014 } |
|
1015 |
|
1016 return retval; |
|
1017 } |
|
1018 |
3536
|
1019 std::string |
4468
|
1020 octave_base_stream::getl (int max_len, bool& err, const std::string& who) |
2117
|
1021 { |
4468
|
1022 return do_gets (max_len, err, true, who); |
2117
|
1023 } |
|
1024 |
3536
|
1025 std::string |
4468
|
1026 octave_base_stream::gets (int max_len, bool& err, const std::string& who) |
2117
|
1027 { |
4468
|
1028 return do_gets (max_len, err, false, who); |
2117
|
1029 } |
|
1030 |
|
1031 octave_value |
3810
|
1032 octave_base_stream::read (const Array<double>& size, |
2317
|
1033 oct_data_conv::data_type dt, int skip, |
3553
|
1034 oct_mach_info::float_format ffmt, |
|
1035 int& char_count) |
2117
|
1036 { |
2317
|
1037 Matrix retval; |
|
1038 |
3553
|
1039 char_count = 0; |
2117
|
1040 |
3523
|
1041 std::istream *isp = input_stream (); |
2117
|
1042 |
|
1043 if (isp) |
|
1044 { |
3523
|
1045 std::istream& is = *isp; |
2117
|
1046 |
|
1047 int nr = -1; |
|
1048 int nc = -1; |
|
1049 |
3268
|
1050 bool ignore; |
|
1051 |
|
1052 get_size (size, nr, nc, ignore, "fread"); |
2117
|
1053 |
|
1054 if (! error_state) |
2317
|
1055 { |
4574
|
1056 if (ffmt == oct_mach_info::flt_fmt_unknown) |
3553
|
1057 ffmt = float_format (); |
|
1058 |
|
1059 int tmp = retval.read (is, nr, nc, dt, skip, ffmt); |
2317
|
1060 |
|
1061 if (tmp < 0) |
|
1062 error ("fread: read error"); |
|
1063 else |
3553
|
1064 char_count = tmp; |
2317
|
1065 } |
2117
|
1066 } |
|
1067 else |
|
1068 invalid_operation ("fread", "reading"); |
|
1069 |
|
1070 return retval; |
|
1071 } |
|
1072 |
3779
|
1073 #if defined (__GNUG__) && ! defined (CXX_ISO_COMPLIANT_LIBRARY) |
3636
|
1074 |
3640
|
1075 #define OCTAVE_SCAN(is, fmt, arg) is.scan ((fmt).text, arg) |
3636
|
1076 |
|
1077 #else |
|
1078 |
3640
|
1079 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) |
3636
|
1080 |
3779
|
1081 // XXX FIXME XXX -- this needs to be fixed to handle formats which |
|
1082 // specify a maximum width. |
|
1083 |
3636
|
1084 template <class T> |
|
1085 std::istream& |
3779
|
1086 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) |
3636
|
1087 { |
3779
|
1088 T& ref = *valptr; |
|
1089 |
|
1090 switch (fmt.type) |
|
1091 { |
|
1092 case 'o': |
|
1093 is >> std::oct >> ref; |
|
1094 break; |
|
1095 |
|
1096 case 'x': |
|
1097 is >> std::hex >> ref; |
|
1098 break; |
|
1099 |
|
1100 default: |
|
1101 is >> ref; |
|
1102 break; |
|
1103 } |
3639
|
1104 |
3638
|
1105 return is; |
3636
|
1106 } |
|
1107 |
3779
|
1108 // Note that this specialization is only used for reading characters, not |
|
1109 // character strings. See BEGIN_S_CONVERSION for details. |
|
1110 |
|
1111 template<> |
|
1112 std::istream& |
4661
|
1113 octave_scan<> (std::istream& is, const scanf_format_elt& /* fmt */, |
|
1114 char* valptr) |
3779
|
1115 { |
|
1116 return is >> valptr; |
|
1117 } |
3636
|
1118 |
4595
|
1119 template std::istream& |
|
1120 octave_scan (std::istream&, const scanf_format_elt&, int*); |
|
1121 |
|
1122 template std::istream& |
|
1123 octave_scan (std::istream&, const scanf_format_elt&, long int*); |
|
1124 |
|
1125 template std::istream& |
|
1126 octave_scan (std::istream&, const scanf_format_elt&, short int*); |
|
1127 |
|
1128 template std::istream& |
|
1129 octave_scan (std::istream&, const scanf_format_elt&, unsigned int*); |
|
1130 |
|
1131 template std::istream& |
|
1132 octave_scan (std::istream&, const scanf_format_elt&, unsigned long int*); |
|
1133 |
|
1134 template std::istream& |
|
1135 octave_scan (std::istream&, const scanf_format_elt&, unsigned short int*); |
|
1136 |
|
1137 #if 0 |
|
1138 template std::istream& |
|
1139 octave_scan (std::istream&, const scanf_format_elt&, float*); |
|
1140 #endif |
|
1141 |
|
1142 template std::istream& |
|
1143 octave_scan (std::istream&, const scanf_format_elt&, double*); |
|
1144 |
3636
|
1145 #endif |
|
1146 |
2572
|
1147 template <class T> |
|
1148 void |
3636
|
1149 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt, |
|
1150 T valptr, Matrix& mval, double *data, int& idx, |
|
1151 int& conversion_count, int nr, int max_size, |
|
1152 bool discard) |
2572
|
1153 { |
3640
|
1154 OCTAVE_SCAN (is, fmt, valptr); |
2572
|
1155 |
|
1156 if (is) |
|
1157 { |
|
1158 if (idx == max_size && ! discard) |
|
1159 { |
|
1160 max_size *= 2; |
|
1161 |
|
1162 if (nr > 0) |
|
1163 mval.resize (nr, max_size / nr, 0.0); |
|
1164 else |
|
1165 mval.resize (max_size, 1, 0.0); |
|
1166 |
|
1167 data = mval.fortran_vec (); |
|
1168 } |
|
1169 |
|
1170 if (! discard) |
3268
|
1171 { |
3559
|
1172 conversion_count++; |
3268
|
1173 data[idx++] = *(valptr); |
|
1174 } |
2572
|
1175 } |
|
1176 } |
|
1177 |
|
1178 template void |
3878
|
1179 do_scanf_conv (std::istream&, const scanf_format_elt&, int*, |
|
1180 Matrix&, double*, int&, int&, int, int, bool); |
2572
|
1181 |
3233
|
1182 template void |
3636
|
1183 do_scanf_conv (std::istream&, const scanf_format_elt&, long int*, |
|
1184 Matrix&, double*, int&, int&, int, int, bool); |
3233
|
1185 |
|
1186 template void |
3636
|
1187 do_scanf_conv (std::istream&, const scanf_format_elt&, short int*, |
|
1188 Matrix&, double*, int&, int&, int, int, bool); |
3233
|
1189 |
3878
|
1190 template void |
|
1191 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned int*, |
|
1192 Matrix&, double*, int&, int&, int, int, bool); |
|
1193 |
|
1194 template void |
|
1195 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned long int*, |
|
1196 Matrix&, double*, int&, int&, int, int, bool); |
|
1197 |
|
1198 template void |
|
1199 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned short int*, |
|
1200 Matrix&, double*, int&, int&, int, int, bool); |
|
1201 |
2600
|
1202 #if 0 |
2572
|
1203 template void |
3636
|
1204 do_scanf_conv (std::istream&, const scanf_format_elt&, float*, |
|
1205 Matrix&, double*, int&, int&, int, int, bool); |
2600
|
1206 #endif |
2572
|
1207 |
|
1208 template void |
3636
|
1209 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
|
1210 Matrix&, double*, int&, int&, int, int, bool); |
2572
|
1211 |
3483
|
1212 #define DO_WHITESPACE_CONVERSION() \ |
|
1213 do \ |
|
1214 { \ |
|
1215 int c = EOF; \ |
|
1216 \ |
|
1217 while (is && (c = is.get ()) != EOF && isspace (c)) \ |
|
1218 /* skip whitespace */; \ |
|
1219 \ |
|
1220 if (c != EOF) \ |
|
1221 is.putback (c); \ |
|
1222 } \ |
|
1223 while (0) |
|
1224 |
|
1225 #define DO_LITERAL_CONVERSION() \ |
|
1226 do \ |
|
1227 { \ |
|
1228 int c = EOF; \ |
|
1229 \ |
|
1230 int n = strlen (fmt); \ |
|
1231 int i = 0; \ |
|
1232 \ |
|
1233 while (i < n && is && (c = is.get ()) != EOF) \ |
|
1234 { \ |
|
1235 if (c == fmt[i]) \ |
|
1236 { \ |
|
1237 i++; \ |
|
1238 continue; \ |
|
1239 } \ |
|
1240 else \ |
|
1241 { \ |
|
1242 is.putback (c); \ |
|
1243 break; \ |
|
1244 } \ |
|
1245 } \ |
|
1246 \ |
|
1247 if (i != n) \ |
3538
|
1248 is.setstate (std::ios::failbit); \ |
3483
|
1249 } \ |
|
1250 while (0) |
|
1251 |
3640
|
1252 #define DO_PCT_CONVERSION() \ |
|
1253 do \ |
|
1254 { \ |
|
1255 int c = is.get (); \ |
|
1256 \ |
|
1257 if (c != EOF) \ |
|
1258 { \ |
|
1259 if (c != '%') \ |
|
1260 { \ |
|
1261 is.putback (c); \ |
|
1262 is.setstate (std::ios::failbit); \ |
|
1263 } \ |
|
1264 } \ |
|
1265 else \ |
|
1266 is.setstate (std::ios::failbit); \ |
|
1267 } \ |
|
1268 while (0) |
|
1269 |
3483
|
1270 #define BEGIN_C_CONVERSION() \ |
3538
|
1271 is.unsetf (std::ios::skipws); \ |
3483
|
1272 \ |
|
1273 int width = elt->width ? elt->width : 1; \ |
|
1274 \ |
4051
|
1275 char *tbuf = new char[width + 1]; \ |
3483
|
1276 \ |
|
1277 int c = EOF; \ |
|
1278 int n = 0; \ |
|
1279 \ |
|
1280 while (is && n < width && (c = is.get ()) != EOF) \ |
4051
|
1281 tbuf[n++] = (char) c; \ |
|
1282 \ |
|
1283 tbuf[n] = '\0'; \ |
3483
|
1284 \ |
4051
|
1285 std::string tmp = tbuf; \ |
|
1286 \ |
|
1287 delete [] tbuf |
3483
|
1288 |
|
1289 // For a `%s' format, skip initial whitespace and then read until the |
|
1290 // next whitespace character. |
|
1291 #define BEGIN_S_CONVERSION() \ |
|
1292 int width = elt->width; \ |
|
1293 \ |
4051
|
1294 std::string tmp; \ |
3483
|
1295 \ |
|
1296 do \ |
|
1297 { \ |
|
1298 if (width) \ |
|
1299 { \ |
4051
|
1300 char *tbuf = new char [width+1]; \ |
|
1301 \ |
|
1302 OCTAVE_SCAN (is, *elt, tbuf); \ |
3483
|
1303 \ |
4051
|
1304 tbuf[width] = '\0'; \ |
|
1305 tmp = tbuf; \ |
|
1306 delete [] tbuf; \ |
3483
|
1307 } \ |
|
1308 else \ |
|
1309 { \ |
4051
|
1310 is >> std::ws >> tmp; \ |
3483
|
1311 } \ |
|
1312 } \ |
|
1313 while (0) |
|
1314 |
|
1315 // This format must match a nonempty sequence of characters. |
|
1316 #define BEGIN_CHAR_CLASS_CONVERSION() \ |
|
1317 int width = elt->width; \ |
|
1318 \ |
4051
|
1319 std::string tmp; \ |
3483
|
1320 \ |
|
1321 do \ |
|
1322 { \ |
|
1323 if (width) \ |
|
1324 { \ |
4051
|
1325 char *tbuf = new char[width+1]; \ |
|
1326 \ |
|
1327 OCTAVE_SCAN (is, *elt, tbuf); \ |
3483
|
1328 \ |
4051
|
1329 tbuf[width] = '\0'; \ |
|
1330 tmp = tbuf; \ |
|
1331 delete [] tbuf; \ |
3483
|
1332 } \ |
|
1333 else \ |
|
1334 { \ |
4051
|
1335 OSSTREAM buf; \ |
3483
|
1336 \ |
3523
|
1337 std::string char_class = elt->char_class; \ |
3483
|
1338 \ |
|
1339 int c = EOF; \ |
|
1340 \ |
|
1341 if (elt->type == '[') \ |
|
1342 { \ |
|
1343 while (is && (c = is.get ()) != EOF \ |
|
1344 && char_class.find (c) != NPOS) \ |
|
1345 buf << (char) c; \ |
|
1346 } \ |
|
1347 else \ |
|
1348 { \ |
|
1349 while (is && (c = is.get ()) != EOF \ |
|
1350 && char_class.find (c) == NPOS) \ |
|
1351 buf << (char) c; \ |
|
1352 } \ |
|
1353 \ |
|
1354 if (c != EOF) \ |
|
1355 is.putback (c); \ |
|
1356 \ |
4051
|
1357 buf << OSSTREAM_ENDS; \ |
|
1358 tmp = OSSTREAM_STR (buf); \ |
|
1359 OSSTREAM_FREEZE (buf); \ |
3483
|
1360 \ |
4051
|
1361 if (tmp.empty ()) \ |
3538
|
1362 is.setstate (std::ios::failbit); \ |
3483
|
1363 } \ |
|
1364 } \ |
|
1365 while (0) |
|
1366 |
3410
|
1367 #define FINISH_CHARACTER_CONVERSION() \ |
|
1368 do \ |
|
1369 { \ |
4051
|
1370 width = tmp.length (); \ |
3410
|
1371 \ |
|
1372 if (is) \ |
|
1373 { \ |
|
1374 int i = 0; \ |
|
1375 \ |
|
1376 if (! discard) \ |
|
1377 { \ |
|
1378 conversion_count++; \ |
|
1379 \ |
|
1380 while (i < width && tmp[i] != '\0') \ |
|
1381 { \ |
|
1382 if (data_index == max_size) \ |
|
1383 { \ |
|
1384 max_size *= 2; \ |
|
1385 \ |
4420
|
1386 if (all_char_conv) \ |
3410
|
1387 { \ |
4420
|
1388 if (one_elt_size_spec) \ |
3410
|
1389 mval.resize (1, max_size, 0.0); \ |
4420
|
1390 else if (nr > 0) \ |
|
1391 mval.resize (nr, max_size / nr, 0.0); \ |
3410
|
1392 else \ |
4420
|
1393 panic_impossible (); \ |
3410
|
1394 } \ |
4420
|
1395 else if (nr > 0) \ |
|
1396 { \ |
|
1397 if (nc <= 0) \ |
|
1398 mval.resize (nr, max_size / nr, 0.0); \ |
|
1399 else \ |
|
1400 panic_impossible (); \ |
|
1401 } \ |
|
1402 else \ |
|
1403 mval.resize (max_size, 1, 0.0); \ |
3410
|
1404 \ |
|
1405 data = mval.fortran_vec (); \ |
|
1406 } \ |
|
1407 \ |
|
1408 data[data_index++] = tmp[i++]; \ |
|
1409 } \ |
|
1410 } \ |
|
1411 } \ |
|
1412 } \ |
|
1413 while (0) |
2117
|
1414 |
|
1415 octave_value |
|
1416 octave_base_stream::do_scanf (scanf_format_list& fmt_list, |
3268
|
1417 int nr, int nc, bool one_elt_size_spec, |
4468
|
1418 int& conversion_count, const std::string& who) |
2117
|
1419 { |
3268
|
1420 conversion_count = 0; |
|
1421 |
3640
|
1422 int nconv = fmt_list.num_conversions (); |
|
1423 |
3268
|
1424 int data_index = 0; |
2121
|
1425 |
2117
|
1426 octave_value retval = Matrix (); |
|
1427 |
3268
|
1428 if (nr == 0 || nc == 0) |
|
1429 { |
|
1430 if (one_elt_size_spec) |
|
1431 nc = 0; |
|
1432 |
|
1433 return Matrix (nr, nc, 0.0); |
|
1434 } |
|
1435 |
3523
|
1436 std::istream *isp = input_stream (); |
2117
|
1437 |
|
1438 bool all_char_conv = fmt_list.all_character_conversions (); |
|
1439 |
|
1440 Matrix mval; |
|
1441 double *data = 0; |
|
1442 int max_size = 0; |
3268
|
1443 int max_conv = 0; |
2117
|
1444 |
|
1445 int final_nr = 0; |
|
1446 int final_nc = 0; |
|
1447 |
3268
|
1448 if (all_char_conv) |
|
1449 { |
4420
|
1450 // Any of these could be resized later (if we have %s |
|
1451 // conversions, we may read more than one element for each |
|
1452 // conversion). |
|
1453 |
3268
|
1454 if (one_elt_size_spec) |
|
1455 { |
3410
|
1456 max_size = 512; |
|
1457 mval.resize (1, max_size, 0.0); |
|
1458 |
3268
|
1459 if (nr > 0) |
|
1460 max_conv = nr; |
|
1461 } |
4420
|
1462 else if (nr > 0) |
3268
|
1463 { |
4420
|
1464 if (nc > 0) |
|
1465 { |
|
1466 mval.resize (nr, nc, 0.0); |
|
1467 max_size = max_conv = nr * nc; |
|
1468 } |
|
1469 else |
|
1470 { |
|
1471 mval.resize (nr, 32, 0.0); |
|
1472 max_size = nr * 32; |
|
1473 } |
3268
|
1474 } |
4420
|
1475 else |
|
1476 panic_impossible (); |
3268
|
1477 } |
|
1478 else if (nr > 0) |
2117
|
1479 { |
|
1480 if (nc > 0) |
|
1481 { |
4420
|
1482 // Will not resize later. |
2117
|
1483 mval.resize (nr, nc, 0.0); |
|
1484 max_size = nr * nc; |
3268
|
1485 max_conv = max_size; |
2117
|
1486 } |
|
1487 else |
|
1488 { |
4420
|
1489 // Maybe resize later. |
2117
|
1490 mval.resize (nr, 32, 0.0); |
2121
|
1491 max_size = nr * 32; |
2117
|
1492 } |
|
1493 } |
|
1494 else |
|
1495 { |
4420
|
1496 // Maybe resize later. |
2117
|
1497 mval.resize (32, 1, 0.0); |
|
1498 max_size = 32; |
|
1499 } |
|
1500 |
4420
|
1501 data = mval.fortran_vec (); |
|
1502 |
2117
|
1503 if (isp) |
|
1504 { |
3523
|
1505 std::istream& is = *isp; |
2117
|
1506 |
|
1507 const scanf_format_elt *elt = fmt_list.first (); |
|
1508 |
3538
|
1509 std::ios::fmtflags flags = is.flags (); |
2213
|
1510 |
2117
|
1511 for (;;) |
|
1512 { |
4153
|
1513 OCTAVE_QUIT; |
|
1514 |
2117
|
1515 if (elt) |
|
1516 { |
3268
|
1517 if (max_conv > 0 && conversion_count == max_conv) |
|
1518 { |
|
1519 if (all_char_conv && one_elt_size_spec) |
|
1520 { |
|
1521 final_nr = 1; |
|
1522 final_nc = data_index; |
|
1523 } |
|
1524 else |
|
1525 { |
|
1526 final_nr = nr; |
|
1527 final_nc = (data_index - 1) / nr + 1; |
|
1528 } |
|
1529 |
|
1530 break; |
|
1531 } |
|
1532 else if (data_index == max_size) |
2117
|
1533 { |
3410
|
1534 max_size *= 2; |
|
1535 |
4420
|
1536 if (all_char_conv) |
2687
|
1537 { |
4420
|
1538 if (one_elt_size_spec) |
3410
|
1539 mval.resize (1, max_size, 0.0); |
4420
|
1540 else if (nr > 0) |
|
1541 mval.resize (nr, max_size / nr, 0.0); |
3410
|
1542 else |
4420
|
1543 panic_impossible (); |
2687
|
1544 } |
4420
|
1545 else if (nr > 0) |
|
1546 { |
|
1547 if (nc <= 0) |
|
1548 mval.resize (nr, max_size / nr, 0.0); |
|
1549 else |
|
1550 panic_impossible (); |
|
1551 } |
|
1552 else |
|
1553 mval.resize (max_size, 1, 0.0); |
3410
|
1554 |
|
1555 data = mval.fortran_vec (); |
2117
|
1556 } |
|
1557 |
|
1558 const char *fmt = elt->text; |
|
1559 |
|
1560 bool discard = elt->discard; |
|
1561 |
|
1562 switch (elt->type) |
|
1563 { |
3483
|
1564 case scanf_format_elt::whitespace_conversion: |
|
1565 DO_WHITESPACE_CONVERSION (); |
|
1566 break; |
|
1567 |
|
1568 case scanf_format_elt::literal_conversion: |
|
1569 DO_LITERAL_CONVERSION (); |
|
1570 break; |
|
1571 |
2117
|
1572 case '%': |
3640
|
1573 DO_PCT_CONVERSION (); |
3483
|
1574 break; |
2117
|
1575 |
3878
|
1576 case 'd': case 'i': |
2117
|
1577 { |
3233
|
1578 switch (elt->modifier) |
|
1579 { |
|
1580 case 'h': |
|
1581 { |
|
1582 short 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 case 'l': |
|
1590 { |
|
1591 long 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 |
|
1598 default: |
|
1599 { |
|
1600 int tmp; |
3779
|
1601 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1602 data_index, conversion_count, |
3233
|
1603 nr, max_size, discard); |
|
1604 } |
3483
|
1605 break; |
3233
|
1606 } |
2117
|
1607 } |
3483
|
1608 break; |
2117
|
1609 |
3878
|
1610 case 'o': case 'u': case 'x': |
|
1611 { |
|
1612 switch (elt->modifier) |
|
1613 { |
|
1614 case 'h': |
|
1615 { |
|
1616 unsigned short 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 case 'l': |
|
1624 { |
|
1625 unsigned long 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 default: |
|
1633 { |
|
1634 unsigned int tmp; |
|
1635 do_scanf_conv (is, *elt, &tmp, mval, data, |
|
1636 data_index, conversion_count, |
|
1637 nr, max_size, discard); |
|
1638 } |
|
1639 break; |
|
1640 } |
|
1641 } |
|
1642 break; |
|
1643 |
2117
|
1644 case 'e': case 'f': case 'g': |
|
1645 { |
2600
|
1646 double tmp; |
|
1647 |
3779
|
1648 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268
|
1649 data_index, conversion_count, |
2600
|
1650 nr, max_size, discard); |
2117
|
1651 } |
3483
|
1652 break; |
2117
|
1653 |
2213
|
1654 case 'c': |
3410
|
1655 { |
3483
|
1656 BEGIN_C_CONVERSION (); |
3410
|
1657 |
|
1658 FINISH_CHARACTER_CONVERSION (); |
3483
|
1659 |
|
1660 is.setf (flags); |
3410
|
1661 } |
|
1662 break; |
2213
|
1663 |
2117
|
1664 case 's': |
|
1665 { |
3483
|
1666 BEGIN_S_CONVERSION (); |
3268
|
1667 |
3410
|
1668 FINISH_CHARACTER_CONVERSION (); |
2117
|
1669 } |
3483
|
1670 break; |
|
1671 |
|
1672 case '[': case '^': |
|
1673 { |
|
1674 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1675 |
|
1676 FINISH_CHARACTER_CONVERSION (); |
|
1677 } |
|
1678 break; |
|
1679 |
|
1680 case 'p': |
4468
|
1681 error ("%s: unsupported format specifier", who.c_str ()); |
2117
|
1682 break; |
|
1683 |
|
1684 default: |
4468
|
1685 error ("%s: internal format error", who.c_str ()); |
2117
|
1686 break; |
|
1687 } |
|
1688 |
|
1689 if (! ok ()) |
|
1690 { |
|
1691 break; |
|
1692 } |
|
1693 else if (! is) |
|
1694 { |
3268
|
1695 if (all_char_conv) |
2117
|
1696 { |
3268
|
1697 if (one_elt_size_spec) |
|
1698 { |
|
1699 final_nr = 1; |
|
1700 final_nc = data_index; |
|
1701 } |
|
1702 else if (data_index > nr) |
2117
|
1703 { |
2759
|
1704 final_nr = nr; |
3268
|
1705 final_nc = (data_index - 1) / nr + 1; |
2117
|
1706 } |
|
1707 else |
|
1708 { |
3268
|
1709 final_nr = data_index; |
|
1710 final_nc = 1; |
|
1711 } |
|
1712 } |
|
1713 else if (nr > 0) |
|
1714 { |
|
1715 if (data_index > nr) |
|
1716 { |
|
1717 final_nr = nr; |
|
1718 final_nc = (data_index - 1) / nr + 1; |
|
1719 } |
|
1720 else |
|
1721 { |
|
1722 final_nr = data_index; |
2117
|
1723 final_nc = 1; |
|
1724 } |
|
1725 } |
|
1726 else |
|
1727 { |
3268
|
1728 final_nr = data_index; |
2759
|
1729 final_nc = 1; |
|
1730 } |
|
1731 |
3337
|
1732 // If it looks like we have a matching failure, then |
|
1733 // reset the failbit in the stream state. |
|
1734 |
3538
|
1735 if (is.rdstate () & std::ios::failbit) |
|
1736 is.clear (is.rdstate () & (~std::ios::failbit)); |
3337
|
1737 |
2759
|
1738 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1739 |
|
1740 if (interactive && name () == "stdin") |
2759
|
1741 { |
|
1742 is.clear (); |
|
1743 |
|
1744 // Skip to end of line. |
|
1745 |
|
1746 bool err; |
4468
|
1747 do_gets (-1, err, false, who); |
2117
|
1748 } |
|
1749 |
|
1750 break; |
|
1751 } |
|
1752 } |
|
1753 else |
|
1754 { |
4468
|
1755 error ("%s: internal format error", who.c_str ()); |
2117
|
1756 break; |
|
1757 } |
|
1758 |
3640
|
1759 elt = fmt_list.next (nconv > 0); |
2117
|
1760 } |
|
1761 } |
|
1762 |
|
1763 if (ok ()) |
|
1764 { |
2121
|
1765 mval.resize (final_nr, final_nc, 0.0); |
2117
|
1766 |
3268
|
1767 retval = mval; |
|
1768 |
2117
|
1769 if (all_char_conv) |
3268
|
1770 retval = retval.convert_to_str (); |
2117
|
1771 } |
|
1772 |
|
1773 return retval; |
|
1774 } |
|
1775 |
|
1776 octave_value |
3810
|
1777 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
4468
|
1778 int& conversion_count, const std::string& who) |
2117
|
1779 { |
|
1780 octave_value retval = Matrix (); |
|
1781 |
3559
|
1782 conversion_count = 0; |
2117
|
1783 |
3523
|
1784 std::istream *isp = input_stream (); |
2117
|
1785 |
|
1786 if (isp) |
|
1787 { |
|
1788 scanf_format_list fmt_list (fmt); |
|
1789 |
3640
|
1790 if (fmt_list.num_conversions () == -1) |
4468
|
1791 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
1792 else |
2117
|
1793 { |
3640
|
1794 int nr = -1; |
|
1795 int nc = -1; |
|
1796 |
|
1797 bool one_elt_size_spec; |
|
1798 |
4468
|
1799 get_size (size, nr, nc, one_elt_size_spec, who); |
3640
|
1800 |
|
1801 if (! error_state) |
|
1802 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, |
4468
|
1803 conversion_count, who); |
2215
|
1804 } |
|
1805 } |
|
1806 else |
4468
|
1807 invalid_operation (who, "reading"); |
2572
|
1808 |
|
1809 return retval; |
|
1810 } |
|
1811 |
2712
|
1812 bool |
|
1813 octave_base_stream::do_oscanf (const scanf_format_elt *elt, |
4468
|
1814 octave_value& retval, const std::string& who) |
2572
|
1815 { |
2712
|
1816 bool quit = false; |
2215
|
1817 |
3523
|
1818 std::istream *isp = input_stream (); |
2215
|
1819 |
|
1820 if (isp) |
|
1821 { |
3523
|
1822 std::istream& is = *isp; |
2215
|
1823 |
3538
|
1824 std::ios::fmtflags flags = is.flags (); |
2215
|
1825 |
|
1826 if (elt) |
|
1827 { |
|
1828 const char *fmt = elt->text; |
|
1829 |
|
1830 bool discard = elt->discard; |
|
1831 |
|
1832 switch (elt->type) |
|
1833 { |
3483
|
1834 case scanf_format_elt::whitespace_conversion: |
|
1835 DO_WHITESPACE_CONVERSION (); |
|
1836 break; |
|
1837 |
|
1838 case scanf_format_elt::literal_conversion: |
|
1839 DO_LITERAL_CONVERSION (); |
|
1840 break; |
|
1841 |
2215
|
1842 case '%': |
|
1843 { |
3640
|
1844 DO_PCT_CONVERSION (); |
|
1845 |
|
1846 if (! is) |
2712
|
1847 quit = true; |
3640
|
1848 |
2215
|
1849 } |
|
1850 break; |
|
1851 |
3878
|
1852 case 'd': case 'i': |
2215
|
1853 { |
|
1854 int tmp; |
|
1855 |
3640
|
1856 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
1857 { |
|
1858 if (! discard) |
4233
|
1859 retval = tmp; |
2712
|
1860 } |
|
1861 else |
|
1862 quit = true; |
2215
|
1863 } |
|
1864 break; |
|
1865 |
3878
|
1866 case 'o': case 'u': case 'x': |
|
1867 { |
|
1868 long int tmp; |
|
1869 |
|
1870 if (OCTAVE_SCAN (is, *elt, &tmp)) |
|
1871 { |
|
1872 if (! discard) |
4254
|
1873 retval = tmp; |
3878
|
1874 } |
|
1875 else |
|
1876 quit = true; |
|
1877 } |
|
1878 break; |
|
1879 |
2215
|
1880 case 'e': case 'f': case 'g': |
|
1881 { |
2600
|
1882 double tmp; |
|
1883 |
3640
|
1884 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712
|
1885 { |
|
1886 if (! discard) |
|
1887 retval = tmp; |
|
1888 } |
|
1889 else |
|
1890 quit = true; |
2215
|
1891 } |
|
1892 break; |
|
1893 |
|
1894 case 'c': |
|
1895 { |
3483
|
1896 BEGIN_C_CONVERSION (); |
|
1897 |
|
1898 if (! discard) |
|
1899 retval = tmp; |
|
1900 |
|
1901 if (! is) |
2712
|
1902 quit = true; |
2215
|
1903 |
|
1904 is.setf (flags); |
|
1905 } |
|
1906 break; |
|
1907 |
|
1908 case 's': |
|
1909 { |
3483
|
1910 BEGIN_S_CONVERSION (); |
|
1911 |
|
1912 if (! discard) |
|
1913 retval = tmp; |
2572
|
1914 |
3268
|
1915 if (! is) |
|
1916 quit = true; |
2215
|
1917 } |
|
1918 break; |
|
1919 |
3483
|
1920 case '[': case '^': |
|
1921 { |
|
1922 BEGIN_CHAR_CLASS_CONVERSION (); |
|
1923 |
|
1924 if (! discard) |
|
1925 retval = tmp; |
|
1926 |
|
1927 if (! is) |
|
1928 quit = true; |
|
1929 } |
|
1930 break; |
|
1931 |
|
1932 case 'p': |
4468
|
1933 error ("%s: unsupported format specifier", who.c_str ()); |
2215
|
1934 break; |
|
1935 |
|
1936 default: |
4468
|
1937 error ("%s: internal format error", who.c_str ()); |
2215
|
1938 break; |
|
1939 } |
|
1940 } |
|
1941 |
|
1942 if (ok () && is.fail ()) |
|
1943 { |
4468
|
1944 error ("%s: read error", who.c_str ()); |
3483
|
1945 |
2215
|
1946 // XXX FIXME XXX -- is this the right thing to do? |
3342
|
1947 |
|
1948 if (interactive && name () == "stdin") |
2215
|
1949 { |
|
1950 // Skip to end of line. |
|
1951 |
|
1952 bool err; |
4468
|
1953 do_gets (-1, err, false, who); |
2215
|
1954 } |
|
1955 } |
|
1956 } |
|
1957 |
2712
|
1958 return quit; |
2215
|
1959 } |
|
1960 |
|
1961 octave_value_list |
4468
|
1962 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
1963 { |
|
1964 octave_value_list retval; |
|
1965 |
3523
|
1966 std::istream *isp = input_stream (); |
2215
|
1967 |
|
1968 if (isp) |
|
1969 { |
3523
|
1970 std::istream& is = *isp; |
2215
|
1971 |
|
1972 scanf_format_list fmt_list (fmt); |
|
1973 |
|
1974 int nconv = fmt_list.num_conversions (); |
|
1975 |
3640
|
1976 if (nconv == -1) |
4468
|
1977 ::error ("%s: invalid format specified", who.c_str ()); |
3640
|
1978 else |
2215
|
1979 { |
3640
|
1980 is.clear (); |
|
1981 |
|
1982 int len = fmt_list.length (); |
|
1983 |
|
1984 retval.resize (nconv+1, Matrix ()); |
|
1985 |
|
1986 const scanf_format_elt *elt = fmt_list.first (); |
|
1987 |
|
1988 int num_values = 0; |
|
1989 |
|
1990 bool quit = false; |
|
1991 |
|
1992 for (int i = 0; i < len; i++) |
|
1993 { |
|
1994 octave_value tmp; |
|
1995 |
4468
|
1996 quit = do_oscanf (elt, tmp, who); |
3640
|
1997 |
|
1998 if (quit) |
|
1999 break; |
|
2000 else |
|
2001 { |
|
2002 if (tmp.is_defined ()) |
|
2003 retval (num_values++) = tmp; |
|
2004 |
|
2005 if (! ok ()) |
|
2006 break; |
|
2007 |
|
2008 elt = fmt_list.next (nconv > 0); |
|
2009 } |
|
2010 } |
|
2011 |
4233
|
2012 retval(nconv) = num_values; |
3640
|
2013 |
|
2014 if (! quit) |
|
2015 { |
|
2016 // Pick up any trailing stuff. |
|
2017 if (ok () && len > nconv) |
|
2018 { |
|
2019 octave_value tmp; |
3704
|
2020 |
|
2021 elt = fmt_list.next (); |
|
2022 |
4468
|
2023 do_oscanf (elt, tmp, who); |
3640
|
2024 } |
|
2025 } |
2117
|
2026 } |
|
2027 } |
|
2028 else |
4468
|
2029 invalid_operation (who, "reading"); |
2117
|
2030 |
|
2031 return retval; |
|
2032 } |
|
2033 |
|
2034 // Functions that are defined for all output streams (output streams |
|
2035 // are those that define os). |
|
2036 |
|
2037 int |
|
2038 octave_base_stream::flush (void) |
|
2039 { |
|
2040 int retval = -1; |
|
2041 |
3523
|
2042 std::ostream *os = output_stream (); |
2117
|
2043 |
|
2044 if (os) |
|
2045 { |
|
2046 os->flush (); |
|
2047 |
|
2048 if (os->good ()) |
|
2049 retval = 0; |
|
2050 } |
|
2051 else |
|
2052 invalid_operation ("fflush", "writing"); |
|
2053 |
|
2054 return retval; |
|
2055 } |
|
2056 |
|
2057 int |
2317
|
2058 octave_base_stream::write (const octave_value& data, |
|
2059 oct_data_conv::data_type dt, int skip, |
3553
|
2060 oct_mach_info::float_format ffmt) |
2117
|
2061 { |
|
2062 int retval = -1; |
|
2063 |
3523
|
2064 std::ostream *osp = output_stream (); |
2117
|
2065 |
|
2066 if (osp) |
|
2067 { |
3523
|
2068 std::ostream& os = *osp; |
2117
|
2069 |
4691
|
2070 int status = 0; |
|
2071 |
|
2072 // XXX FIXME XXX -- the octave_value class should probably have |
|
2073 // a write method that would handle the dispatch for us? |
|
2074 // |
|
2075 // If DATA is a character matrix, then it is a bit of a kluge to |
|
2076 // force it to be a double matrix and then write it out as uchar |
|
2077 // data, but this is the quick fix... |
|
2078 |
|
2079 Matrix mval = data.matrix_value (true); |
2117
|
2080 |
|
2081 if (! error_state) |
|
2082 { |
4574
|
2083 if (ffmt == oct_mach_info::flt_fmt_unknown) |
3553
|
2084 ffmt = float_format (); |
|
2085 |
4691
|
2086 status = mval.write (os, dt, skip, ffmt); |
|
2087 |
|
2088 if (status < 0) |
2317
|
2089 error ("fwrite: write error"); |
|
2090 else |
4691
|
2091 retval = status; |
2117
|
2092 } |
|
2093 } |
|
2094 else |
|
2095 invalid_operation ("fwrite", "writing"); |
|
2096 |
|
2097 return retval; |
|
2098 } |
|
2099 |
|
2100 class |
|
2101 printf_value_cache |
|
2102 { |
|
2103 public: |
|
2104 |
3653
|
2105 enum state { ok, conversion_error }; |
2117
|
2106 |
|
2107 printf_value_cache (const octave_value_list& args) |
|
2108 : values (args), val_idx (0), elt_idx (0), |
|
2109 n_vals (values.length ()), n_elts (0), data (0), |
|
2110 curr_state (ok) { } |
|
2111 |
|
2112 ~printf_value_cache (void) { } |
|
2113 |
|
2114 // Get the current value as a double and advance the internal pointer. |
|
2115 double double_value (void); |
|
2116 |
|
2117 // Get the current value as an int and advance the internal pointer. |
|
2118 int int_value (void); |
|
2119 |
|
2120 // Get the current value as a string and advance the internal pointer. |
3523
|
2121 std::string string_value (void); |
2117
|
2122 |
3145
|
2123 operator bool () const { return (curr_state == ok); } |
2117
|
2124 |
3653
|
2125 bool exhausted (void) { return (val_idx >= n_vals); } |
2117
|
2126 |
|
2127 private: |
|
2128 |
|
2129 const octave_value_list values; |
|
2130 int val_idx; |
|
2131 int elt_idx; |
|
2132 int n_vals; |
|
2133 int n_elts; |
|
2134 const double *data; |
4874
|
2135 NDArray curr_val; |
2117
|
2136 state curr_state; |
|
2137 |
|
2138 // Must create value cache with values! |
|
2139 |
|
2140 printf_value_cache (void); |
|
2141 |
|
2142 // No copying! |
|
2143 |
|
2144 printf_value_cache (const printf_value_cache&); |
|
2145 |
|
2146 printf_value_cache& operator = (const printf_value_cache&); |
|
2147 }; |
|
2148 |
|
2149 double |
|
2150 printf_value_cache::double_value (void) |
|
2151 { |
|
2152 double retval = 0.0; |
|
2153 |
3711
|
2154 if (exhausted ()) |
|
2155 curr_state = conversion_error; |
|
2156 |
|
2157 while (! exhausted ()) |
2117
|
2158 { |
|
2159 if (! data) |
|
2160 { |
|
2161 octave_value tmp_val = values (val_idx); |
|
2162 |
4874
|
2163 curr_val = tmp_val.array_value (); |
2117
|
2164 |
|
2165 if (! error_state) |
|
2166 { |
|
2167 elt_idx = 0; |
|
2168 n_elts = curr_val.length (); |
|
2169 data = curr_val.data (); |
|
2170 } |
|
2171 else |
|
2172 { |
|
2173 curr_state = conversion_error; |
|
2174 break; |
|
2175 } |
|
2176 } |
|
2177 |
|
2178 if (elt_idx < n_elts) |
|
2179 { |
3653
|
2180 retval = data[elt_idx++]; |
|
2181 |
|
2182 if (elt_idx >= n_elts) |
|
2183 { |
|
2184 elt_idx = 0; |
|
2185 val_idx++; |
|
2186 data = 0; |
|
2187 } |
|
2188 |
2117
|
2189 break; |
|
2190 } |
|
2191 else |
|
2192 { |
|
2193 val_idx++; |
|
2194 data = 0; |
3969
|
2195 |
|
2196 if (n_elts == 0 && exhausted ()) |
|
2197 curr_state = conversion_error; |
|
2198 |
2117
|
2199 continue; |
|
2200 } |
|
2201 } |
|
2202 |
|
2203 return retval; |
|
2204 } |
|
2205 |
|
2206 int |
|
2207 printf_value_cache::int_value (void) |
|
2208 { |
|
2209 int retval = 0; |
|
2210 |
|
2211 double dval = double_value (); |
|
2212 |
|
2213 if (! error_state) |
|
2214 { |
|
2215 if (D_NINT (dval) == dval) |
|
2216 retval = NINT (dval); |
|
2217 else |
|
2218 curr_state = conversion_error; |
|
2219 } |
|
2220 |
|
2221 return retval; |
|
2222 } |
|
2223 |
3536
|
2224 std::string |
2117
|
2225 printf_value_cache::string_value (void) |
|
2226 { |
3523
|
2227 std::string retval; |
2117
|
2228 |
4425
|
2229 if (exhausted ()) |
|
2230 curr_state = conversion_error; |
4257
|
2231 else |
2117
|
2232 { |
4425
|
2233 octave_value tval = values (val_idx++); |
|
2234 |
|
2235 if (tval.rows () == 1) |
|
2236 retval = tval.string_value (); |
|
2237 else |
|
2238 { |
|
2239 // In the name of Matlab compatibility. |
|
2240 |
|
2241 charMatrix chm = tval.char_matrix_value (); |
|
2242 |
|
2243 int nr = chm.rows (); |
|
2244 int nc = chm.columns (); |
|
2245 |
|
2246 int k = 0; |
|
2247 |
|
2248 retval.resize (nr * nc, '\0'); |
|
2249 |
|
2250 for (int j = 0; j < nc; j++) |
|
2251 for (int i = 0; i < nr; i++) |
|
2252 retval[k++] = chm(i,j); |
|
2253 } |
|
2254 |
|
2255 if (error_state) |
|
2256 curr_state = conversion_error; |
2117
|
2257 } |
4257
|
2258 |
2117
|
2259 return retval; |
|
2260 } |
|
2261 |
|
2262 // Ugh again and again. |
|
2263 |
2572
|
2264 template <class T> |
3620
|
2265 int |
3523
|
2266 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
4468
|
2267 int sa_2, T arg, const std::string& who) |
2572
|
2268 { |
3620
|
2269 int retval = 0; |
|
2270 |
2572
|
2271 switch (nsa) |
|
2272 { |
|
2273 case 2: |
3640
|
2274 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572
|
2275 break; |
|
2276 |
|
2277 case 1: |
3640
|
2278 retval = octave_format (os, fmt, sa_1, arg); |
2572
|
2279 break; |
|
2280 |
|
2281 case 0: |
3640
|
2282 retval = octave_format (os, fmt, arg); |
2572
|
2283 break; |
|
2284 |
|
2285 default: |
4468
|
2286 ::error ("%s: internal error handling format", who.c_str ()); |
2572
|
2287 break; |
|
2288 } |
3620
|
2289 |
|
2290 return retval; |
2572
|
2291 } |
|
2292 |
3620
|
2293 template int |
4468
|
2294 do_printf_conv (std::ostream&, const char*, int, int, int, int, |
|
2295 const std::string&); |
|
2296 |
|
2297 template int |
|
2298 do_printf_conv (std::ostream&, const char*, int, int, int, long, |
|
2299 const std::string&); |
|
2300 |
3878
|
2301 template int |
4468
|
2302 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, |
|
2303 const std::string&); |
|
2304 |
|
2305 template int |
|
2306 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, |
|
2307 const std::string&); |
|
2308 |
|
2309 template int |
|
2310 do_printf_conv (std::ostream&, const char*, int, int, int, double, |
|
2311 const std::string&); |
|
2312 |
|
2313 template int |
|
2314 do_printf_conv (std::ostream&, const char*, int, int, int, const char*, |
|
2315 const std::string&); |
2117
|
2316 |
|
2317 int |
|
2318 octave_base_stream::do_printf (printf_format_list& fmt_list, |
4468
|
2319 const octave_value_list& args, |
|
2320 const std::string& who) |
2117
|
2321 { |
3620
|
2322 int retval = 0; |
2117
|
2323 |
3640
|
2324 int nconv = fmt_list.num_conversions (); |
|
2325 |
3523
|
2326 std::ostream *osp = output_stream (); |
2117
|
2327 |
|
2328 if (osp) |
|
2329 { |
3523
|
2330 std::ostream& os = *osp; |
2117
|
2331 |
|
2332 const printf_format_elt *elt = fmt_list.first (); |
|
2333 |
|
2334 printf_value_cache val_cache (args); |
|
2335 |
|
2336 for (;;) |
|
2337 { |
4153
|
2338 OCTAVE_QUIT; |
|
2339 |
2117
|
2340 if (elt) |
|
2341 { |
3640
|
2342 // NSA is the number of `star' args to convert. |
|
2343 |
|
2344 int nsa = (elt->fw < 0) + (elt->prec < 0); |
2117
|
2345 |
|
2346 int sa_1 = 0; |
|
2347 int sa_2 = 0; |
|
2348 |
|
2349 if (nsa > 0) |
|
2350 { |
|
2351 sa_1 = val_cache.int_value (); |
|
2352 |
|
2353 if (! val_cache) |
|
2354 break; |
|
2355 else |
|
2356 { |
|
2357 if (nsa > 1) |
|
2358 { |
|
2359 sa_2 = val_cache.int_value (); |
|
2360 |
|
2361 if (! val_cache) |
|
2362 break; |
|
2363 } |
|
2364 } |
|
2365 } |
|
2366 |
|
2367 const char *fmt = elt->text; |
|
2368 |
3640
|
2369 if (elt->type == '%') |
|
2370 { |
|
2371 os << "%"; |
|
2372 retval++; |
|
2373 } |
|
2374 else if (elt->args == 0 && elt->text) |
|
2375 { |
|
2376 os << elt->text; |
|
2377 retval += strlen (elt->text); |
|
2378 } |
4257
|
2379 else if (elt->type == 's') |
3640
|
2380 { |
|
2381 std::string val = val_cache.string_value (); |
|
2382 |
|
2383 if (val_cache) |
|
2384 retval += do_printf_conv (os, fmt, nsa, sa_1, |
4468
|
2385 sa_2, val.c_str (), who); |
3640
|
2386 else |
|
2387 break; |
|
2388 } |
2117
|
2389 else |
|
2390 { |
3640
|
2391 double val = val_cache.double_value (); |
|
2392 |
|
2393 if (val_cache) |
2117
|
2394 { |
4693
|
2395 if (lo_ieee_is_NaN_or_NA (val) || xisinf (val) |
|
2396 || ((val > INT_MAX || val < INT_MIN) |
|
2397 && (elt->type == 'd' |
|
2398 || elt->type == 'i' |
|
2399 || elt->type == 'c' |
|
2400 || elt->type == 'o' |
|
2401 || elt->type == 'x' |
|
2402 || elt->type == 'X' |
|
2403 || elt->type == 'u'))) |
4303
|
2404 { |
|
2405 std::string tfmt = fmt; |
|
2406 |
4693
|
2407 if (lo_ieee_is_NaN_or_NA (val) || xisinf (val)) |
4305
|
2408 { |
|
2409 tfmt.replace (tfmt.rfind (elt->type), 1, 1, 's'); |
|
2410 |
|
2411 const char *tval = xisinf (val) |
4693
|
2412 ? (val < 0 ? "-Inf" : "Inf") |
|
2413 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); |
4305
|
2414 |
|
2415 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2416 nsa, sa_1, sa_2, |
|
2417 tval, who); |
4305
|
2418 } |
|
2419 else |
|
2420 { |
|
2421 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); |
|
2422 |
|
2423 retval += do_printf_conv (os, tfmt.c_str (), |
4468
|
2424 nsa, sa_1, sa_2, |
|
2425 val, who); |
4305
|
2426 } |
4303
|
2427 } |
|
2428 else |
3640
|
2429 { |
4303
|
2430 switch (elt->type) |
|
2431 { |
|
2432 case 'd': case 'i': case 'c': |
|
2433 { |
|
2434 if (elt->modifier == 'l') |
|
2435 retval += do_printf_conv |
|
2436 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2437 static_cast<long int> (val), who); |
4303
|
2438 else |
|
2439 retval += do_printf_conv |
|
2440 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2441 static_cast<int> (val), who); |
4303
|
2442 } |
|
2443 break; |
|
2444 |
|
2445 case 'o': case 'x': case 'X': case 'u': |
|
2446 { |
|
2447 if (elt->modifier == 'l') |
|
2448 retval += do_printf_conv |
|
2449 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2450 static_cast<unsigned long int> (val), |
|
2451 who); |
4303
|
2452 else |
|
2453 retval += do_printf_conv |
|
2454 (os, fmt, nsa, sa_1, sa_2, |
4468
|
2455 static_cast<unsigned int> (val), who); |
4303
|
2456 } |
|
2457 break; |
|
2458 |
|
2459 case 'f': case 'e': case 'E': |
|
2460 case 'g': case 'G': |
|
2461 retval |
|
2462 += do_printf_conv (os, fmt, nsa, sa_1, sa_2, |
4468
|
2463 val, who); |
4303
|
2464 break; |
|
2465 |
|
2466 default: |
4468
|
2467 error ("%s: invalid format specifier", |
|
2468 who.c_str ()); |
4303
|
2469 return -1; |
|
2470 break; |
|
2471 } |
3640
|
2472 } |
2117
|
2473 } |
|
2474 else |
3620
|
2475 break; |
2117
|
2476 } |
|
2477 |
3620
|
2478 if (! os) |
2117
|
2479 { |
4468
|
2480 error ("%s: write error", who.c_str ()); |
2117
|
2481 break; |
|
2482 } |
|
2483 } |
|
2484 else |
|
2485 { |
4468
|
2486 ::error ("%s: internal error handling format", who.c_str ()); |
2117
|
2487 retval = -1; |
|
2488 break; |
|
2489 } |
|
2490 |
3640
|
2491 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
|
2492 |
|
2493 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
|
2494 break; |
|
2495 } |
2117
|
2496 } |
3640
|
2497 else |
4468
|
2498 invalid_operation (who, "writing"); |
2117
|
2499 |
|
2500 return retval; |
|
2501 } |
|
2502 |
|
2503 int |
3640
|
2504 octave_base_stream::printf (const std::string& fmt, |
4468
|
2505 const octave_value_list& args, |
|
2506 const std::string& who) |
2117
|
2507 { |
3640
|
2508 int retval = 0; |
|
2509 |
|
2510 printf_format_list fmt_list (fmt); |
|
2511 |
|
2512 if (fmt_list.num_conversions () == -1) |
4468
|
2513 ::error ("%s: invalid format specified", who.c_str ()); |
2117
|
2514 else |
4468
|
2515 retval = do_printf (fmt_list, args, who); |
2117
|
2516 |
|
2517 return retval; |
|
2518 } |
|
2519 |
|
2520 int |
4468
|
2521 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117
|
2522 { |
|
2523 int retval = -1; |
|
2524 |
3523
|
2525 std::ostream *osp = output_stream (); |
2117
|
2526 |
|
2527 if (osp) |
|
2528 { |
3523
|
2529 std::ostream& os = *osp; |
2117
|
2530 |
|
2531 os << s; |
|
2532 |
|
2533 if (os) |
|
2534 { |
|
2535 // XXX FIXME XXX -- why does this seem to be necessary? |
|
2536 // Without it, output from a loop like |
|
2537 // |
|
2538 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
|
2539 // |
|
2540 // doesn't seem to go to the pager immediately. |
|
2541 |
|
2542 os.flush (); |
|
2543 |
|
2544 if (os) |
|
2545 retval = 0; |
|
2546 else |
4468
|
2547 error ("%s: write error", who.c_str ()); |
2117
|
2548 } |
|
2549 else |
4468
|
2550 error ("%s: write error", who.c_str ()); |
2117
|
2551 } |
|
2552 else |
4468
|
2553 invalid_operation (who, "writing"); |
2117
|
2554 |
|
2555 return retval; |
|
2556 } |
|
2557 |
|
2558 int |
|
2559 octave_base_stream::rewind (void) |
|
2560 { |
3538
|
2561 return seek (0, std::ios::beg); |
2117
|
2562 } |
|
2563 |
|
2564 // Return current error message for this stream. |
|
2565 |
3536
|
2566 std::string |
2435
|
2567 octave_base_stream::error (bool clear_err, int& err_num) |
2117
|
2568 { |
2435
|
2569 err_num = fail ? -1 : 0; |
2117
|
2570 |
3523
|
2571 std::string tmp = errmsg; |
2117
|
2572 |
|
2573 if (clear_err) |
|
2574 clear (); |
|
2575 |
|
2576 return tmp; |
|
2577 } |
|
2578 |
|
2579 void |
4468
|
2580 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117
|
2581 { |
4468
|
2582 // Note that this is not ::error () ! |
|
2583 |
|
2584 error (who + ": stream not open for " + rw); |
2117
|
2585 } |
|
2586 |
3552
|
2587 octave_stream::octave_stream (octave_base_stream *bs) |
3340
|
2588 : rep (bs) |
|
2589 { |
|
2590 if (rep) |
|
2591 rep->count = 1; |
|
2592 } |
|
2593 |
|
2594 octave_stream::~octave_stream (void) |
|
2595 { |
|
2596 if (rep && --rep->count == 0) |
|
2597 delete rep; |
|
2598 } |
|
2599 |
|
2600 octave_stream::octave_stream (const octave_stream& s) |
|
2601 : rep (s.rep) |
|
2602 { |
|
2603 if (rep) |
|
2604 rep->count++; |
|
2605 } |
|
2606 |
|
2607 octave_stream& |
|
2608 octave_stream::operator = (const octave_stream& s) |
|
2609 { |
|
2610 if (rep != s.rep) |
|
2611 { |
|
2612 if (rep && --rep->count == 0) |
|
2613 delete rep; |
|
2614 |
|
2615 rep = s.rep; |
|
2616 |
|
2617 if (rep) |
|
2618 rep->count++; |
|
2619 } |
|
2620 |
|
2621 return *this; |
|
2622 } |
|
2623 |
2117
|
2624 int |
|
2625 octave_stream::flush (void) |
|
2626 { |
|
2627 int retval = -1; |
|
2628 |
|
2629 if (stream_ok ("fflush")) |
|
2630 retval = rep->flush (); |
|
2631 |
|
2632 return retval; |
|
2633 } |
|
2634 |
3536
|
2635 std::string |
4468
|
2636 octave_stream::getl (int max_len, bool& err, const std::string& who) |
2117
|
2637 { |
3523
|
2638 std::string retval; |
2117
|
2639 |
4468
|
2640 if (stream_ok (who)) |
|
2641 retval = rep->getl (max_len, err, who); |
2117
|
2642 |
|
2643 return retval; |
|
2644 } |
|
2645 |
3536
|
2646 std::string |
4468
|
2647 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
|
2648 const std::string& who) |
2117
|
2649 { |
3523
|
2650 std::string retval; |
2117
|
2651 |
|
2652 err = false; |
|
2653 |
|
2654 int conv_err = 0; |
|
2655 |
|
2656 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2657 |
|
2658 if (conv_err || max_len < 0) |
|
2659 { |
|
2660 err = true; |
4468
|
2661 ::error ("%s: invalid maximum length specified", who.c_str ()); |
2117
|
2662 } |
|
2663 else |
4468
|
2664 retval = getl (max_len, err, who); |
2117
|
2665 |
|
2666 return retval; |
|
2667 } |
|
2668 |
3536
|
2669 std::string |
4468
|
2670 octave_stream::gets (int max_len, bool& err, const std::string& who) |
2117
|
2671 { |
3523
|
2672 std::string retval; |
2117
|
2673 |
4468
|
2674 if (stream_ok (who)) |
|
2675 retval = rep->gets (max_len, err, who); |
2117
|
2676 |
|
2677 return retval; |
|
2678 } |
|
2679 |
3536
|
2680 std::string |
4468
|
2681 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
|
2682 const std::string& who) |
2117
|
2683 { |
3523
|
2684 std::string retval; |
2117
|
2685 |
|
2686 err = false; |
|
2687 |
|
2688 int conv_err = 0; |
|
2689 |
|
2690 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2691 |
|
2692 if (conv_err || max_len < 0) |
|
2693 { |
|
2694 err = true; |
4468
|
2695 ::error ("%s: invalid maximum length specified", who.c_str ()); |
2117
|
2696 } |
|
2697 else |
4468
|
2698 retval = gets (max_len, err, who); |
2117
|
2699 |
|
2700 return retval; |
|
2701 } |
|
2702 |
|
2703 int |
4797
|
2704 octave_stream::seek (long offset, int origin) |
2117
|
2705 { |
|
2706 int retval = -1; |
|
2707 |
|
2708 if (stream_ok ("fseek")) |
|
2709 retval = rep->seek (offset, origin); |
|
2710 |
|
2711 return retval; |
|
2712 } |
|
2713 |
|
2714 int |
|
2715 octave_stream::seek (const octave_value& tc_offset, |
|
2716 const octave_value& tc_origin) |
|
2717 { |
|
2718 int retval = -1; |
|
2719 |
4797
|
2720 long xoffset = tc_offset.long_value (true); |
4645
|
2721 |
|
2722 if (! error_state) |
2117
|
2723 { |
4645
|
2724 int conv_err = 0; |
|
2725 |
4797
|
2726 int origin = SEEK_SET; |
2117
|
2727 |
2341
|
2728 if (tc_origin.is_string ()) |
2117
|
2729 { |
3523
|
2730 std::string xorigin = tc_origin.string_value (); |
2341
|
2731 |
|
2732 if (xorigin == "bof") |
4797
|
2733 origin = SEEK_SET; |
2341
|
2734 else if (xorigin == "cof") |
4797
|
2735 origin = SEEK_CUR; |
2341
|
2736 else if (xorigin == "eof") |
4797
|
2737 origin = SEEK_END; |
2117
|
2738 else |
|
2739 conv_err = -1; |
|
2740 } |
2341
|
2741 else |
|
2742 { |
|
2743 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
|
2744 |
|
2745 if (! conv_err) |
|
2746 { |
|
2747 if (xorigin == -1) |
4797
|
2748 origin = SEEK_SET; |
2341
|
2749 else if (xorigin == 0) |
4797
|
2750 origin = SEEK_CUR; |
2341
|
2751 else if (xorigin == 1) |
4797
|
2752 origin = SEEK_END; |
2341
|
2753 else |
|
2754 conv_err = -1; |
|
2755 } |
|
2756 } |
2117
|
2757 |
|
2758 if (! conv_err) |
|
2759 retval = seek (xoffset, origin); |
|
2760 else |
|
2761 error ("fseek: invalid value for origin"); |
|
2762 } |
|
2763 else |
|
2764 error ("fseek: invalid value for offset"); |
|
2765 |
|
2766 return retval; |
|
2767 } |
|
2768 |
4797
|
2769 long |
|
2770 octave_stream::tell (void) |
2117
|
2771 { |
4797
|
2772 long retval = -1; |
2117
|
2773 |
|
2774 if (stream_ok ("tell")) |
|
2775 retval = rep->tell (); |
|
2776 |
|
2777 return retval; |
|
2778 } |
|
2779 |
|
2780 int |
|
2781 octave_stream::rewind (void) |
|
2782 { |
|
2783 int retval = -1; |
|
2784 |
|
2785 if (stream_ok ("frewind")) |
|
2786 retval = rep->rewind (); |
|
2787 |
|
2788 return retval; |
|
2789 } |
|
2790 |
3340
|
2791 bool |
|
2792 octave_stream::is_open (void) const |
|
2793 { |
|
2794 bool retval = false; |
|
2795 |
|
2796 if (stream_ok ("is_open")) |
|
2797 retval = rep->is_open (); |
|
2798 |
|
2799 return retval; |
|
2800 } |
|
2801 |
|
2802 void |
|
2803 octave_stream::close (void) |
|
2804 { |
|
2805 if (stream_ok ("close")) |
|
2806 rep->close (); |
|
2807 } |
|
2808 |
2117
|
2809 octave_value |
3810
|
2810 octave_stream::read (const Array<double>& size, |
2317
|
2811 oct_data_conv::data_type dt, int skip, |
|
2812 oct_mach_info::float_format flt_fmt, int& count) |
2117
|
2813 { |
|
2814 octave_value retval; |
|
2815 |
|
2816 if (stream_ok ("fread")) |
2317
|
2817 retval = rep->read (size, dt, skip, flt_fmt, count); |
2117
|
2818 |
|
2819 return retval; |
|
2820 } |
|
2821 |
|
2822 int |
|
2823 octave_stream::write (const octave_value& data, |
2317
|
2824 oct_data_conv::data_type dt, int skip, |
|
2825 oct_mach_info::float_format flt_fmt) |
2117
|
2826 { |
|
2827 int retval = -1; |
|
2828 |
|
2829 if (stream_ok ("fwrite")) |
2317
|
2830 retval = rep->write (data, dt, skip, flt_fmt); |
2117
|
2831 |
|
2832 return retval; |
|
2833 } |
|
2834 |
|
2835 octave_value |
3810
|
2836 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
4468
|
2837 int& count, const std::string& who) |
2117
|
2838 { |
|
2839 octave_value retval; |
|
2840 |
4468
|
2841 if (stream_ok (who)) |
|
2842 retval = rep->scanf (fmt, size, count, who); |
2117
|
2843 |
|
2844 return retval; |
|
2845 } |
|
2846 |
2215
|
2847 octave_value_list |
4468
|
2848 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215
|
2849 { |
|
2850 octave_value_list retval; |
|
2851 |
4468
|
2852 if (stream_ok (who)) |
|
2853 retval = rep->oscanf (fmt, who); |
2215
|
2854 |
|
2855 return retval; |
|
2856 } |
|
2857 |
2117
|
2858 int |
4468
|
2859 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
|
2860 const std::string& who) |
2117
|
2861 { |
|
2862 int retval = -1; |
|
2863 |
4468
|
2864 if (stream_ok (who)) |
|
2865 retval = rep->printf (fmt, args, who); |
2117
|
2866 |
|
2867 return retval; |
|
2868 } |
|
2869 |
|
2870 int |
4468
|
2871 octave_stream::puts (const std::string& s, const std::string& who) |
2117
|
2872 { |
|
2873 int retval = -1; |
|
2874 |
4468
|
2875 if (stream_ok (who)) |
|
2876 retval = rep->puts (s, who); |
2117
|
2877 |
|
2878 return retval; |
|
2879 } |
|
2880 |
|
2881 // XXX FIXME XXX -- maybe this should work for string arrays too. |
|
2882 |
|
2883 int |
4468
|
2884 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117
|
2885 { |
|
2886 int retval = -1; |
|
2887 |
|
2888 if (tc_s.is_string ()) |
|
2889 { |
3523
|
2890 std::string s = tc_s.string_value (); |
4468
|
2891 retval = rep->puts (s, who); |
2117
|
2892 } |
|
2893 else |
4468
|
2894 { |
|
2895 // Note that this is not ::error () ! |
|
2896 |
|
2897 error (who + ": argument must be a string"); |
|
2898 } |
2117
|
2899 |
|
2900 return retval; |
|
2901 } |
|
2902 |
|
2903 bool |
|
2904 octave_stream::eof (void) const |
|
2905 { |
|
2906 int retval = -1; |
|
2907 |
|
2908 if (stream_ok ("feof")) |
|
2909 retval = rep->eof (); |
|
2910 |
|
2911 return retval; |
|
2912 } |
|
2913 |
3536
|
2914 std::string |
2435
|
2915 octave_stream::error (bool clear, int& err_num) |
2117
|
2916 { |
3523
|
2917 std::string retval; |
2117
|
2918 |
|
2919 if (stream_ok ("ferror", false)) |
2435
|
2920 retval = rep->error (clear, err_num); |
2117
|
2921 |
|
2922 return retval; |
|
2923 } |
|
2924 |
3536
|
2925 std::string |
3340
|
2926 octave_stream::name (void) const |
2117
|
2927 { |
3523
|
2928 std::string retval; |
2117
|
2929 |
|
2930 if (stream_ok ("name")) |
|
2931 retval = rep->name (); |
|
2932 |
|
2933 return retval; |
|
2934 } |
|
2935 |
|
2936 int |
3340
|
2937 octave_stream::mode (void) const |
2117
|
2938 { |
|
2939 int retval = 0; |
|
2940 |
|
2941 if (stream_ok ("mode")) |
|
2942 retval = rep->mode (); |
|
2943 |
|
2944 return retval; |
|
2945 } |
|
2946 |
2317
|
2947 oct_mach_info::float_format |
3340
|
2948 octave_stream::float_format (void) const |
2117
|
2949 { |
4574
|
2950 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317
|
2951 |
|
2952 if (stream_ok ("float_format")) |
|
2953 retval = rep->float_format (); |
2117
|
2954 |
|
2955 return retval; |
|
2956 } |
|
2957 |
3536
|
2958 std::string |
2117
|
2959 octave_stream::mode_as_string (int mode) |
|
2960 { |
3523
|
2961 std::string retval = "???"; |
3775
|
2962 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
|
2963 |
|
2964 if (in_mode == std::ios::in) |
|
2965 retval = "r"; |
|
2966 else if (in_mode == std::ios::out |
4078
|
2967 || in_mode == (std::ios::out | std::ios::trunc)) |
3775
|
2968 retval = "w"; |
4078
|
2969 else if (in_mode == (std::ios::out | std::ios::app)) |
3775
|
2970 retval = "a"; |
4078
|
2971 else if (in_mode == (std::ios::in | std::ios::out)) |
3775
|
2972 retval = "r+"; |
4078
|
2973 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775
|
2974 retval = "w+"; |
4078
|
2975 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775
|
2976 retval = "a+"; |
4078
|
2977 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775
|
2978 retval = "rb"; |
4078
|
2979 else if (in_mode == (std::ios::out | std::ios::binary) |
|
2980 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775
|
2981 retval = "wb"; |
4078
|
2982 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775
|
2983 retval = "ab"; |
4078
|
2984 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775
|
2985 retval = "r+b"; |
4078
|
2986 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
|
2987 | std::ios::binary)) |
3775
|
2988 retval = "w+b"; |
4078
|
2989 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
|
2990 | std::ios::binary)) |
3775
|
2991 retval = "a+b"; |
2117
|
2992 |
|
2993 return retval; |
|
2994 } |
|
2995 |
|
2996 void |
4468
|
2997 octave_stream::invalid_stream_error (const std::string& who) const |
2117
|
2998 { |
4468
|
2999 ::error ("%s: attempt to use invalid I/O stream", who.c_str ()); |
2117
|
3000 } |
|
3001 |
|
3002 octave_stream_list *octave_stream_list::instance = 0; |
|
3003 |
2926
|
3004 bool |
|
3005 octave_stream_list::instance_ok (void) |
|
3006 { |
|
3007 bool retval = true; |
|
3008 |
|
3009 if (! instance) |
|
3010 instance = new octave_stream_list (); |
|
3011 |
|
3012 if (! instance) |
|
3013 { |
|
3014 ::error ("unable to create stream list object!"); |
|
3015 |
|
3016 retval = false; |
|
3017 } |
|
3018 |
|
3019 return retval; |
|
3020 } |
|
3021 |
|
3022 octave_value |
3340
|
3023 octave_stream_list::insert (const octave_stream& os) |
2926
|
3024 { |
3340
|
3025 return (instance_ok ()) ? instance->do_insert (os) : octave_value (-1.0); |
2926
|
3026 } |
|
3027 |
3340
|
3028 octave_stream |
3523
|
3029 octave_stream_list::lookup (int fid, const std::string& who) |
2926
|
3030 { |
3341
|
3031 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3032 } |
|
3033 |
3340
|
3034 octave_stream |
3523
|
3035 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926
|
3036 { |
3341
|
3037 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926
|
3038 } |
|
3039 |
|
3040 int |
3523
|
3041 octave_stream_list::remove (int fid, const std::string& who) |
2926
|
3042 { |
3341
|
3043 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3044 } |
|
3045 |
|
3046 int |
3523
|
3047 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926
|
3048 { |
3341
|
3049 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926
|
3050 } |
|
3051 |
|
3052 void |
|
3053 octave_stream_list::clear (void) |
|
3054 { |
|
3055 if (instance) |
|
3056 instance->do_clear (); |
|
3057 } |
|
3058 |
|
3059 string_vector |
|
3060 octave_stream_list::get_info (int fid) |
|
3061 { |
|
3062 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3063 } |
|
3064 |
|
3065 string_vector |
|
3066 octave_stream_list::get_info (const octave_value& fid) |
|
3067 { |
|
3068 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); |
|
3069 } |
|
3070 |
3536
|
3071 std::string |
2926
|
3072 octave_stream_list::list_open_files (void) |
|
3073 { |
3523
|
3074 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926
|
3075 } |
|
3076 |
|
3077 octave_value |
|
3078 octave_stream_list::open_file_numbers (void) |
|
3079 { |
|
3080 return (instance_ok ()) |
|
3081 ? instance->do_open_file_numbers () : octave_value (); |
|
3082 } |
|
3083 |
|
3084 int |
|
3085 octave_stream_list::get_file_number (const octave_value& fid) |
|
3086 { |
|
3087 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; |
|
3088 } |
|
3089 |
2902
|
3090 octave_value |
3340
|
3091 octave_stream_list::do_insert (const octave_stream& os) |
2117
|
3092 { |
3340
|
3093 octave_value retval; |
|
3094 |
2902
|
3095 int stream_number = -1; |
|
3096 |
3340
|
3097 // Insert item in first open slot, increasing size of list if |
|
3098 // necessary. |
|
3099 |
|
3100 for (int i = 0; i < curr_len; i++) |
2117
|
3101 { |
3340
|
3102 octave_stream tmp = list(i); |
|
3103 |
|
3104 if (! tmp) |
2117
|
3105 { |
3340
|
3106 list(i) = os; |
|
3107 stream_number = i; |
|
3108 break; |
2117
|
3109 } |
|
3110 } |
3340
|
3111 |
|
3112 if (stream_number < 0) |
|
3113 { |
|
3114 int total_len = list.length (); |
|
3115 |
|
3116 if (curr_len == total_len) |
|
3117 list.resize (total_len * 2); |
|
3118 |
|
3119 list(curr_len) = os; |
|
3120 stream_number = curr_len; |
|
3121 curr_len++; |
|
3122 } |
2117
|
3123 |
2902
|
3124 return octave_value (os, stream_number); |
2117
|
3125 } |
|
3126 |
3341
|
3127 static void |
3523
|
3128 gripe_invalid_file_id (int fid, const std::string& who) |
3341
|
3129 { |
|
3130 if (who.empty ()) |
|
3131 ::error ("invalid stream number = %d", fid); |
|
3132 else |
|
3133 ::error ("%s: invalid stream number = %d", who.c_str (), fid); |
|
3134 } |
|
3135 |
3340
|
3136 octave_stream |
3523
|
3137 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117
|
3138 { |
3340
|
3139 octave_stream retval; |
2117
|
3140 |
|
3141 if (fid >= 0 && fid < curr_len) |
3340
|
3142 retval = list(fid); |
3341
|
3143 else |
|
3144 gripe_invalid_file_id (fid, who); |
2117
|
3145 |
|
3146 return retval; |
|
3147 } |
|
3148 |
3340
|
3149 octave_stream |
3341
|
3150 octave_stream_list::do_lookup (const octave_value& fid, |
3523
|
3151 const std::string& who) const |
2117
|
3152 { |
3340
|
3153 octave_stream retval; |
2117
|
3154 |
|
3155 int i = get_file_number (fid); |
|
3156 |
|
3157 if (! error_state) |
3341
|
3158 retval = do_lookup (i, who); |
2117
|
3159 |
|
3160 return retval; |
|
3161 } |
|
3162 |
|
3163 int |
3523
|
3164 octave_stream_list::do_remove (int fid, const std::string& who) |
2117
|
3165 { |
|
3166 int retval = -1; |
|
3167 |
3531
|
3168 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
|
3169 // (std::cerr). |
2117
|
3170 |
|
3171 if (fid > 2 && fid < curr_len) |
|
3172 { |
3340
|
3173 octave_stream os = list(fid); |
2117
|
3174 |
3341
|
3175 if (os.is_valid ()) |
2117
|
3176 { |
3340
|
3177 os.close (); |
|
3178 list(fid) = octave_stream (); |
2117
|
3179 retval = 0; |
|
3180 } |
3341
|
3181 else |
|
3182 gripe_invalid_file_id (fid, who); |
2117
|
3183 } |
3341
|
3184 else |
|
3185 gripe_invalid_file_id (fid, who); |
2117
|
3186 |
|
3187 return retval; |
|
3188 } |
|
3189 |
|
3190 int |
3523
|
3191 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117
|
3192 { |
|
3193 int retval = -1; |
|
3194 |
|
3195 int i = get_file_number (fid); |
|
3196 |
|
3197 if (! error_state) |
3341
|
3198 retval = do_remove (i, who); |
2117
|
3199 |
|
3200 return retval; |
|
3201 } |
|
3202 |
|
3203 void |
|
3204 octave_stream_list::do_clear (void) |
|
3205 { |
|
3206 // Do flush stdout and stderr. |
|
3207 |
3340
|
3208 list(0) . flush (); |
|
3209 list(1) . flush (); |
2117
|
3210 |
|
3211 // But don't delete them or stdin. |
|
3212 |
|
3213 for (int i = 3; i < curr_len; i++) |
3340
|
3214 list(i) = octave_stream (); |
2117
|
3215 } |
|
3216 |
|
3217 string_vector |
|
3218 octave_stream_list::do_get_info (int fid) const |
|
3219 { |
|
3220 string_vector retval; |
|
3221 |
3340
|
3222 octave_stream os = do_lookup (fid); |
2117
|
3223 |
3341
|
3224 if (os.is_valid ()) |
2117
|
3225 { |
|
3226 retval.resize (3); |
|
3227 |
3340
|
3228 retval(0) = os.name (); |
|
3229 retval(1) = octave_stream::mode_as_string (os.mode ()); |
|
3230 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
2117
|
3231 } |
|
3232 else |
3341
|
3233 ::error ("invalid file id = %d", fid); |
2117
|
3234 |
|
3235 return retval; |
|
3236 } |
|
3237 |
|
3238 string_vector |
|
3239 octave_stream_list::do_get_info (const octave_value& fid) const |
|
3240 { |
|
3241 string_vector retval; |
|
3242 |
|
3243 int conv_err = 0; |
|
3244 |
|
3245 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3246 |
|
3247 if (! conv_err) |
|
3248 retval = do_get_info (int_fid); |
|
3249 else |
2915
|
3250 ::error ("file id must be a file object or integer value"); |
2117
|
3251 |
|
3252 return retval; |
|
3253 } |
|
3254 |
3536
|
3255 std::string |
2117
|
3256 octave_stream_list::do_list_open_files (void) const |
|
3257 { |
3523
|
3258 std::string retval; |
2117
|
3259 |
4051
|
3260 OSSTREAM buf; |
2117
|
3261 |
|
3262 buf << "\n" |
|
3263 << " number mode arch name\n" |
|
3264 << " ------ ---- ---- ----\n"; |
|
3265 |
|
3266 for (int i = 0; i < curr_len; i++) |
|
3267 { |
3340
|
3268 octave_stream os = list(i); |
2117
|
3269 |
4326
|
3270 buf << " " |
|
3271 << std::setiosflags (std::ios::right) |
|
3272 << std::setw (4) << i << " " |
|
3273 << std::setiosflags (std::ios::left) |
|
3274 << std::setw (3) |
|
3275 << octave_stream::mode_as_string (os.mode ()) |
|
3276 << " " |
|
3277 << std::setw (9) |
|
3278 << oct_mach_info::float_format_as_string (os.float_format ()) |
|
3279 << " " |
|
3280 << os.name () << "\n"; |
2117
|
3281 } |
|
3282 |
4051
|
3283 buf << "\n" << OSSTREAM_ENDS; |
|
3284 |
|
3285 retval = OSSTREAM_STR (buf); |
|
3286 |
|
3287 OSSTREAM_FREEZE (buf); |
2117
|
3288 |
|
3289 return retval; |
|
3290 } |
|
3291 |
|
3292 octave_value |
|
3293 octave_stream_list::do_open_file_numbers (void) const |
|
3294 { |
|
3295 Matrix retval (1, curr_len, 0.0); |
|
3296 |
|
3297 int num_open = 0; |
|
3298 |
|
3299 // Skip stdin, stdout, and stderr. |
|
3300 |
|
3301 for (int i = 3; i < curr_len; i++) |
|
3302 { |
3340
|
3303 if (list(i)) |
2117
|
3304 retval (0, num_open++) = i; |
|
3305 } |
|
3306 |
|
3307 retval.resize ((num_open > 0), num_open); |
|
3308 |
|
3309 return retval; |
|
3310 } |
|
3311 |
|
3312 int |
2609
|
3313 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117
|
3314 { |
|
3315 int retval = -1; |
|
3316 |
|
3317 if (fid.is_string ()) |
|
3318 { |
3523
|
3319 std::string nm = fid.string_value (); |
2117
|
3320 |
3531
|
3321 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
|
3322 // are unnamed. |
2117
|
3323 |
|
3324 for (int i = 3; i < curr_len; i++) |
|
3325 { |
3340
|
3326 octave_stream os = list(i); |
|
3327 |
|
3328 if (os && os.name () == nm) |
2117
|
3329 { |
|
3330 retval = i; |
|
3331 break; |
|
3332 } |
|
3333 } |
|
3334 } |
|
3335 else |
|
3336 { |
|
3337 int conv_err = 0; |
|
3338 |
|
3339 int int_fid = convert_to_valid_int (fid, conv_err); |
|
3340 |
|
3341 if (conv_err) |
3523
|
3342 ::error ("file id must be a file object, std::string, or integer value"); |
2117
|
3343 else |
|
3344 retval = int_fid; |
|
3345 } |
|
3346 |
|
3347 return retval; |
|
3348 } |
|
3349 |
|
3350 /* |
|
3351 ;;; Local Variables: *** |
|
3352 ;;; mode: C++ *** |
|
3353 ;;; End: *** |
|
3354 */ |