main
1#ifndef KC_HEADER_IO_STREAM_INCLUDED
2#define KC_HEADER_IO_STREAM_INCLUDED
3
4#include "../types/types.h"
5#include "../memory/arenas.h"
6
7typedef status (*StreamInteractFunction)(void*, u64*, u8*);
8typedef status (*StreamCloseFunction)(void*);
9
10typedef struct _SourceGeneric {
11 void* self;
12 status (*consume)(void *self, u64 *amount, u8* buf);
13 status (*close)(void *self);
14} SourceGeneric;
15
16typedef struct _SinkGeneric{
17 void* self;
18 status (*produce)(void *self, u64 *amount, u8* buf);
19 status (*close)(void *self);
20} SinkGeneric;
21
22status Stream_into_arena( SourceGeneric *s, Arena *a, u64 blocksize, u64 *amount_out );
23
24
25#ifdef KC_IMPLEMENT
26
27 status Stream_into_arena( SourceGeneric *s, Arena *a, u64 blocksize, u64 *amount_out ) {
28 *amount_out = 0;
29 u8* block = 0;
30 u64 read = blocksize;
31 status state;
32
33 PROPAGATE_ERROR( Arena_take_amount( a, blocksize, &block ) );
34
35 read = blocksize;
36 while( (state = s->consume(s->self, &read, block)) == STATUS_SUCCESS ) {
37 read = blocksize;
38 PROPAGATE_ERROR( Arena_take_amount( a, blocksize, &block ) );
39 *amount_out += read;
40 }
41
42 if( state == STATUS_STREAM_ENDED ) {
43 *amount_out += read;
44 return STATUS_SUCCESS;
45 } else {
46 return state;
47 }
48 }
49
50#endif
51
52#endif