summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/coread.c
blob: 622228c0adef0adbdd5524cbea168e65f234e778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#define i_implement
#include <stc/cstr.h>
#include <stc/algo/coroutine.h>
#include <errno.h>

// Read file line by line using coroutines:

struct file_read {
    const char* filename;
    int cco_state;
    FILE* fp;
    cstr line;
};

void file_read(struct file_read* g)
{
    cco_routine(g) {
        g->fp = fopen(g->filename, "r");
        if (!g->fp) cco_return;
        g->line = cstr_init();

        cco_await(!cstr_getline(&g->line, g->fp));

    cco_final:
        printf("finish\n");
        cstr_drop(&g->line);
        if (g->fp) fclose(g->fp);
    }
}

int main(void)
{
    struct file_read g = {__FILE__};
    int n = 0;
    cco_block_on(&g, file_read)
    {
        printf("%3d %s\n", ++n, cstr_str(&g.line));
        //if (n == 10) cco_stop(&g);
    }
}