implemented readAll()
This commit is contained in:
parent
4f08946f64
commit
5ede29cd05
4 changed files with 77 additions and 22 deletions
|
|
@ -1675,10 +1675,13 @@ when not defined(EcmaScript) and not defined(NimrodVM):
|
||||||
proc FlushFile*(f: TFile) {.importc: "fflush", noDecl.}
|
proc FlushFile*(f: TFile) {.importc: "fflush", noDecl.}
|
||||||
## Flushes `f`'s buffer.
|
## Flushes `f`'s buffer.
|
||||||
|
|
||||||
|
proc readAll*(file: TFile): TaintedString
|
||||||
|
## Reads all data from the stream `file`. Raises an IO exception
|
||||||
|
## in case of an Error
|
||||||
|
|
||||||
proc readFile*(filename: string): TaintedString
|
proc readFile*(filename: string): TaintedString
|
||||||
## Opens a file named `filename` for reading. Then reads the
|
## Opens a file named `filename` for reading. Then calls readAll
|
||||||
## file's content completely into a string and
|
## and closes the file afterwards. Returns the string.
|
||||||
## closes the file afterwards. Returns the string.
|
|
||||||
## Raises an IO exception in case of an error.
|
## Raises an IO exception in case of an error.
|
||||||
|
|
||||||
proc writeFile*(filename, content: string)
|
proc writeFile*(filename, content: string)
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,13 @@ proc putc(c: Char, stream: TFile) {.importc: "putc", nodecl.}
|
||||||
proc fprintf(f: TFile, frmt: CString) {.importc: "fprintf", nodecl, varargs.}
|
proc fprintf(f: TFile, frmt: CString) {.importc: "fprintf", nodecl, varargs.}
|
||||||
proc strlen(c: cstring): int {.importc: "strlen", nodecl.}
|
proc strlen(c: cstring): int {.importc: "strlen", nodecl.}
|
||||||
|
|
||||||
|
|
||||||
|
# C routine that is used here:
|
||||||
|
proc fread(buf: Pointer, size, n: int, f: TFile): int {.
|
||||||
|
importc: "fread", noDecl.}
|
||||||
|
proc fseek(f: TFile, offset: clong, whence: int): int {.
|
||||||
|
importc: "fseek", noDecl.}
|
||||||
|
proc ftell(f: TFile): int {.importc: "ftell", noDecl.}
|
||||||
proc setvbuf(stream: TFile, buf: pointer, typ, size: cint): cint {.
|
proc setvbuf(stream: TFile, buf: pointer, typ, size: cint): cint {.
|
||||||
importc, nodecl.}
|
importc, nodecl.}
|
||||||
|
|
||||||
|
|
@ -36,6 +43,9 @@ var
|
||||||
IOFBF {.importc: "_IOFBF", nodecl.}: cint
|
IOFBF {.importc: "_IOFBF", nodecl.}: cint
|
||||||
IONBF {.importc: "_IONBF", nodecl.}: cint
|
IONBF {.importc: "_IONBF", nodecl.}: cint
|
||||||
|
|
||||||
|
const
|
||||||
|
BUF_SIZE = 4000
|
||||||
|
|
||||||
proc raiseEIO(msg: string) {.noinline, noreturn.} =
|
proc raiseEIO(msg: string) {.noinline, noreturn.} =
|
||||||
raise newException(EIO, msg)
|
raise newException(EIO, msg)
|
||||||
|
|
||||||
|
|
@ -82,21 +92,44 @@ proc write(f: TFile, c: Char) = putc(c, f)
|
||||||
proc write(f: TFile, a: openArray[string]) =
|
proc write(f: TFile, a: openArray[string]) =
|
||||||
for x in items(a): write(f, x)
|
for x in items(a): write(f, x)
|
||||||
|
|
||||||
|
proc readAllBuffer(file: TFile): string =
|
||||||
|
# This proc is for TFile we want to read but don't know how many
|
||||||
|
# bytes we need to read before the buffer is empty.
|
||||||
|
result = ""
|
||||||
|
var buffer = newString(BUF_SIZE)
|
||||||
|
var bytesRead = BUF_SIZE
|
||||||
|
while bytesRead == BUF_SIZE:
|
||||||
|
bytesRead = readBuffer(file, addr(buffer[0]), BUF_SIZE)
|
||||||
|
result.add(buffer)
|
||||||
|
|
||||||
|
proc readAllFile(file: TFile): string =
|
||||||
|
# We aquire the filesize beforehand and hope it doesn't change.
|
||||||
|
# Speeds things up.
|
||||||
|
var len = getFileSize(file)
|
||||||
|
if len >= high(int):
|
||||||
|
raiseEIO("file too big to fit in memory")
|
||||||
|
result = newString(int(len))
|
||||||
|
if readBuffer(file, addr(result[0]), int(len)) != len:
|
||||||
|
raiseEIO("error while reading from file")
|
||||||
|
|
||||||
|
proc hasDefinedLength(file: TFile): bool =
|
||||||
|
var oldPos = getFilePos(file)
|
||||||
|
discard fseek(file, 0, 2) # seek the end of the file
|
||||||
|
result = ftell(file) >= 0
|
||||||
|
setFilePos(file, oldPos)
|
||||||
|
|
||||||
|
proc readAll(file: TFile): TaintedString =
|
||||||
|
# Separate handling needed because we need to buffer when we
|
||||||
|
# don't know the overall length of the TFile.
|
||||||
|
if hasDefinedLength(file):
|
||||||
|
result = readAllBuffer(file).TaintedSTring
|
||||||
|
else:
|
||||||
|
result = readAllFile(file).TaintedString
|
||||||
|
|
||||||
proc readFile(filename: string): TaintedString =
|
proc readFile(filename: string): TaintedString =
|
||||||
var f = open(filename)
|
var f = open(filename)
|
||||||
try:
|
try:
|
||||||
var len = getFileSize(f)
|
result = readAllFile(f)
|
||||||
if len < high(int):
|
|
||||||
when taintMode:
|
|
||||||
result = newString(int(len)).TaintedString
|
|
||||||
if readBuffer(f, addr(string(result)[0]), int(len)) != len:
|
|
||||||
raiseEIO("error while reading from file")
|
|
||||||
else:
|
|
||||||
result = newString(int(len))
|
|
||||||
if readBuffer(f, addr(result[0]), int(len)) != len:
|
|
||||||
raiseEIO("error while reading from file")
|
|
||||||
else:
|
|
||||||
raiseEIO("file too big to fit in memory")
|
|
||||||
finally:
|
finally:
|
||||||
close(f)
|
close(f)
|
||||||
|
|
||||||
|
|
@ -157,13 +190,6 @@ proc open(f: var TFile, filehandle: TFileHandle, mode: TFileMode): bool =
|
||||||
f = fdopen(filehandle, FormatOpen[mode])
|
f = fdopen(filehandle, FormatOpen[mode])
|
||||||
result = f != nil
|
result = f != nil
|
||||||
|
|
||||||
# C routine that is used here:
|
|
||||||
proc fread(buf: Pointer, size, n: int, f: TFile): int {.
|
|
||||||
importc: "fread", noDecl.}
|
|
||||||
proc fseek(f: TFile, offset: clong, whence: int): int {.
|
|
||||||
importc: "fseek", noDecl.}
|
|
||||||
proc ftell(f: TFile): int {.importc: "ftell", noDecl.}
|
|
||||||
|
|
||||||
proc fwrite(buf: Pointer, size, n: int, f: TFile): int {.
|
proc fwrite(buf: Pointer, size, n: int, f: TFile): int {.
|
||||||
importc: "fwrite", noDecl.}
|
importc: "fwrite", noDecl.}
|
||||||
|
|
||||||
|
|
|
||||||
1
tests/system/helpers/readall_echo.nim
Normal file
1
tests/system/helpers/readall_echo.nim
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
echo(stdin.readAll)
|
||||||
25
tests/system/io.nim
Normal file
25
tests/system/io.nim
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import
|
||||||
|
unittest, osproc, streams, os
|
||||||
|
const STRING_DATA = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
|
||||||
|
const TEST_FILE = "tests/testdata/string"
|
||||||
|
|
||||||
|
proc echoLoop(str: string): string =
|
||||||
|
result = ""
|
||||||
|
var process = startProcess("tests/system/helpers/readall_echo")
|
||||||
|
var input = process.inputStream
|
||||||
|
input.write(str)
|
||||||
|
input.close()
|
||||||
|
var output = process.outputStream
|
||||||
|
discard process.waitForExit
|
||||||
|
while not output.atEnd:
|
||||||
|
result.add(output.readLine)
|
||||||
|
|
||||||
|
suite "io":
|
||||||
|
suite "readAll":
|
||||||
|
test "stdin":
|
||||||
|
check:
|
||||||
|
echoLoop(STRING_DATA) == STRING_DATA
|
||||||
|
echoLoop(STRING_DATA[0..3999]) == STRING_DATA[0..3999]
|
||||||
|
test "file":
|
||||||
|
check:
|
||||||
|
readFile(TEST_FILE) == STRING_DATA
|
||||||
Loading…
Add table
Add a link
Reference in a new issue