From 288727f09a1b3458c268497d111349e608c3f9fa Mon Sep 17 00:00:00 2001 From: "bozo.kopic" Date: Tue, 2 Aug 2022 01:20:12 +0200 Subject: init --- src_c/stream.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src_c/stream.c (limited to 'src_c/stream.c') diff --git a/src_c/stream.c b/src_c/stream.c new file mode 100644 index 0000000..37f5d3d --- /dev/null +++ b/src_c/stream.c @@ -0,0 +1,108 @@ +#include "stream.h" + + +static lsp_int16_t str_getchar(lsp_in_stream_t *s) { + lsp_str_stream_t *ss = (lsp_str_stream_t *)s; + + if (ss->pos >= lsp_mem_get_string_len(ss->m, ss->str)) + return LSP_EOF; + + return lsp_mem_get_string_data(ss->m, ss->str, ss->pos++); +} + + +void lsp_in_stream_init(lsp_in_stream_t *s, lsp_stream_getchar_t getchar) { + s->getchar = getchar; + s->next_available = false; +} + + +lsp_status_t lsp_in_stream_read(lsp_in_stream_t *s, lsp_uint8_t *v) { + if (s->next_available) { + s->next_available = false; + *v = s->next_value; + return s->next_status; + } + + lsp_int16_t c = s->getchar(s); + *v = c; + return ((c == LSP_EOF) ? LSP_EOF : LSP_SUCCESS); +} + + +lsp_status_t lsp_in_stream_peek(lsp_in_stream_t *s, lsp_uint8_t *v) { + if (s->next_available) { + *v = s->next_value; + return s->next_status; + } + + lsp_int16_t c = s->getchar(s); + s->next_available = true; + s->next_value = c; + s->next_status = ((c == LSP_EOF) ? LSP_EOF : LSP_SUCCESS); + + *v = s->next_value; + return s->next_status; +} + + +void lsp_out_stream_init(lsp_out_stream_t *s, lsp_stream_putchar_t putchar) { + s->putchar = putchar; +} + + +lsp_status_t lsp_out_stream_write(lsp_out_stream_t *s, lsp_uint8_t v) { + if (s->putchar(s, v) == LSP_EOF) + return LSP_EOF; + + return LSP_SUCCESS; +} + + +lsp_status_t lsp_out_stream_write_str(lsp_out_stream_t *s, char *str) { + while (*str) { + lsp_status_t status = lsp_out_stream_write(s, *(str++)); + if (status != LSP_SUCCESS) + return status; + } + + return LSP_SUCCESS; +} + + +lsp_status_t lsp_out_stream_write_int(lsp_out_stream_t *s, lsp_int32_t v) { + lsp_status_t status; + if (v < 0) { + v *= -1; + status = lsp_out_stream_write(s, '-'); + if (status != LSP_SUCCESS) + return status; + } + + lsp_uint8_t size = 0; + for (lsp_int32_t i = v; i; i /= 10) + size++; + if (size < 1) + size = 1; + + for (lsp_uint8_t i = 0; i < size; ++i) { + lsp_int32_t temp = v; + for (lsp_uint8_t j = i; j < size - 1; ++j) + temp /= 10; + + lsp_uint8_t digit = temp % 10; + status = lsp_out_stream_write(s, '0' + digit); + if (status != LSP_SUCCESS) + return status; + } + + return LSP_SUCCESS; +} + + +void lsp_str_stream_init(lsp_str_stream_t *s, lsp_mem_t *m, lsp_addr_t str) { + lsp_in_stream_init((lsp_in_stream_t *)s, str_getchar); + s->m = m; + s->str = str; + s->pos = 0; +} -- cgit v1.2.3-70-g09d2