[73abe95] | 1 | // |
---|
[e06be49] | 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. |
---|
[73abe95] | 6 | // |
---|
[a0cfa44] | 7 | // fmtLines.cc -- format characters into blocks of 4 and groups of 5 blocks per line |
---|
[73abe95] | 8 | // |
---|
[e06be49] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Sun Sep 17 21:56:15 2017 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[f8cd310] | 12 | // Last Modified On : Fri Mar 22 13:41:16 2019 |
---|
| 13 | // Update Count : 58 |
---|
[73abe95] | 14 | // |
---|
[e06be49] | 15 | |
---|
[73abe95] | 16 | #include <fstream.hfa> |
---|
| 17 | #include <coroutine.hfa> |
---|
[e06be49] | 18 | |
---|
| 19 | coroutine Format { |
---|
| 20 | char ch; // used for communication |
---|
| 21 | int g, b; // global because used in destructor |
---|
| 22 | }; |
---|
| 23 | |
---|
[971d9f2] | 24 | void main( Format & fmt ) with( fmt ) { |
---|
[adb6b30f] | 25 | for () { // for as many characters |
---|
[5ea5b28] | 26 | for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks |
---|
| 27 | for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters |
---|
[adb6b30f] | 28 | for () { // for newline characters |
---|
[e06be49] | 29 | suspend(); |
---|
[5ea5b28] | 30 | if ( ch != '\n' ) break; // ignore newline |
---|
[e06be49] | 31 | } // for |
---|
[971d9f2] | 32 | sout | ch; // print character |
---|
[e06be49] | 33 | } // for |
---|
| 34 | sout | " "; // print block separator |
---|
| 35 | } // for |
---|
[200fcb3] | 36 | sout | nl; // print group separator |
---|
[e06be49] | 37 | } // for |
---|
| 38 | } // main |
---|
| 39 | |
---|
[a0cfa44] | 40 | void ?{}( Format & fmt ) { |
---|
| 41 | resume( fmt ); // prime (start) coroutine |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void ^?{}( Format & fmt ) with( fmt ) { |
---|
[200fcb3] | 45 | if ( g != 0 || b != 0 ) sout | nl; |
---|
[a0cfa44] | 46 | } |
---|
| 47 | |
---|
| 48 | void format( Format & fmt ) { |
---|
[6545adff] | 49 | resume( fmt ); |
---|
[5dd3098] | 50 | } // format |
---|
[e06be49] | 51 | |
---|
| 52 | int main() { |
---|
[a0cfa44] | 53 | Format fmt; |
---|
[200fcb3] | 54 | sout | nlOff; // turn off auto newline |
---|
[e06be49] | 55 | |
---|
[adb6b30f] | 56 | eof: for () { // read until end of file |
---|
[a0cfa44] | 57 | sin | fmt.ch; // read one character |
---|
| 58 | if ( eof( sin ) ) break eof; // eof ? |
---|
| 59 | format( fmt ); // push character for formatting |
---|
[e06be49] | 60 | } // for |
---|
| 61 | } // main |
---|
| 62 | |
---|
| 63 | // Local Variables: // |
---|
| 64 | // tab-width: 4 // |
---|
[f8cd310] | 65 | // compile-command: "cfa fmtLines.cfa" // |
---|
[e06be49] | 66 | // End: // |
---|