diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2015-08-25 22:22:32 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2015-08-25 22:22:32 +0200 |
commit | f091ce258e36e1346f9dfc2b31bbba334f64aec2 (patch) | |
tree | 819d5ea4d1e8e38a0d440e972815fbded6f349f7 | |
parent | 19f58704ee33a85b6c42c38bf5c761f2ecf66af6 (diff) | |
parent | 1def8ec9f9e3042dd765d29f21954b504b0a8c46 (diff) | |
download | Nim-f091ce258e36e1346f9dfc2b31bbba334f64aec2.tar.gz |
Merge pull request #3239 from xyz32/devel
When reading files, check if the eof flag is set before throwing.
-rw-r--r-- | lib/system/sysio.nim | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index f51354da8..265f92fa2 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -37,6 +37,7 @@ proc fread(buf: pointer, size, n: int, f: File): int {. proc fseek(f: File, offset: clong, whence: int): int {. importc: "fseek", header: "<stdio.h>", tags: [].} proc ftell(f: File): int {.importc: "ftell", header: "<stdio.h>", tags: [].} +proc ferror(f: File): int {.importc: "ferror", header: "<stdio.h>", tags: [].} proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {. importc, header: "<stdio.h>", tags: [].} proc memchr(s: pointer, c: cint, n: csize): pointer {. @@ -150,9 +151,17 @@ proc rawFileSize(file: File): int = proc readAllFile(file: File, len: int): string = # We acquire the filesize beforehand and hope it doesn't change. # Speeds things up. - result = newString(int(len)) - if readBuffer(file, addr(result[0]), int(len)) != len: + result = newString(len) + let bytes = readBuffer(file, addr(result[0]), len) + if endOfFile(file): + if bytes < len: + result.setLen(bytes) + elif ferror(file) != 0: raiseEIO("error while reading from file") + else: + # We read all the bytes but did not reach the EOF + # Try to read it as a buffer + result.add(readAllBuffer(file)) proc readAllFile(file: File): string = var len = rawFileSize(file) @@ -191,11 +200,13 @@ proc endOfFile(f: File): bool = return c < 0'i32 proc writeLn[Ty](f: File, x: varargs[Ty, `$`]) = - for i in items(x): write(f, i) + for i in items(x): + write(f, i) write(f, "\n") proc writeLine[Ty](f: File, x: varargs[Ty, `$`]) = - for i in items(x): write(f, i) + for i in items(x): + write(f, i) write(f, "\n") when declared(stdout): |