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