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