Source Edit

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.

Basic usage

The basic flow of using this module is:

  1. Open input stream
  2. Read or write stream
  3. Close stream

StringStream example

  1. import std/streams
  2. var strm = newStringStream("""The first line
  3. the second line
  4. the third line""")
  5. var line = ""
  6. while strm.readLine(line):
  7. echo line
  8. # Output:
  9. # The first line
  10. # the second line
  11. # the third line
  12. strm.close()

FileStream example

Write file stream example:

  1. import std/streams
  2. var strm = newFileStream("somefile.txt", fmWrite)
  3. var line = ""
  4. if not isNil(strm):
  5. strm.writeLine("The first line")
  6. strm.writeLine("the second line")
  7. strm.writeLine("the third line")
  8. strm.close()
  9. # Output (somefile.txt):
  10. # The first line
  11. # the second line
  12. # the third line

Read file stream example:

  1. import std/streams
  2. var strm = newFileStream("somefile.txt", fmRead)
  3. var line = ""
  4. if not isNil(strm):
  5. while strm.readLine(line):
  6. echo line
  7. strm.close()
  8. # Output:
  9. # The first line
  10. # the second line
  11. # the third line

See also

Imports

since

Types

  1. FileStream = ref FileStreamObj

A stream that encapsulates a File.

Note: Not available for JS backend.

Source Edit

  1. FileStreamObj = object of Stream

A file stream object.

Note: Not available for JS backend.

Source Edit

  1. Stream = ref StreamObj

All procedures of this module use this type. Procedures don’t directly use StreamObj. Source Edit

  1. StreamObj = object of RootObj
  2. closeImpl*: proc (s: Stream) {.nimcall, ...raises: [IOError, OSError],
  3. tags: [WriteIOEffect], gcsafe.}
  4. atEndImpl*: proc (s: Stream): bool {.nimcall,
  5. ...raises: [Defect, IOError, OSError],
  6. tags: [], gcsafe.}
  7. setPositionImpl*: proc (s: Stream; pos: int) {.nimcall,
  8. ...raises: [Defect, IOError, OSError], tags: [], gcsafe.}
  9. getPositionImpl*: proc (s: Stream): int {.nimcall,
  10. ...raises: [Defect, IOError, OSError], tags: [], gcsafe.}
  11. readDataStrImpl*: proc (s: Stream; buffer: var string; slice: Slice[int]): int {.
  12. nimcall, ...raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
  13. readLineImpl*: proc (s: Stream; line: var string): bool {.nimcall,
  14. ...raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
  15. readDataImpl*: proc (s: Stream; buffer: pointer; bufLen: int): int {.nimcall,
  16. ...raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
  17. peekDataImpl*: proc (s: Stream; buffer: pointer; bufLen: int): int {.nimcall,
  18. ...raises: [Defect, IOError, OSError], tags: [ReadIOEffect], gcsafe.}
  19. writeDataImpl*: proc (s: Stream; buffer: pointer; bufLen: int) {.nimcall,
  20. ...raises: [Defect, IOError, OSError], tags: [WriteIOEffect], gcsafe.}
  21. flushImpl*: proc (s: Stream) {.nimcall, ...raises: [Defect, IOError, OSError],
  22. tags: [WriteIOEffect], gcsafe.}

Stream interface that supports writing or reading.

Note:

  • That these fields here shouldn’t be used directly. They are accessible so that a stream implementation can override them.

Source Edit

  1. StringStream = ref StringStreamObj

A stream that encapsulates a string. Source Edit

  1. StringStreamObj = object of StreamObj
  2. data*: string ## A string data.
  3. ## This is updated when called `writeLine` etc.

A string stream object. Source Edit

Procs

  1. proc atEnd(s: Stream): bool {....raises: [IOError, OSError], tags: [], forbids: [].}

Checks if more data can be read from s. Returns true if all data has been read.

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. var line = ""
  3. doAssert strm.atEnd() == false
  4. while strm.readLine(line):
  5. discard
  6. doAssert strm.atEnd() == true
  7. strm.close()

Source Edit

  1. proc close(s: Stream) {....raises: [IOError, OSError], tags: [WriteIOEffect],
  2. forbids: [].}

Closes the stream s.

See also:

Example:

  1. block:
  2. let strm = newStringStream("The first line\nthe second line\nthe third line")
  3. ## do something...
  4. strm.close()
  5. block:
  6. let strm = newFileStream("amissingfile.txt")
  7. # deferring works even if newFileStream fails
  8. defer: strm.close()
  9. if not isNil(strm):
  10. ## do something...

Source Edit

  1. proc flush(s: Stream) {....raises: [IOError, OSError], tags: [WriteIOEffect],
  2. forbids: [].}

Flushes the buffers that the stream s might use.

This procedure causes any unwritten data for that stream to be delivered to the host environment to be written to the file.

See also:

Example:

  1. from std/os import removeFile
  2. var strm = newFileStream("somefile.txt", fmWrite)
  3. doAssert "Before write:" & readFile("somefile.txt") == "Before write:"
  4. strm.write("hello")
  5. doAssert "After write:" & readFile("somefile.txt") == "After write:"
  6. strm.flush()
  7. doAssert "After flush:" & readFile("somefile.txt") == "After flush:hello"
  8. strm.write("HELLO")
  9. strm.flush()
  10. doAssert "After flush:" & readFile("somefile.txt") == "After flush:helloHELLO"
  11. strm.close()
  12. doAssert "After close:" & readFile("somefile.txt") == "After close:helloHELLO"
  13. removeFile("somefile.txt")

Source Edit

  1. proc getPosition(s: Stream): int {....raises: [IOError, OSError], tags: [],
  2. forbids: [].}

Retrieves the current position in the stream s.

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. doAssert strm.getPosition() == 0
  3. discard strm.readLine()
  4. doAssert strm.getPosition() == 15
  5. strm.close()

Source Edit

  1. proc newFileStream(f: File): owned FileStream {....raises: [], tags: [],
  2. forbids: [].}

Creates a new stream from the file f.

Note: Not available for JS backend.

See also:

Example:

  1. ## Input (somefile.txt):
  2. ## The first line
  3. ## the second line
  4. ## the third line
  5. var f: File
  6. if open(f, "somefile.txt", fmRead, -1):
  7. var strm = newFileStream(f)
  8. var line = ""
  9. while strm.readLine(line):
  10. echo line
  11. ## Output:
  12. ## The first line
  13. ## the second line
  14. ## the third line
  15. strm.close()

Source Edit

  1. proc newFileStream(filename: string; mode: FileMode = fmRead; bufSize: int = -1): owned
  2. FileStream {....raises: [], tags: [], forbids: [].}

Creates a new stream from the file named filename with the mode mode.

If the file cannot be opened, nil is returned. See the io module for a list of available FileMode enums.

Note:

  • This function returns nil in case of failure. To prevent unexpected behavior and ensure proper error handling, use openFileStream proc instead.
  • Not available for JS backend.

See also:

Example:

  1. from std/os import removeFile
  2. var strm = newFileStream("somefile.txt", fmWrite)
  3. if not isNil(strm):
  4. strm.writeLine("The first line")
  5. strm.writeLine("the second line")
  6. strm.writeLine("the third line")
  7. strm.close()
  8. ## Output (somefile.txt)
  9. ## The first line
  10. ## the second line
  11. ## the third line
  12. removeFile("somefile.txt")

Source Edit

  1. proc newStringStream(s: sink string = ""): owned StringStream {....raises: [],
  2. tags: [], forbids: [].}

Creates a new stream from the string s.

See also:

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. doAssert strm.readLine() == "The first line"
  3. doAssert strm.readLine() == "the second line"
  4. doAssert strm.readLine() == "the third line"
  5. strm.close()

Source Edit

  1. proc openFileStream(filename: string; mode: FileMode = fmRead; bufSize: int = -1): owned
  2. FileStream {....raises: [IOError], tags: [], forbids: [].}

Creates a new stream from the file named filename with the mode mode. If the file cannot be opened, an IO exception is raised.

Note: Not available for JS backend.

See also:

Example:

  1. try:
  2. ## Input (somefile.txt):
  3. ## The first line
  4. ## the second line
  5. ## the third line
  6. var strm = openFileStream("somefile.txt")
  7. echo strm.readLine()
  8. ## Output:
  9. ## The first line
  10. strm.close()
  11. except:
  12. stderr.write getCurrentExceptionMsg()

Source Edit

  1. proc peek[T](s: Stream; result: var T)

Generic peek procedure. Peeks result from the stream s.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream("012")
  2. ## peekInt
  3. var i: int8
  4. strm.peek(i)
  5. doAssert i == 48
  6. ## peekData
  7. var buffer: array[2, char]
  8. strm.peek(buffer)
  9. doAssert buffer == ['0', '1']
  10. strm.close()

Source Edit

  1. proc peekBool(s: Stream): bool {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks a bool from the stream s.

A bool is one byte long and it is true for every non-zero (0000_0000) value. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(true)
  4. strm.write(false)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekBool() == true
  9. ## not false
  10. doAssert strm.peekBool() == true
  11. doAssert strm.readBool() == true
  12. doAssert strm.peekBool() == false
  13. strm.close()

Source Edit

  1. proc peekChar(s: Stream): char {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks a char from the stream s. Raises IOError if an error occurred. Returns ‘\0’ as an EOF marker.

Example:

  1. var strm = newStringStream("12\n3")
  2. doAssert strm.peekChar() == '1'
  3. doAssert strm.peekChar() == '1'
  4. discard strm.readAll()
  5. doAssert strm.peekChar() == '\x00'
  6. strm.close()

Source Edit

  1. proc peekData(s: Stream; buffer: pointer; bufLen: int): int {.
  2. ...raises: [IOError, OSError], tags: [ReadIOEffect], forbids: [].}

Low level proc that reads data into an untyped buffer of bufLen size without moving stream position.

JS note: buffer is treated as a ptr string and written to between 0..<bufLen.

Example:

  1. var strm = newStringStream("abcde")
  2. var buffer: array[6, char]
  3. doAssert strm.peekData(addr(buffer), 1024) == 5
  4. doAssert buffer == ['a', 'b', 'c', 'd', 'e', '\x00']
  5. doAssert strm.atEnd() == false
  6. strm.close()

Source Edit

  1. proc peekFloat32(s: Stream): float32 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

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

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'f32)
  4. strm.write(2'f32)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekFloat32() == 1'f32
  9. ## not 2'f32
  10. doAssert strm.peekFloat32() == 1'f32
  11. doAssert strm.readFloat32() == 1'f32
  12. doAssert strm.peekFloat32() == 2'f32
  13. strm.close()

Source Edit

  1. proc peekFloat64(s: Stream): float64 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

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

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'f64)
  4. strm.write(2'f64)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekFloat64() == 1'f64
  9. ## not 2'f64
  10. doAssert strm.peekFloat64() == 1'f64
  11. doAssert strm.readFloat64() == 1'f64
  12. doAssert strm.peekFloat64() == 2'f64
  13. strm.close()

Source Edit

  1. proc peekInt8(s: Stream): int8 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an int8 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i8)
  4. strm.write(2'i8)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekInt8() == 1'i8
  9. ## not 2'i8
  10. doAssert strm.peekInt8() == 1'i8
  11. doAssert strm.readInt8() == 1'i8
  12. doAssert strm.peekInt8() == 2'i8
  13. strm.close()

Source Edit

  1. proc peekInt16(s: Stream): int16 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an int16 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i16)
  4. strm.write(2'i16)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekInt16() == 1'i16
  9. ## not 2'i16
  10. doAssert strm.peekInt16() == 1'i16
  11. doAssert strm.readInt16() == 1'i16
  12. doAssert strm.peekInt16() == 2'i16
  13. strm.close()

Source Edit

  1. proc peekInt32(s: Stream): int32 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an int32 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i32)
  4. strm.write(2'i32)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekInt32() == 1'i32
  9. ## not 2'i32
  10. doAssert strm.peekInt32() == 1'i32
  11. doAssert strm.readInt32() == 1'i32
  12. doAssert strm.peekInt32() == 2'i32
  13. strm.close()

Source Edit

  1. proc peekInt64(s: Stream): int64 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an int64 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i64)
  4. strm.write(2'i64)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekInt64() == 1'i64
  9. ## not 2'i64
  10. doAssert strm.peekInt64() == 1'i64
  11. doAssert strm.readInt64() == 1'i64
  12. doAssert strm.peekInt64() == 2'i64
  13. strm.close()

Source Edit

  1. proc peekLine(s: Stream): string {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

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

Note: This is not very efficient.

See also:

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. doAssert strm.peekLine() == "The first line"
  3. ## not "the second line"
  4. doAssert strm.peekLine() == "The first line"
  5. doAssert strm.readLine() == "The first line"
  6. doAssert strm.peekLine() == "the second line"
  7. strm.close()

Source Edit

  1. proc peekLine(s: Stream; line: var string): bool {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

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:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. var line = ""
  3. doAssert strm.peekLine(line) == true
  4. doAssert line == "The first line"
  5. doAssert strm.peekLine(line) == true
  6. ## not "the second line"
  7. doAssert line == "The first line"
  8. doAssert strm.readLine(line) == true
  9. doAssert line == "The first line"
  10. doAssert strm.peekLine(line) == true
  11. doAssert line == "the second line"
  12. strm.close()

Source Edit

  1. proc peekStr(s: Stream; length: int): string {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks a string of length length from the stream s. Raises IOError if an error occurred.

Example:

  1. var strm = newStringStream("abcde")
  2. doAssert strm.peekStr(2) == "ab"
  3. ## not "cd
  4. doAssert strm.peekStr(2) == "ab"
  5. doAssert strm.readStr(2) == "ab"
  6. doAssert strm.peekStr(2) == "cd"
  7. strm.close()

Source Edit

  1. proc peekStr(s: Stream; length: int; str: var string) {.
  2. ...raises: [IOError, OSError], tags: [ReadIOEffect], forbids: [].}

Peeks a string of length length from the stream s. Raises IOError if an error occurred. Source Edit

  1. proc peekUint8(s: Stream): uint8 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an uint8 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u8)
  4. strm.write(2'u8)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekUint8() == 1'u8
  9. ## not 2'u8
  10. doAssert strm.peekUint8() == 1'u8
  11. doAssert strm.readUint8() == 1'u8
  12. doAssert strm.peekUint8() == 2'u8
  13. strm.close()

Source Edit

  1. proc peekUint16(s: Stream): uint16 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an uint16 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u16)
  4. strm.write(2'u16)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekUint16() == 1'u16
  9. ## not 2'u16
  10. doAssert strm.peekUint16() == 1'u16
  11. doAssert strm.readUint16() == 1'u16
  12. doAssert strm.peekUint16() == 2'u16
  13. strm.close()

Source Edit

  1. proc peekUint32(s: Stream): uint32 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an uint32 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u32)
  4. strm.write(2'u32)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekUint32() == 1'u32
  9. ## not 2'u32
  10. doAssert strm.peekUint32() == 1'u32
  11. doAssert strm.readUint32() == 1'u32
  12. doAssert strm.peekUint32() == 2'u32
  13. strm.close()

Source Edit

  1. proc peekUint64(s: Stream): uint64 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Peeks an uint64 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use peekStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u64)
  4. strm.write(2'u64)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.peekUint64() == 1'u64
  9. ## not 2'u64
  10. doAssert strm.peekUint64() == 1'u64
  11. doAssert strm.readUint64() == 1'u64
  12. doAssert strm.peekUint64() == 2'u64
  13. strm.close()

Source Edit

  1. proc read[T](s: Stream; result: var T)

Generic read procedure. Reads result from the stream s.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream("012")
  2. ## readInt
  3. var i: int8
  4. strm.read(i)
  5. doAssert i == 48
  6. ## readData
  7. var buffer: array[2, char]
  8. strm.read(buffer)
  9. doAssert buffer == ['1', '2']
  10. strm.close()

Source Edit

  1. proc readAll(s: Stream): string {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads all available data.

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. doAssert strm.readAll() == "The first line\nthe second line\nthe third line"
  3. doAssert strm.atEnd() == true
  4. strm.close()

Source Edit

  1. proc readBool(s: Stream): bool {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads a bool from the stream s.

A bool is one byte long and it is true for every non-zero (0000_0000) value. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(true)
  4. strm.write(false)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readBool() == true
  9. doAssert strm.readBool() == false
  10. doAssertRaises(IOError): discard strm.readBool()
  11. strm.close()

Source Edit

  1. proc readChar(s: Stream): char {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads a char from the stream s.

Raises IOError if an error occurred. Returns ‘\0’ as an EOF marker.

Example:

  1. var strm = newStringStream("12\n3")
  2. doAssert strm.readChar() == '1'
  3. doAssert strm.readChar() == '2'
  4. doAssert strm.readChar() == '\n'
  5. doAssert strm.readChar() == '3'
  6. doAssert strm.readChar() == '\x00'
  7. strm.close()

Source Edit

  1. proc readData(s: Stream; buffer: pointer; bufLen: int): int {.
  2. ...raises: [IOError, OSError], tags: [ReadIOEffect], forbids: [].}

Low level proc that reads data into an untyped buffer of bufLen size.

JS note: buffer is treated as a ptr string and written to between 0..<bufLen.

Example:

  1. var strm = newStringStream("abcde")
  2. var buffer: array[6, char]
  3. doAssert strm.readData(addr(buffer), 1024) == 5
  4. doAssert buffer == ['a', 'b', 'c', 'd', 'e', '\x00']
  5. doAssert strm.atEnd() == true
  6. strm.close()

Source Edit

  1. proc readDataStr(s: Stream; buffer: var string; slice: Slice[int]): int {.
  2. ...raises: [IOError, OSError], tags: [ReadIOEffect], forbids: [].}

Low level proc that reads data into a string buffer at slice.

Example:

  1. var strm = newStringStream("abcde")
  2. var buffer = "12345"
  3. doAssert strm.readDataStr(buffer, 0..3) == 4
  4. doAssert buffer == "abcd5"
  5. strm.close()

Source Edit

  1. proc readFloat32(s: Stream): float32 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads a float32 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'f32)
  4. strm.write(2'f32)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readFloat32() == 1'f32
  9. doAssert strm.readFloat32() == 2'f32
  10. doAssertRaises(IOError): discard strm.readFloat32()
  11. strm.close()

Source Edit

  1. proc readFloat64(s: Stream): float64 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads a float64 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'f64)
  4. strm.write(2'f64)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readFloat64() == 1'f64
  9. doAssert strm.readFloat64() == 2'f64
  10. doAssertRaises(IOError): discard strm.readFloat64()
  11. strm.close()

Source Edit

  1. proc readInt8(s: Stream): int8 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an int8 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i8)
  4. strm.write(2'i8)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readInt8() == 1'i8
  9. doAssert strm.readInt8() == 2'i8
  10. doAssertRaises(IOError): discard strm.readInt8()
  11. strm.close()

Source Edit

  1. proc readInt16(s: Stream): int16 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an int16 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i16)
  4. strm.write(2'i16)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readInt16() == 1'i16
  9. doAssert strm.readInt16() == 2'i16
  10. doAssertRaises(IOError): discard strm.readInt16()
  11. strm.close()

Source Edit

  1. proc readInt32(s: Stream): int32 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an int32 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i32)
  4. strm.write(2'i32)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readInt32() == 1'i32
  9. doAssert strm.readInt32() == 2'i32
  10. doAssertRaises(IOError): discard strm.readInt32()
  11. strm.close()

Source Edit

  1. proc readInt64(s: Stream): int64 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an int64 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'i64)
  4. strm.write(2'i64)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readInt64() == 1'i64
  9. doAssert strm.readInt64() == 2'i64
  10. doAssertRaises(IOError): discard strm.readInt64()
  11. strm.close()

Source Edit

  1. proc readLine(s: Stream): string {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

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

Note: This is not very efficient.

See also:

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. doAssert strm.readLine() == "The first line"
  3. doAssert strm.readLine() == "the second line"
  4. doAssert strm.readLine() == "the third line"
  5. doAssertRaises(IOError): discard strm.readLine()
  6. strm.close()

Source Edit

  1. proc readLine(s: Stream; line: var string): bool {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads 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 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:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. var line = ""
  3. doAssert strm.readLine(line) == true
  4. doAssert line == "The first line"
  5. doAssert strm.readLine(line) == true
  6. doAssert line == "the second line"
  7. doAssert strm.readLine(line) == true
  8. doAssert line == "the third line"
  9. doAssert strm.readLine(line) == false
  10. doAssert line == ""
  11. strm.close()

Source Edit

  1. proc readStr(s: Stream; length: int): string {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads a string of length length from the stream s. Raises IOError if an error occurred.

Example:

  1. var strm = newStringStream("abcde")
  2. doAssert strm.readStr(2) == "ab"
  3. doAssert strm.readStr(2) == "cd"
  4. doAssert strm.readStr(2) == "e"
  5. doAssert strm.readStr(2) == ""
  6. strm.close()

Source Edit

  1. proc readStr(s: Stream; length: int; str: var string) {.
  2. ...raises: [IOError, OSError], tags: [ReadIOEffect], forbids: [].}

Reads a string of length length from the stream s. Raises IOError if an error occurred. Source Edit

  1. proc readUint8(s: Stream): uint8 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an uint8 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u8)
  4. strm.write(2'u8)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readUint8() == 1'u8
  9. doAssert strm.readUint8() == 2'u8
  10. doAssertRaises(IOError): discard strm.readUint8()
  11. strm.close()

Source Edit

  1. proc readUint16(s: Stream): uint16 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an uint16 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u16)
  4. strm.write(2'u16)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readUint16() == 1'u16
  9. doAssert strm.readUint16() == 2'u16
  10. doAssertRaises(IOError): discard strm.readUint16()
  11. strm.close()

Source Edit

  1. proc readUint32(s: Stream): uint32 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an uint32 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u32)
  4. strm.write(2'u32)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readUint32() == 1'u32
  9. doAssert strm.readUint32() == 2'u32
  10. doAssertRaises(IOError): discard strm.readUint32()
  11. strm.close()

Source Edit

  1. proc readUint64(s: Stream): uint64 {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Reads an uint64 from the stream s. Raises IOError if an error occurred.

Note: Not available for JS backend. Use readStr for now.

Example:

  1. var strm = newStringStream()
  2. ## setup for reading data
  3. strm.write(1'u64)
  4. strm.write(2'u64)
  5. strm.flush()
  6. strm.setPosition(0)
  7. ## get data
  8. doAssert strm.readUint64() == 1'u64
  9. doAssert strm.readUint64() == 2'u64
  10. doAssertRaises(IOError): discard strm.readUint64()
  11. strm.close()

Source Edit

  1. proc setPosition(s: Stream; pos: int) {....raises: [IOError, OSError], tags: [],
  2. forbids: [].}

Sets the position pos of the stream s.

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. strm.setPosition(4)
  3. doAssert strm.readLine() == "first line"
  4. strm.setPosition(0)
  5. doAssert strm.readLine() == "The first line"
  6. strm.close()

Source Edit

  1. proc write(s: Stream; args: varargs[string, `$`]) {....raises: [IOError, OSError],
  2. tags: [WriteIOEffect], forbids: [].}

Writes one or more strings to the the stream. No length fields or terminating zeros are written.

Example:

  1. var strm = newStringStream("")
  2. strm.write(1, 2, 3, 4)
  3. strm.setPosition(0)
  4. doAssert strm.readLine() == "1234"
  5. strm.close()

Source Edit

  1. proc write(s: Stream; x: string) {....raises: [IOError, OSError],
  2. tags: [WriteIOEffect], forbids: [].}

Writes the string x to the stream s. No length field or terminating zero is written.

Example:

  1. var strm = newStringStream("")
  2. strm.write("THE FIRST LINE")
  3. strm.setPosition(0)
  4. doAssert strm.readLine() == "THE FIRST LINE"
  5. strm.close()

Source Edit

  1. proc write[T](s: Stream; x: T)

Generic write procedure. Writes x to the stream s. Implementation:

Note: Not available for JS backend. Use write(Stream, string) for now.

  1. s.writeData(s, unsafeAddr(x), sizeof(x))

Example:

  1. var strm = newStringStream("")
  2. strm.write("abcde")
  3. strm.setPosition(0)
  4. doAssert strm.readAll() == "abcde"
  5. strm.close()

Source Edit

  1. proc writeData(s: Stream; buffer: pointer; bufLen: int) {.
  2. ...raises: [IOError, OSError], tags: [WriteIOEffect], forbids: [].}

Low level proc that writes an untyped buffer of bufLen size to the stream s.

JS note: buffer is treated as a ptr string and read between 0..<bufLen.

Example:

  1. ## writeData
  2. var strm = newStringStream("")
  3. var buffer = ['a', 'b', 'c', 'd', 'e']
  4. strm.writeData(addr(buffer), sizeof(buffer))
  5. doAssert strm.atEnd() == true
  6. ## readData
  7. strm.setPosition(0)
  8. var buffer2: array[6, char]
  9. doAssert strm.readData(addr(buffer2), sizeof(buffer2)) == 5
  10. doAssert buffer2 == ['a', 'b', 'c', 'd', 'e', '\x00']
  11. strm.close()

Source Edit

  1. proc writeLine(s: Stream; args: varargs[string, `$`]) {.
  2. ...raises: [IOError, OSError], tags: [WriteIOEffect], forbids: [].}

Writes one or more strings to the the stream s followed by a new line. No length field or terminating zero is written.

Example:

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

Source Edit

Iterators

  1. iterator lines(s: Stream): string {....raises: [IOError, OSError],
  2. tags: [ReadIOEffect], forbids: [].}

Iterates over every line in the stream. The iteration is based on readLine.

See also:

Example:

  1. var strm = newStringStream("The first line\nthe second line\nthe third line")
  2. var lines: seq[string]
  3. for line in strm.lines():
  4. lines.add line
  5. doAssert lines == @["The first line", "the second line", "the third line"]
  6. strm.close()

Source Edit