diff --git a/faststreams/inputs.nim b/faststreams/inputs.nim index a36edb0..3c0092c 100644 --- a/faststreams/inputs.nim +++ b/faststreams/inputs.nim @@ -399,6 +399,30 @@ let fileInputVTable = InputStreamVTable( 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, offset = 0, pageSize = defaultPageSize): InputStreamHandle @@ -415,14 +439,7 @@ proc fileInput*(filename: string, ## Initial position in the file where reading should start. ## let file = system.open(filename, fmRead) - - if offset != 0: - setFilePos(file, offset) - - makeHandle FileInputStream( - vtable: vtableAddr fileInputVTable, - buffers: initPageBuffers(pageSize), - file: file) + return fileInput(file, offset, pageSize) proc unsafeMemoryInput*(mem: openArray[byte]): InputStreamHandle = let head = unsafeAddr mem[0] @@ -948,4 +965,3 @@ proc pos*(s: InputStream): int {.inline.} = when fsAsyncSupport: template pos*(s: AsyncInputStream): int = pos InputStream(s) -