From 1370963723917eed6c93e28c9970a2b27be57ea4 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 4 Jun 2023 20:26:14 +0200 Subject: libio: remove buffer_offset from istream_t Instead, make the buffer const, let the user adjust the pointer and size. The offset can then be inferred in precache. Signed-off-by: David Oberhollenzer --- lib/io/src/win32/istream.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'lib/io/src/win32') diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c index 918b868..94c8584 100644 --- a/lib/io/src/win32/istream.c +++ b/lib/io/src/win32/istream.c @@ -23,27 +23,25 @@ static int file_precache(istream_t *strm) DWORD diff, actual; HANDLE hnd; - if (strm->buffer_offset >= strm->buffer_used) { - strm->buffer_offset = 0; - strm->buffer_used = 0; - } else if (strm->buffer_offset > 0) { - memmove(strm->buffer, - strm->buffer + strm->buffer_offset, - strm->buffer_used - strm->buffer_offset); - - strm->buffer_used -= strm->buffer_offset; - strm->buffer_offset = 0; - } - if (strm->eof) return 0; + assert(strm->buffer >= file->buffer); + assert(strm->buffer <= (file->buffer + BUFSZ)); + assert(strm->buffer_used <= BUFSZ); + assert((size_t)(strm->buffer - file->buffer) <= + (BUFSZ - strm->buffer_used)); + + if (strm->buffer_used > 0) + memmove(file->buffer, strm->buffer, strm->buffer_used); + + strm->buffer = file->buffer; hnd = file->path == NULL ? GetStdHandle(STD_INPUT_HANDLE) : file->hnd; while (strm->buffer_used < sizeof(file->buffer)) { diff = sizeof(file->buffer) - strm->buffer_used; - if (!ReadFile(hnd, strm->buffer + strm->buffer_used, + if (!ReadFile(hnd, file->buffer + strm->buffer_used, diff, &actual, NULL)) { DWORD error = GetLastError(); -- cgit v1.2.3