[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 |
---|
[389fbf5] | 9 | // Last Modified On : Sat Sep 2 14:27:46 2023 |
---|
| 10 | // Update Count : 65 |
---|
[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 | |
---|
| 47 | rep("pre1"); |
---|
| 48 | rep("pre2a"); |
---|
| 49 | rep("pre2b"); |
---|
| 50 | scanf("\n"); // next test does not start with %s so does not tolerate leading whitespace |
---|
| 51 | } |
---|
[ca8824d] | 52 | { |
---|
| 53 | char s[] = "yyyyyyyyyyyyyyyyyyyy"; |
---|
[dc5072f] | 54 | const char sk[] = "abc"; |
---|
| 55 | scanf( "abc " ); scanf( sk ); for ( 5 ) scanf( "%*c" ); printf( "1 %s\n", s ); |
---|
[329487c] | 56 | scanf( "%s", s ); printf( "2 %s\n", s ); |
---|
| 57 | scanf( "%*s" ); printf( "3 %s\n", s ); |
---|
| 58 | scanf( "%8s", s ); printf( "4 %s\n", s ); |
---|
| 59 | scanf( "%*8s" ); printf( "5 %s\n", s ); |
---|
| 60 | |
---|
| 61 | scanf( "%[abc]", s ); printf( "6 %s\n", s ); |
---|
| 62 | scanf( "%[^abc]", s ); printf( "7 %s\n", s ); |
---|
| 63 | scanf( "%*[abc]" ); printf( "8 %s\n", s ); |
---|
| 64 | scanf( "%*[^abc]" ); printf( "9 %s\n", s ); |
---|
| 65 | scanf( "%8[abc]", s ); printf( "10 %s\n", s ); |
---|
| 66 | scanf( "%8[^abc]", s ); printf( "11 %s\n", s ); |
---|
| 67 | scanf( "%*8[abc]" ); printf( "12 %s\n", s ); |
---|
| 68 | scanf( "%*8[^abc]" ); printf( "13 %s\n", s ); |
---|
[ca8824d] | 69 | } |
---|
| 70 | { |
---|
| 71 | char s[] = "yyyyyyyyyyyyyyyyyyyy"; |
---|
[dc5072f] | 72 | char sk[] = "abc"; |
---|
[389fbf5] | 73 | sin | "abc " | skip( sk ) | skip( 5 ); sout | "1" | s; |
---|
[329487c] | 74 | sin | wdi( sizeof(s), s ); sout | "2" | s; |
---|
| 75 | sin | ignore( s ); sout | "3" | s; |
---|
| 76 | sin | wdi( sizeof(s), 8, s ); sout | "4" | s; |
---|
| 77 | sin | ignore( wdi( sizeof(s), 8, s ) ); sout | "5" | s; |
---|
| 78 | |
---|
| 79 | sin | incl( "abc", wdi( sizeof(s), s ) ); sout | "6" | s; |
---|
| 80 | sin | excl( "abc", wdi( sizeof(s), s ) ); sout | "7" | s; |
---|
[74c6748] | 81 | sin | ignore( incl( "abc", wdi( sizeof(s), s ) ) ); sout | "8" | s; |
---|
| 82 | sin | ignore( excl( "abc", wdi( sizeof(s), s ) ) ); sout | "9" | s; |
---|
[329487c] | 83 | sin | incl( "abc", wdi( sizeof(s), 8, s ) ); sout | "10" | s; |
---|
| 84 | sin | excl( "abc", wdi( sizeof(s), 8, s ) ); sout | "11" | s; |
---|
| 85 | sin | ignore( incl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "12" | s; |
---|
| 86 | sin | ignore( excl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "13" | s; |
---|
[ca8824d] | 87 | } |
---|
[f54e6ec] | 88 | /* Keep harmonized with collections/string-istream-manip */ |
---|
[ca8824d] | 89 | { |
---|
| 90 | char c; |
---|
[329487c] | 91 | sin | c; sout | c; |
---|
| 92 | sin | ignore( c ); sout | c; |
---|
[ca8824d] | 93 | |
---|
| 94 | signed char sc; |
---|
[329487c] | 95 | sin | sc; sout | sc; |
---|
| 96 | sin | wdi( 3, sc ); sout | sc; |
---|
| 97 | sin | ignore( sc ); sout | sc; |
---|
| 98 | sin | ignore( wdi( 3, sc ) ); sout | sc; |
---|
[ca8824d] | 99 | |
---|
| 100 | unsigned char usc; |
---|
[329487c] | 101 | sin | usc; sout | usc; |
---|
| 102 | sin | wdi( 3, usc ); sout | usc; |
---|
| 103 | sin | ignore( usc ); sout | usc; |
---|
| 104 | sin | ignore( wdi( 3, usc ) ); sout | usc; |
---|
[ca8824d] | 105 | |
---|
| 106 | signed short int ssi; |
---|
[329487c] | 107 | sin | ssi; sout | ssi; |
---|
| 108 | sin | wdi( 3, ssi ); sout | ssi; |
---|
| 109 | sin | ignore( ssi ); sout | ssi; |
---|
| 110 | sin | ignore( wdi( 3, ssi ) ); sout | ssi; |
---|
[ca8824d] | 111 | |
---|
| 112 | unsigned short int usi; |
---|
[329487c] | 113 | sin | usi; sout | usi; |
---|
| 114 | sin | wdi( 3, usi ); sout | usi; |
---|
| 115 | sin | ignore( usi ); sout | usi; |
---|
| 116 | sin | ignore( wdi( 3, usi ) ); sout | usi; |
---|
[ca8824d] | 117 | |
---|
| 118 | signed int si; |
---|
[329487c] | 119 | sin | si; sout | si; |
---|
| 120 | sin | wdi( 3, si ); sout | si; |
---|
| 121 | sin | ignore( si ); sout | si; |
---|
| 122 | sin | ignore( wdi( 3, si ) ); sout | si; |
---|
[ca8824d] | 123 | |
---|
| 124 | unsigned int ui; |
---|
[329487c] | 125 | sin | ui; sout | ui; |
---|
| 126 | sin | wdi( 3, ui ); sout | ui; |
---|
| 127 | sin | ignore( ui ); sout | ui; |
---|
| 128 | sin | ignore( wdi( 3, ui ) ); sout | ui; |
---|
[ca8824d] | 129 | |
---|
| 130 | signed long int sli; |
---|
[329487c] | 131 | sin | sli; sout | sli; |
---|
| 132 | sin | wdi( 3, sli ); sout | sli; |
---|
| 133 | sin | ignore( sli ); sout | sli; |
---|
| 134 | sin | ignore( wdi( 3, sli ) ); sout | sli; |
---|
[ca8824d] | 135 | |
---|
| 136 | unsigned long int uli; |
---|
[329487c] | 137 | sin | uli; sout | uli; |
---|
| 138 | sin | wdi( 3, uli ); sout | uli; |
---|
| 139 | sin | ignore( uli ); sout | uli; |
---|
| 140 | sin | ignore( wdi( 3, uli ) ); sout | uli; |
---|
[ca8824d] | 141 | |
---|
| 142 | signed long long int slli; |
---|
[329487c] | 143 | sin | slli; sout | slli; |
---|
| 144 | sin | wdi( 3, slli ); sout | slli; |
---|
| 145 | sin | ignore( slli ); sout | slli; |
---|
| 146 | sin | ignore( wdi( 3, slli ) ); sout | slli; |
---|
[ca8824d] | 147 | |
---|
| 148 | unsigned long long int ulli; |
---|
[329487c] | 149 | sin | ulli; sout | ulli; |
---|
| 150 | sin | wdi( 3, ulli ); sout | ulli; |
---|
| 151 | sin | ignore( ulli ); sout | ulli; |
---|
| 152 | sin | ignore( wdi( 3, ulli ) ); sout | ulli; |
---|
[ca8824d] | 153 | |
---|
| 154 | float f; |
---|
[329487c] | 155 | sin | f; sout | f; |
---|
| 156 | sin | wdi( 8, f ); sout | f; |
---|
| 157 | sin | ignore( f ); sout | f; |
---|
| 158 | sin | ignore( wdi( 8, f ) ); sout | f; |
---|
[ca8824d] | 159 | |
---|
| 160 | double d; |
---|
[329487c] | 161 | sin | d; sout | d; |
---|
| 162 | sin | wdi( 8, d ); sout | d; |
---|
| 163 | sin | ignore( d ); sout | d; |
---|
| 164 | sin | ignore( wdi( 8, d ) ); sout | d; |
---|
[ca8824d] | 165 | |
---|
| 166 | long double ld; |
---|
[329487c] | 167 | sin | ld; sout | ld; |
---|
| 168 | sin | wdi( 8, ld ); sout | ld; |
---|
| 169 | sin | ignore( ld ); sout | ld; |
---|
| 170 | sin | ignore( wdi( 8, ld ) ); sout | ld; |
---|
[ca8824d] | 171 | |
---|
| 172 | float _Complex fc; |
---|
[329487c] | 173 | sin | fc; sout | fc; |
---|
| 174 | sin | wdi( 8, fc ); sout | fc; |
---|
| 175 | sin | ignore( fc ); sout | fc; |
---|
| 176 | sin | ignore( wdi( 8, fc ) ); sout | fc; |
---|
[ca8824d] | 177 | |
---|
| 178 | double _Complex dc; |
---|
[329487c] | 179 | sin | dc; sout | dc; |
---|
| 180 | sin | wdi( 8, dc ); sout | dc; |
---|
| 181 | sin | ignore( dc ); sout | dc; |
---|
| 182 | sin | ignore( wdi( 8, dc ) ); sout | dc; |
---|
[ca8824d] | 183 | |
---|
| 184 | long double _Complex ldc; |
---|
[329487c] | 185 | sin | ldc; sout | ldc; |
---|
| 186 | sin | wdi( 8, ldc ); sout | ldc; |
---|
| 187 | sin | ignore( ldc ); sout | ldc; |
---|
| 188 | sin | ignore( wdi( 8, ldc ) ); sout | ldc; |
---|
[ca8824d] | 189 | } |
---|
[6527150] | 190 | #if defined( __SIZEOF_INT128__ ) |
---|
[20f5eb8] | 191 | { |
---|
| 192 | int128 val; |
---|
| 193 | for ( 15 ) { |
---|
| 194 | sin | val; |
---|
| 195 | sout | val; |
---|
| 196 | } |
---|
| 197 | } |
---|
[6527150] | 198 | #endif // __SIZEOF_INT128__ |
---|
[ca8824d] | 199 | } // main |
---|
| 200 | |
---|
| 201 | // Local Variables: // |
---|
| 202 | // tab-width: 4 // |
---|
[dc5072f] | 203 | // compile-command: "cfa -Wall -Wextra manipulatorsInput.cfa" // |
---|
[ca8824d] | 204 | // End: // |
---|