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