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