From b6c8edfb73aa608e337a7d3e61848147b9d75244 Mon Sep 17 00:00:00 2001 From: jjgarcia Date: Tue, 21 Dec 2004 14:58:18 +0000 Subject: [PATCH] Merging the extra bits of the byte buffer is tricky when the file was opened only for output. --- src/c/file.d | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/c/file.d b/src/c/file.d index 959393d62..8b59be359 100644 --- a/src/c/file.d +++ b/src/c/file.d @@ -782,6 +782,23 @@ flush_output_stream_binary(cl_object strm) if (c != EOF) b |= (unsigned char)(c & ~MAKE_BIT_MASK(nb)); fseek(strm->stream.file, -1, SEEK_CUR); + } else { + /* Tricky part -> check whether we are at the EOF */ + long current_offset = ftell(strm->stream.file); + fseek(strm->stream.file, 0, SEEK_END); + if (ftell(strm->stream.file) - current_offset > 0) { + /* No, we weren't at the EOF, try merging. * + * As the file was opened only for writing, * + * this mean re-opening the file... */ + cl_object fn = si_coerce_to_filename(strm->stream.object1); + fseek(freopen(fn->string.self, "rb", strm->stream.file), + current_offset, + SEEK_SET); + b |= (unsigned char)(ecl_read_byte8(strm) & ~MAKE_BIT_MASK(nb)); + fseek(freopen(fn->string.self, "wb", strm->stream.file), + current_offset, + SEEK_SET); + } } ecl_write_byte8(b, strm); fseek(strm->stream.file, -1, SEEK_CUR); /* I/O synchronization */