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
|
---|
9 | // Last Modified On : Mon Feb 5 21:54:49 2024
|
---|
10 | // Update Count : 134
|
---|
11 | //
|
---|
12 |
|
---|
13 | #include <fstream.hfa>
|
---|
14 | #include <stdio.h> // scanf
|
---|
15 |
|
---|
16 | int main() {
|
---|
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"); // 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 |
|
---|
54 | scanf("\n"); // next test does not start with %s so does not tolerate leading whitespace
|
---|
55 | }
|
---|
56 | {
|
---|
57 | char s[] = "yyyyyyyyyyyyyyyyyyyy"; // Input characters consumed:
|
---|
58 | const char sk_fmt[] = "%*[abc]";
|
---|
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|
|
---|
64 |
|
---|
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|
|
---|
74 |
|
---|
75 | int rc;
|
---|
76 | s[0] = 'q'; s[1] = '\0'; rc = 99;
|
---|
77 | rc = scanf( "%[abc]", s ); printf( "14 rc=%d, %s\n", rc, s ); // ||
|
---|
78 | s[0] = 'q'; s[1] = '\0'; rc = 99;
|
---|
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|
|
---|
84 |
|
---|
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|
|
---|
109 | }
|
---|
110 | {
|
---|
111 | char s[] = "yyyyyyyyyyyyyyyyyyyy";
|
---|
112 | char sk[] = "abc";
|
---|
113 | sin | "abc " | skip( sk ) | skip( 5 ); sout | "1" | s;
|
---|
114 | sin | wdi( sizeof(s), s ); sout | "2" | s;
|
---|
115 | sin | ignore( s ); sout | "3" | s;
|
---|
116 | sin | wdi( sizeof(s), 8, s ); sout | "4" | s;
|
---|
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;
|
---|
121 | sin | ignore( incl( "abc", wdi( sizeof(s), s ) ) ); sout | "8" | s;
|
---|
122 | sin | ignore( excl( "abc", wdi( sizeof(s), s ) ) ); sout | "9" | s;
|
---|
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;
|
---|
127 | sin | nl;
|
---|
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;
|
---|
133 | sin | skip( "u" ) | nl;
|
---|
134 | sin | getline( wdi( sizeof(s), s ) ); sout | "16" | s;
|
---|
135 | sin | getline( wdi( sizeof(s), s ), '%' ) | nl; sout | "17" | s;
|
---|
136 | sin | ignore( getline( wdi( sizeof(s), s ), '%' ) ) | nl; sout | "18" | s;
|
---|
137 |
|
---|
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;
|
---|
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;
|
---|
150 | sin | nl;
|
---|
151 | }
|
---|
152 | // Keep harmonized with collections/string-istream-manip
|
---|
153 | {
|
---|
154 | char c;
|
---|
155 | sin | c; sout | c;
|
---|
156 | sin | ignore( c ); sout | c;
|
---|
157 | sin | nl;
|
---|
158 |
|
---|
159 | char ca[3] = { 'a', 'b', 'c' };
|
---|
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;
|
---|
163 |
|
---|
164 | signed char sc;
|
---|
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;
|
---|
169 |
|
---|
170 | unsigned char usc;
|
---|
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;
|
---|
175 |
|
---|
176 | signed short int ssi;
|
---|
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;
|
---|
181 |
|
---|
182 | unsigned short int usi;
|
---|
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;
|
---|
187 |
|
---|
188 | signed int si;
|
---|
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;
|
---|
193 |
|
---|
194 | unsigned int ui;
|
---|
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;
|
---|
199 |
|
---|
200 | signed long int sli;
|
---|
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;
|
---|
205 |
|
---|
206 | unsigned long int uli;
|
---|
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;
|
---|
211 |
|
---|
212 | signed long long int slli;
|
---|
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;
|
---|
217 |
|
---|
218 | unsigned long long int ulli;
|
---|
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;
|
---|
223 |
|
---|
224 | float f;
|
---|
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;
|
---|
229 |
|
---|
230 | double d;
|
---|
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;
|
---|
235 |
|
---|
236 | long double ld;
|
---|
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;
|
---|
241 |
|
---|
242 | float _Complex fc;
|
---|
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;
|
---|
247 |
|
---|
248 | double _Complex dc;
|
---|
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;
|
---|
253 |
|
---|
254 | long double _Complex ldc;
|
---|
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;
|
---|
259 | }
|
---|
260 | #if defined( __SIZEOF_INT128__ )
|
---|
261 | {
|
---|
262 | int128 val;
|
---|
263 | for ( 15 ) {
|
---|
264 | sin | val;
|
---|
265 | sout | val;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | #endif // __SIZEOF_INT128__
|
---|
269 | } // main
|
---|
270 |
|
---|
271 | // Local Variables: //
|
---|
272 | // tab-width: 4 //
|
---|
273 | // compile-command: "cfa -Wall -Wextra manipulatorsInput.cfa" //
|
---|
274 | // End: //
|
---|