LineNumberReadStream.st
author Stefan Vogel <sv@exept.de>
Wed, 17 Oct 2012 18:45:18 +0200
changeset 2827 dd4e71ae9244
parent 2538 8e34a8db4002
child 3180 7c7ce3f9483e
permissions -rw-r--r--
added: #skip: changed: #position:

"
 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.
"
"{ Package: 'stx:libbasic2' }"

FilteringStream subclass:#LineNumberReadStream
	instanceVariableNames:'lineNumber lineStartPosition'
	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, and optionally
    the current line's start position (if the input stream is positionable).
    while passing text from its inputStream to its outputStream.

    Can be placed in-between text processing and the text's
    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"
!

lineStartPosition
    "return the current lines start position"

    ^ lineStartPosition
! !

!LineNumberReadStream methodsFor:'initialization'!

initialize
    super initialize.

    lineNumber := 1.
    (inputStream isPositionable) ifTrue:[
	lineStartPosition := inputStream position.
    ].
    filter := [:char |

		char == Character cr ifTrue:[
		    lineNumber := lineNumber + 1.
		    inputStream isPositionable ifTrue:[
			lineStartPosition := inputStream position.
		    ].
		].
		char
	      ].

    "Modified: 11.1.1997 / 16:58:42 / cg"
! !

!LineNumberReadStream methodsFor:'queries'!

atEnd
    "return true, if the receiver stream is at the end"

    readAhead notNil ifTrue:[^ false].
    ^ inputStream atEnd
!

peekOrNil
    "peek ahead for the next character, or return nil"

    readAhead notNil ifTrue:[
	^ readAhead
    ].
    ^ inputStream peekOrNil
!

position:aPosition
    readAhead := nil.
    inputStream position:aPosition
!

skip:numberToSkip
    readAhead := nil.
    inputStream skip:numberToSkip
! !

!LineNumberReadStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/LineNumberReadStream.st,v 1.9 2012-10-17 16:45:18 stefan Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/LineNumberReadStream.st,v 1.9 2012-10-17 16:45:18 stefan Exp $'
! !