streams3

Types

Stream = concept svar vs
    atEnd(s) is bool
    setPosition(vs, int)
    getPosition(s) is int
    readDataStr(vs, var string, Slice[int]) is int
    readData(vs, pointer, int) is int
    peekData(vs, pointer, int) is int
    writeData(vs, pointer, int)
    readChar(vs) is char
    readLine(vs, var string) is bool
    readLine(vs) is string
    close(vs)

Procs

proc peekLine(s: var Stream): string

Peeks a line from a stream s. Raises IOError if an error occurred.

Note: This is not very efficient.

See also:

Example:

var strm = newStringStream("The first line\nthe second line\nthe third line")
doAssert strm.peekLine() == "The first line"
## not "the second line"
doAssert strm.peekLine() == "The first line"
doAssert strm.readLine() == "The first line"
doAssert strm.peekLine() == "the second line"
strm.close()
proc peekLine(s: var Stream; line: var string): bool

Peeks a line of text from the stream s into line. line must not be 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.

See also:

Example:

var strm = newStringStream("The first line\nthe second line\nthe third line")
var line = ""
doAssert strm.peekLine(line) == true
doAssert line == "The first line"
doAssert strm.peekLine(line) == true
## not "the second line"
doAssert line == "The first line"
doAssert strm.readLine(line) == true
doAssert line == "The first line"
doAssert strm.peekLine(line) == true
doAssert line == "the second line"
strm.close()
proc peekStr(s: var Stream; length: int): string
Peeks a string of length length from the stream s. Raises IOError if an error occurred.

Example:

var strm = newStringStream("abcde")
doAssert strm.peekStr(2) == "ab"
## not "cd
doAssert strm.peekStr(2) == "ab"
doAssert strm.readStr(2) == "ab"
doAssert strm.peekStr(2) == "cd"
strm.close()
proc peekStr(s: var Stream; length: int; str: var string)
Peeks a string of length length from the stream s. Raises IOError if an error occurred.
proc readAll(s: var Stream): string
Reads all available data.

Example:

var strm = newStringStream("The first line\nthe second line\nthe third line")
doAssert strm.readAll() == "The first line\nthe second line\nthe third line"
doAssert strm.atEnd() == true
strm.close()
proc readStr(s: Stream; length: int; str: var string)
Reads a string of length length from the stream s. Raises IOError if an error occurred.
proc readStr(s: var Stream; length: int): string
Reads a string of length length from the stream s. Raises IOError if an error occurred.

Example:

var strm = newStringStream("abcde")
doAssert strm.readStr(2) == "ab"
doAssert strm.readStr(2) == "cd"
doAssert strm.readStr(2) == "e"
doAssert strm.readStr(2) == ""
strm.close()
proc write(s: var Stream; x: string)
Writes the string x to the stream s. No length field or terminating zero is written.

Example:

var strm = newStringStream("")
strm.write("THE FIRST LINE")
strm.setPosition(0)
doAssert strm.readLine() == "THE FIRST LINE"
strm.close()
proc writeLine(s: var Stream; args: varargs[string, `$`])
Writes one or more strings to the the stream s followed by a new line. No length field or terminating zero is written.

Example:

var strm = StringStream.init("")
strm.writeLine(1, 2)
strm.writeLine(3, 4)
strm.setPosition(0)
doAssert strm.readAll() == "12\n34\n"
strm.close()

Iterators

iterator lines(s: var Stream): string
Iterates over every line in the stream. The iteration is based on readLine.

Example:

var strm = StringStream.init("The first line\nthe second line\nthe third line")
var lines: seq[string]
for line in strm.lines():
  lines.add line
doAssert lines == @["The first line", "the second line", "the third line"]
strm.close()