Use new buffers for in-memory I/O

New buffers are used so there is no conflict between the in-memory string/byte-vector buffer and the buffers used by `read`.
This commit is contained in:
Justin Ethier 2017-08-19 18:13:05 -04:00
parent 05fb1cdb7b
commit c7b395506b
2 changed files with 15 additions and 15 deletions

View file

@ -790,7 +790,7 @@ typedef struct {
size_t mem_buf_len;
unsigned short read_len;
char *str_bv_in_mem_buf;
char *str_bv_in_mem_buf_len;
size_t str_bv_in_mem_buf_len;
} port_type;
#define CYC_IO_BUF_LEN 1024

View file

@ -42,10 +42,10 @@ port_type *Cyc_io_open_input_string(void *data, object str)
p = (port_type *)Cyc_heap_alloc_port(data, &sp);
errno = 0;
#if CYC_HAVE_FMEMOPEN
p->mem_buf = malloc(sizeof(char) * (string_len(str) + 1));
p->mem_buf_len = string_len(str);
memcpy(p->mem_buf, string_str(str), string_len(str));
p->fp = fmemopen(p->mem_buf, string_len(str) + 1, "r");
p->str_bv_in_mem_buf = malloc(sizeof(char) * (string_len(str) + 1));
p->str_bv_in_mem_buf_len = string_len(str);
memcpy(p->str_bv_in_mem_buf, string_str(str), string_len(str));
p->fp = fmemopen(p->str_bv_in_mem_buf, string_len(str) + 1, "r");
#endif
if (p->fp == NULL){
Cyc_rt_raise2(data, "Unable to open input memory stream", obj_int2obj(errno));
@ -63,10 +63,10 @@ port_type *Cyc_io_open_input_bytevector(void *data, object 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");
p->str_bv_in_mem_buf = malloc(sizeof(char) * ((bytevector)bv)->len);
p->str_bv_in_mem_buf_len = ((bytevector)bv)->len;
memcpy(p->str_bv_in_mem_buf, ((bytevector)bv)->data, ((bytevector)bv)->len);
p->fp = fmemopen(p->str_bv_in_mem_buf, ((bytevector)bv)->len, "r");
#endif
if (p->fp == NULL){
Cyc_rt_raise2(data, "Unable to open input memory stream", obj_int2obj(errno));
@ -82,7 +82,7 @@ port_type *Cyc_io_open_output_string(void *data)
p = (port_type *)Cyc_heap_alloc_port(data, &sp);
errno = 0;
#if CYC_HAVE_OPEN_MEMSTREAM
p->fp = open_memstream(&(p->mem_buf), &(p->mem_buf_len));
p->fp = open_memstream(&(p->str_bv_in_mem_buf), &(p->str_bv_in_mem_buf_len));
#endif
if (p->fp == NULL){
Cyc_rt_raise2(data, "Unable to open output memory stream", obj_int2obj(errno));
@ -97,11 +97,11 @@ void Cyc_io_get_output_string(void *data, object cont, object port)
if (p->fp) {
fflush(p->fp);
}
if (p->mem_buf == NULL) {
if (p->str_bv_in_mem_buf == NULL) {
Cyc_rt_raise2(data, "Not an in-memory port", port);
}
{
make_string_with_len(s, p->mem_buf, p->mem_buf_len);
make_string_with_len(s, p->str_bv_in_mem_buf, p->str_bv_in_mem_buf_len);
return_closcall1(data, cont, &s);
}
}
@ -113,14 +113,14 @@ void Cyc_io_get_output_bytevector(void *data, object cont, object port)
if (p->fp) {
fflush(p->fp);
}
if (p->mem_buf == NULL) {
if (p->str_bv_in_mem_buf == NULL) {
Cyc_rt_raise2(data, "Not an in-memory port", port);
}
{
make_empty_bytevector(bv);
bv.len = p->mem_buf_len;
bv.len = p->str_bv_in_mem_buf_len;
bv.data = alloca(sizeof(char) * bv.len);
memcpy(bv.data, p->mem_buf, p->mem_buf_len);
memcpy(bv.data, p->str_bv_in_mem_buf, p->str_bv_in_mem_buf_len);
return_closcall1(data, cont, &bv);
}
}