Use seLen as sugested, and fix typos

This commit is contained in:
xyz 2015-08-23 18:29:38 -04:00
commit 385a883e14
2 changed files with 4 additions and 5 deletions

View file

@ -2594,10 +2594,10 @@ when not defined(JS): #and not defined(nimscript):
## Closes the file. ## Closes the file.
proc endOfFile*(f: File): bool {.tags: [], benign.} 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.} 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 {. proc readChar*(f: File): char {.
importc: "fgetc", header: "<stdio.h>", tags: [ReadIOEffect].} importc: "fgetc", header: "<stdio.h>", tags: [ReadIOEffect].}

View file

@ -38,7 +38,6 @@ proc fseek(f: File, offset: clong, whence: int): int {.
importc: "fseek", header: "<stdio.h>", tags: [].} importc: "fseek", header: "<stdio.h>", tags: [].}
proc ftell(f: File): int {.importc: "ftell", 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 ferror(f: File): int {.importc: "ferror", header: "<stdio.h>", tags: [].}
proc feof(f: File): int {.importc: "feof", header: "<stdio.h>", tags: [].}
proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {. proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {.
importc, header: "<stdio.h>", tags: [].} importc, header: "<stdio.h>", tags: [].}
proc memchr(s: pointer, c: cint, n: csize): pointer {. 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) let bytes = readBuffer(file, addr(result[0]), len)
if endOfFile(file): if endOfFile(file):
if bytes < len: if bytes < len:
result = substr(result, 0 , bytes - 1) result.setLen(bytes)
elif fileError(file): elif fileError(file):
raiseEIO("error while reading from file") raiseEIO("error while reading from file")
else: 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 # Try to read it as a buffer
result = readAllBuffer(file) result = readAllBuffer(file)