This module provides a stream interface and two implementations thereof: the FileStream and the StringStream which implement the stream interface for Nim file objects (File) and strings. Other modules may provide other implementations for this standard stream interface.
Examples:
import streams var ss = newStringStream("""The first line the second line the third line""") line = "" while ss.readLine(line): echo line ss.close() var fs = newFileStream("somefile.txt", fmRead) if not isNil(fs): while fs.readLine(line): echo line fs.close()
Stream = ref StreamObj
StreamObj = object of RootObj closeImpl*: proc (s: Stream) {...}{.nimcall, tags: [], gcsafe.} atEndImpl*: proc (s: Stream): bool {...}{.nimcall, tags: [], gcsafe.} setPositionImpl*: proc (s: Stream; pos: int) {...}{.nimcall, tags: [], gcsafe.} getPositionImpl*: proc (s: Stream): int {...}{.nimcall, tags: [], gcsafe.} readDataImpl*: proc (s: Stream; buffer: pointer; bufLen: int): int {...}{.nimcall, tags: [ReadIOEffect], gcsafe.} peekDataImpl*: proc (s: Stream; buffer: pointer; bufLen: int): int {...}{.nimcall, tags: [ReadIOEffect], gcsafe.} writeDataImpl*: proc (s: Stream; buffer: pointer; bufLen: int) {...}{.nimcall, tags: [WriteIOEffect], gcsafe.} flushImpl*: proc (s: Stream) {...}{.nimcall, tags: [WriteIOEffect], gcsafe.}
StringStream = ref StringStreamObj
StringStreamObj = object of StreamObj data*: string pos: int
FileStream = ref FileStreamObj
FileStreamObj = object of Stream f: File
proc flush(s: Stream) {...}{.raises: [Exception], tags: [WriteIOEffect].}
proc close(s: Stream) {...}{.raises: [Exception], tags: [].}
proc close(s, unused: Stream) {...}{.deprecated, raises: [Exception], tags: [].}
proc atEnd(s: Stream): bool {...}{.raises: [Exception], tags: [].}
proc setPosition(s: Stream; pos: int) {...}{.raises: [Exception], tags: [].}
proc getPosition(s: Stream): int {...}{.raises: [Exception], tags: [].}
proc readData(s: Stream; buffer: pointer; bufLen: int): int {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc readAll(s: Stream): string {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc peekData(s: Stream; buffer: pointer; bufLen: int): int {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc writeData(s: Stream; buffer: pointer; bufLen: int) {...}{.raises: [Exception], tags: [WriteIOEffect].}
proc writeData(s, unused: Stream; buffer: pointer; bufLen: int) {...}{.deprecated, raises: [Exception], tags: [WriteIOEffect].}
proc write[T](s: Stream; x: T)
s.writeData(s, addr(x), sizeof(x))
proc write(s: Stream; x: string) {...}{.raises: [Exception], tags: [WriteIOEffect].}
proc write(s: Stream; args: varargs[string, `$`]) {...}{.raises: [Exception], tags: [WriteIOEffect].}
proc writeLine(s: Stream; args: varargs[string, `$`]) {...}{.raises: [Exception], tags: [WriteIOEffect].}
proc readChar(s: Stream): char {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc peekChar(s: Stream): char {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc readBool(s: Stream): bool {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekBool(s: Stream): bool {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readInt8(s: Stream): int8 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekInt8(s: Stream): int8 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readInt16(s: Stream): int16 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekInt16(s: Stream): int16 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readInt32(s: Stream): int32 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekInt32(s: Stream): int32 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readInt64(s: Stream): int64 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekInt64(s: Stream): int64 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readUint8(s: Stream): uint8 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekUint8(s: Stream): uint8 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readUint16(s: Stream): uint16 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekUint16(s: Stream): uint16 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readUint32(s: Stream): uint32 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekUint32(s: Stream): uint32 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readUint64(s: Stream): uint64 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekUint64(s: Stream): uint64 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readFloat32(s: Stream): float32 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekFloat32(s: Stream): float32 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readFloat64(s: Stream): float64 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekFloat64(s: Stream): float64 {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc readStr(s: Stream; length: int): TaintedString {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc peekStr(s: Stream; length: int): TaintedString {...}{.raises: [Exception], tags: [ReadIOEffect].}
proc readLine(s: Stream; line: var TaintedString): bool {...}{.raises: [Exception], tags: [ReadIOEffect].}
nil
! May throw an IO exception. A line of text may be delimited by ```LF`` or CRLF
. The newline character(s) are not part of the returned string. Returns false
if the end of the file has been reached, true
otherwise. If false
is returned line contains no new data. proc peekLine(s: Stream; line: var TaintedString): bool {...}{. raises: [Exception, Exception], tags: [ReadIOEffect].}
nil
! May throw an IO exception. A line of text may be delimited by CR
, LF
or CRLF
. The newline character(s) are not part of the returned string. Returns false
if the end of the file has been reached, true
otherwise. If false
is returned line contains no new data. proc readLine(s: Stream): TaintedString {...}{.raises: [Exception, IOError], tags: [ReadIOEffect].}
proc peekLine(s: Stream): TaintedString {...}{.raises: [Exception, Exception, IOError], tags: [ReadIOEffect].}
proc newStringStream(s: string = ""): StringStream {...}{.raises: [], tags: [].}
proc newFileStream(f: File): FileStream {...}{.raises: [], tags: [].}
proc newFileStream(filename: string; mode: FileMode = fmRead; bufSize: int = -1): FileStream {...}{. raises: [], tags: [].}
proc openFileStream(filename: string; mode: FileMode = fmRead; bufSize: int = -1): FileStream {...}{. raises: [IOError], tags: [].}
iterator lines(s: Stream): TaintedString {...}{.raises: [Exception], tags: [ReadIOEffect].}
readLine
.
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/streams.html