| [d447238] | 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 Feb 21 10:56:33 2021 | 
|---|
|  | 13 | // Update Count     : 121 | 
|---|
|  | 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       | s1 | wdi( size, s2 );                                                // C string, length unchecked and 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 | sepDisable | fc | dc | ldc;                                      // complex without separator | 
|---|
|  | 125 | sout | fc | sepOn | dc | ldc;                                           // local separator add | 
|---|
|  | 126 | sout | sepEnable | fc | dc | ldc;                                       // complex with separator | 
|---|
|  | 127 | sout | fc | sepOff | dc | ldc;                                          // local separator removal | 
|---|
|  | 128 | sout | s1 | sepOff | s2;                                                        // local separator removal | 
|---|
|  | 129 | sout | s1 | "" | s2;                                                            // local separator removal | 
|---|
|  | 130 | sout | nl; | 
|---|
|  | 131 |  | 
|---|
|  | 132 | sout | "change separator"; | 
|---|
|  | 133 | sout | "from \"" | sep | "\"" | nonl; | 
|---|
|  | 134 | sepSet( sout, ", $" );                                                          // change separator, maximum of 15 characters | 
|---|
|  | 135 | sout | " to \"" | sep | "\""; | 
|---|
|  | 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 \"" | sep | "\" " | nonl; | 
|---|
|  | 142 | sepSet( sout, " " );                                                            // restore separator | 
|---|
|  | 143 | sout | "to \"" | sep | "\""; | 
|---|
|  | 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 sepOn/sepOff"; | 
|---|
|  | 151 | sout | sepOn | 1 | 2 | 3 | sepOn;                                       // no separator at start/end of line | 
|---|
|  | 152 | sout | 1 | sepOff | 2 | 3;                                                      // locally turn off implicit separator | 
|---|
|  | 153 | sout | sepOn | sepOn | 1 | 2 | 3 | sepOn | sepOff | sepOn | '\n' | nonl; // no separator at start/end of line | 
|---|
|  | 154 | sout | 1 | 2 | 3 | "\n\n" | sepOn | nonl;                       // no separator at start of next line | 
|---|
|  | 155 | sout | 1 | 2 | 3; | 
|---|
|  | 156 | sout | nl; | 
|---|
|  | 157 |  | 
|---|
|  | 158 | sout | "check enable/disable"; | 
|---|
|  | 159 | sout | sepDisable | 1 | 2 | 3;                                          // globally turn off implicit separation | 
|---|
|  | 160 | sout | 1 | sepOn | 2 | 3;                                                       // locally turn on implicit separator | 
|---|
|  | 161 | sout | sepEnable | 1 | 2 | 3 | sepDisable;                      // globally turn on/off implicit separation | 
|---|
|  | 162 | sout | 1 | 2 | 3 | sepEnable;                                           // globally turn on implicit separation | 
|---|
|  | 163 | sout | 1 | 2 | 3 | sepOn | sepDisable;                          // ignore seperate at end of line | 
|---|
|  | 164 | sout | 1 | 2 | 3 | sepOn | sepEnable;                           // 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 | " \"" | sep | "\""; | 
|---|
|  | 172 | sepSetTuple( sout, ", " );                                                      // reset tuple separator to ", " | 
|---|
|  | 173 | sout | t1 | t2 | " \"" | sep | "\""; | 
|---|
|  | 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 | sepOn | t3 | sepDisable | t3 | sepEnable | 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: // | 
|---|