diff options
| author | Tyge Løvset <[email protected]> | 2023-07-18 02:36:04 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-18 02:36:04 +0200 |
| commit | da70303c149b37dbf442e41038a00836132562ee (patch) | |
| tree | 06d795c66d6759e06de5f2d166215b461ac40d1d /misc/examples/coread.c | |
| parent | 071b41c0fe95cb3f9a72bbe0417d856e7989ca08 (diff) | |
| parent | 23eeedb3fc298602732f394adba6a43c876ca7d8 (diff) | |
| download | STC-modified-da70303c149b37dbf442e41038a00836132562ee.tar.gz STC-modified-da70303c149b37dbf442e41038a00836132562ee.zip | |
Merge branch 'dev43' into master
Diffstat (limited to 'misc/examples/coread.c')
| -rw-r--r-- | misc/examples/coread.c | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/misc/examples/coread.c b/misc/examples/coread.c index 0a7f4816..a13f6be5 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -1,39 +1,41 @@ +#define i_implement #include <stc/cstr.h> #include <stc/algo/coroutine.h> #include <errno.h> // Read file line by line using coroutines: -struct file_nextline { +struct file_read { const char* filename; int cco_state; FILE* fp; cstr line; }; -bool file_nextline(struct file_nextline* U) +int file_read(struct file_read* g) { - cco_begin(U) - U->fp = fopen(U->filename, "r"); - U->line = cstr_init(); + cco_routine(g) { + g->fp = fopen(g->filename, "r"); + if (!g->fp) cco_return; + g->line = cstr_init(); - while (cstr_getline(&U->line, U->fp)) - cco_yield(true); + cco_await(!cstr_getline(&g->line, g->fp)); - cco_final: // this label is required. - printf("finish\n"); - cstr_drop(&U->line); - fclose(U->fp); - cco_end(false); + cco_cleanup: + printf("finish\n"); + cstr_drop(&g->line); + if (g->fp) fclose(g->fp); + } + return 0; } int main(void) { - struct file_nextline it = {__FILE__}; + struct file_read g = {__FILE__}; int n = 0; - while (file_nextline(&it)) + cco_block_on(file_read(&g)) { - printf("%3d %s\n", ++n, cstr_str(&it.line)); - //if (n == 10) cco_stop(&it); + printf("%3d %s\n", ++n, cstr_str(&g.line)); + //if (n == 10) cco_stop(&g); } } |
