fixes for readAll

This commit is contained in:
Simon Hafner 2011-12-30 14:15:12 +01:00
commit 24917aaf1e
4 changed files with 38 additions and 22 deletions

View file

@ -1677,10 +1677,10 @@ when not defined(EcmaScript) and not defined(NimrodVM):
proc readAll*(file: TFile): TaintedString proc readAll*(file: TFile): TaintedString
## Reads all data from the stream `file`. Raises an IO exception ## Reads all data from the stream `file`. Raises an IO exception
## in case of an Error ## in case of an error
proc readFile*(filename: string): TaintedString proc readFile*(filename: string): TaintedString
## Opens a file named `filename` for reading. Then calls readAll ## Opens a file named `filename` for reading. Then calls `readAll`
## and closes the file afterwards. Returns the string. ## and 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.

View file

@ -44,7 +44,7 @@ var
IONBF {.importc: "_IONBF", nodecl.}: cint IONBF {.importc: "_IONBF", nodecl.}: cint
const const
BUF_SIZE = 4000 buf_size = 4000
proc raiseEIO(msg: string) {.noinline, noreturn.} = proc raiseEIO(msg: string) {.noinline, noreturn.} =
raise newException(EIO, msg) raise newException(EIO, msg)
@ -96,40 +96,45 @@ proc readAllBuffer(file: TFile): string =
# This proc is for TFile we want to read but don't know how many # 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. # bytes we need to read before the buffer is empty.
result = "" result = ""
var buffer = newString(BUF_SIZE) var buffer = newString(buf_size)
var bytesRead = BUF_SIZE var bytesRead = buf_size
while bytesRead == BUF_SIZE: while bytesRead == buf_size:
bytesRead = readBuffer(file, addr(buffer[0]), BUF_SIZE) bytesRead = readBuffer(file, addr(buffer[0]), buf_size)
result.add(buffer) result.add(buffer)
proc readAllFile(file: TFile): string = proc rawFileSize(file: TFile): int =
# this does not raise an error opposed to `getFileSize`
var oldPos = ftell(file)
discard fseek(file, 0, 2) # seek the end of the file
result = ftell(file)
discard fseek(file, clong(oldPos), 0)
proc readAllFile(file: TFile, len: int): string =
# We aquire the filesize beforehand and hope it doesn't change. # We aquire the filesize beforehand and hope it doesn't change.
# Speeds things up. # Speeds things up.
var len = getFileSize(file)
if len >= high(int): if len >= high(int):
raiseEIO("file too big to fit in memory") raiseEIO("file too big to fit in memory")
result = newString(int(len)) result = newString(int(len))
if readBuffer(file, addr(result[0]), int(len)) != len: if readBuffer(file, addr(result[0]), int(len)) != len:
raiseEIO("error while reading from file") raiseEIO("error while reading from file")
proc hasDefinedLength(file: TFile): bool = proc readAllFile(file: TFile): string =
var oldPos = getFilePos(file) var len = rawFileSize(file)
discard fseek(file, 0, 2) # seek the end of the file result = readAllFile(file, len)
result = ftell(file) >= 0
setFilePos(file, oldPos)
proc readAll(file: TFile): TaintedString = proc readAll(file: TFile): TaintedString =
# Separate handling needed because we need to buffer when we # Separate handling needed because we need to buffer when we
# don't know the overall length of the TFile. # don't know the overall length of the TFile.
if hasDefinedLength(file): var len = rawFileSize(file)
result = readAllBuffer(file).TaintedSTring if len >= 0:
result = readAllFile(file, len).TaintedSTring
else: else:
result = readAllFile(file).TaintedString result = readAllBuffer(file).TaintedString
proc readFile(filename: string): TaintedString = proc readFile(filename: string): TaintedString =
var f = open(filename) var f = open(filename)
try: try:
result = readAllFile(f) result = readAllFile(f).TaintedString
finally: finally:
close(f) close(f)

View file

@ -128,12 +128,19 @@ proc rejectThreadTests(r: var TResults, options: string) =
rejectSingleTest(r, "tests/threads/tthreadanalysis3", options) rejectSingleTest(r, "tests/threads/tthreadanalysis3", options)
rejectSingleTest(r, "tests/threads/tthreadheapviolation1", options) rejectSingleTest(r, "tests/threads/tthreadheapviolation1", options)
# ------------------------- IO tests -----------------------------------
proc runIOTests(r: var TResults, options: string) =
discard callCompiler(r"nimrod cc --hints:on $# $#", "tests/system/helpers/readall_echo", options)
runSingleTest(r, "tests/system/io", options)
# ------------------------- register special tests here ----------------------- # ------------------------- register special tests here -----------------------
proc runSpecialTests(r: var TResults, options: string) = proc runSpecialTests(r: var TResults, options: string) =
runRodFiles(r, options) runRodFiles(r, options)
runDLLTests(r, options) runDLLTests(r, options)
runGCTests(r, options) runGCTests(r, options)
runThreadTests(r, options & " --threads:on") runThreadTests(r, options & " --threads:on")
runIOTests(r, options)
proc rejectSpecialTests(r: var TResults, options: string) = proc rejectSpecialTests(r: var TResults, options: string) =
rejectThreadTests(r, options) rejectThreadTests(r, options)

View file

@ -1,11 +1,15 @@
discard """output: '''[OK] stdin
[OK] file'''"""
import import
unittest, osproc, streams, os unittest, osproc, streams, os, readall_echo
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 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" const TEST_FILE = "tests/testdata/string"
proc echoLoop(str: string): string = proc echoLoop(str: string): string =
result = "" result = ""
var process = startProcess("tests/system/helpers/readall_echo") var process = startProcess(os.addFileExt("tests/system/helpers/readall_echo", ExeExt))
var input = process.inputStream var input = process.inputStream
input.write(str) input.write(str)
input.close() input.close()