Added overload for fileInput

- Make it symetrical to `fileOutput`
This commit is contained in:
Ivan Yonchovski 2022-01-18 16:03:34 +02:00 • committed by zah
commit eb2ad0c441

View file

@ -399,6 +399,30 @@ let fileInputVTable = InputStreamVTable(
raise newException(IOError, "Failed to close file", err) raise newException(IOError, "Failed to close file", err)
) )
proc fileInput*(file: File,
offset = 0,
pageSize = defaultPageSize): InputStreamHandle
{.raises: [IOError, OSError].} =
## Creates an input stream for reading the contents of a file
## through Nim's `io` module.
##
## Parameters:
##
## ``file``
## The file to read.
##
## ``offset``
## Initial position in the file where reading should start.
##
if offset != 0:
setFilePos(file, offset)
makeHandle FileInputStream(
vtable: vtableAddr fileInputVTable,
buffers: initPageBuffers(pageSize),
file: file)
proc fileInput*(filename: string, proc fileInput*(filename: string,
offset = 0, offset = 0,
pageSize = defaultPageSize): InputStreamHandle pageSize = defaultPageSize): InputStreamHandle
@ -415,14 +439,7 @@ proc fileInput*(filename: string,
## Initial position in the file where reading should start. ## Initial position in the file where reading should start.
## ##
let file = system.open(filename, fmRead) let file = system.open(filename, fmRead)
return fileInput(file, offset, pageSize)
if offset != 0:
setFilePos(file, offset)
makeHandle FileInputStream(
vtable: vtableAddr fileInputVTable,
buffers: initPageBuffers(pageSize),
file: file)
proc unsafeMemoryInput*(mem: openArray[byte]): InputStreamHandle = proc unsafeMemoryInput*(mem: openArray[byte]): InputStreamHandle =
let head = unsafeAddr mem[0] let head = unsafeAddr mem[0]
@ -948,4 +965,3 @@ proc pos*(s: InputStream): int {.inline.} =
when fsAsyncSupport: when fsAsyncSupport:
template pos*(s: AsyncInputStream): int = template pos*(s: AsyncInputStream): int =
pos InputStream(s) pos InputStream(s)