comparison src/file-io.cc @ 368:5c987c27f3d7

[project @ 1994-02-16 08:33:09 by jwe]
author jwe
date Wed, 16 Feb 1994 08:33:09 +0000
parents 04d377033730
children 58d05e7b5b2d
comparison
equal deleted inserted replaced
367:04d377033730 368:5c987c27f3d7
139 const char * 139 const char *
140 file_info::mode (void) const 140 file_info::mode (void) const
141 { 141 {
142 return file_mode; 142 return file_mode;
143 } 143 }
144
145 144
146 // double linked list containing relevant information about open files 145 // double linked list containing relevant information about open files
147 static DLList <file_info> file_list; 146 static DLList <file_info> file_list;
148 147
149 void 148 void
227 error ("problems automatically opening file for user"); 226 error ("problems automatically opening file for user");
228 227
229 return (Pix) NULL; 228 return (Pix) NULL;
230 } 229 }
231 230
231 static Pix
232 file_io_get_file (const tree_constant arg, const char *mode,
233 const char *warn_for)
234 {
235 Pix p = return_valid_file (arg);
236
237 if (p == (Pix) NULL)
238 {
239 if (arg.is_string_type ())
240 {
241 char *name = arg.string_value ();
242
243 struct stat buffer;
244 int status = stat (name, &buffer);
245
246 if (status == 0)
247 {
248 if ((buffer.st_mode & S_IFREG) == S_IFREG)
249 p = fopen_file_for_user (arg, mode);
250 else
251 error ("%s: invalid file type", warn_for);
252 }
253 else
254 error ("%s: can't stat file `%s'", warn_for, name);
255 }
256 else
257 error ("%s: invalid file specifier", warn_for);
258 }
259
260 return p;
261 }
232 262
233 tree_constant * 263 tree_constant *
234 fclose_internal (const tree_constant *args) 264 fclose_internal (const tree_constant *args)
235 { 265 {
236 tree_constant *retval = NULL_TREE_CONST; 266 tree_constant *retval = NULL_TREE_CONST;
319 tree_constant * 349 tree_constant *
320 fgets_internal (const tree_constant *args, int nargout) 350 fgets_internal (const tree_constant *args, int nargout)
321 { 351 {
322 tree_constant *retval = NULL_TREE_CONST; 352 tree_constant *retval = NULL_TREE_CONST;
323 353
324 Pix p = return_valid_file (args[1]); 354 Pix p = file_io_get_file (args[1], "r", "fgets");
325 355
326 if (p == (Pix) NULL) 356 if (p == (Pix) NULL)
327 { 357 return retval;
328 if (args[1].is_string_type ()) 358
329 {
330 struct stat buffer;
331 char *name = args[1].string_value ();
332 if (stat (name, &buffer) == 0
333 && (buffer.st_mode & S_IFREG) == S_IFREG)
334 {
335 p = fopen_file_for_user (args[1], "r");
336 }
337 else
338 {
339 error ("fgets: file dosen't exist");
340 return retval;
341 }
342 }
343 else
344 return retval;
345 }
346
347 int length = 0; 359 int length = 0;
348
349 if (args[2].is_scalar_type ()) 360 if (args[2].is_scalar_type ())
350 { 361 {
351 length = (int) args[2].double_value (); 362 length = (int) args[2].double_value ();
352 if ((double) NINT (length) != length) 363 if ((double) NINT (length) != length)
353 { 364 {
354 error ("fgets: length not an integer value"); 365 error ("fgets: length not an integer value");
355 return retval; 366 return retval;
356 } 367 }
357 } 368 }
358 369
370 file_info file = file_list (p);
371
359 char string[length+1]; 372 char string[length+1];
360 file_info file = file_list (p);
361 char *success = fgets (string, length+1, file.fptr ()); 373 char *success = fgets (string, length+1, file.fptr ());
362 374
363 if (success == (char *) NULL) 375 if (success == (char *) NULL)
364 { 376 {
365 retval = new tree_constant[2]; 377 retval = new tree_constant[2];
471 tree_constant * 483 tree_constant *
472 frewind_internal (const tree_constant *args) 484 frewind_internal (const tree_constant *args)
473 { 485 {
474 tree_constant *retval = NULL_TREE_CONST; 486 tree_constant *retval = NULL_TREE_CONST;
475 487
476 Pix p = return_valid_file (args[1]); 488 Pix p = file_io_get_file (args[1], "a+", "frewind");
477 if (p == (Pix) NULL) 489
478 p = fopen_file_for_user (args[1], "a+"); 490 if (p != (Pix) NULL)
479 491 {
480 file_info file = file_list (p); 492 file_info file = file_list (p);
481 rewind (file.fptr ()); 493 rewind (file.fptr ());
494 }
482 495
483 return retval; 496 return retval;
484 } 497 }
485 498
486 tree_constant * 499 tree_constant *
487 fseek_internal (const tree_constant *args, int nargin) 500 fseek_internal (const tree_constant *args, int nargin)
488 { 501 {
489 tree_constant *retval = NULL_TREE_CONST; 502 tree_constant *retval = NULL_TREE_CONST;
490 503
491 Pix p = return_valid_file (args[1]); 504 Pix p = file_io_get_file (args[1], "a+", "fseek");
492 505
493 if (p == (Pix) NULL) 506 if (p == (Pix) NULL)
494 p = fopen_file_for_user (args[1], "a+"); 507 return retval;
495 508
496 long origin = SEEK_SET; 509 long origin = SEEK_SET;
497 long offset = 0; 510 long offset = 0;
498 if (args[2].is_scalar_type ()) 511 if (args[2].is_scalar_type ())
499 { 512 {
539 552
540 tree_constant * 553 tree_constant *
541 ftell_internal (const tree_constant *args) 554 ftell_internal (const tree_constant *args)
542 { 555 {
543 tree_constant *retval = NULL_TREE_CONST; 556 tree_constant *retval = NULL_TREE_CONST;
544 Pix p = return_valid_file (args[1]); 557
545 558 Pix p = file_io_get_file (args[1], "a+", "ftell");
546 if (p == (Pix) NULL) 559
547 p = fopen_file_for_user (args[1], "a+"); 560 if (p != (Pix) NULL)
548 561 {
549 file_info file = file_list (p); 562 file_info file = file_list (p);
550 long offset = ftell (file.fptr ()); 563 long offset = ftell (file.fptr ());
551 retval = new tree_constant[2]; 564 retval = new tree_constant[2];
552 retval[0] = tree_constant ((double) offset); 565 retval[0] = tree_constant ((double) offset);
553 566
554 if (offset == -1L) 567 if (offset == -1L)
555 error ("ftell: write error"); 568 error ("ftell: write error");
569 }
556 570
557 return retval; 571 return retval;
558 } 572 }
559 573
560 void 574 void
783 char *fmt; 797 char *fmt;
784 file_info file; 798 file_info file;
785 799
786 if (strcmp (type, "fprintf") == 0) 800 if (strcmp (type, "fprintf") == 0)
787 { 801 {
788 Pix p;
789
790 if (args[2].is_string_type ()) 802 if (args[2].is_string_type ())
791 { 803 {
792 fmt = args[2].string_value (); 804 fmt = args[2].string_value ();
793 fmt_arg_count++; 805 fmt_arg_count++;
794 } 806 }
796 { 808 {
797 error ("%s: format must be a string", type); 809 error ("%s: format must be a string", type);
798 return retval; 810 return retval;
799 } 811 }
800 812
801 if (args[1].is_scalar_type ()) 813 Pix p = file_io_get_file (args[1], "a+", type);
802 { 814
803 p = return_valid_file (args[1]); 815 if (p == (Pix) NULL)
804 if (p == (Pix) NULL) 816 return retval;
805 return retval;
806 }
807 else if (args[1].is_string_type ())
808 {
809 p = return_valid_file (args[1]);
810 if (p == (Pix) NULL)
811 p = fopen_file_for_user (args[1], "a+");
812 }
813 else
814 {
815 error ("%s: invalid file specifier", type);
816 return retval;
817 }
818 817
819 file = file_list (p); 818 file = file_list (p);
819
820 if (file.mode () == "r") 820 if (file.mode () == "r")
821 { 821 {
822 error ("%s: file is read only", type); 822 error ("%s: file is read only", type);
823 return retval; 823 return retval;
824 } 824 }
825
825 fmt = args[2].string_value (); 826 fmt = args[2].string_value ();
827
826 fmt_arg_count++; 828 fmt_arg_count++;
827 } 829 }
828 else if (args[1].is_string_type ()) 830 else if (args[1].is_string_type ())
829 { 831 {
830 fmt = args[1].string_value (); 832 fmt = args[1].string_value ();
894 896
895 return retval; 897 return retval;
896 } 898 }
897 899
898 static int 900 static int
899 process_scanf_format (const char *s, const tree_constant *args, 901 process_scanf_format (const char *s, ostrstream& fmt,
900 ostrstream& fmt, const char *type, int nargout, 902 const char *type, int nargout, FILE* fptr,
901 FILE* fptr, tree_constant *values) 903 tree_constant *values)
902 { 904 {
903 fmt << "%"; 905 fmt << "%";
904
905 tree_constant_rep::constant_type arg_type;
906 906
907 int chars_from_fmt_str = 0; 907 int chars_from_fmt_str = 0;
908 int store_value = 1; 908 int store_value = 1;
909 int string_width = -1; 909 int string_width = -1;
910 int success = 1; 910 int success = 1;
941 } 941 }
942 942
943 if (*s == '\0') 943 if (*s == '\0')
944 goto invalid_format; 944 goto invalid_format;
945 945
946 if (fmt_arg_count >= nargout && store_value) 946 // Even if we don't have a place to store them, attempt to convert
947 { 947 // everything specified by the format string.
948 error ("%s: not enough arguments", type); 948
949 return -1; 949 if (fmt_arg_count >= nargout)
950 } 950 store_value = 0;
951
952 arg_type = args[fmt_arg_count].const_type ();
953 951
954 switch (*s) 952 switch (*s)
955 { 953 {
956 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': 954 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
957 { 955 {
1031 break; 1029 break;
1032 default: 1030 default:
1033 goto invalid_format; 1031 goto invalid_format;
1034 } 1032 }
1035 1033
1036 if (success > 0 || (success == 0 && store_value == 0)) 1034 if (success > 0)
1037 return chars_from_fmt_str; 1035 return chars_from_fmt_str;
1038 1036 else if (success == 0)
1039 if (success == 0)
1040 warning ("%s: invalid conversion", type); 1037 warning ("%s: invalid conversion", type);
1041 else if (success == EOF) 1038 else if (success == EOF)
1042 { 1039 {
1043 if (strcmp (type, "fscanf") == 0) 1040 if (strcmp (type, "fscanf") == 0)
1044 warning ("%s: end of file reached before final conversion", type); 1041 warning ("%s: end of file reached before final conversion", type);
1068 1065
1069 fmt_arg_count = 0; 1066 fmt_arg_count = 0;
1070 1067
1071 if (strcmp (type, "scanf") != 0) 1068 if (strcmp (type, "scanf") != 0)
1072 { 1069 {
1073 if ( args[2].is_string_type ()) 1070 if (args[2].is_string_type ())
1074 scanf_fmt = args[2].string_value (); 1071 scanf_fmt = args[2].string_value ();
1075 else 1072 else
1076 { 1073 {
1077 error ("%s: format must be a string", type); 1074 error ("%s: format must be a string", type);
1078 return retval; 1075 return retval;
1081 1078
1082 int doing_fscanf = (strcmp (type, "fscanf") == 0); 1079 int doing_fscanf = (strcmp (type, "fscanf") == 0);
1083 1080
1084 if (doing_fscanf) 1081 if (doing_fscanf)
1085 { 1082 {
1086 Pix p; 1083 Pix p = file_io_get_file (args[1], "r", type);
1087 if (args[1].is_scalar_type () 1084
1088 || args[1].is_string_type ()) 1085 if (p == (Pix) NULL)
1089 { 1086 return retval;
1090 p = return_valid_file (args[1]);
1091 if (p == (Pix) NULL)
1092 return retval;
1093 }
1094 else
1095 {
1096 error ("%s: invalid file specifier", type);
1097 return retval;
1098 }
1099 1087
1100 file = file_list (p); 1088 file = file_list (p);
1101 1089
1102 if (strcmp (file.mode (), "w") == 0 || strcmp (file.mode (), "a") == 0) 1090 if (strcmp (file.mode (), "w") == 0 || strcmp (file.mode (), "a") == 0)
1103 { 1091 {
1106 } 1094 }
1107 1095
1108 fptr = file.fptr (); 1096 fptr = file.fptr ();
1109 } 1097 }
1110 1098
1111 if (args[1].is_string_type () || (doing_fscanf && file.number () == 0)) 1099 if ((fptr == (FILE *) NULL && args[1].is_string_type ())
1100 || (doing_fscanf && file.number () == 0))
1112 { 1101 {
1113 char *string; 1102 char *string;
1114 1103
1115 if (strcmp (type, "scanf") == 0) 1104 if (strcmp (type, "scanf") == 0)
1116 scanf_fmt = args[1].string_value (); 1105 scanf_fmt = args[1].string_value ();
1179 continue; 1168 continue;
1180 } 1169 }
1181 1170
1182 // We must be looking at a format specifier. Extract it or fail. 1171 // We must be looking at a format specifier. Extract it or fail.
1183 1172
1184 int status = process_scanf_format (ptr, args, fmt, type, 1173 int status = process_scanf_format (ptr, fmt, type, nargout,
1185 nargout, fptr, retval); 1174 fptr, retval);
1186 1175
1187 if (status < 0) 1176 if (status < 0)
1188 { 1177 {
1189 if (fmt_arg_count == 0) 1178 if (fmt_arg_count == 0)
1190 { 1179 {