klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit d2b2f4a88a4e25ff0669b9d1d47f41eaa3987053
parent dfc183e18788e04d2e9b0a34988badb4e8d087f2
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Fri, 18 Nov 2011 17:09:19 -0300

Added bytevector & string port support to flush-output-port, newline, write-u8 and write-char.

Diffstat:
Msrc/kwrite.c | 39+++++++++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/src/kwrite.c b/src/kwrite.c @@ -613,6 +613,7 @@ void kwrite_char_to_port(klisp_State *K, TValue port, TValue ch) { K->curr_port = port; /* this isn't needed but all other i/o functions set it */ + if (ttisfport(port)) { FILE *file = kfport_file(port); int res = fputc(chvalue(ch), file); @@ -621,8 +622,24 @@ void kwrite_char_to_port(klisp_State *K, TValue port, TValue ch) clearerr(file); /* clear error for next time */ kwrite_error(K, "error writing char"); } + } else if (ttismport(port)) { + if (kport_is_binary(port)) { + /* bytebuffer port */ + if (kmport_off(port) >= kbytevector_size(kmport_buf(port))) { + kmport_resize_buffer(K, port, kmport_off(port) + 1); + } + kbytevector_buf(kmport_buf(port))[kmport_off(port)] = chvalue(ch); + ++kmport_off(port); + } else { + /* string port */ + if (kmport_off(port) >= kstring_size(kmport_buf(port))) { + kmport_resize_buffer(K, port, kmport_off(port) + 1); + } + kstring_buf(kmport_buf(port))[kmport_off(port)] = chvalue(ch); + ++kmport_off(port); + } } else { - kwrite_error(K, "mem ports not yet supported"); + kwrite_error(K, "unknown port type"); return; } } @@ -639,8 +656,26 @@ void kwrite_u8_to_port(klisp_State *K, TValue port, TValue u8) clearerr(file); /* clear error for next time */ kwrite_error(K, "error writing u8"); } + } else if (ttismport(port)) { + if (kport_is_binary(port)) { + /* bytebuffer port */ + if (kmport_off(port) >= kbytevector_size(kmport_buf(port))) { + kmport_resize_buffer(K, port, kmport_off(port) + 1); + } + kbytevector_buf(kmport_buf(port))[kmport_off(port)] = + (uint8_t) ivalue(u8); + ++kmport_off(port); + } else { + /* string port */ + if (kmport_off(port) >= kstring_size(kmport_buf(port))) { + kmport_resize_buffer(K, port, kmport_off(port) + 1); + } + kstring_buf(kmport_buf(port))[kmport_off(port)] = + (char) ivalue(u8); + ++kmport_off(port); + } } else { - kwrite_error(K, "mem ports not yet supported"); + kwrite_error(K, "unknown port type"); return; } }