aboutsummaryrefslogtreecommitdiff
path: root/src_c/stream.c
diff options
context:
space:
mode:
authorbozo.kopic <bozo@kopic.xyz>2022-08-02 01:20:12 +0200
committerbozo.kopic <bozo@kopic.xyz>2022-09-25 02:40:23 +0200
commit288727f09a1b3458c268497d111349e608c3f9fa (patch)
treed62565249fa3c7127856c65405752572fc41aca9 /src_c/stream.c
Diffstat (limited to 'src_c/stream.c')
-rw-r--r--src_c/stream.c108
1 files changed, 108 insertions, 0 deletions
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;
+}