[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 | // |
---|
| 7 | // fmtLines.cfa -- format characters into blocks of 4 and groups of 5 blocks per line |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Thu Mar 5 16:09:08 2020 |
---|
[3720c9aa] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Jun 10 21:56:22 2021 |
---|
| 13 | // Update Count : 2 |
---|
[dfa4360] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <fstream.hfa> |
---|
| 17 | |
---|
| 18 | generator Format { |
---|
| 19 | char ch; // used for communication |
---|
| 20 | int g, b; // global because used in destructor |
---|
| 21 | }; |
---|
| 22 | |
---|
| 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 |
---|
| 28 | suspend; |
---|
| 29 | if ( ch != '\n' ) break; // ignore newline |
---|
| 30 | } // for |
---|
| 31 | sout | ch; // print character |
---|
| 32 | } // for |
---|
| 33 | sout | " "; // print block separator |
---|
| 34 | } // for |
---|
| 35 | sout | nl; // print group separator |
---|
| 36 | } // for |
---|
| 37 | } // main |
---|
| 38 | |
---|
| 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 |
---|
| 50 | |
---|
| 51 | int main() { |
---|
| 52 | Format fmt; |
---|
| 53 | sout | nlOff; // turn off auto newline |
---|
| 54 | |
---|
| 55 | eof: for () { // read until end of file |
---|
| 56 | sin | fmt.ch; // read one character |
---|
| 57 | if ( eof( sin ) ) break eof; // eof ? |
---|
| 58 | format( fmt ); // push character for formatting |
---|
| 59 | } // for |
---|
| 60 | } // main |
---|
| 61 | |
---|
| 62 | // Local Variables: // |
---|
| 63 | // tab-width: 4 // |
---|
| 64 | // compile-command: "cfa fmtLines.cfa" // |
---|
| 65 | // End: // |
---|