From eed1000252444850523aa5ffe7d86782bd48a26a Mon Sep 17 00:00:00 2001 From: JamesP Date: Sat, 26 Sep 2015 08:18:09 +1000 Subject: [PATCH 1/4] add examples to top of module for stringStream and fileStream --- lib/pure/streams.nim | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 8aa8d35d88..3be47744ca 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -11,6 +11,26 @@ ## 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: +## +## .. code-block:: Nim +## +## 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() include "system/inclrtl" From ff9a3d39d76602c929fcadb453a2ae2ca79559bc Mon Sep 17 00:00:00 2001 From: JamesP Date: Sat, 26 Sep 2015 08:18:42 +1000 Subject: [PATCH 2/4] add default file mode to newFileStream() --- lib/pure/streams.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 3be47744ca..406a0ec6e8 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -391,7 +391,7 @@ when not defined(js): result.writeDataImpl = fsWriteData result.flushImpl = fsFlush - proc newFileStream*(filename: string, mode: FileMode): FileStream = + proc newFileStream*(filename: string, mode: FileMode = fmRead): FileStream = ## creates a new stream from the file named `filename` with the mode `mode`. ## If the file cannot be opened, nil is returned. See the `system ## `_ module for a list of available FileMode enums. From e0707797a52f68423b2cc9c287d6356366d41c56 Mon Sep 17 00:00:00 2001 From: JamesP Date: Sat, 26 Sep 2015 08:34:59 +1000 Subject: [PATCH 3/4] add test for newFileStream() opening a missing file --- tests/stdlib/tstreams2.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/stdlib/tstreams2.nim diff --git a/tests/stdlib/tstreams2.nim b/tests/stdlib/tstreams2.nim new file mode 100644 index 0000000000..b3e957261b --- /dev/null +++ b/tests/stdlib/tstreams2.nim @@ -0,0 +1,14 @@ +discard """ + test newFileStream opening a missing file returns nil + file: "tstreams2.nim" + output: 'fs is: nil' +""" +import streams +var + fs = newFileStream("amissingfile.txt") + line = "" +echo "fs is: ",repr(fs) +if not isNil(fs): + while fs.readLine(line): + echo line + fs.close() From 772c25bfe8424bb187690ee0959375cd5027d897 Mon Sep 17 00:00:00 2001 From: JamesP Date: Sun, 27 Sep 2015 15:49:40 +1000 Subject: [PATCH 4/4] fix discard output: section --- tests/stdlib/tstreams2.nim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/stdlib/tstreams2.nim b/tests/stdlib/tstreams2.nim index b3e957261b..90102d8e3a 100644 --- a/tests/stdlib/tstreams2.nim +++ b/tests/stdlib/tstreams2.nim @@ -1,7 +1,6 @@ discard """ - test newFileStream opening a missing file returns nil file: "tstreams2.nim" - output: 'fs is: nil' + output: '''fs is: nil''' """ import streams var