summary refs log tree commit diff stats
path: root/tests/exception/tcontinuexc.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2015-09-05 11:28:17 +0200
committerAraq <rumpf_a@web.de>2015-09-05 11:34:38 +0200
commit49d810f3410c19803f2d6fbfb3feb03e1fc13fee (patch)
treef2c3db06b635a1c45d06d4b49e67ca9795e5a83f /tests/exception/tcontinuexc.nim
parent0f2a37cf1b64bfa34da0f899a1f76b28665c8c89 (diff)
downloadNim-49d810f3410c19803f2d6fbfb3feb03e1fc13fee.tar.gz
Nimble is officially a part of Nim now
Diffstat (limited to 'tests/exception/tcontinuexc.nim')
0 files changed, 0 insertions, 0 deletions
10 111 112 113 114 115 116 117 118 119 120 121
#
#
#            Nim's Runtime Library
#        (c) Copyright 2020 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements stream wrapper.
##
## **Since** version 1.2.

import std/[deques, streams]

when defined(nimPreviewSlimSystem):
  import std/assertions


type
  PipeOutStream*[T] = ref object of T
    # When stream peek operation is called, it reads from base stream
    # type using `baseReadDataImpl` and stores the content to this buffer.
    # Next stream read operation returns data in the buffer so that previus peek
    # operation looks like didn't changed read positon.
    # When stream read operation that returns N byte data is called and the size is smaller than buffer size,
    # first N elements are removed from buffer.
    # Deque type can do such operation more efficiently than seq type.
    buffer: Deque[char]
    baseReadLineImpl: typeof(StreamObj.readLineImpl)
    baseReadDataImpl: typeof(StreamObj.readDataImpl)

proc posReadLine[T](s: Stream, line: var string): bool =
  var s = PipeOutStream[T](s)
  assert s.baseReadLineImpl != nil

  let n = s.buffer.len
  line.setLen(0)
  for i in 0..<n:
    var c = s.buffer.popFirst
    if c == '\c':
      c = readChar(s)
      return true
    elif c == '\L': return true
    elif c == '\0':
      return line.len > 0
    line.add(c)

  var line2: string
  result = s.baseReadLineImpl(s, line2)
  line.add line2

proc posReadData[T](s: Stream, buffer: pointer, bufLen: int): int =
  var s = PipeOutStream[T](s)
  assert s.baseReadDataImpl != nil

  let
    dest = cast[ptr UncheckedArray[char]](buffer)
    n = min(s.buffer.len, bufLen)
  result = n
  for i in 0..<n:
    dest[i] = s.buffer.popFirst
  if bufLen > n:
    result += s.baseReadDataImpl(s, addr dest[n], bufLen - n)

proc posReadDataStr[T](s: Stream, buffer: var string, slice: Slice[int]): int =
  posReadData[T](s, addr buffer[slice.a], slice.len)

proc posPeekData[T](s: Stream, buffer: pointer, bufLen: int): int =
  var s = PipeOutStream[T](s)
  assert s.baseReadDataImpl != nil

  let
    dest = cast[ptr UncheckedArray[char]](buffer)
    n = min(s.buffer.len, bufLen)

  result = n
  for i in 0..<n:
    dest[i] = s.buffer[i]

  if bufLen > n:
    let
      newDataNeeded = bufLen - n
      numRead = s.baseReadDataImpl(s, addr dest[n], newDataNeeded)
    result += numRead
    for i in 0..<numRead:
      s.buffer.addLast dest[n + i]

proc newPipeOutStream*[T](s: sink (ref T)): owned PipeOutStream[T] =
  ## Wrap pipe for reading with PipeOutStream so that you can use peek* procs and generate runtime error
  ## when setPosition/getPosition is called or write operation is performed.
  ##
  ## Example:
  ##   ```Nim
  ##   import std/[osproc, streamwrapper]
  ##   var
  ##     p = startProcess(exePath)
  ##     outStream = p.outputStream().newPipeOutStream()
  ##   echo outStream.peekChar
  ##   p.close()
  ##   ```

  assert s.readDataImpl != nil

  new(result)
  for dest, src in fields((ref T)(result)[], s[]):
    dest = src
  wasMoved(s[])
  if result.readLineImpl != nil:
    result.baseReadLineImpl = result.readLineImpl
    result.readLineImpl = posReadLine[T]
  result.baseReadDataImpl = result.readDataImpl
  result.readDataImpl = posReadData[T]
  result.readDataStrImpl = posReadDataStr[T]
  result.peekDataImpl = posPeekData[T]

  # Set nil to anything you may not call.
  result.setPositionImpl = nil
  result.getPositionImpl = nil
  result.writeDataImpl = nil
  result.flushImpl = nil