From dc6c0559e962a0df40be2258bf920827dbe9e304 Mon Sep 17 00:00:00 2001 From: xyz Date: Sat, 22 Aug 2015 10:59:20 -0400 Subject: When reading files, check if the eof flag is set before throwing. --- lib/system.nim | 5 ++++- lib/system/sysio.nim | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 2a3f72f3b..ee94f85d2 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2594,7 +2594,10 @@ when not defined(JS): #and not defined(nimscript): ## Closes the file. proc endOfFile*(f: File): bool {.tags: [], benign.} - ## Returns true iff `f` is at the end. + ## Returns true if `f` is at the end. + + proc fileError*(f: File): bool {.tags: [], benign.} + ## Returns true if the error indicator of `f` is set. proc readChar*(f: File): char {. importc: "fgetc", header: "", tags: [ReadIOEffect].} diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 58ad8ace0..7918569e1 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -37,6 +37,8 @@ proc fread(buf: pointer, size, n: int, f: File): int {. proc fseek(f: File, offset: clong, whence: int): int {. importc: "fseek", header: "", tags: [].} proc ftell(f: File): int {.importc: "ftell", header: "", tags: [].} +proc ferror(f: File): int {.importc: "ferror", header: "", tags: [].} +proc feof(f: File): int {.importc: "feof", header: "", tags: [].} proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {. importc, header: "", tags: [].} proc memchr(s: pointer, c: cint, n: csize): pointer {. @@ -147,9 +149,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 = substr(result, 0 , bytes - 1) + elif fileError(file): raiseEIO("error while reading from file") + else: + # We red all the bytes but did not reach the EOF + # Try to read it as a buffer + result = readAllBuffer(file) proc readAllFile(file: File): string = var len = rawFileSize(file) @@ -187,12 +197,17 @@ proc endOfFile(f: File): bool = ungetc(c, f) return c < 0'i32 +proc fileError(f: File): bool = + result = (ferror(f) != 0) + 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): -- cgit 1.4.1-2-gfad0 From 385a883e1425e511bb593332c0e53f72ab9146a1 Mon Sep 17 00:00:00 2001 From: xyz Date: Sun, 23 Aug 2015 18:29:38 -0400 Subject: Use seLen as sugested, and fix typos --- lib/system.nim | 4 ++-- lib/system/sysio.nim | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index ee94f85d2..80aedaa8c 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2594,10 +2594,10 @@ when not defined(JS): #and not defined(nimscript): ## Closes the file. proc endOfFile*(f: File): bool {.tags: [], benign.} - ## Returns true if `f` is at the end. + ## Returns true iff `f` is at the end. proc fileError*(f: File): bool {.tags: [], benign.} - ## Returns true if the error indicator of `f` is set. + ## Returns true iff the error indicator of `f` is set. proc readChar*(f: File): char {. importc: "fgetc", header: "", tags: [ReadIOEffect].} diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 7918569e1..edf3d347a 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -38,7 +38,6 @@ proc fseek(f: File, offset: clong, whence: int): int {. importc: "fseek", header: "", tags: [].} proc ftell(f: File): int {.importc: "ftell", header: "", tags: [].} proc ferror(f: File): int {.importc: "ferror", header: "", tags: [].} -proc feof(f: File): int {.importc: "feof", header: "", tags: [].} proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {. importc, header: "", tags: [].} proc memchr(s: pointer, c: cint, n: csize): pointer {. @@ -153,11 +152,11 @@ proc readAllFile(file: File, len: int): string = let bytes = readBuffer(file, addr(result[0]), len) if endOfFile(file): if bytes < len: - result = substr(result, 0 , bytes - 1) + result.setLen(bytes) elif fileError(file): raiseEIO("error while reading from file") else: - # We red all the bytes but did not reach the EOF + # We read all the bytes but did not reach the EOF # Try to read it as a buffer result = readAllBuffer(file) -- cgit 1.4.1-2-gfad0 From 88247e6857beb73666b7bc763bd897b26806dd9f Mon Sep 17 00:00:00 2001 From: xyz Date: Sun, 23 Aug 2015 18:54:15 -0400 Subject: Append the readAllBuffer to what was read already --- lib/system/sysio.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index edf3d347a..6db92ddf9 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -158,7 +158,7 @@ proc readAllFile(file: File, len: int): string = else: # We read all the bytes but did not reach the EOF # Try to read it as a buffer - result = readAllBuffer(file) + result.add(readAllBuffer(file)) proc readAllFile(file: File): string = var len = rawFileSize(file) -- cgit 1.4.1-2-gfad0 From 1def8ec9f9e3042dd765d29f21954b504b0a8c46 Mon Sep 17 00:00:00 2001 From: xyz Date: Tue, 25 Aug 2015 06:48:58 -0400 Subject: remove fileError --- lib/system.nim | 3 --- lib/system/sysio.nim | 5 +---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 80aedaa8c..2a3f72f3b 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2596,9 +2596,6 @@ when not defined(JS): #and not defined(nimscript): proc endOfFile*(f: File): bool {.tags: [], benign.} ## Returns true iff `f` is at the end. - proc fileError*(f: File): bool {.tags: [], benign.} - ## Returns true iff the error indicator of `f` is set. - proc readChar*(f: File): char {. importc: "fgetc", header: "", tags: [ReadIOEffect].} ## Reads a single character from the stream `f`. diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 6db92ddf9..8ad48d368 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -153,7 +153,7 @@ proc readAllFile(file: File, len: int): string = if endOfFile(file): if bytes < len: result.setLen(bytes) - elif fileError(file): + elif ferror(file) != 0: raiseEIO("error while reading from file") else: # We read all the bytes but did not reach the EOF @@ -196,9 +196,6 @@ proc endOfFile(f: File): bool = ungetc(c, f) return c < 0'i32 -proc fileError(f: File): bool = - result = (ferror(f) != 0) - proc writeLn[Ty](f: File, x: varargs[Ty, `$`]) = for i in items(x): write(f, i) -- cgit 1.4.1-2-gfad0