1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // io.cfa --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Wed Mar 2 16:56:02 2016
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Sun Nov 26 09:15:58 2023
|
---|
13 | // Update Count : 129
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <fstream.hfa>
|
---|
17 |
|
---|
18 | #define xstr(s) str(s)
|
---|
19 | #define str(s) #s
|
---|
20 |
|
---|
21 | int main() {
|
---|
22 | sout | nlOff; // auto nl off
|
---|
23 |
|
---|
24 | sout | "opening delimiters" | nl;
|
---|
25 | sout | "x (" | 1;
|
---|
26 | sout | "x [" | 2;
|
---|
27 | sout | "x {" | 3;
|
---|
28 | sout | "x =" | 4;
|
---|
29 | sout | "x $" | 5;
|
---|
30 | sout | "x £" | 6;
|
---|
31 | sout | "x ¥" | 7;
|
---|
32 | sout | "x ¡" | 8;
|
---|
33 | sout | "x ¿" | 9;
|
---|
34 | sout | "x «" | 10;
|
---|
35 | sout | nl | nl;
|
---|
36 |
|
---|
37 | sout | "closing delimiters" | nl;
|
---|
38 | sout | 1 | ", x";
|
---|
39 | sout | 2 | ". x";
|
---|
40 | sout | 3 | "; x";
|
---|
41 | sout | 4 | "! x";
|
---|
42 | sout | 5 | "? x";
|
---|
43 | sout | 6 | "% x";
|
---|
44 | sout | 7 | "¢ x";
|
---|
45 | sout | 8 | "» x";
|
---|
46 | sout | 9 | ") x";
|
---|
47 | sout | 10 | "] x";
|
---|
48 | sout | 11 | "} x";
|
---|
49 | sout | nl | nl;
|
---|
50 |
|
---|
51 | sout | "opening/closing delimiters" | nl;
|
---|
52 | sout | "x`" | 1 | "`x'" | 2;
|
---|
53 | sout | "'x\"" | 3 | "\"x:" | 4;
|
---|
54 | sout | ":x " | 5 | " x\t" | 6;
|
---|
55 | sout | "\tx\f" | 7 | "\fx\v" | 8;
|
---|
56 | sout | "\vx\n" | 9 | "\nx\r" | 10;
|
---|
57 | sout | "\rx";
|
---|
58 | sout | nl | nl;
|
---|
59 |
|
---|
60 | sout | nlOn; // auto nl on
|
---|
61 |
|
---|
62 | sout | "override opening/closing delimiters";
|
---|
63 | sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4;
|
---|
64 | sout | nl;
|
---|
65 |
|
---|
66 | sout | "spacing";
|
---|
67 | sout | 0 | 1 | 2 | 3;
|
---|
68 | sout | '0' | '1' | '2' | '3';
|
---|
69 | sout | 0 | "" | 1 | "" | 2 | "" | 3;
|
---|
70 | sout | nl;
|
---|
71 |
|
---|
72 | sout | "expressions";
|
---|
73 | int x = 3, y = 5, z = 7;
|
---|
74 | sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2);
|
---|
75 | sout | nl;
|
---|
76 |
|
---|
77 | _Bool b; // basic types
|
---|
78 | char c;
|
---|
79 | signed char sc;
|
---|
80 | unsigned char usc;
|
---|
81 | short int si;
|
---|
82 | unsigned short int usi;
|
---|
83 | int i;
|
---|
84 | unsigned int ui;
|
---|
85 | long int li;
|
---|
86 | unsigned long int uli;
|
---|
87 | long long int lli;
|
---|
88 | unsigned long long int ulli;
|
---|
89 | float f;
|
---|
90 | double d;
|
---|
91 | long double ld;
|
---|
92 | float _Complex fc;
|
---|
93 | double _Complex dc;
|
---|
94 | long double _Complex ldc;
|
---|
95 | enum { size = 10 };
|
---|
96 | char s1[size], s2[size];
|
---|
97 |
|
---|
98 | ifstream in = { xstr(IN_DIR) "io.data" }; // create / open file
|
---|
99 |
|
---|
100 | sout | "input bacis types";
|
---|
101 | in | b; // boolean
|
---|
102 | in | c | sc | usc; // character
|
---|
103 | in | si | usi | i | ui | li | uli | lli | ulli; // integral
|
---|
104 | in | f | d | ld; // floating point
|
---|
105 | in | fc | dc | ldc; // floating-point complex
|
---|
106 | in | wdi( size, s1 ) | wdi( size, s2 ); // C string, length checked
|
---|
107 | sout | nl;
|
---|
108 |
|
---|
109 | sout | "output basic types";
|
---|
110 | sout | b; // boolean
|
---|
111 | sout | c | ' ' | sc | ' ' | usc; // character
|
---|
112 | sout | si | usi | i | ui | li | uli | lli | ulli; // integral
|
---|
113 | sout | f | d | ld; // floating point
|
---|
114 | sout | fc | dc | ldc; // complex
|
---|
115 | sout | nl;
|
---|
116 |
|
---|
117 | sout | "tuples";
|
---|
118 | [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ];
|
---|
119 | sout | t1 | t2; // print tuple
|
---|
120 | sout | nl;
|
---|
121 |
|
---|
122 | sout | "toggle separator";
|
---|
123 | sout | f | "" | d | "" | ld; // floating point without separator
|
---|
124 | sout | sepOff | fc | dc | ldc; // complex without separator
|
---|
125 | sout | fc | sep | dc | ldc; // local separator add
|
---|
126 | sout | sepOn | fc | dc | ldc; // complex with separator
|
---|
127 | sout | fc | nosep | dc | ldc; // local separator removal
|
---|
128 | sout | s1 | nosep | s2; // local separator removal
|
---|
129 | sout | s1 | "" | s2; // local separator removal
|
---|
130 | sout | nl;
|
---|
131 |
|
---|
132 | sout | "change separator";
|
---|
133 | sout | "from \"" | sepVal | "\"" | nonl;
|
---|
134 | sepSet( sout, ", $" ); // change separator, maximum of 15 characters
|
---|
135 | sout | " to \"" | sepVal | "\"";
|
---|
136 | sout | f | d | ld;
|
---|
137 | sout | fc | dc | ldc;
|
---|
138 | sout | s1 | s2;
|
---|
139 | sout | t1 | t2; // print tuple
|
---|
140 | sout | nl;
|
---|
141 | sout | "from \"" | sepVal | "\" " | nonl;
|
---|
142 | sepSet( sout, " " ); // restore separator
|
---|
143 | sout | "to \"" | sepVal | "\"";
|
---|
144 | sout | f | d | ld;
|
---|
145 | sout | fc | dc | ldc;
|
---|
146 | sout | s1 | s2;
|
---|
147 | sout | t1 | t2; // print tuple
|
---|
148 | sout | nl;
|
---|
149 |
|
---|
150 | sout | "check sep/nosep";
|
---|
151 | sout | sep | 1 | 2 | 3 | sep; // no separator at start/end of line
|
---|
152 | sout | 1 | nosep | 2 | 3; // locally turn off implicit separator
|
---|
153 | sout | sep | sep | 1 | 2 | 3 | sep | nosep | sep | '\n' | nonl; // no separator at start/end of line
|
---|
154 | sout | 1 | 2 | 3 | "\n\n" | sep | nonl; // no separator at start of next line
|
---|
155 | sout | 1 | 2 | 3;
|
---|
156 | sout | nl;
|
---|
157 |
|
---|
158 | sout | "check enable/disable";
|
---|
159 | sout | sepOff | 1 | 2 | 3; // globally turn off implicit separation
|
---|
160 | sout | 1 | sep | 2 | 3; // locally turn on implicit separator
|
---|
161 | sout | sepOn | 1 | 2 | 3 | sepOff; // globally turn on/off implicit separation
|
---|
162 | sout | 1 | 2 | 3 | sepOn; // globally turn on implicit separation
|
---|
163 | sout | 1 | 2 | 3 | sep | sepOff; // ignore seperate at end of line
|
---|
164 | sout | 1 | 2 | 3 | sep | sepOn; // separator at end of line
|
---|
165 | sout | 1 | 2 | 3;
|
---|
166 | sout | nl;
|
---|
167 |
|
---|
168 | // sout | fmt( d, "%8.3f" );
|
---|
169 |
|
---|
170 | sepSetTuple( sout, " " ); // set tuple separator from ", " to " "
|
---|
171 | sout | t1 | t2 | " \"" | sepVal | "\"";
|
---|
172 | sepSetTuple( sout, ", " ); // reset tuple separator to ", "
|
---|
173 | sout | t1 | t2 | " \"" | sepVal | "\"";
|
---|
174 | sout | t1 | t2; // print tuple
|
---|
175 | sout | nl;
|
---|
176 |
|
---|
177 | [int, int, const char *, double] t3 = { 3, 4, "a", 7.2 };
|
---|
178 | sout | [ 3, 4, (const char*)"a", 7.2 ]; // workaround trac#207: the const cast should not be needed
|
---|
179 | sout | t3;
|
---|
180 | sepSetTuple( sout, " " );
|
---|
181 | sout | t3;
|
---|
182 | sout | sep | t3 | sepOff | t3 | sepOn | t3;
|
---|
183 | sepSet( sout, "^" );
|
---|
184 | sepSetTuple( sout, "-" );
|
---|
185 | sout | t3 | 3 | 4 | t3;
|
---|
186 | }
|
---|
187 |
|
---|
188 | // Local Variables: //
|
---|
189 | // tab-width: 4 //
|
---|
190 | // compile-command: "cfa -DIN_DIR=\".in/\" io.cfa" //
|
---|
191 | // End: //
|
---|