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 : Wed Jan 3 11:15:04 2024 |
---|
10 | // Update Count : 103 |
---|
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"; |
---|
58 | const char sk_fmt[] = "%*[abc]"; |
---|
59 | scanf( "abc " ); scanf( sk_fmt ); for ( 5 ) scanf( "%*c" ); printf( "1 %s\n", s ); |
---|
60 | scanf( "%s", s ); printf( "2 %s\n", s ); |
---|
61 | scanf( "%*s" ); printf( "3 %s\n", s ); |
---|
62 | scanf( "%8s", s ); printf( "4 %s\n", s ); |
---|
63 | scanf( "%*8s" ); printf( "5 %s\n", s ); |
---|
64 | |
---|
65 | scanf( "%[abc]", s ); printf( "6 %s\n", s ); |
---|
66 | scanf( "%[^abc]", s ); printf( "7 %s\n", s ); |
---|
67 | scanf( "%*[abc]" ); printf( "8 %s\n", s ); |
---|
68 | scanf( "%*[^abc]" ); printf( "9 %s\n", s ); |
---|
69 | scanf( "%8[abc]", s ); printf( "10 %s\n", s ); |
---|
70 | scanf( "%8[^abc]", s ); printf( "11 %s\n", s ); |
---|
71 | scanf( "%*8[abc]" ); printf( "12 %s\n", s ); |
---|
72 | scanf( "%*8[^abc]" ); printf( "13 %s\n", s ); |
---|
73 | |
---|
74 | int rc; |
---|
75 | s[0] = 'q'; s[1] = '\0'; rc = 99; |
---|
76 | rc = scanf( "%[abc]", s ); printf( "14 rc=%d, %s\n", rc, s ); |
---|
77 | s[0] = 'q'; s[1] = '\0'; rc = 99; |
---|
78 | rc = scanf( "%[^u]", s ); printf( "15 rc=%d, %s\n", rc, s ); |
---|
79 | scanf( "%*[u]\n" ); |
---|
80 | scanf( "%[^\n]\n", s ); printf( "16 %s\n", s ); |
---|
81 | scanf( "%[^%%]%%\n", s ); printf( "17 %s\n", s ); |
---|
82 | |
---|
83 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace |
---|
84 | scanf( "\"%[^\"]\"", s ); printf( "18 %s\n", s ); |
---|
85 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace |
---|
86 | scanf( "'%[^']'", s ); printf( "19 %s\n", s ); |
---|
87 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace |
---|
88 | scanf( "{%[^}]}", s ); printf( "20 %s\n", s ); |
---|
89 | scanf( "%*[ \f\n\r\t\v]" ); // ignore whitespace |
---|
90 | scanf( "X%[^Y]Y", s ); printf( "21 %s\n", s ); |
---|
91 | scanf( "\n" ); // must start next line |
---|
92 | } |
---|
93 | { |
---|
94 | char s[] = "yyyyyyyyyyyyyyyyyyyy"; |
---|
95 | char sk[] = "abc"; |
---|
96 | sin | "abc " | skip( sk ) | skip( 5 ); sout | "1" | s; |
---|
97 | sin | wdi( sizeof(s), s ); sout | "2" | s; |
---|
98 | sin | ignore( s ); sout | "3" | s; |
---|
99 | sin | wdi( sizeof(s), 8, s ); sout | "4" | s; |
---|
100 | sin | ignore( wdi( sizeof(s), 8, s ) ); sout | "5" | s; |
---|
101 | |
---|
102 | sin | incl( "abc", wdi( sizeof(s), s ) ); sout | "6" | s; |
---|
103 | sin | excl( "abc", wdi( sizeof(s), s ) ); sout | "7" | s; |
---|
104 | sin | ignore( incl( "abc", wdi( sizeof(s), s ) ) ); sout | "8" | s; |
---|
105 | sin | ignore( excl( "abc", wdi( sizeof(s), s ) ) ); sout | "9" | s; |
---|
106 | sin | incl( "abc", wdi( sizeof(s), 8, s ) ); sout | "10" | s; |
---|
107 | sin | excl( "abc", wdi( sizeof(s), 8, s ) ); sout | "11" | s; |
---|
108 | sin | ignore( incl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "12" | s; |
---|
109 | sin | ignore( excl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "13" | s; |
---|
110 | |
---|
111 | s[0] = 'q'; s[1] = '\0'; |
---|
112 | sin | incl( "abc", wdi( sizeof(s), s ) ); sout | "14" | s; |
---|
113 | s[0] = 'q'; s[1] = '\0'; |
---|
114 | sin | excl( "u", wdi( sizeof(s), s ) ); sout | "15" | s; |
---|
115 | sin | skip( "u" ) | "\n"; |
---|
116 | sin | getline( wdi( sizeof(s), s ) ); sout | "16" | s; |
---|
117 | sin | getline( wdi( sizeof(s), s ), '%' ) | "\n"; sout | "17" | s; |
---|
118 | |
---|
119 | sin | quoted( wdi( sizeof(s), s ) ); sout | "18" | s; |
---|
120 | sin | quoted( wdi( sizeof(s), s ), '\'' ); sout | "19" | s; |
---|
121 | sin | quoted( wdi( sizeof(s), s ), '{', '}' ); sout | "20" | s; |
---|
122 | sin | quoted( wdi( sizeof(s), s ), 'X', 'Y' ); sout | "21" | s; |
---|
123 | } |
---|
124 | // Keep harmonized with collections/string-istream-manip |
---|
125 | { |
---|
126 | char c; |
---|
127 | sin | c; sout | c; |
---|
128 | sin | ignore( c ); sout | c; |
---|
129 | |
---|
130 | signed char sc; |
---|
131 | sin | sc; sout | sc; |
---|
132 | sin | wdi( 3, sc ); sout | sc; |
---|
133 | sin | ignore( sc ); sout | sc; |
---|
134 | sin | ignore( wdi( 3, sc ) ); sout | sc; |
---|
135 | |
---|
136 | unsigned char usc; |
---|
137 | sin | usc; sout | usc; |
---|
138 | sin | wdi( 3, usc ); sout | usc; |
---|
139 | sin | ignore( usc ); sout | usc; |
---|
140 | sin | ignore( wdi( 3, usc ) ); sout | usc; |
---|
141 | |
---|
142 | signed short int ssi; |
---|
143 | sin | ssi; sout | ssi; |
---|
144 | sin | wdi( 3, ssi ); sout | ssi; |
---|
145 | sin | ignore( ssi ); sout | ssi; |
---|
146 | sin | ignore( wdi( 3, ssi ) ); sout | ssi; |
---|
147 | |
---|
148 | unsigned short int usi; |
---|
149 | sin | usi; sout | usi; |
---|
150 | sin | wdi( 3, usi ); sout | usi; |
---|
151 | sin | ignore( usi ); sout | usi; |
---|
152 | sin | ignore( wdi( 3, usi ) ); sout | usi; |
---|
153 | |
---|
154 | signed int si; |
---|
155 | sin | si; sout | si; |
---|
156 | sin | wdi( 3, si ); sout | si; |
---|
157 | sin | ignore( si ); sout | si; |
---|
158 | sin | ignore( wdi( 3, si ) ); sout | si; |
---|
159 | |
---|
160 | unsigned int ui; |
---|
161 | sin | ui; sout | ui; |
---|
162 | sin | wdi( 3, ui ); sout | ui; |
---|
163 | sin | ignore( ui ); sout | ui; |
---|
164 | sin | ignore( wdi( 3, ui ) ); sout | ui; |
---|
165 | |
---|
166 | signed long int sli; |
---|
167 | sin | sli; sout | sli; |
---|
168 | sin | wdi( 3, sli ); sout | sli; |
---|
169 | sin | ignore( sli ); sout | sli; |
---|
170 | sin | ignore( wdi( 3, sli ) ); sout | sli; |
---|
171 | |
---|
172 | unsigned long int uli; |
---|
173 | sin | uli; sout | uli; |
---|
174 | sin | wdi( 3, uli ); sout | uli; |
---|
175 | sin | ignore( uli ); sout | uli; |
---|
176 | sin | ignore( wdi( 3, uli ) ); sout | uli; |
---|
177 | |
---|
178 | signed long long int slli; |
---|
179 | sin | slli; sout | slli; |
---|
180 | sin | wdi( 3, slli ); sout | slli; |
---|
181 | sin | ignore( slli ); sout | slli; |
---|
182 | sin | ignore( wdi( 3, slli ) ); sout | slli; |
---|
183 | |
---|
184 | unsigned long long int ulli; |
---|
185 | sin | ulli; sout | ulli; |
---|
186 | sin | wdi( 3, ulli ); sout | ulli; |
---|
187 | sin | ignore( ulli ); sout | ulli; |
---|
188 | sin | ignore( wdi( 3, ulli ) ); sout | ulli; |
---|
189 | |
---|
190 | float f; |
---|
191 | sin | f; sout | f; |
---|
192 | sin | wdi( 8, f ); sout | f; |
---|
193 | sin | ignore( f ); sout | f; |
---|
194 | sin | ignore( wdi( 8, f ) ); sout | f; |
---|
195 | |
---|
196 | double d; |
---|
197 | sin | d; sout | d; |
---|
198 | sin | wdi( 8, d ); sout | d; |
---|
199 | sin | ignore( d ); sout | d; |
---|
200 | sin | ignore( wdi( 8, d ) ); sout | d; |
---|
201 | |
---|
202 | long double ld; |
---|
203 | sin | ld; sout | ld; |
---|
204 | sin | wdi( 8, ld ); sout | ld; |
---|
205 | sin | ignore( ld ); sout | ld; |
---|
206 | sin | ignore( wdi( 8, ld ) ); sout | ld; |
---|
207 | |
---|
208 | float _Complex fc; |
---|
209 | sin | fc; sout | fc; |
---|
210 | sin | wdi( 8, fc ); sout | fc; |
---|
211 | sin | ignore( fc ); sout | fc; |
---|
212 | sin | ignore( wdi( 8, fc ) ); sout | fc; |
---|
213 | |
---|
214 | double _Complex dc; |
---|
215 | sin | dc; sout | dc; |
---|
216 | sin | wdi( 8, dc ); sout | dc; |
---|
217 | sin | ignore( dc ); sout | dc; |
---|
218 | sin | ignore( wdi( 8, dc ) ); sout | dc; |
---|
219 | |
---|
220 | long double _Complex ldc; |
---|
221 | sin | ldc; sout | ldc; |
---|
222 | sin | wdi( 8, ldc ); sout | ldc; |
---|
223 | sin | ignore( ldc ); sout | ldc; |
---|
224 | sin | ignore( wdi( 8, ldc ) ); sout | ldc; |
---|
225 | } |
---|
226 | #if defined( __SIZEOF_INT128__ ) |
---|
227 | { |
---|
228 | int128 val; |
---|
229 | for ( 15 ) { |
---|
230 | sin | val; |
---|
231 | sout | val; |
---|
232 | } |
---|
233 | } |
---|
234 | #endif // __SIZEOF_INT128__ |
---|
235 | } // main |
---|
236 | |
---|
237 | // Local Variables: // |
---|
238 | // tab-width: 4 // |
---|
239 | // compile-command: "cfa -Wall -Wextra manipulatorsInput.cfa" // |
---|
240 | // End: // |
---|