Changeset fc12f05 for libcfa/src/iostream.cfa
- Timestamp:
- Nov 13, 2023, 3:43:43 AM (2 years ago)
- Branches:
- master
- Children:
- 25f2798
- Parents:
- 0030b508 (diff), 2174191 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/iostream.cfa
r0030b508 rfc12f05 1 2 1 // 3 2 // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo … … 11 10 // Created On : Wed May 27 17:56:53 2015 12 11 // Last Modified By : Peter A. Buhr 13 // Last Modified On : S un Oct 8 12:10:21202314 // Update Count : 1 56412 // Last Modified On : Sat Nov 11 07:06:27 2023 13 // Update Count : 1803 15 14 // 16 15 … … 785 784 return is; 786 785 } // ?|? 787 ISTYPE_VOID_IMPL( bool & )788 786 789 787 istype & ?|?( istype & is, char & c ) { … … 797 795 return is; 798 796 } // ?|? 799 ISTYPE_VOID_IMPL( char & )800 797 801 798 istype & ?|?( istype & is, signed char & sc ) { … … 803 800 return is; 804 801 } // ?|? 805 ISTYPE_VOID_IMPL( signed char & )806 802 807 803 istype & ?|?( istype & is, unsigned char & usc ) { … … 809 805 return is; 810 806 } // ?|? 811 ISTYPE_VOID_IMPL( unsigned char & )812 807 813 808 istype & ?|?( istype & is, short int & si ) { … … 815 810 return is; 816 811 } // ?|? 817 ISTYPE_VOID_IMPL( short int & )818 812 819 813 istype & ?|?( istype & is, unsigned short int & usi ) { … … 821 815 return is; 822 816 } // ?|? 823 ISTYPE_VOID_IMPL( unsigned short int & )824 817 825 818 istype & ?|?( istype & is, int & i ) { … … 827 820 return is; 828 821 } // ?|? 829 ISTYPE_VOID_IMPL( int & )830 822 831 823 istype & ?|?( istype & is, unsigned int & ui ) { … … 833 825 return is; 834 826 } // ?|? 835 ISTYPE_VOID_IMPL( unsigned int & )836 827 837 828 istype & ?|?( istype & is, long int & li ) { … … 839 830 return is; 840 831 } // ?|? 841 ISTYPE_VOID_IMPL( long int & )842 832 843 833 istype & ?|?( istype & is, unsigned long int & ulli ) { … … 845 835 return is; 846 836 } // ?|? 847 ISTYPE_VOID_IMPL( unsigned long int & )848 837 849 838 istype & ?|?( istype & is, long long int & lli ) { … … 851 840 return is; 852 841 } // ?|? 853 ISTYPE_VOID_IMPL( long long int & )854 842 855 843 istype & ?|?( istype & is, unsigned long long int & ulli ) { … … 857 845 return is; 858 846 } // ?|? 859 ISTYPE_VOID_IMPL( unsigned long long int & )860 847 861 848 #if defined( __SIZEOF_INT128__ ) … … 863 850 return (istype &)(is | (unsigned int128 &)llli); 864 851 } // ?|? 865 ISTYPE_VOID_IMPL( int128 & )866 852 867 853 istype & ?|?( istype & is, unsigned int128 & ullli ) { … … 880 866 return is; 881 867 } // ?|? 882 ISTYPE_VOID_IMPL( unsigned int128 & )883 868 #endif // __SIZEOF_INT128__ 884 869 … … 887 872 return is; 888 873 } // ?|? 889 ISTYPE_VOID_IMPL( float & )890 874 891 875 istype & ?|?( istype & is, double & d ) { … … 893 877 return is; 894 878 } // ?|? 895 ISTYPE_VOID_IMPL( double & )896 879 897 880 istype & ?|?( istype & is, long double & ld ) { … … 899 882 return is; 900 883 } // ?|? 901 ISTYPE_VOID_IMPL( long double & )902 884 903 885 istype & ?|?( istype & is, float _Complex & fc ) { … … 907 889 return is; 908 890 } // ?|? 909 ISTYPE_VOID_IMPL( float _Complex & )910 891 911 892 istype & ?|?( istype & is, double _Complex & dc ) { … … 915 896 return is; 916 897 } // ?|? 917 ISTYPE_VOID_IMPL( double _Complex & )918 898 919 899 istype & ?|?( istype & is, long double _Complex & ldc ) { … … 923 903 return is; 924 904 } // ?|? 925 ISTYPE_VOID_IMPL( long double _Complex & )926 905 927 906 istype & ?|?( istype & is, const char fmt[] ) { … … 929 908 return is; 930 909 } // ?|? 931 ISTYPE_VOID_IMPL( const char * )932 910 933 911 // manipulators … … 937 915 938 916 void ?|?( istype & is, istype & (* manip)( istype & ) ) { 939 manip( is ); ends( is );917 manip( is ); 940 918 } // ?|? 941 919 … … 966 944 char fmtstr[ sizeof("%*[]") + nscanset ]; 967 945 int pos = 0; 968 fmtstr[pos] = '%'; pos += 1; 969 fmtstr[pos] = '*'; pos += 1; 970 fmtstr[pos] = '['; pos += 1; 946 strcpy( &fmtstr[pos], "%*[" ); pos += 3; 971 947 strcpy( &fmtstr[pos], f.scanset ); pos += nscanset; 972 fmtstr[pos] = ']'; pos += 1; 973 fmtstr[pos] = '\0'; 974 fmt( is, fmtstr, (void*)0 ); // last arg is dummy: suppress gcc warning 975 } 976 else for ( f.wd ) fmt( is, "%*c" ); 948 strcpy( &fmtstr[pos], "]" ); 949 fmt( is, fmtstr, "" ); // skip scanset 950 } else { 951 char ch; 952 // fprintf( stderr, "skip " ); 953 for ( f.wd ) { // skip N characters 954 if ( eof( is ) ) break; 955 fmt( is, "%c", &ch ); 956 // fprintf( stderr, "`%c' ", ch ); 957 } // for 958 } // if 977 959 return is; 978 960 } 979 ISTYPE_VOID_IMPL( _Istream_Cskip )980 961 981 962 istype & ?|?( istype & is, _Istream_Cstr f ) { 982 const char * scanset = f.scanset; 963 const char * scanset; 964 size_t nscanset = 0; 983 965 if ( f.flags.delimiter ) scanset = f.delimiter; // getline ? 984 985 size_t len = 0; 986 if ( scanset ) len = strlen( scanset ); 987 char fmtstr[len + 16]; 988 int start = 1; 966 else scanset = f.scanset; 967 if ( scanset ) nscanset = strlen( scanset ); 968 969 char fmtstr[nscanset + 32]; // storage for scanset and format codes 989 970 fmtstr[0] = '%'; 990 if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; } 991 // no maximum width necessary because text ignored => width is read width 992 if ( f.wd != -1 ) { 971 972 int pos = 1; 973 int args; 974 bool check = true; 975 976 if ( f.flags.ignore ) { check = false; fmtstr[1] = '*'; pos += 1; } 977 int rwd = f.wd; 978 if ( f.wd != -1 ) { // => just ignore versus ignore with width 993 979 // wd is buffer bytes available (for input chars + null terminator) 994 980 // rwd is count of input chars 995 int rwd; 996 if (f.flags.rwd) { 997 verify (f.wd >= 0); 998 rwd = f.wd; 981 // no maximum width necessary because text ignored => width is read width 982 if ( f.flags.rwd ) check = false; 983 else rwd = f.wd - 1; 984 pos += sprintf( &fmtstr[pos], "%d", rwd ); 985 } // if 986 987 if ( ! scanset ) { // %s, %*s, %ws, %*ws 988 // fprintf( stderr, "cstr %s\n", f.s ); 989 strcpy( &fmtstr[pos], "s%n" ); 990 int len = 0; // may not be set in fmt 991 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 992 else args = fmt( is, fmtstr, f.s, &len ); 993 // fprintf( stderr, "cstr %s %d %d %d %s\n", fmtstr, args, len, f.wd, f.s ); 994 if ( check && len >= rwd && ! eof( is ) ) { // might not fit 995 char peek; 996 fmt( is, "%c", &peek ); // check for whitespace terminator 997 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 998 if ( ! eof( is ) ) { 999 ungetc( is, peek ); 1000 if ( ! isspace( peek ) ) throw ExceptionInst( cstring_length ); 1001 } // if 1002 } // if 1003 // FIX ME: CFA strings need to be modified to NOT change the argument for this case, then this can be removed. 1004 if ( rwd > 0 && args == 0 ) f.s[0]= '\0'; // read failed => no pattern match => set string to null 1005 } else { 1006 if ( f.flags.delimiter ) { // getline 1007 // fprintf( stderr, "getline\n" ); 1008 sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset ); 1009 // fprintf( stderr, "getline %s %d\n", fmtstr, f.wd ); 1010 int len = 0; // may not be set in fmt 1011 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1012 else args = fmt( is, fmtstr, f.s, &len ); 1013 // fprintf( stderr, "getline %s %d %d %d\n", fmtstr, args, f.wd, eof( is ) ); 1014 if ( check && len == rwd && ! eof( is ) ) { // might not fit 1015 char peek; 1016 fmt( is, "%c", &peek ); // check for delimiter 1017 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 1018 if ( ! eof( is ) ) { 1019 if ( peek != f.delimiter[0] ) { 1020 ungetc( is, peek ); 1021 throw ExceptionInst( cstring_length ); 1022 } // if 1023 } // if 1024 } else fmt( is, "%*c" ); // remove delimiter 999 1025 } else { 1000 verify (f.wd >= 1); 1001 rwd = f.wd - 1; 1026 // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] 1027 // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] 1028 sprintf( &fmtstr[pos], "[%s%s]%%n", f.flags.inex ? "^" : "", scanset ); 1029 // fprintf( stderr, "incl/excl %s %d\n", fmtstr, f.wd ); 1030 int len = 0; // may not be set in fmt 1031 if ( f.flags.ignore ) args = fmt( is, fmtstr, &len ); // no string argument for '*' 1032 else args = fmt( is, fmtstr, f.s, &len ); 1033 // fprintf( stderr, "incl/excl %s \"%s\" %d %d %d %d %d %c\n", fmtstr, f.s, args, f.wd, len, eof( is ), check, f.s[f.wd] ); 1034 if ( check && len == rwd && ! eof( is ) ) { // might not fit 1035 // fprintf( stderr, "overflow\n" ); 1036 char peek; 1037 fmt( is, "%c", &peek ); // check for whitespace terminator 1038 // fprintf( stderr, "peek %d '%c'\n", args, peek ); 1039 if ( ! eof( is ) ) { 1040 ungetc( is, peek ); 1041 if ( f.flags.inex ^ strchr( f.scanset, peek ) != 0p ) throw ExceptionInst( cstring_length ); 1042 } // if 1043 } // if 1002 1044 } // if 1003 start += sprintf( &fmtstr[start], "%d", rwd ); 1004 } 1005 1006 if ( ! scanset ) { 1007 // %s, %*s, %ws, %*ws 1008 fmtstr[start] = 's'; fmtstr[start + 1] = '\0'; 1009 // printf( "cstr %s\n", fmtstr ); 1010 } else { 1011 // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] 1012 // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] 1013 fmtstr[start] = '['; start += 1; 1014 if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; } 1015 strcpy( &fmtstr[start], scanset ); // copy includes '\0' 1016 len += start; 1017 fmtstr[len] = ']'; fmtstr[len + 1] = '\0'; 1018 // printf( "incl/excl %s\n", fmtstr ); 1019 } // if 1020 1021 int check = f.wd - 2; 1022 if (! f.flags.ignore ) { 1023 f.s[0] = '\0'; 1024 if ( ! f.flags.rwd ) f.s[check] = '\0'; // insert sentinel 1025 } 1026 len = fmt( is, fmtstr, f.s ); 1027 //fprintf( stderr, "KK %s %zd %d %c %s\n", fmtstr, len, check, f.s[check], f.s ); 1028 1029 if ( ! f.flags.ignore && ! f.flags.rwd && f.s[check] != '\0' ) { // sentinel overwritten ? 1030 // buffer filled, but would we have kept going? 1031 if ( ! eof( is ) ) { 1032 char peek; 1033 fmt( is, "%c", &peek ); 1034 ungetc( is, peek ); 1035 bool hasMore; 1036 if (f.flags.delimiter) { // getline 1037 hasMore = (peek != f.delimiter[0]); 1038 } else if (f.scanset) { // incl/excl 1039 bool peekMatch = strchr(f.scanset, peek) != 0p; 1040 hasMore = f.flags.inex ? (!peekMatch) : (peekMatch); 1041 } else { // %s 1042 hasMore = !isspace(peek); 1043 } 1044 if (hasMore) throw (cstring_length){ &cstring_length_vt }; 1045 } // if 1046 } // if 1047 1048 if ( f.flags.delimiter ) { // getline ? 1049 if ( len == 0 ) f.s[0] = '\0'; // empty read => argument unchanged => set empty 1050 if ( ! eof( is ) ) { // ignore delimiter, may not be present because of width 1051 char delimiter; 1052 fmt( is, "%c", &delimiter ); 1053 if ( delimiter != f.delimiter[0] ) ungetc( is, delimiter ); 1054 } // if 1055 } //if 1056 return is; 1057 } // ?|? 1058 ISTYPE_VOID_IMPL( _Istream_Cstr ) 1045 if ( rwd > 0 && args == 0 ) f.s[0]= '\0'; // read failed => no pattern match => set string to null 1046 } // if 1047 if ( args == 1 && eof( is ) ) { // data but scan ended at EOF 1048 // fprintf( stderr, "clear\n" ); 1049 clear( is ); // => reset EOF => detect again on next read 1050 } // if 1051 return is; 1052 } // ?|? 1059 1053 1060 1054 istype & ?|?( istype & is, _Istream_Char f ) { … … 1062 1056 return is; 1063 1057 } // ?|? 1064 ISTYPE_VOID_IMPL( _Istream_Char )1065 1058 } // distribution 1066 1059 … … 1079 1072 return is; \ 1080 1073 } /* ?|? */ \ 1081 ISTYPE_VOID_IMPL( _Istream_Manip(T) ) \1082 1074 } // distribution 1083 1075 … … 1107 1099 return is; 1108 1100 } // ?|? 1109 ISTYPE_VOID_IMPL( _Istream_Manip(float _Complex) )1110 1101 1111 1102 istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) { … … 1118 1109 return is; 1119 1110 } // ?|? 1120 ISTYPE_VOID_IMPL( _Istream_Manip(double _Complex) )1121 1111 1122 1112 istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) { … … 1129 1119 return is; 1130 1120 } // ?|? 1131 ISTYPE_VOID_IMPL( _Istream_Manip(long double _Complex) )1132 1121 } // distribution 1133 1122
Note:
See TracChangeset
for help on using the changeset viewer.