LineNumberReadStream.st
changeset 478 778953b40429
child 885 c31412b26306
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LineNumberReadStream.st	Sat Jan 11 19:54:37 1997 +0100
@@ -0,0 +1,134 @@
+"
+ COPYRIGHT (c) 1996 by eXept Software AG
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+
+
+FilteringStream subclass:#LineNumberReadStream
+	instanceVariableNames:'lineNumber'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Streams-Misc'
+!
+
+!LineNumberReadStream class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1996 by eXept Software AG
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+
+!
+
+documentation
+"
+    This filter keeps track of the current line, while
+    passing text from its inputStream to its outputStream.
+
+    Can be placed in-between text processing and the texts
+    input stream, and let it keep track of the lineNumber.
+
+    [see also:]
+        ReadStream WriteStream ExternalStream FileStream
+        FilteringStream FilteringLineStream
+
+    [author:]
+        Claus Gittinger
+"
+!
+
+examples
+"
+  count lines in a Makefile:
+                                                                        [exBegin]
+    |in filter|
+
+    in := 'Makefile' asFilename readStream.
+
+    filter := LineNumberReadStream readingFrom:in.
+
+    filter upToEnd.
+    filter close.
+
+    Transcript showCR:('Makefile has %1 lines' bindWith:(filter lineNumber - 1)).
+                                                                        [exEnd]
+
+
+
+  find a specific string and output its lineNr:
+                                                                        [exBegin]
+    |in filter|
+
+    in := 'Makefile' asFilename readStream.
+
+    filter := LineNumberReadStream readingFrom:in.
+
+    filter skipThroughAll:'start of'.
+    filter close.
+
+    Transcript showCR:('found in line %1' bindWith:(filter lineNumber)).
+                                                                        [exEnd]
+"
+
+
+! !
+
+!LineNumberReadStream methodsFor:'accessing'!
+
+lineNumber
+    "return the current lineNumber"
+
+    ^ lineNumber
+
+    "Created: 11.1.1997 / 16:57:10 / cg"
+    "Modified: 11.1.1997 / 16:57:19 / cg"
+!
+
+lineNumber:something
+    "set the current lineNumber"
+
+    lineNumber := something.
+
+    "Created: 11.1.1997 / 16:57:10 / cg"
+    "Modified: 11.1.1997 / 16:57:22 / cg"
+! !
+
+!LineNumberReadStream methodsFor:'initialization'!
+
+initialize
+    super initialize.
+
+    lineNumber := 1.
+    filter := [:char |
+
+                char == Character cr ifTrue:[
+                    lineNumber := lineNumber + 1.
+                ].
+                char
+              ].
+
+    "Modified: 11.1.1997 / 16:58:42 / cg"
+! !
+
+!LineNumberReadStream class methodsFor:'documentation'!
+
+version
+    ^ '$Header: /cvs/stx/stx/libbasic2/LineNumberReadStream.st,v 1.1 1997-01-11 18:54:37 cg Exp $'
+! !