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