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