From 4b8c53c088e393147a77e56bb76f6f997be043ed Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 18 Dec 2017 17:27:53 -0500 Subject: [PATCH] Issue #236 - Added C functions for single-byte I/O --- include/cyclone/runtime.h | 3 ++ runtime.c | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/include/cyclone/runtime.h b/include/cyclone/runtime.h index 80ea3f87..d2b89ee3 100644 --- a/include/cyclone/runtime.h +++ b/include/cyclone/runtime.h @@ -267,6 +267,9 @@ object Cyc_io_close_output_port(void *data, object port); object Cyc_io_flush_output_port(void *data, object port); object Cyc_io_read_char(void *data, object cont, object port); object Cyc_io_peek_char(void *data, object cont, object port); +object Cyc_write_u8(void *data, object c, object port); +object Cyc_io_read_u8(void *data, object cont, object port); +object Cyc_io_peek_u8(void *data, object cont, object port); object Cyc_io_read_line(void *data, object cont, object port); void Cyc_io_read_token(void *data, object cont, object port); /**@}*/ diff --git a/runtime.c b/runtime.c index a163cd87..7df4729c 100644 --- a/runtime.c +++ b/runtime.c @@ -1113,6 +1113,21 @@ object Cyc_write_char(void *data, object c, object port) return quote_void; } +object Cyc_write_u8(void *data, object c, object port) +{ + Cyc_check_port(data, port); + if (obj_is_char(c)) { + FILE *fp = ((port_type *) port)->fp; + if (fp){ + char_type unbox = obj_obj2char(c); + fprintf(fp, "%c", unbox); + } + } else { + Cyc_rt_raise2(data, "Argument is not a character", c); + } + return quote_void; +} + /* Fast versions of member and assoc */ object memberp(void *data, object x, list l) { @@ -6401,6 +6416,29 @@ object Cyc_io_peek_char(void *data, object cont, object port) return Cyc_EOF; } +object Cyc_io_peek_u8(void *data, object cont, object port) +{ + FILE *stream; + port_type *p; + int c; + + Cyc_check_port(data, port); + { + p = (port_type *)port; + stream = ((port_type *) port)->fp; + if (stream == NULL) { + Cyc_rt_raise2(data, "Unable to read from closed port: ", port); + } + set_thread_blocked(data, cont); + if (p->mem_buf_len == 0 || p->mem_buf_len == p->buf_idx) { + _read_next_char(data, cont, p); + } + c = p->mem_buf[p->buf_idx]; + return_thread_runnable(data, (c != EOF) ? obj_char2obj(c) : Cyc_EOF); + } + return Cyc_EOF; +} + // TODO: full requirements are: // // Returns #t if a character is ready on the textual input @@ -6442,6 +6480,26 @@ object Cyc_io_read_char(void *data, object cont, object port) return Cyc_EOF; } +object Cyc_io_read_u8(void *data, object cont, object port) +{ + port_type *p = (port_type *)port; + Cyc_check_port(data, port); + if (p->fp == NULL) { + Cyc_rt_raise2(data, "Unable to read from closed port: ", port); + } + { + char_type codepoint; + int c; + set_thread_blocked(data, cont); + _read_next_char(data, cont, p); + c = p->mem_buf[p->buf_idx++]; + codepoint = (char_type) c; + p->col_num++; + return_thread_runnable(data, (c != EOF) ? obj_char2obj(codepoint) : Cyc_EOF); + } + return Cyc_EOF; +} + /* TODO: this function needs some work, but approximates what is needed */ object Cyc_io_read_line(void *data, object cont, object port) {