Added bytevector ports

This commit is contained in:
Justin Ethier 2016-08-26 18:27:22 -04:00
parent bc6d3ac3d0
commit c564db263a
4 changed files with 55 additions and 3 deletions

View file

@ -14,6 +14,7 @@ Features:
- Added `parameterize` from section 4.2.6 of R7RS to the `(scheme base)` library.
- Added ` let-values` and ` let*-values` to `(scheme base)`.
- Added string ports to `(scheme base)` - `open-input-string`, `open-output-string`, and `get-output-string`.
- Added bytevector ports to `(scheme base)` - `get-output-bytevector`, `open-input-bytevector`, and `open-output-bytevector`.
- Modified the makefile to also search current working directories for headers and libraries.
Bug Fixes:

View file

@ -218,7 +218,9 @@ port_type Cyc_io_open_input_file(void *data, object str);
port_type Cyc_io_open_output_file(void *data, object str);
port_type *Cyc_io_open_output_string(void *data);
port_type *Cyc_io_open_input_string(void *data, object str);
port_type *Cyc_io_open_input_bytevector(void *data, object bv);
void Cyc_io_get_output_string(void *data, object cont, object port);
void Cyc_io_get_output_bytevector(void *data, object cont, object port);
object Cyc_io_close_port(void *data, object port);
object Cyc_io_close_input_port(void *data, object port);
object Cyc_io_close_output_port(void *data, object port);

View file

@ -53,6 +53,27 @@ port_type *Cyc_io_open_input_string(void *data, object str)
return p;
}
port_type *Cyc_io_open_input_bytevector(void *data, object bv)
{
// // Allocate port on the heap so the location of mem_buf does not change
port_type *p;
make_port(sp, NULL, 0);
Cyc_check_bvec(data, bv);
p = (port_type *)Cyc_heap_alloc_port(data, &sp);
errno = 0;
#if CYC_HAVE_FMEMOPEN
p->mem_buf = malloc(sizeof(char) * ((bytevector)bv)->len);
p->mem_buf_len = ((bytevector)bv)->len;
memcpy(p->mem_buf, ((bytevector)bv)->data, ((bytevector)bv)->len);
p->fp = fmemopen(p->mem_buf, ((bytevector)bv)->len, "r");
#endif
if (p->fp == NULL){
Cyc_rt_raise2(data, "Unable to open input memory stream", obj_int2obj(errno));
}
return p;
}
port_type *Cyc_io_open_output_string(void *data)
{
// Allocate port on the heap so the location of mem_buf does not change
@ -85,3 +106,22 @@ void Cyc_io_get_output_string(void *data, object cont, object port)
}
}
void Cyc_io_get_output_bytevector(void *data, object cont, object port)
{
port_type *p = (port_type *)port;
Cyc_check_port(data, port);
if (p->fp) {
fflush(p->fp);
}
if (p->mem_buf == NULL) {
Cyc_rt_raise2(data, "Not an in-memory port", port);
}
{
make_empty_bytevector(bv);
bv.len = p->mem_buf_len;
bv.data = alloca(sizeof(char) * bv.len);
memcpy(bv.data, p->mem_buf, p->mem_buf_len);
return_closcall1(data, cont, &bv);
}
}

View file

@ -141,6 +141,9 @@
get-output-string
open-output-string
open-input-string
get-output-bytevector
open-input-bytevector
open-output-bytevector
features
Cyc-version
any
@ -179,9 +182,6 @@
; Possibly missing functions:
;
; ; following byte vector functions are not implemented yet:
; get-output-bytevector
; open-input-bytevector
; open-output-bytevector
; read-bytevector
; read-bytevector!
; write-bytevector
@ -1169,6 +1169,15 @@
"(void *data, int argc, closure _, object k, object port)"
" Cyc_io_get_output_string(data, k, port);
")
(define-c get-output-bytevector
"(void *data, int argc, closure _, object k, object port)"
" Cyc_io_get_output_bytevector(data, k, port);
")
(define-c open-input-bytevector
"(void *data, int argc, closure _, object k, object bv)"
" port_type *p = Cyc_io_open_input_bytevector(data, bv);
return_closcall1(data, k, p); ")
(define open-output-bytevector open-output-string)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syntax-rules