Mercurial > hg > octave-nkf
comparison src/file-io.cc @ 636:fae2bd91c027
[project @ 1994-08-23 18:39:50 by jwe]
author | jwe |
---|---|
date | Tue, 23 Aug 1994 18:39:50 +0000 |
parents | aecbe369233b |
children | ae87e84f1be3 |
comparison
equal
deleted
inserted
replaced
635:5338832d2cf6 | 636:fae2bd91c027 |
---|---|
188 if (strcmp (file.name (), file_name) == 0) | 188 if (strcmp (file.name (), file_name) == 0) |
189 return p; | 189 return p; |
190 file_list.next (p); | 190 file_list.next (p); |
191 } | 191 } |
192 } | 192 } |
193 else if (arg.is_scalar_type ()) | 193 else |
194 { | 194 { |
195 double file_num = arg.double_value (); | 195 double file_num = arg.double_value (); |
196 if ((double) NINT (file_num) != file_num) | 196 |
197 error ("file number not an integer value"); | 197 if (! error_state) |
198 { | |
199 if ((double) NINT (file_num) != file_num) | |
200 error ("file number not an integer value"); | |
201 else | |
202 { | |
203 Pix p = file_list.first (); | |
204 file_info file; | |
205 for (int i = 0; i < file_count; i++) | |
206 { | |
207 file = file_list (p); | |
208 if (file.number () == file_num) | |
209 return p; | |
210 file_list.next (p); | |
211 } | |
212 error ("no file with that number"); | |
213 } | |
214 } | |
198 else | 215 else |
199 { | 216 error ("inapproriate file specifier"); |
200 Pix p = file_list.first (); | 217 } |
201 file_info file; | |
202 for (int i = 0; i < file_count; i++) | |
203 { | |
204 file = file_list (p); | |
205 if (file.number () == file_num) | |
206 return p; | |
207 file_list.next (p); | |
208 } | |
209 error ("no file with that number"); | |
210 } | |
211 } | |
212 else | |
213 error ("inapproriate file specifier"); | |
214 | 218 |
215 return 0; | 219 return 0; |
216 } | 220 } |
217 | 221 |
218 static Pix | 222 static Pix |
219 fopen_file_for_user (const tree_constant& arg, const char *mode, | 223 fopen_file_for_user (const char *name, const char *mode, |
220 const char *warn_for) | 224 const char *warn_for) |
221 { | 225 { |
222 char *file_name = arg.string_value (); | 226 FILE *file_ptr = fopen (name, mode); |
223 | |
224 FILE *file_ptr = fopen (file_name, mode); | |
225 if (file_ptr) | 227 if (file_ptr) |
226 { | 228 { |
227 file_info file (++file_count, file_name, file_ptr, mode); | 229 file_info file (++file_count, name, file_ptr, mode); |
228 file_list.append (file); | 230 file_list.append (file); |
229 | 231 |
230 Pix p = file_list.first (); | 232 Pix p = file_list.first (); |
231 file_info file_from_list; | 233 file_info file_from_list; |
232 | 234 |
233 for (int i = 0; i < file_count; i++) | 235 for (int i = 0; i < file_count; i++) |
234 { | 236 { |
235 file_from_list = file_list (p); | 237 file_from_list = file_list (p); |
236 if (strcmp (file_from_list.name (), file_name) == 0) | 238 if (strcmp (file_from_list.name (), name) == 0) |
237 return p; | 239 return p; |
238 file_list.next (p); | 240 file_list.next (p); |
239 } | 241 } |
240 } | 242 } |
241 | 243 |
242 error ("%s: unable to open file `%s'", warn_for, file_name); | 244 error ("%s: unable to open file `%s'", warn_for, name); |
243 | 245 |
244 return 0; | 246 return 0; |
245 } | 247 } |
246 | 248 |
247 static Pix | 249 static Pix |
248 file_io_get_file (const tree_constant arg, const char *mode, | 250 file_io_get_file (const tree_constant& arg, const char *mode, |
249 const char *warn_for) | 251 const char *warn_for) |
250 { | 252 { |
251 Pix p = return_valid_file (arg); | 253 Pix p = return_valid_file (arg); |
252 | 254 |
253 if (! p) | 255 if (! p) |
260 int status = stat (name, &buffer); | 262 int status = stat (name, &buffer); |
261 | 263 |
262 if (status == 0) | 264 if (status == 0) |
263 { | 265 { |
264 if ((buffer.st_mode & S_IFREG) == S_IFREG) | 266 if ((buffer.st_mode & S_IFREG) == S_IFREG) |
265 p = fopen_file_for_user (arg, mode, warn_for); | 267 p = fopen_file_for_user (name, mode, warn_for); |
266 else | 268 else |
267 error ("%s: invalid file type", warn_for); | 269 error ("%s: invalid file type", warn_for); |
268 } | 270 } |
269 else if (status < 0 && *mode != 'r') | 271 else if (status < 0 && *mode != 'r') |
270 p = fopen_file_for_user (arg, mode, warn_for); | 272 p = fopen_file_for_user (name, mode, warn_for); |
271 else | 273 else |
272 error ("%s: can't stat file `%s'", warn_for, name); | 274 error ("%s: can't stat file `%s'", warn_for, name); |
273 } | 275 } |
274 else | 276 else |
275 error ("%s: invalid file specifier", warn_for); | 277 error ("%s: invalid file specifier", warn_for); |
313 | 315 |
314 int success = fclose (file.fptr ()); | 316 int success = fclose (file.fptr ()); |
315 file_list.del (p); | 317 file_list.del (p); |
316 file_count--; | 318 file_count--; |
317 | 319 |
318 retval.resize (1); | |
319 if (success == 0) | 320 if (success == 0) |
320 retval(0) = tree_constant (1.0); // succeeded | 321 retval(0) = 1.0; // succeeded |
321 else | 322 else |
322 { | 323 { |
323 error ("fclose: error on closing file"); | 324 error ("fclose: error on closing file"); |
324 retval(0) = tree_constant (0.0); // failed | 325 retval(0) = 0.0; // failed |
325 } | 326 } |
326 | 327 |
327 return retval; | 328 return retval; |
328 } | 329 } |
329 | 330 |
365 if (file.number () == 1) | 366 if (file.number () == 1) |
366 flush_output_to_pager (); | 367 flush_output_to_pager (); |
367 else | 368 else |
368 success = fflush (file.fptr ()); | 369 success = fflush (file.fptr ()); |
369 | 370 |
370 retval.resize (1); | |
371 if (success == 0) | 371 if (success == 0) |
372 retval(0) = tree_constant (1.0); // succeeded | 372 retval(0) = 1.0; // succeeded |
373 else | 373 else |
374 { | 374 { |
375 error ("fflush: write error"); | 375 error ("fflush: write error"); |
376 retval(0) = tree_constant (0.0); // failed | 376 retval(0) = 0.0; // failed |
377 } | 377 } |
378 | 378 |
379 return retval; | 379 return retval; |
380 } | 380 } |
381 | 381 |
419 Pix p = file_io_get_file (args(1), "r", "fgets"); | 419 Pix p = file_io_get_file (args(1), "r", "fgets"); |
420 | 420 |
421 if (! p) | 421 if (! p) |
422 return retval; | 422 return retval; |
423 | 423 |
424 int length = 0; | 424 |
425 if (args(2).is_scalar_type ()) | 425 double dlen = args(2).double_value (); |
426 { | 426 |
427 length = (int) args(2).double_value (); | 427 if (error_state) |
428 if ((double) NINT (length) != length) | 428 return retval; |
429 { | 429 |
430 error ("fgets: length not an integer value"); | 430 int length = NINT (dlen); |
431 return retval; | 431 |
432 } | 432 if ((double) length != dlen) |
433 { | |
434 error ("fgets: length not an integer value"); | |
435 return retval; | |
433 } | 436 } |
434 | 437 |
435 file_info file = file_list (p); | 438 file_info file = file_list (p); |
436 | 439 |
437 char string[length+1]; | 440 char string [length+1]; |
438 char *success = fgets (string, length+1, file.fptr ()); | 441 char *success = fgets (string, length+1, file.fptr ()); |
439 | 442 |
440 if (! success) | 443 if (! success) |
441 { | 444 { |
442 retval.resize (1); | 445 retval(0) = -1.0; |
443 retval(0) = tree_constant (-1.0); | |
444 return retval; | 446 return retval; |
445 } | 447 } |
446 | 448 |
447 if (nargout == 2) | 449 if (nargout == 2) |
448 { | 450 retval(1) = (double) strlen (string); |
449 retval.resize (2); | 451 |
450 retval(1) = tree_constant ((double) strlen (string)); | 452 retval(0) = string; |
451 } | |
452 else | |
453 retval.resize (1); | |
454 | |
455 retval(0) = tree_constant (string); | |
456 | 453 |
457 return retval; | 454 return retval; |
458 } | 455 } |
459 | 456 |
460 DEFUN ("fopen", Ffopen, Sfopen, 3, 1, | 457 DEFUN ("fopen", Ffopen, Sfopen, 3, 1, |
498 | 495 |
499 if (p) | 496 if (p) |
500 { | 497 { |
501 file_info file = file_list (p); | 498 file_info file = file_list (p); |
502 | 499 |
503 retval.resize (1); | 500 retval(0) = (double) file.number (); |
504 retval(0) = tree_constant ((double) file.number ()); | |
505 | 501 |
506 return retval; | 502 return retval; |
507 } | 503 } |
508 | 504 |
509 if (! args(2).is_string ()) | 505 if (! args(2).is_string ()) |
539 int number = file_count++; | 535 int number = file_count++; |
540 | 536 |
541 file_info file (number, name, file_ptr, mode); | 537 file_info file (number, name, file_ptr, mode); |
542 file_list.append (file); | 538 file_list.append (file); |
543 | 539 |
544 retval.resize (1); | 540 retval(0) = (double) number; |
545 retval(0) = tree_constant ((double) number); | |
546 | 541 |
547 return retval; | 542 return retval; |
548 } | 543 } |
549 | 544 |
550 DEFUN ("freport", Ffreport, Sfreport, 1, 1, | 545 DEFUN ("freport", Ffreport, Sfreport, 1, 1, |
644 | 639 |
645 if (! p) | 640 if (! p) |
646 return retval; | 641 return retval; |
647 | 642 |
648 long origin = SEEK_SET; | 643 long origin = SEEK_SET; |
649 long offset = 0; | 644 |
650 if (args(2).is_scalar_type ()) | 645 double doff = args(2).double_value (); |
651 { | 646 |
652 offset = (long) args(2).double_value (); | 647 if (error_state) |
653 if ((double) NINT (offset) != offset) | 648 return retval; |
654 { | 649 |
655 error ("fseek: offset not an integer value"); | 650 long offset = NINT (doff); |
651 | |
652 if ((double) offset != doff) | |
653 { | |
654 error ("fseek: offset not an integer value"); | |
655 return retval; | |
656 } | |
657 | |
658 if (nargin == 4) | |
659 { | |
660 double dorig = args(3).double_value (); | |
661 | |
662 if (error_state) | |
663 return retval; | |
664 | |
665 origin = NINT (dorig); | |
666 | |
667 if ((double) dorig != origin) | |
668 { | |
669 error ("fseek: origin not an integer value"); | |
656 return retval; | 670 return retval; |
657 } | 671 } |
658 } | 672 |
659 | |
660 if (nargin == 4 && args(3).is_scalar_type ()) | |
661 { | |
662 origin = (long) args(3).double_value (); | |
663 if (origin == -1) | 673 if (origin == -1) |
664 origin = SEEK_CUR; | 674 origin = SEEK_CUR; |
665 else if (origin == -2) | 675 else if (origin == -2) |
666 origin = SEEK_END; | 676 origin = SEEK_END; |
667 else | 677 else |
668 { | 678 { |
669 if ((double) NINT (origin) != origin) | 679 error ("fseek: invalid value for origin"); |
670 { | 680 return retval; |
671 error ("fseek: origin not an integer value"); | |
672 return retval; | |
673 } | |
674 } | 681 } |
675 } | 682 } |
676 | 683 |
677 file_info file = file_list (p); | 684 file_info file = file_list (p); |
678 int success = fseek (file.fptr (), offset, origin); | 685 int success = fseek (file.fptr (), offset, origin); |
679 retval.resize (1); | |
680 | 686 |
681 if (success == 0) | 687 if (success == 0) |
682 retval(0) = tree_constant (1.0); // succeeded | 688 retval(0) = 1.0; // succeeded |
683 else | 689 else |
684 { | 690 { |
685 error ("fseek: file error"); | 691 error ("fseek: file error"); |
686 retval(0) = tree_constant (0.0); // failed | 692 retval(0) = 0.0; // failed |
687 } | 693 } |
688 | 694 |
689 return retval; | 695 return retval; |
690 } | 696 } |
691 | 697 |
716 | 722 |
717 if (p) | 723 if (p) |
718 { | 724 { |
719 file_info file = file_list (p); | 725 file_info file = file_list (p); |
720 long offset = ftell (file.fptr ()); | 726 long offset = ftell (file.fptr ()); |
721 retval.resize (1); | 727 |
722 retval(0) = tree_constant ((double) offset); | 728 retval(0) = (double) offset; |
723 | 729 |
724 if (offset == -1L) | 730 if (offset == -1L) |
725 error ("ftell: write error"); | 731 error ("ftell: write error"); |
726 } | 732 } |
727 | 733 |
779 { | 785 { |
780 error ("%s: not enough arguments", type); | 786 error ("%s: not enough arguments", type); |
781 return -1; | 787 return -1; |
782 } | 788 } |
783 | 789 |
784 if (! args(fmt_arg_count).is_scalar_type ()) | 790 double tmp_len = args(fmt_arg_count++).double_value (); |
791 | |
792 if (error_state) | |
785 { | 793 { |
786 error ("%s: `*' must be replaced by an integer", type); | 794 error ("%s: `*' must be replaced by an integer", type); |
787 return -1; | 795 return -1; |
788 } | 796 } |
789 | 797 |
790 fmt << NINT (args(fmt_arg_count++).double_value ()); | 798 fmt << NINT (tmp_len); |
791 s++; | 799 s++; |
792 chars_from_fmt_str++; | 800 chars_from_fmt_str++; |
793 } | 801 } |
794 else | 802 else |
795 { | 803 { |
818 { | 826 { |
819 error ("%s: not enough arguments", type); | 827 error ("%s: not enough arguments", type); |
820 return -1; | 828 return -1; |
821 } | 829 } |
822 | 830 |
823 if (! args(fmt_arg_count).is_scalar_type ()) | 831 double tmp_len = args(fmt_arg_count++).double_value (); |
832 | |
833 if (error_state) | |
824 { | 834 { |
825 error ("%s: `*' must be replaced by an integer", type); | 835 error ("%s: `*' must be replaced by an integer", type); |
826 return -1; | 836 return -1; |
827 } | 837 } |
828 | 838 |
829 fmt << NINT (args(fmt_arg_count++).double_value ()); | 839 fmt << NINT (tmp_len); |
830 s++; | 840 s++; |
831 chars_from_fmt_str++; | 841 chars_from_fmt_str++; |
832 } | 842 } |
833 else | 843 else |
834 { | 844 { |
858 } | 868 } |
859 | 869 |
860 switch (*s) | 870 switch (*s) |
861 { | 871 { |
862 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': | 872 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
863 | 873 { |
864 if (! args(fmt_arg_count).is_scalar_type ()) | 874 double d = args(fmt_arg_count++).double_value (); |
865 goto invalid_conversion; | 875 |
866 else | 876 int val = NINT (d); |
867 { | 877 |
868 chars_from_fmt_str++; | 878 if (error_state || (double) val != d) |
869 fmt << *s << ends; | 879 goto invalid_conversion; |
870 double d = args(fmt_arg_count++).double_value (); | 880 else |
871 if ((int) d != d) | 881 { |
872 goto invalid_conversion; | 882 chars_from_fmt_str++; |
873 else | 883 fmt << *s << ends; |
874 { | 884 char *tmp_fmt = fmt.str (); |
875 char *s = fmt.str (); | 885 sb.form (tmp_fmt, val); |
876 sb.form (s, (int) d); | 886 delete [] tmp_fmt; |
877 delete [] s; | 887 return chars_from_fmt_str; |
878 return chars_from_fmt_str; | 888 } |
879 } | 889 } |
880 } | |
881 | 890 |
882 case 'e': case 'E': case 'f': case 'g': case 'G': | 891 case 'e': case 'E': case 'f': case 'g': case 'G': |
883 | 892 { |
884 if (! args(fmt_arg_count).is_scalar_type ()) | 893 double val = args(fmt_arg_count++).double_value (); |
885 goto invalid_conversion; | 894 |
886 else | 895 if (error_state) |
887 { | 896 goto invalid_conversion; |
888 chars_from_fmt_str++; | 897 else |
889 fmt << *s << ends; | 898 { |
890 char *s = fmt.str (); | 899 chars_from_fmt_str++; |
891 sb.form (s, args(fmt_arg_count++).double_value ()); | 900 fmt << *s << ends; |
892 delete [] s; | 901 char *tmp_fmt = fmt.str (); |
893 return chars_from_fmt_str; | 902 sb.form (tmp_fmt, val); |
894 } | 903 delete [] tmp_fmt; |
904 return chars_from_fmt_str; | |
905 } | |
906 } | |
895 | 907 |
896 case 's': | 908 case 's': |
897 | 909 { |
898 if (! args(fmt_arg_count).is_string ()) | 910 char *val = args(fmt_arg_count++).string_value (); |
899 goto invalid_conversion; | 911 |
900 else | 912 if (error_state) |
901 { | 913 goto invalid_conversion; |
902 chars_from_fmt_str++; | 914 else |
903 fmt << *s << ends; | 915 { |
904 char *s = fmt.str (); | 916 chars_from_fmt_str++; |
905 sb.form (s, args(fmt_arg_count++).string_value ()); | 917 fmt << *s << ends; |
906 delete [] s; | 918 char *tmp_fmt = fmt.str (); |
907 return chars_from_fmt_str; | 919 sb.form (tmp_fmt, val); |
908 } | 920 delete [] tmp_fmt; |
921 return chars_from_fmt_str; | |
922 } | |
923 } | |
909 | 924 |
910 case 'c': | 925 case 'c': |
911 | 926 { |
912 if (! args(fmt_arg_count).is_string ()) | 927 char *val = args(fmt_arg_count++).string_value (); |
913 goto invalid_conversion; | 928 |
914 else | 929 if (error_state || strlen (val) != 1) |
915 { | 930 goto invalid_conversion; |
916 chars_from_fmt_str++; | 931 else |
917 fmt << *s << ends; | 932 { |
918 char *str = args(fmt_arg_count++).string_value (); | 933 chars_from_fmt_str++; |
919 if (strlen (str) != 1) | 934 fmt << *s << ends; |
920 goto invalid_conversion; | 935 char *tmp_fmt = fmt.str (); |
921 else | 936 sb.form (tmp_fmt, *val); |
922 { | 937 delete [] tmp_fmt; |
923 char *s = fmt.str (); | 938 return chars_from_fmt_str; |
924 sb.form (s, *str); | 939 } |
925 delete [] s; | 940 } |
926 return chars_from_fmt_str; | |
927 } | |
928 } | |
929 | 941 |
930 default: | 942 default: |
931 goto invalid_format; | 943 goto invalid_format; |
932 } | 944 } |
933 | 945 |
1003 char *fmt; | 1015 char *fmt; |
1004 file_info file; | 1016 file_info file; |
1005 | 1017 |
1006 if (strcmp (type, "fprintf") == 0) | 1018 if (strcmp (type, "fprintf") == 0) |
1007 { | 1019 { |
1008 if (args(2).is_string ()) | 1020 Pix p = file_io_get_file (args(1), "a+", type); |
1009 { | 1021 |
1010 fmt = args(2).string_value (); | 1022 if (! p) |
1011 fmt_arg_count++; | 1023 return retval; |
1012 } | 1024 |
1013 else | 1025 file = file_list (p); |
1026 | |
1027 if (file.mode () == "r") | |
1028 { | |
1029 error ("%s: file is read only", type); | |
1030 return retval; | |
1031 } | |
1032 | |
1033 fmt = args(2).string_value (); | |
1034 | |
1035 if (error_state) | |
1014 { | 1036 { |
1015 error ("%s: format must be a string", type); | 1037 error ("%s: format must be a string", type); |
1016 return retval; | 1038 return retval; |
1017 } | 1039 } |
1018 | 1040 |
1019 Pix p = file_io_get_file (args(1), "a+", type); | 1041 fmt_arg_count += 2; |
1020 | 1042 } |
1021 if (! p) | 1043 else |
1022 return retval; | 1044 { |
1023 | 1045 fmt = args(1).string_value (); |
1024 file = file_list (p); | 1046 |
1025 | 1047 if (error_state) |
1026 if (file.mode () == "r") | 1048 { |
1027 { | 1049 error ("%s: invalid format string", type); |
1028 error ("%s: file is read only", type); | |
1029 return retval; | 1050 return retval; |
1030 } | 1051 } |
1031 | 1052 |
1032 fmt = args(2).string_value (); | |
1033 | |
1034 fmt_arg_count++; | 1053 fmt_arg_count++; |
1035 } | |
1036 else if (args(1).is_string ()) | |
1037 { | |
1038 fmt = args(1).string_value (); | |
1039 fmt_arg_count++; | |
1040 } | |
1041 else | |
1042 { | |
1043 error ("%s: invalid format string", type); | |
1044 return retval; | |
1045 } | 1054 } |
1046 | 1055 |
1047 // Scan fmt for % escapes and print out the arguments. | 1056 // Scan fmt for % escapes and print out the arguments. |
1048 | 1057 |
1049 ostrstream output_buf; | 1058 ostrstream output_buf; |
1091 warning ("%s: unknown failure writing to file", type); | 1100 warning ("%s: unknown failure writing to file", type); |
1092 delete [] msg; | 1101 delete [] msg; |
1093 } | 1102 } |
1094 else if (strcmp (type, "sprintf") == 0) | 1103 else if (strcmp (type, "sprintf") == 0) |
1095 { | 1104 { |
1096 retval.resize (1); | |
1097 char *msg = output_buf.str (); | 1105 char *msg = output_buf.str (); |
1098 retval(0) = tree_constant (msg); | 1106 retval(0) = msg; |
1099 delete [] msg; | 1107 delete [] msg; |
1100 } | 1108 } |
1101 | 1109 |
1102 return retval; | 1110 return retval; |
1103 } | 1111 } |
1163 int temp; | 1171 int temp; |
1164 char *str = fmt.str (); | 1172 char *str = fmt.str (); |
1165 success = fscanf (fptr, str, &temp); | 1173 success = fscanf (fptr, str, &temp); |
1166 delete [] str; | 1174 delete [] str; |
1167 if (success > 0 && store_value) | 1175 if (success > 0 && store_value) |
1168 values(fmt_arg_count++) = tree_constant ((double) temp); | 1176 values(fmt_arg_count++) = (double) temp; |
1169 } | 1177 } |
1170 break; | 1178 break; |
1171 | 1179 |
1172 case 'e': case 'E': case 'f': case 'g': case 'G': | 1180 case 'e': case 'E': case 'f': case 'g': case 'G': |
1173 { | 1181 { |
1176 double temp; | 1184 double temp; |
1177 char *str = fmt.str (); | 1185 char *str = fmt.str (); |
1178 success = fscanf (fptr, str, &temp); | 1186 success = fscanf (fptr, str, &temp); |
1179 delete [] str; | 1187 delete [] str; |
1180 if (success > 0 && store_value) | 1188 if (success > 0 && store_value) |
1181 values(fmt_arg_count++) = tree_constant (temp); | 1189 values(fmt_arg_count++) = temp; |
1182 } | 1190 } |
1183 break; | 1191 break; |
1184 | 1192 |
1185 case 's': | 1193 case 's': |
1186 { | 1194 { |
1195 string_width = 0; | 1203 string_width = 0; |
1196 long original_position = ftell (fptr); | 1204 long original_position = ftell (fptr); |
1197 | 1205 |
1198 int c; | 1206 int c; |
1199 | 1207 |
1200 while ((c = getc (fptr)) != EOF | 1208 while ((c = getc (fptr)) != EOF && isspace (c)) |
1201 && (c == ' ' || c == '\n' || c != '\t')) | |
1202 ; // Don't count leading whitespace. | 1209 ; // Don't count leading whitespace. |
1203 | 1210 |
1204 if (c != EOF) | 1211 if (c != EOF) |
1205 string_width++; | 1212 string_width++; |
1206 | 1213 |
1207 for (;;) | 1214 for (;;) |
1208 { | 1215 { |
1209 c = getc (fptr); | 1216 c = getc (fptr); |
1210 if (c != EOF && c != ' ' && c != '\n' && c != '\t') | 1217 if (c != EOF && ! isspace (c)) |
1211 string_width++; | 1218 string_width++; |
1212 else | 1219 else |
1213 break; | 1220 break; |
1214 } | 1221 } |
1215 | 1222 |
1216 fseek (fptr, original_position, SEEK_SET); | 1223 fseek (fptr, original_position, SEEK_SET); |
1217 } | 1224 } |
1218 chars_from_fmt_str++; | 1225 chars_from_fmt_str++; |
1219 char temp[string_width+1]; | 1226 char temp [string_width+1]; |
1220 fmt << *s << ends; | 1227 fmt << *s << ends; |
1221 char *str = fmt.str (); | 1228 char *str = fmt.str (); |
1222 success = fscanf (fptr, str, temp); | 1229 success = fscanf (fptr, str, temp); |
1223 delete [] str; | 1230 delete [] str; |
1231 temp[string_width] = '\0'; | |
1224 if (success > 0 && store_value) | 1232 if (success > 0 && store_value) |
1225 values(fmt_arg_count++) = tree_constant (temp); | 1233 values(fmt_arg_count++) = temp; |
1226 } | 1234 } |
1227 break; | 1235 break; |
1228 | 1236 |
1229 case 'c': | 1237 case 'c': |
1230 { | 1238 { |
1231 if (string_width < 1) | 1239 if (string_width < 1) |
1232 string_width = 1; | 1240 string_width = 1; |
1233 chars_from_fmt_str++; | 1241 chars_from_fmt_str++; |
1234 char temp[string_width+1]; | 1242 char temp [string_width+1]; |
1235 memset (temp, '\0', string_width+1); | 1243 memset (temp, '\0', string_width+1); |
1236 fmt << *s << ends; | 1244 fmt << *s << ends; |
1237 char *str = fmt.str (); | 1245 char *str = fmt.str (); |
1238 success = fscanf (fptr, str, temp); | 1246 success = fscanf (fptr, str, temp); |
1239 delete [] str; | 1247 delete [] str; |
1240 temp[string_width] = '\0'; | 1248 temp[string_width] = '\0'; |
1241 if (success > 0 && store_value) | 1249 if (success > 0 && store_value) |
1242 values(fmt_arg_count++) = tree_constant (temp); | 1250 values(fmt_arg_count++) = temp; |
1243 } | 1251 } |
1244 break; | 1252 break; |
1245 | 1253 |
1246 default: | 1254 default: |
1247 goto invalid_format; | 1255 goto invalid_format; |
1335 | 1343 |
1336 fmt_arg_count = 0; | 1344 fmt_arg_count = 0; |
1337 | 1345 |
1338 if (strcmp (type, "scanf") != 0) | 1346 if (strcmp (type, "scanf") != 0) |
1339 { | 1347 { |
1340 if (args(2).is_string ()) | 1348 scanf_fmt = args(2).string_value (); |
1341 scanf_fmt = args(2).string_value (); | 1349 |
1342 else | 1350 if (error_state) |
1343 { | 1351 { |
1344 error ("%s: format must be a string", type); | 1352 error ("%s: format must be a string", type); |
1345 return retval; | 1353 return retval; |
1346 } | 1354 } |
1347 } | 1355 } |
1415 return retval; | 1423 return retval; |
1416 } | 1424 } |
1417 | 1425 |
1418 // Scan scanf_fmt for % escapes and assign the arguments. | 1426 // Scan scanf_fmt for % escapes and assign the arguments. |
1419 | 1427 |
1420 retval.resize (nargout ? nargout : 1); | 1428 retval.resize (nargout); |
1421 | 1429 |
1422 char *ptr = scanf_fmt; | 1430 char *ptr = scanf_fmt; |
1423 | 1431 |
1424 for (;;) | 1432 for (;;) |
1425 { | 1433 { |
1566 | 1574 |
1567 // Get type and number of bytes per element to read. | 1575 // Get type and number of bytes per element to read. |
1568 char *prec = "uchar"; | 1576 char *prec = "uchar"; |
1569 if (nargin > 3) | 1577 if (nargin > 3) |
1570 { | 1578 { |
1571 if (args(3).is_string ()) | 1579 prec = args(3).string_value (); |
1572 prec = args(3).string_value (); | 1580 |
1573 else | 1581 if (error_state) |
1574 { | 1582 { |
1575 error ("fread: precision must be a specified as a string"); | 1583 error ("fread: precision must be a specified as a string"); |
1576 return retval; | 1584 return retval; |
1577 } | 1585 } |
1578 } | 1586 } |
1593 | 1601 |
1594 if (nargin > 2) | 1602 if (nargin > 2) |
1595 { | 1603 { |
1596 if (args(2).is_scalar_type ()) | 1604 if (args(2).is_scalar_type ()) |
1597 { | 1605 { |
1598 tree_constant tmpa = args(2).make_numeric (); | 1606 dnr = args(2).double_value (); |
1599 dnr = tmpa.double_value (); | 1607 |
1608 if (error_state) | |
1609 return retval; | |
1610 | |
1600 dnc = 1.0; | 1611 dnc = 1.0; |
1601 } | 1612 } |
1602 else if (args(2).is_matrix_type ()) | 1613 else |
1603 { | 1614 { |
1604 ColumnVector tmp = args(2).vector_value (); | 1615 ColumnVector tmp = args(2).vector_value (); |
1605 | 1616 |
1606 if (tmp.length () == 2) | 1617 if (error_state || tmp.length () != 2) |
1607 { | |
1608 dnr = tmp.elem (0); | |
1609 dnc = tmp.elem (1); | |
1610 } | |
1611 else | |
1612 { | 1618 { |
1613 error ("fread: invalid size specification\n"); | 1619 error ("fread: invalid size specification\n"); |
1614 return retval; | 1620 return retval; |
1615 } | 1621 } |
1622 | |
1623 dnr = tmp.elem (0); | |
1624 dnc = tmp.elem (1); | |
1616 } | 1625 } |
1617 | 1626 |
1618 if ((xisinf (dnr)) && (xisinf (dnc))) | 1627 if ((xisinf (dnr)) && (xisinf (dnc))) |
1619 { | 1628 { |
1620 error ("fread: number of rows and columns cannot both be infinite"); | 1629 error ("fread: number of rows and columns cannot both be infinite"); |
1658 // Read data. | 1667 // Read data. |
1659 | 1668 |
1660 int count = m.read (fptr, prec); | 1669 int count = m.read (fptr, prec); |
1661 | 1670 |
1662 if (nargout > 1) | 1671 if (nargout > 1) |
1663 { | 1672 retval(1) = (double) count; |
1664 retval.resize (2); | 1673 |
1665 retval(1) = tree_constant ((double) count); | 1674 retval(0) = m; |
1666 } | |
1667 else | |
1668 retval.resize (1); | |
1669 | |
1670 retval(0) = tree_constant (m); | |
1671 | 1675 |
1672 return retval; | 1676 return retval; |
1673 } | 1677 } |
1674 | 1678 |
1675 DEFUN ("fwrite", Ffwrite, Sfwrite, 4, 1, | 1679 DEFUN ("fwrite", Ffwrite, Sfwrite, 4, 1, |
1728 | 1732 |
1729 // Get type and number of bytes per element to read. | 1733 // Get type and number of bytes per element to read. |
1730 char *prec = "uchar"; | 1734 char *prec = "uchar"; |
1731 if (nargin > 3) | 1735 if (nargin > 3) |
1732 { | 1736 { |
1733 if (args(3).is_string ()) | 1737 prec = args(3).string_value (); |
1734 prec = args(3).string_value (); | 1738 |
1735 else | 1739 if (error_state) |
1736 { | 1740 { |
1737 error ("fwrite: precision must be a specified as a string"); | 1741 error ("fwrite: precision must be a specified as a string"); |
1738 return retval; | 1742 return retval; |
1739 } | 1743 } |
1740 } | 1744 } |
1741 | 1745 |
1742 file_info file = file_list (p); | 1746 file_info file = file_list (p); |
1743 | 1747 |
1744 Matrix m = args(2).matrix_value (); | 1748 Matrix m = args(2).matrix_value (); |
1745 | 1749 |
1746 int count = m.write (file.fptr (), prec); | 1750 if (! error_state) |
1747 | 1751 { |
1748 retval.resize (1); | 1752 int count = m.write (file.fptr (), prec); |
1749 retval(0) = tree_constant ((double) count); | 1753 |
1754 retval(0) = (double) count; | |
1755 } | |
1750 | 1756 |
1751 return retval; | 1757 return retval; |
1752 } | 1758 } |
1753 | 1759 |
1754 DEFUN ("feof", Ffeof, Sfeof, 2, 1, | 1760 DEFUN ("feof", Ffeof, Sfeof, 2, 1, |
1788 if (! p) | 1794 if (! p) |
1789 return retval; | 1795 return retval; |
1790 | 1796 |
1791 file_info file = file_list (p); | 1797 file_info file = file_list (p); |
1792 | 1798 |
1793 retval.resize (1); | |
1794 retval(0) = (double) feof (file.fptr ()); | 1799 retval(0) = (double) feof (file.fptr ()); |
1795 | 1800 |
1796 return retval; | 1801 return retval; |
1797 } | 1802 } |
1798 | 1803 |
1837 file_info file = file_list (p); | 1842 file_info file = file_list (p); |
1838 | 1843 |
1839 int ierr = ferror (file.fptr ()); | 1844 int ierr = ferror (file.fptr ()); |
1840 | 1845 |
1841 if (nargout > 1) | 1846 if (nargout > 1) |
1842 { | 1847 retval(1) = (double) ierr; |
1843 retval.resize (2); | 1848 |
1844 retval(1) = tree_constant ((double) ierr); | 1849 retval(0) = strsave (strerror (ierr)); |
1845 } | |
1846 else | |
1847 retval.resize (1); | |
1848 | |
1849 retval(0) = tree_constant (strsave (strerror (ierr))); | |
1850 | 1850 |
1851 return retval; | 1851 return retval; |
1852 } | 1852 } |
1853 | 1853 |
1854 /* | 1854 /* |