# # # Nimrod's Runtime Library # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## This module provides a stream interface and two implementations thereof: ## the `PFileStream` and the `PStringStream` which implement the stream ## interface for Nimrod file objects (`TFile`) and strings. Other modules ## may provide other implementations for this standard stream interface. proc newEIO(msg: string): ref EIO = new(result) result.msg = msg type PStream* = ref TStream TStream* = object of TObject ## 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. closeImpl*: proc (s: PStream) {.nimcall, tags: [].} atEndImpl*: proc (s: PStream): bool {.nimcall, tags: [].} setPositionImpl*: proc (s: PStream, pos: int) {.nimcall, tags: [].} getPositionImpl*: proc (s: PStream): int {.nimcall, tags: [].} readDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int): int {.nimcall, tags: [FReadIO].} writeDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int) {.nimcall, tags: [FWriteIO].} flushImpl*: proc (s: PStream) {.nimcall, tags: [FWriteIO].} proc flush*(s: PStream) = ## flushes the buffers that the stream `s` might use. if not isNil(s.flushImpl): s.flushImpl(s) proc close*(s: PStream) = ## closes the stream `s`. if not isNil(s.closeImpl): s.closeImpl(s) proc close*(s, unused: PStream) {.deprecated.} = ## closes the stream `s`. s.closeImpl(s) proc atEnd*(s: PStream): bool = ## checks if more data can be read from `f`. Returns true if all data has ## been read. result = s.atEndImpl(s) proc atEnd*(s, unused: PStream): bool {.deprecated.} = ## checks if more data can be read from `f`. Returns true if all data has ## been read. result = s.atEndImpl(s) proc setPosition*(s: PStream, pos: int) = ## sets the position `pos` of the stream `s`. s.setPositionImpl(s, pos) proc setPosition*(s, unused: PStream, pos: int) {.deprecated.} = ## sets the position `pos` of the stream `s`. s.setPositionImpl(s, pos) proc getPosition*(s: PStream): int = ## retrieves the current position in the stream `s`. result = s.getPositionImpl(s) proc getPosition*(s, unused: PStream): int {.deprecated.} = ## retrieves the current position in the stream `s`. result = s.getPositionImpl(s) proc readData*(s: PStream, buffer: pointer, bufLen: int): int = ## low level proc that reads data into an untyped `buffer` of `bufLen` size. result = s.readDataImpl(s, buffer, bufLen) proc readData*(s, unused: PStream, buffer: pointer, bufLen: int): int {.deprecated.} = ## low level proc that reads data into an untyped `buffer` of `bufLen` size. result = s.readDataImpl(s, buffer, bufLen) proc writeData*(s: PStream, buffer: pointer, bufLen: int) = ## low level proc that writes an untyped `buffer` of `bufLen` size ## to the stream `s`. s.writeDataImpl(s, buffer, bufLen) proc writeData*(s, unused: PStream, buffer: pointer, bufLen: int) {.deprecated.} = ## low level proc that writes an untyped `buffer` of `bufLen` size ## to the stream `s`. s.writeDataImpl(s, buffer, bufLen) proc write*[T](s: PStream, x: T) = ## generic write procedure. Writes `x` to the stream `s`. Implementation: ## ## .. code-block:: Nimrod ## ## s.writeData(s, addr(x), sizeof(x)) var y: T shallowCopy(y, x) writeData(s, addr(y), sizeof(y)) proc write*(s: PStream, x: string) = ## writes the string `x` to the the stream `s`. No length field or ## terminating zero is written. writeData(s, cstring(x), x.len) proc writeln*(s: PStream, 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. for str in args: write(s, str) write(s, "\n") proc read[T](s: PStream, result: var T) = ## generic read procedure. Reads `result` from the stream `s`. if readData(s
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Democracy: Fundamentals (Unfinished)</title>
<link rel="stylesheet" href="/style.css" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<meta charset="utf-8" />
</head>
<body class="indent">
<h1>Democracy: Fundamentals (Unfinished)</h1>
<p>Article ID: 4</p>
<p