[ca8824d] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // manipulatorsInput.cfa -- |
---|
| 5 | // |
---|
| 6 | // Author : Peter A. Buhr |
---|
| 7 | // Created On : Sat Jun 8 17:58:54 2019 |
---|
| 8 | // Last Modified By : Peter A. Buhr |
---|
[da22726] | 9 | // Last Modified On : Mon Feb 5 21:54:49 2024 |
---|
| 10 | // Update Count : 134 |
---|
[ca8824d] | 11 | // |
---|
| 12 | |
---|
| 13 | #include <fstream.hfa> |
---|
| 14 | #include <stdio.h> // scanf |
---|
| 15 | |
---|
| 16 | int main() { |
---|
[2fa0237] | 17 | { |
---|
| 18 | // Upfront checks to ensure buffer safety. Once these pass, the simpler `wdi(sizeof(s),s)` |
---|
| 19 | // usage, as in the scanf alignment cases below, is justified. |
---|
| 20 | struct { |
---|
| 21 | char buf[8]; |
---|
| 22 | char canary; |
---|
| 23 | } data; |
---|
| 24 | static_assert( sizeof(data.buf) == 8 ); |
---|
| 25 | static_assert( &data.buf[8] == &data.canary ); // canary comes right after buf |
---|
| 26 | |
---|
| 27 | void rep(const char* casename) { |
---|
| 28 | data.canary = 42; |
---|
| 29 | bool caught = false; |
---|
| 30 | try { |
---|
| 31 | sin | wdi( sizeof(data.buf), data.buf ); |
---|
| 32 | } catch (cstring_length*) { |
---|
| 33 | caught = true; |
---|
| 34 | } |
---|
| 35 | printf( "%s \"%s\"", casename, data.buf ); |
---|
| 36 | if ( caught ) { |
---|
| 37 | printf(", exception occurred"); |
---|
| 38 | } |
---|
| 39 | if ( data.canary == 42 ) { |
---|
| 40 | printf(", canary ok"); |
---|
| 41 | } else { |
---|
| 42 | printf(", canary overwritten to %d", data.canary); |
---|
| 43 | } |
---|
| 44 | printf("\n"); |
---|
| 45 | } |
---|
| 46 | |
---|
[0860d9c] | 47 | rep("pre1"); // 123456 | 123456 |
---|
| 48 | rep("pre2"); // 1234567 | 1234567 |
---|
| 49 | rep("pre3a"); // 12345678 | 1234567 |
---|
| 50 | rep("pre3b"); // | 8 |
---|
| 51 | rep("pre4a"); // 123456789 | 1234567 |
---|
| 52 | rep("pre4b"); // | 89 |
---|
| 53 | |
---|
[2fa0237] | 54 | scanf("\n"); // next test does not start with %s so does not tolerate leading whitespace |
---|
| 55 | } |
---|
[ca8824d] | 56 | { |
---|
[baa1d5d] | 57 | char s[] = "yyyyyyyyyyyyyyyyyyyy"; // Input characters consumed: |
---|
[bf1cbde] | 58 | const char sk_fmt[] = "%*[abc]"; |
---|
[baa1d5d] | 59 | scanf( "abc " ); scanf( sk_fmt ); for ( 5 ) scanf( "%*c" ); printf( "1 %s\n", s ); // |abc |\ncccccb| \nxx\n| |
---|
| 60 | scanf( "%s", s ); printf( "2 %s\n", s ); // |abcxxx| |
---|
| 61 | scanf( "%*s" ); printf( "3 %s\n", s ); // |\nabcyyy| |
---|
| 62 | scanf( "%8s", s ); printf( "4 %s\n", s ); // |\naaaaaaaa| |
---|
| 63 | scanf( "%*8s" ); printf( "5 %s\n", s ); // |xxxxxxxx| |
---|
[329487c] | 64 | |
---|
[baa1d5d] | 65 | scanf( "%[abc]", s ); printf( "6 %s\n", s ); // |aabbccbb| |
---|
| 66 | scanf( "%[^abc]", s ); printf( "7 %s\n", s ); // |dddwww| |
---|
| 67 | scanf( "%*[abc]" ); printf( "8 %s\n", s ); // |bbbbbbbb| |
---|
| 68 | scanf( "%*[^abc]" ); printf( "9 %s\n", s ); // |wwwwwwww| |
---|
| 69 | scanf( "%8[abc]", s ); printf( "10 %s\n", s ); // |aaaaaaaa| |
---|
| 70 | scanf( "%8[^abc]", s ); printf( "11 %s\n", s ); // |wwwwwwww| |
---|
| 71 | scanf( "%*8[abc]" ); printf( "12 %s\n", s ); // |aaaaaaaa| |
---|
| 72 | scanf( "%*8[^abc]" ); printf( "13 %s\n", s ); // |wwwwwwww| |
---|
| 73 | scanf( "\n" ); // must start next line // |\n| |
---|
[8cffa4f] | 74 | |
---|
| 75 | int rc; |
---|
| 76 | s[0] = 'q'; s[1] = '\0'; rc = 99; |
---|
[baa1d5d] | 77 | rc = scanf( "%[abc]", s ); printf( "14 rc=%d, %s\n", rc, s ); // || |
---|
[8cffa4f] | 78 | s[0] = 'q'; s[1] = '\0'; rc = 99; |
---|
[baa1d5d] | 79 | rc = scanf( "%[^u]", s ); printf( "15 rc=%d, %s\n", rc, s ); // || |
---|
| 80 | scanf( "%*[u]\n" ); // |uuuuu\n| |
---|
| 81 | scanf( "%[^\n]\n", s ); printf( "16 %s\n", s ); // |get this line\n| |
---|
| 82 | scanf( "%[^%%]%%\n", s ); printf( "17 %s\n", s ); // |@# this line 1)-{}%\n| |
---|
| 83 | scanf( "%*[^%%]%%\n", s ); printf( "18 %s\n", s ); // |@# this line 1)-{}%\n| |
---|
[ca2feff] | 84 | |
---|
[baa1d5d] | 85 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // || |
---|
| 86 | scanf( "\"%[^\"]\"", s ); printf( "19 %s\n", s ); // |"abc"| |
---|
| 87 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 88 | scanf( "'%[^']'", s ); printf( "20 %s\n", s ); // |'abc '| |
---|
| 89 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 90 | scanf( "{%[^}]}", s ); printf( "21 %s\n", s ); // |{ d d\n\nd }| |
---|
| 91 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 92 | scanf( "X%[^Y]Y", s ); printf( "22 %s\n", s ); // |X ZC44%Y| |
---|
| 93 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 94 | scanf( "X%*[^Y]Y", s ); printf( "23 %s\n", s ); // |X ZC44%Y| |
---|
| 95 | scanf( "\n" ); // must start next line // |\n| |
---|
| 96 | |
---|
| 97 | char ch; |
---|
| 98 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 99 | scanf( "'%c'", &ch ); printf( "24 %c\n", ch ); // |x| |
---|
| 100 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 101 | scanf( "\"%c\"", &ch ); printf( "25 %c\n", ch ); // |x| |
---|
| 102 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 103 | scanf( "{%c}", &ch ); printf( "26 %c\n", ch ); // |x| |
---|
| 104 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 105 | scanf( "X%cY", &ch ); printf( "27 %c\n", ch ); // |x| |
---|
| 106 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace // |\n| |
---|
| 107 | scanf( "X%*cY", &ch ); printf( "28 %c\n", ch ); // |x| |
---|
| 108 | scanf( "\n" ); // must start next line // |\n| |
---|
[ca8824d] | 109 | } |
---|
| 110 | { |
---|
| 111 | char s[] = "yyyyyyyyyyyyyyyyyyyy"; |
---|
[dc5072f] | 112 | char sk[] = "abc"; |
---|
[389fbf5] | 113 | sin | "abc " | skip( sk ) | skip( 5 ); sout | "1" | s; |
---|
[329487c] | 114 | sin | wdi( sizeof(s), s ); sout | "2" | s; |
---|
| 115 | sin | ignore( s ); sout | "3" | s; |
---|
[da22726] | 116 | sin | wdi( sizeof(s), 8, s ); sout | "4" | s; |
---|
[329487c] | 117 | sin | ignore( wdi( sizeof(s), 8, s ) ); sout | "5" | s; |
---|
| 118 | |
---|
| 119 | sin | incl( "abc", wdi( sizeof(s), s ) ); sout | "6" | s; |
---|
| 120 | sin | excl( "abc", wdi( sizeof(s), s ) ); sout | "7" | s; |
---|
[74c6748] | 121 | sin | ignore( incl( "abc", wdi( sizeof(s), s ) ) ); sout | "8" | s; |
---|
| 122 | sin | ignore( excl( "abc", wdi( sizeof(s), s ) ) ); sout | "9" | s; |
---|
[329487c] | 123 | sin | incl( "abc", wdi( sizeof(s), 8, s ) ); sout | "10" | s; |
---|
| 124 | sin | excl( "abc", wdi( sizeof(s), 8, s ) ); sout | "11" | s; |
---|
| 125 | sin | ignore( incl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "12" | s; |
---|
| 126 | sin | ignore( excl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "13" | s; |
---|
[b771581] | 127 | sin | nl; |
---|
[8cffa4f] | 128 | |
---|
| 129 | s[0] = 'q'; s[1] = '\0'; |
---|
| 130 | sin | incl( "abc", wdi( sizeof(s), s ) ); sout | "14" | s; |
---|
| 131 | s[0] = 'q'; s[1] = '\0'; |
---|
| 132 | sin | excl( "u", wdi( sizeof(s), s ) ); sout | "15" | s; |
---|
[da22726] | 133 | sin | skip( "u" ) | nl; |
---|
[ca2feff] | 134 | sin | getline( wdi( sizeof(s), s ) ); sout | "16" | s; |
---|
[da22726] | 135 | sin | getline( wdi( sizeof(s), s ), '%' ) | nl; sout | "17" | s; |
---|
| 136 | sin | ignore( getline( wdi( sizeof(s), s ), '%' ) ) | nl; sout | "18" | s; |
---|
[ca2feff] | 137 | |
---|
[5764204] | 138 | sin | quoted( wdi( sizeof(s), s ) ); sout | "19" | s; |
---|
| 139 | sin | quoted( wdi( sizeof(s), s ), '\'' ); sout | "20" | s; |
---|
| 140 | sin | quoted( wdi( sizeof(s), s ), '{', '}' ); sout | "21" | s; |
---|
| 141 | sin | quoted( wdi( sizeof(s), s ), 'X', 'Y' ); sout | "22" | s; |
---|
| 142 | sin | ignore( quoted( wdi( sizeof(s), s ), 'X', 'Y' ) ); sout | "23" | s; |
---|
[baa1d5d] | 143 | |
---|
| 144 | char ch; |
---|
| 145 | sin | quoted( ch ); sout | "24 " | ch; |
---|
| 146 | sin | quoted( ch, '\"' ); sout | "25 " | ch; |
---|
| 147 | sin | quoted( ch, '{', '}' ); sout | "26 " | ch; |
---|
| 148 | sin | quoted( ch, 'X', 'Y' ); sout | "27 " | ch; |
---|
| 149 | sin | ignore( quoted( ch, 'X', 'Y' ) ); sout | "28 " | ch; |
---|
[b771581] | 150 | sin | nl; |
---|
[ca8824d] | 151 | } |
---|
[baa1d5d] | 152 | // Keep harmonized with collections/string-istream-manip |
---|
[ca8824d] | 153 | { |
---|
| 154 | char c; |
---|
[329487c] | 155 | sin | c; sout | c; |
---|
| 156 | sin | ignore( c ); sout | c; |
---|
[b771581] | 157 | sin | nl; |
---|
[ca8824d] | 158 | |
---|
[baa1d5d] | 159 | char ca[3] = { 'a', 'b', 'c' }; |
---|
[b771581] | 160 | sin | wdi( sizeof(ca), ca[0] ); sout | ca[0] | ca[1] | ca[2]; |
---|
| 161 | sin | ignore( wdi( sizeof(ca), ca[0] ) ); sout | ca[0] | ca[1] | ca[2]; |
---|
| 162 | sin | nl; |
---|
[baa1d5d] | 163 | |
---|
[ca8824d] | 164 | signed char sc; |
---|
[329487c] | 165 | sin | sc; sout | sc; |
---|
| 166 | sin | wdi( 3, sc ); sout | sc; |
---|
| 167 | sin | ignore( sc ); sout | sc; |
---|
| 168 | sin | ignore( wdi( 3, sc ) ); sout | sc; |
---|
[ca8824d] | 169 | |
---|
| 170 | unsigned char usc; |
---|
[329487c] | 171 | sin | usc; sout | usc; |
---|
| 172 | sin | wdi( 3, usc ); sout | usc; |
---|
| 173 | sin | ignore( usc ); sout | usc; |
---|
| 174 | sin | ignore( wdi( 3, usc ) ); sout | usc; |
---|
[ca8824d] | 175 | |
---|
| 176 | signed short int ssi; |
---|
[329487c] | 177 | sin | ssi; sout | ssi; |
---|
| 178 | sin | wdi( 3, ssi ); sout | ssi; |
---|
| 179 | sin | ignore( ssi ); sout | ssi; |
---|
| 180 | sin | ignore( wdi( 3, ssi ) ); sout | ssi; |
---|
[ca8824d] | 181 | |
---|
| 182 | unsigned short int usi; |
---|
[329487c] | 183 | sin | usi; sout | usi; |
---|
| 184 | sin | wdi( 3, usi ); sout | usi; |
---|
| 185 | sin | ignore( usi ); sout | usi; |
---|
| 186 | sin | ignore( wdi( 3, usi ) ); sout | usi; |
---|
[ca8824d] | 187 | |
---|
| 188 | signed int si; |
---|
[329487c] | 189 | sin | si; sout | si; |
---|
| 190 | sin | wdi( 3, si ); sout | si; |
---|
| 191 | sin | ignore( si ); sout | si; |
---|
| 192 | sin | ignore( wdi( 3, si ) ); sout | si; |
---|
[ca8824d] | 193 | |
---|
| 194 | unsigned int ui; |
---|
[329487c] | 195 | sin | ui; sout | ui; |
---|
| 196 | sin | wdi( 3, ui ); sout | ui; |
---|
| 197 | sin | ignore( ui ); sout | ui; |
---|
| 198 | sin | ignore( wdi( 3, ui ) ); sout | ui; |
---|
[ca8824d] | 199 | |
---|
| 200 | signed long int sli; |
---|
[329487c] | 201 | sin | sli; sout | sli; |
---|
| 202 | sin | wdi( 3, sli ); sout | sli; |
---|
| 203 | sin | ignore( sli ); sout | sli; |
---|
| 204 | sin | ignore( wdi( 3, sli ) ); sout | sli; |
---|
[ca8824d] | 205 | |
---|
| 206 | unsigned long int uli; |
---|
[329487c] | 207 | sin | uli; sout | uli; |
---|
| 208 | sin | wdi( 3, uli ); sout | uli; |
---|
| 209 | sin | ignore( uli ); sout | uli; |
---|
| 210 | sin | ignore( wdi( 3, uli ) ); sout | uli; |
---|
[ca8824d] | 211 | |
---|
| 212 | signed long long int slli; |
---|
[329487c] | 213 | sin | slli; sout | slli; |
---|
| 214 | sin | wdi( 3, slli ); sout | slli; |
---|
| 215 | sin | ignore( slli ); sout | slli; |
---|
| 216 | sin | ignore( wdi( 3, slli ) ); sout | slli; |
---|
[ca8824d] | 217 | |
---|
| 218 | unsigned long long int ulli; |
---|
[329487c] | 219 | sin | ulli; sout | ulli; |
---|
| 220 | sin | wdi( 3, ulli ); sout | ulli; |
---|
| 221 | sin | ignore( ulli ); sout | ulli; |
---|
| 222 | sin | ignore( wdi( 3, ulli ) ); sout | ulli; |
---|
[ca8824d] | 223 | |
---|
| 224 | float f; |
---|
[329487c] | 225 | sin | f; sout | f; |
---|
| 226 | sin | wdi( 8, f ); sout | f; |
---|
| 227 | sin | ignore( f ); sout | f; |
---|
| 228 | sin | ignore( wdi( 8, f ) ); sout | f; |
---|
[ca8824d] | 229 | |
---|
| 230 | double d; |
---|
[329487c] | 231 | sin | d; sout | d; |
---|
| 232 | sin | wdi( 8, d ); sout | d; |
---|
| 233 | sin | ignore( d ); sout | d; |
---|
| 234 | sin | ignore( wdi( 8, d ) ); sout | d; |
---|
[ca8824d] | 235 | |
---|
| 236 | long double ld; |
---|
[329487c] | 237 | sin | ld; sout | ld; |
---|
| 238 | sin | wdi( 8, ld ); sout | ld; |
---|
| 239 | sin | ignore( ld ); sout | ld; |
---|
| 240 | sin | ignore( wdi( 8, ld ) ); sout | ld; |
---|
[ca8824d] | 241 | |
---|
| 242 | float _Complex fc; |
---|
[329487c] | 243 | sin | fc; sout | fc; |
---|
| 244 | sin | wdi( 8, fc ); sout | fc; |
---|
| 245 | sin | ignore( fc ); sout | fc; |
---|
| 246 | sin | ignore( wdi( 8, fc ) ); sout | fc; |
---|
[ca8824d] | 247 | |
---|
| 248 | double _Complex dc; |
---|
[329487c] | 249 | sin | dc; sout | dc; |
---|
| 250 | sin | wdi( 8, dc ); sout | dc; |
---|
| 251 | sin | ignore( dc ); sout | dc; |
---|
| 252 | sin | ignore( wdi( 8, dc ) ); sout | dc; |
---|
[ca8824d] | 253 | |
---|
| 254 | long double _Complex ldc; |
---|
[329487c] | 255 | sin | ldc; sout | ldc; |
---|
| 256 | sin | wdi( 8, ldc ); sout | ldc; |
---|
| 257 | sin | ignore( ldc ); sout | ldc; |
---|
| 258 | sin | ignore( wdi( 8, ldc ) ); sout | ldc; |
---|
[ca8824d] | 259 | } |
---|
[6527150] | 260 | #if defined( __SIZEOF_INT128__ ) |
---|
[20f5eb8] | 261 | { |
---|
| 262 | int128 val; |
---|
| 263 | for ( 15 ) { |
---|
| 264 | sin | val; |
---|
| 265 | sout | val; |
---|
| 266 | } |
---|
| 267 | } |
---|
[6527150] | 268 | #endif // __SIZEOF_INT128__ |
---|
[ca8824d] | 269 | } // main |
---|
| 270 | |
---|
| 271 | // Local Variables: // |
---|
| 272 | // tab-width: 4 // |
---|
[dc5072f] | 273 | // compile-command: "cfa -Wall -Wextra manipulatorsInput.cfa" // |
---|
[ca8824d] | 274 | // End: // |
---|