summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/coread.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-25 12:20:22 +0100
committerTyge Løvset <[email protected]>2023-02-25 12:20:22 +0100
commit12a64923057aea6d09f541356bb5d9c929816b82 (patch)
tree0a84271f9f8c936938eb33821a5967baa37d6045 /misc/examples/coread.c
parent25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2 (diff)
downloadSTC-modified-12a64923057aea6d09f541356bb5d9c929816b82.tar.gz
STC-modified-12a64923057aea6d09f541356bb5d9c929816b82.zip
Improved coread.c example a lot.
Diffstat (limited to 'misc/examples/coread.c')
-rw-r--r--misc/examples/coread.c19
1 files changed, 8 insertions, 11 deletions
diff --git a/misc/examples/coread.c b/misc/examples/coread.c
index 6b0bfb37..a9381db8 100644
--- a/misc/examples/coread.c
+++ b/misc/examples/coread.c
@@ -11,33 +11,30 @@ struct file_nextline {
cstr line;
};
-cstr file_nextline(struct file_nextline* U)
+bool file_nextline(struct file_nextline* U)
{
cco_begin(U)
U->fp = fopen(U->filename, "r");
U->line = cstr_NULL;
while (cstr_getline(&U->line, U->fp))
- cco_yield(cstr_clone(U->line));
+ cco_yield(true);
cco_final: // cco_final is needed to support cco_stop.
printf("finish\n");
cstr_drop(&U->line);
fclose(U->fp);
cco_end();
- return cstr_NULL;
+ return false;
}
-
int main(void) {
struct file_nextline z = {__FILE__};
int n = 0;
- do {
- c_with (cstr line = file_nextline(&z), cco_alive(&z), cstr_drop(&line)) {
- printf("%3d %s\n", ++n, cstr_str(&line));
+ while (file_nextline(&z)) {
+ printf("%3d %s\n", ++n, cstr_str(&z.line));
- // stop after 15 lines:
- if (n == 15) file_nextline(cco_stop(&z));
- }
- } while (cco_alive(&z));
+ // stop after 15 lines:
+ if (n == 15) cco_stop(&z);
+ }
}