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