summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/coread.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/coread.c')
-rw-r--r--misc/examples/coread.c34
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);
}
}