PeekableStream.st
author Claus Gittinger <cg@exept.de>
Sat, 25 Sep 1999 15:20:37 +0200
changeset 4814 7825b9141f01
parent 4432 5267a659f8d9
child 5394 f877659e09f7
permissions -rw-r--r--
changes to allow compilation under win32 (does not like strings beginning with a cr - how comes this ?)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     1
"
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1994 by Claus Gittinger
379
5b5a130ccd09 revision added
claus
parents: 369
diff changeset
     3
	      All Rights Reserved
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     4
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    11
"
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    12
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    13
Stream subclass:#PeekableStream
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    14
	instanceVariableNames:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    15
	classVariableNames:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    16
	poolDictionaries:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    17
	category:'Streams'
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    18
!
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    19
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    20
!PeekableStream class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    21
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
 COPYRIGHT (c) 1994 by Claus Gittinger
379
5b5a130ccd09 revision added
claus
parents: 369
diff changeset
    25
	      All Rights Reserved
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    26
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
!
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    35
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
    abstract superclass for all Stream which support read-ahead
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
    (i.e. peeking) of one element.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
    Concrete subclasses must implement a peek method.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    41
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    42
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
    43
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    44
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    45
! !
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    46
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
    47
!PeekableStream methodsFor:'positioning'!
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
    48
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    49
skipAny:skipCollection
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    50
    "skip all characters included in the argument-set.
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    51
     returns the next peeked element or nil, if the end-of-stream was reached."
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    52
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    53
    |nextOne|
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    54
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
    55
    nextOne := self peekOrNil.
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    56
    [nextOne notNil and:[skipCollection includes:nextOne]] whileTrue:[
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
    57
        self next.
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
    58
        nextOne := self peekOrNil
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    59
    ].
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    60
    ^ nextOne
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    61
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    62
    "
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    63
     |s skipChars|
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    64
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    65
     s := ReadStream on:'some numbers1234with\in other99 stuff' withCRs.
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    66
     skipChars := 'abcdefghijklmnopqrstuvwxyz\ ' withCRs.
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    67
     s skipAny:skipChars.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    68
     Transcript showCR:(Integer readFrom:s).
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    69
     s skipAny:skipChars.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    70
     Transcript showCR:(Integer readFrom:s).
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    71
    "
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    72
!
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
    73
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    74
skipSeparators
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    75
    "skip all whitespace; returns the next peeked element or
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    76
     nil, if the end-of-stream was reached.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    77
     The streams elements should be characters.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    78
     Notice: compare this method to skipSpaces"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    79
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    80
    |nextOne|
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    81
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
    82
    nextOne := self peekOrNil.
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    83
    [nextOne notNil and:[nextOne isSeparator]] whileTrue:[
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
    84
        self next.
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
    85
        nextOne := self peekOrNil
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    86
    ].
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    87
    ^ nextOne
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    88
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    89
    "
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    90
     |s|
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    91
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    92
     s := ReadStream on:'one      two\three' withCRs.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    93
     s skipSeparators.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    94
     Transcript showCR:(s nextWord).
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    95
     s skipSeparators.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    96
     Transcript showCR:(s nextWord).
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    97
     s skipSeparators.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    98
     Transcript showCR:(s next displayString).
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    99
    "
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   100
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   101
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   102
skipSeparatorsExceptCR
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   103
    "skip all whitespace except carriage return; returns the 
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   104
     next peeked element or nil, if the end-of-stream was reached.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   105
     The streams elements should be characters.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   106
     Notice: compare this method to skipSpaces and skipSeparators"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   107
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   108
    |nextOne|
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   109
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   110
    nextOne := self peekOrNil.
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   111
    [nextOne notNil 
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   112
     and:[nextOne isSeparator
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   113
     and:[nextOne ~~ Character cr]]] whileTrue:[
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   114
        self next.
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   115
        nextOne := self peekOrNil
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   116
    ].
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   117
    ^ nextOne
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   118
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   119
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   120
skipSpaces
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   121
    "skip all spaces; returns the next peeked element or
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   122
     nil, if the end-of-stream was reached.
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   123
     The streams elements should be characters.
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   124
     Notice: this one skips only spaces (i.e. no cr, tabs etc)
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   125
             usually, skipSeparators is what you want."
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   126
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   127
    |nextOne|
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   128
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   129
    nextOne := self peekOrNil.
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   130
    [nextOne notNil and:[nextOne == Character space]] whileTrue:[
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   131
        self next.
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   132
        nextOne := self peekOrNil
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   133
    ].
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   134
    ^ nextOne
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   135
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   136
    "
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   137
     |s|
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   138
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   139
     s := ReadStream on:'one      two\three' withCRs.
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   140
     s skipSpaces.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   141
     Transcript showCR:(s nextWord).
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   142
     s skipSpaces.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   143
     Transcript showCR:(s nextWord).
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   144
     s skipSpaces.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
   145
     Transcript showCR:(s next displayString).
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   146
    "
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   147
! !
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   148
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   149
!PeekableStream methodsFor:'reading'!
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   150
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   151
nextDecimalInteger
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   152
    "read the next integer in radix 10. Does NOT skip initial whitespace.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   153
     The streams elements should be characters.
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   154
     Be careful - this method returns 0 if not posiioned on a digit intitially
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   155
     or if the end of the stream is encountered."
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   156
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   157
    |nextOne value|
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   158
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   159
    nextOne := self peekOrNil.
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   160
    value := 0.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   161
    [nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   162
        value := (value * 10) + nextOne digitValue.
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   163
        self next.
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   164
        nextOne := self peekOrNil
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   165
    ].
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   166
    ^ value
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   167
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   168
    "
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   169
     |s|
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   170
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   171
     s := '1234 5678' readStream.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   172
     s nextDecimalInteger. 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   173
    "
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   174
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   175
    "
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   176
     |s|
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   177
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   178
     s := '1234 5678' readStream.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   179
     s nextDecimalInteger.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   180
     s skipSpaces.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   181
     s nextDecimalInteger. 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   182
    "
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   183
!
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   184
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   185
nextDelimited:terminator
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   186
    "return the contents of the receiver, up to the next terminator character. 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   187
     Doubled terminators indicate an embedded terminator character.  
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   188
     For example: 'this '' was a quote'. 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   189
     Start postioned before the initial terminator."
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   190
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   191
    | out ch |
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   192
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   193
    out := WriteStream on: (String new: 1000).
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   194
    self atEnd ifTrue: [^ ''].
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   195
    self next == terminator ifFalse: [self skip: -1].       "absorb initial terminator"
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   196
    [(ch := self next) == nil] whileFalse: [
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   197
        (ch == terminator) ifTrue: [
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   198
            self peek == terminator ifFalse: [
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   199
                ^ out contents  "terminator is not doubled; we're done!!"
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   200
            ].
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   201
            self next.  "skip doubled terminator"
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   202
        ].
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   203
        out nextPut: ch.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   204
    ].
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   205
    ^ out contents
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   206
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   207
    "
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   208
     ('*foo bar baz* more foo' readStream nextDelimited:$*) 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   209
     ('*foo bar **baz***' readStream nextDelimited:$*)   
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   210
    "
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   211
!
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   212
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   213
nextPeek
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   214
    "advance to next element and return the peeked element"
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   215
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   216
    self next.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   217
    ^ self peek
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   218
!
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   219
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   220
peek 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   221
    "return the next element of the stream without advancing (i.e.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   222
     the following send of next will return this element again.)
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   223
     - we do not know here how to do it, it must be redefined in subclass"
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   224
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   225
    ^ self subclassResponsibility
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   226
!
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   227
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   228
peekFor:anObject 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   229
    "if the next-to-be-read object is equal to the argument, anObject, read it
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   230
     and return true. Otherwise, leave the receiver unaffected and return false."
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   231
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   232
    self peek = anObject ifTrue:[
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   233
	self next.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   234
	^ true
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   235
    ].
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   236
    ^ false
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   237
!
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   238
2419
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   239
upToMatching:aBlock
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   240
    "Return the next elements up to but not including the next element
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   241
     for which aBlock returns true.
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   242
     The next read will return that matching element."
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   243
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   244
    |answerStream element|
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   245
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   246
    answerStream := WriteStream on:(self contentsSpecies new).
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   247
    [self atEnd] whileFalse: [
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   248
        element := self peek.
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   249
        (aBlock value:element) ifTrue: [^ answerStream contents].
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   250
        answerStream nextPut:element.
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   251
        self next.
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   252
    ].
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   253
    ^ answerStream contents
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   254
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   255
    "
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   256
     'hello world' readStream upToMatching:[:c | c isSeparator].
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   257
    "
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   258
    "
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   259
     |s|
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   260
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   261
     s := 'hello world' readStream.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   262
     s upToMatching:[:c | c isSeparator].
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   263
     s upToEnd
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   264
    "
2419
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   265
2421
0b8966d99a87 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2419
diff changeset
   266
    "Modified: 26.2.1997 / 12:20:57 / cg"
2419
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   267
!
13804903a785 added #upToMatching:
Claus Gittinger <cg@exept.de>
parents: 2060
diff changeset
   268
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   269
upToSeparator
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   270
    "Return the next elements up to but not including the next separator.
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   271
     The next read will return the separator.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   272
     If no separator is encountered, the contents up to the end is returned.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   273
     The elements are supposed to understand #isSeparator 
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   274
     (i.e. the receiver is supposed to be a character-stream)."
68
59faa75185ba *** empty log message ***
claus
parents: 58
diff changeset
   275
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   276
    ^ self upToMatching:[:ch | ch isSeparator]
2060
4f93a792ba9d commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   277
4f93a792ba9d commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   278
    "
4404
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   279
     'hello world' readStream upToSeparator  
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   280
     'helloworld' readStream upToSeparator   
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   281
     'helloworld' readStream upToSeparator   
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   282
     '' readStream upToSeparator   
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   283
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   284
     |s|
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   285
     s := 'hello world' readStream.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   286
     s upToSeparator.
417e6668f732 added #nextDelimited:
Claus Gittinger <cg@exept.de>
parents: 2421
diff changeset
   287
     s upToEnd  
2060
4f93a792ba9d commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   288
    "
4f93a792ba9d commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   289
4f93a792ba9d commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   290
    "Modified: 4.1.1997 / 23:38:05 / cg"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   291
! !
58
b5453a2ff4aa Initial revision
claus
parents:
diff changeset
   292
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   293
!PeekableStream class methodsFor:'documentation'!
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   294
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   295
version
4432
5267a659f8d9 avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents: 4404
diff changeset
   296
    ^ '$Header: /cvs/stx/stx/libbasic/PeekableStream.st,v 1.20 1999-07-22 16:29:18 cg Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   297
! !