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