diff --git a/main/gbx/gbx_stream.c b/main/gbx/gbx_stream.c index 5cfdb0cdd..250fb346b 100644 --- a/main/gbx/gbx_stream.c +++ b/main/gbx/gbx_stream.c @@ -1243,8 +1243,17 @@ void STREAM_write_type(STREAM *stream, TYPE type, VALUE *value) case T_STRING: case T_CSTRING: - write_length(stream, value->_string.len); - STREAM_write(stream, value->_string.addr + value->_string.start, value->_string.len); + if (stream->type == &STREAM_memory) + { + STREAM_write(stream, value->_string.addr + value->_string.start, value->_string.len); + buffer._byte = 0; + STREAM_write(stream, &buffer._byte, 1); + } + else + { + write_length(stream, value->_string.len); + STREAM_write(stream, value->_string.addr + value->_string.start, value->_string.len); + } break; case T_OBJECT: diff --git a/main/gbx/gbx_stream_string.c b/main/gbx/gbx_stream_string.c index 2d81dd8ad..b153a1625 100644 --- a/main/gbx/gbx_stream_string.c +++ b/main/gbx/gbx_stream_string.c @@ -46,16 +46,23 @@ static int stream_open(STREAM *stream, const char *path, int mode) { stream->string.buffer = NULL; stream->common.available_now = TRUE; + stream->string.size = 0; + stream->string.pos = 0; if (path && *path) { - STRING_ref((char *)path); - stream->string.buffer = (char *)path; + if (mode & ST_WRITE) + { + stream->string.buffer = STRING_new(path, STRING_length(path)); + } + else + { + stream->string.buffer = (char *)path; + STRING_ref((char *)path); + } stream->string.size = STRING_length(path); } - stream->string.pos = 0; - return FALSE; } @@ -94,9 +101,14 @@ static int stream_read(STREAM *stream, char *buffer, int len) static int stream_write(STREAM *stream, char *buffer, int len) { + if ((stream->common.mode & ST_WRITE) == 0) + THROW(E_ACCESS); + stream->string.buffer = STRING_add(stream->string.buffer, buffer, len); - stream->string.pos = stream->string.size + len; + stream->string.size += len; + stream->string.pos = stream->string.size; + return FALSE; } diff --git a/main/gbx/gbx_subr_file.c b/main/gbx/gbx_subr_file.c index 6419291f4..d716a1435 100755 --- a/main/gbx/gbx_subr_file.c +++ b/main/gbx/gbx_subr_file.c @@ -213,12 +213,19 @@ void SUBR_close(void) if (stream->type == &STREAM_string) { + char *buffer = stream->string.buffer; + RETURN->type = T_STRING; - RETURN->_string.addr = stream->string.buffer; + RETURN->_string.addr = buffer; RETURN->_string.start = 0; RETURN->_string.len = stream->string.size; + STRING_ref(buffer); STREAM_close(stream); + STRING_free_later(buffer); + + //fprintf(stderr, "buffer ref = %d\n", STRING_from_ptr(buffer)->ref); + SUBR_LEAVE(); } else