summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-11-25 18:17:14 +0100
committerAraq <rumpf_a@web.de>2011-11-25 18:17:14 +0100
commit4b39ac5cbe42e50b3822323d7b111a43e829de6a (patch)
tree30623ffb6b3b3bf8e9173dd76829fdf050ccf363
parent02e8e9c3ea130882c50326ed83240e29eeffb854 (diff)
downloadNim-4b39ac5cbe42e50b3822323d7b111a43e829de6a.tar.gz
deprecated endOfFile and readLine
-rwxr-xr-xlib/pure/streams.nim26
-rwxr-xr-xlib/system.nim31
-rwxr-xr-xlib/system/sysio.nim19
-rwxr-xr-xtests/threads/threadex.nim8
-rwxr-xr-xtodo.txt2
-rwxr-xr-xweb/news.txt2
6 files changed, 59 insertions, 29 deletions
diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim
index 5e8926e6d..6da263d4a 100755
--- a/lib/pure/streams.nim
+++ b/lib/pure/streams.nim
@@ -43,9 +43,10 @@ proc close*(s, unused: PStream) {.deprecated.} =
   ## closes the stream `s`.
   s.closeImpl(s)
 
-proc atEnd*(s: PStream): bool =
+proc atEnd*(s: PStream): bool {.deprecated.} =
   ## checks if more data can be read from `f`. Returns true if all data has
   ## been read.
+  ## **Deprecated since version 0.8.14**: Because Posix supports it poorly.
   result = s.atEndImpl(s)
 
 proc atEnd*(s, unused: PStream): bool {.deprecated.} =
@@ -149,9 +150,10 @@ proc readStr*(s: PStream, length: int): TaintedString =
   var L = readData(s, addr(string(result)[0]), length)
   if L != length: setLen(result.string, L)
 
-proc readLine*(s: PStream): TaintedString =
+proc readLine*(s: PStream): TaintedString {.deprecated.} =
   ## Reads a line from a stream `s`. Note: This is not very efficient. Raises 
   ## `EIO` if an error occured.
+  ## **Deprecated since version 0.8.14**: Because Posix supports it poorly.
   result = TaintedString""
   while not atEnd(s): 
     var c = readChar(s)
@@ -161,6 +163,26 @@ proc readLine*(s: PStream): TaintedString =
     elif c == '\L' or c == '\0': break
     result.string.add(c)
 
+proc readLine*(s: PStream, line: var TaintedString): bool =
+  ## 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 ``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.
+  line.setLen(0)
+  while true:
+    var c = readChar(s)
+    if c == '\c': 
+      c = readChar(s)
+      break
+    elif c == '\L': break
+    elif c == '\0':
+      if line.len > 0: break
+      else: return false
+    line.string.add(c)
+  result = true
+
 type
   PStringStream* = ref TStringStream ## a stream that encapsulates a string
   TStringStream* = object of TStream
diff --git a/lib/system.nim b/lib/system.nim
index 545c8f821..25a69a1bf 100755
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1670,8 +1670,10 @@ when not defined(EcmaScript) and not defined(NimrodVM):
   proc Close*(f: TFile) {.importc: "fclose", nodecl.}
     ## Closes the file.
 
-  proc EndOfFile*(f: TFile): Bool
+  proc EndOfFile*(f: TFile): Bool {.deprecated.}
     ## Returns true iff `f` is at the end.
+    ## **Deprecated since version 0.8.14**: Because Posix supports it poorly.
+    
   proc readChar*(f: TFile): char {.importc: "fgetc", nodecl.}
     ## Reads a single character from the stream `f`. If the stream
     ## has no more characters, `EEndOfFile` is raised.
@@ -1700,10 +1702,21 @@ when not defined(EcmaScript) and not defined(NimrodVM):
   proc write*(f: TFile, a: openArray[string])
     ## Writes a value to the file `f`. May throw an IO exception.
 
-  proc readLine*(f: TFile): TaintedString
+  proc readLine*(f: TFile): TaintedString {.deprecated.}
     ## reads a line of text from the file `f`. 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.
+    ##
+    ## **Deprecated since 0.8.14**: Use the `readLine` that takes a ``var``
+    ## parameter instead.
+  
+  proc readLine*(f: TFile, line: var TaintedString): bool
+    ## reads a line of text from the file `f` 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.
 
   proc writeln*[Ty](f: TFile, x: Ty) {.inline.}
     ## writes a value `x` to `f` and then writes "\n".
@@ -1843,18 +1856,14 @@ when not defined(EcmaScript) and not defined(NimrodVM):
     ## Iterate over any line in the file named `filename`.
     ## If the file does not exist `EIO` is raised.
     var f = open(filename)
-    var res = ""
-    while not endOfFile(f):
-      rawReadLine(f, res)
-      yield TaintedString(res)
-    Close(f)
+    var res = TaintedString(newStringOfCap(80))
+    while f.readLine(res): yield res
+    close(f)
 
   iterator lines*(f: TFile): TaintedString =
     ## Iterate over any line in the file `f`.
-    var res = ""
-    while not endOfFile(f):
-      rawReadLine(f, res)
-      yield TaintedString(res)
+    var res = TaintedString(newStringOfCap(80))
+    while f.readLine(res): yield TaintedString(res)
 
   include "system/assign"
   include "system/repr"
diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim
index 313d9fd95..adf9256fe 100755
--- a/lib/system/sysio.nim
+++ b/lib/system/sysio.nim
@@ -39,29 +39,26 @@ var
 proc raiseEIO(msg: string) {.noinline, noreturn.} =
   raise newException(EIO, msg)
 
-proc rawReadLine(f: TFile, result: var string) =
+proc readLine(f: TFile, line: var TaintedString): bool =
   # of course this could be optimized a bit; but IO is slow anyway...
   # and it was difficult to get this CORRECT with Ansi C's methods
-  setLen(result, 0) # reuse the buffer!
+  setLen(line, 0) # reuse the buffer!
   while True:
     var c = fgetc(f)
     if c < 0'i32:
-      if result.len > 0: break
-      else: raiseEIO("EOF reached")
+      if line.len > 0: break
+      else: return false
     if c == 10'i32: break # LF
     if c == 13'i32:  # CR
       c = fgetc(f) # is the next char LF?
       if c != 10'i32: ungetc(c, f) # no, put the character back
       break
-    add result, chr(int(c))
+    add line.string, chr(int(c))
+  result = true
 
 proc readLine(f: TFile): TaintedString =
-  when taintMode:
-    result = TaintedString""
-    rawReadLine(f, result.string)
-  else:
-    result = ""
-    rawReadLine(f, result)
+  result = TaintedString(newStringOfCap(80))
+  if not readLine(f, result): raiseEIO("EOF reached")
 
 proc write(f: TFile, i: int) = 
   when sizeof(int) == 8:
diff --git a/tests/threads/threadex.nim b/tests/threads/threadex.nim
index 967789ba6..0360c8c04 100755
--- a/tests/threads/threadex.nim
+++ b/tests/threads/threadex.nim
@@ -25,10 +25,10 @@ proc consume() {.thread.} =
 proc produce() {.thread.} =
   var m: TMsg
   var input = open("readme.txt")
-  while not endOfFile(input):
-    if chan.ready:
-      m.data = input.readLine()
-      chan.send(m)
+  var line = ""
+  while input.readLine(line):
+    m.data = line
+    chan.send(m)
   close(input)
   m.k = mEof
   chan.send(m)
diff --git a/todo.txt b/todo.txt
index 10f525ed0..49658c670 100755
--- a/todo.txt
+++ b/todo.txt
@@ -1,7 +1,7 @@
 version 0.8.14
 ==============
 
-- deprecate endOfFile and readline
+- stdlib and compiler should not use deprecated endOfFile and readline
 
 version 0.9.0
 =============
diff --git a/web/news.txt b/web/news.txt
index cfc6fcb15..1affafc1e 100755
--- a/web/news.txt
+++ b/web/news.txt
@@ -52,6 +52,8 @@ Changes affecting backwards compatibility
   because they should not be used directly anymore. 
   Wrapper procs have been created that should be used instead.
 - ``export`` is now a keyword.
+- ``system.endOfFile``, ``system.readLine`` and their stream equivalents
+  are deprecated because Posix supports it poorly.
 
 
 Language Additions