[dfa4360] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2017 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 | // |
---|
[3ac5fd8] | 7 | // fmtLines.cfa -- format characters into blocks of 4 and groups of 5 blocks per line |
---|
[dfa4360] | 8 | // |
---|
[3ac5fd8] | 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Thu Mar 5 16:09:08 2020 |
---|
[dfa4360] | 11 | // Last Modified By : Peter A. Buhr |
---|
[3ac5fd8] | 12 | // Last Modified On : Sat Aug 17 14:21:28 2024 |
---|
| 13 | // Update Count : 5 |
---|
[dfa4360] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <fstream.hfa> |
---|
| 17 | |
---|
[3ac5fd8] | 18 | generator Format { |
---|
[dfa4360] | 19 | char ch; // used for communication |
---|
| 20 | int g, b; // global because used in destructor |
---|
| 21 | }; |
---|
| 22 | |
---|
[3ac5fd8] | 23 | void main( Format & fmt ) with( fmt ) { |
---|
| 24 | for () { // for as many characters |
---|
| 25 | for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks |
---|
| 26 | for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters |
---|
| 27 | for () { // for newline characters |
---|
[dfa4360] | 28 | suspend; |
---|
[3ac5fd8] | 29 | if ( ch != '\n' ) break; // ignore newline |
---|
[dfa4360] | 30 | } // for |
---|
[3ac5fd8] | 31 | sout | ch; // print character |
---|
[dfa4360] | 32 | } // for |
---|
| 33 | sout | " "; // print block separator |
---|
| 34 | } // for |
---|
[3ac5fd8] | 35 | sout | nl; // print group separator |
---|
[dfa4360] | 36 | } // for |
---|
| 37 | } // main |
---|
| 38 | |
---|
[3ac5fd8] | 39 | void ?{}( Format & fmt ) { |
---|
| 40 | resume( fmt ); // prime (start) coroutine |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void ^?{}( Format & fmt ) with( fmt ) { |
---|
| 44 | if ( g != 0 || b != 0 ) sout | nl; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void format( Format & fmt ) { |
---|
| 48 | resume( fmt ); |
---|
| 49 | } // format |
---|
[dfa4360] | 50 | |
---|
| 51 | int main() { |
---|
| 52 | Format fmt; |
---|
[3ac5fd8] | 53 | sout | nlOff; // turn off auto newline |
---|
[dfa4360] | 54 | |
---|
[3ac5fd8] | 55 | try { |
---|
| 56 | for () { // read until end of file |
---|
| 57 | sin | fmt.ch; // read one character |
---|
| 58 | format( fmt ); // push character for formatting |
---|
| 59 | } // for |
---|
| 60 | } catch( end_of_file * ) { |
---|
| 61 | } // try |
---|
[dfa4360] | 62 | } // main |
---|