WriteStream.st
author Claus Gittinger <cg@exept.de>
Tue, 19 Aug 1997 18:08:22 +0200
changeset 2863 aec08a0c2c2e
parent 2410 eea60bba0412
child 2864 328b52ecd339
permissions -rw-r--r--
no need for unsigned compare against 0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
252
claus
parents: 95
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
PositionableStream subclass:#WriteStream
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1136
diff changeset
    14
	instanceVariableNames:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1136
diff changeset
    15
	classVariableNames:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1136
diff changeset
    16
	poolDictionaries:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1136
diff changeset
    17
	category:'Streams'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
1965
4ca38574df3b Fix typo.
Stefan Vogel <sv@exept.de>
parents: 1584
diff changeset
    20
!WriteStream class methodsFor:'documentation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
88
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) 1989 by Claus Gittinger
252
claus
parents: 95
diff changeset
    25
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
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
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
68
59faa75185ba *** empty log message ***
claus
parents: 63
diff changeset
    36
documentation
59faa75185ba *** empty log message ***
claus
parents: 63
diff changeset
    37
"
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
    Streams for writing into - this implementation currently DOES change the 
261
afdf1e7184ad *** empty log message ***
claus
parents: 260
diff changeset
    39
    identity if the streamed-upon collection IF it cannot grow easily. 
afdf1e7184ad *** empty log message ***
claus
parents: 260
diff changeset
    40
    Collections which cannot grow easily are for example: Array, ByteArray and String.
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
    Thus it is slightly incompatible to ST-80 since 'aStream contents' does 
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    42
    not always return the original collection. This may change.
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1136
diff changeset
    43
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1136
diff changeset
    44
    [author:]
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1136
diff changeset
    45
        Claus Gittinger
68
59faa75185ba *** empty log message ***
claus
parents: 63
diff changeset
    46
"
345
claus
parents: 329
diff changeset
    47
! !
claus
parents: 329
diff changeset
    48
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    49
!WriteStream methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    50
a27a279701f8 Initial revision
claus
parents:
diff changeset
    51
contents
345
claus
parents: 329
diff changeset
    52
    "return the current contents (a collection) of the stream.
claus
parents: 329
diff changeset
    53
     Currently, this returns the actual collection if possible
claus
parents: 329
diff changeset
    54
     (and reset is implemented to create a new one) in contrast to
claus
parents: 329
diff changeset
    55
     ST80, where contents returns a copy and reset only sets the writePointer.
claus
parents: 329
diff changeset
    56
     The ST/X behavior creates less temporary garbage in the normal case
2410
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    57
     (where things are written for the contents only) but may be incompatible
345
claus
parents: 329
diff changeset
    58
     with some applications. Time will show, if this is to be changed."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    59
260
cefb485445a7 *** empty log message ***
claus
parents: 252
diff changeset
    60
    |lastIndex|
cefb485445a7 *** empty log message ***
claus
parents: 252
diff changeset
    61
cefb485445a7 *** empty log message ***
claus
parents: 252
diff changeset
    62
    lastIndex := position - 1.
cefb485445a7 *** empty log message ***
claus
parents: 252
diff changeset
    63
    collection size == lastIndex ifFalse:[
2410
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    64
        collection isFixedSize ifTrue:[
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    65
            "
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    66
             grow is expensive - return a copy.
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    67
             (is this what users of writeStream expect ?
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    68
            "
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    69
            collection := collection copyFrom:1 to:lastIndex
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    70
        ] ifFalse:[
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    71
            collection grow:lastIndex
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    72
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
    ^ collection
2410
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    75
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    76
    "Modified: 19.2.1997 / 08:57:28 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
329
claus
parents: 293
diff changeset
    79
reset
345
claus
parents: 329
diff changeset
    80
    "reset the stream; write anew.
2410
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    81
     See the comment in WriteStream>>contents"
329
claus
parents: 293
diff changeset
    82
claus
parents: 293
diff changeset
    83
    collection := collection species new:(collection size).
claus
parents: 293
diff changeset
    84
    super reset
2410
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    85
eea60bba0412 Fix typos.
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    86
    "Modified: 19.2.1997 / 08:57:00 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    89
!WriteStream methodsFor:'positioning'!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    90
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    91
position:index
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    92
    "redefined to allow positioning past the readLimit"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    93
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    94
    ((index > (collection size + 1)) or:[index < 0]) ifTrue: [^ self positionError].
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    95
    position := index
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    96
! !
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    97
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    98
!WriteStream methodsFor:'private'!
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
growCollection
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   101
    "grow the streamed collection to at least 10 elements"
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   102
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   103
    self growCollection:10
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   104
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   105
    "Modified: 19.8.1997 / 17:53:28 / cg"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   106
!
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
growCollection:minNewSize
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   109
    "grow the streamed collection to at least minNewSize"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   110
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   111
    |oldSize newSize newColl|
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   112
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   113
    oldSize := collection size.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   114
    (oldSize == 0) ifTrue:[
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   115
        newSize := minNewSize
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   116
    ] ifFalse:[
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   117
        newSize := oldSize * 2.
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   118
        (newSize < minNewSize) ifTrue:[newSize := minNewSize].
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   119
    ].
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   120
    collection isFixedSize ifTrue:[
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   121
        newColl := collection species new:newSize.
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   122
        newColl replaceFrom:1 to:oldSize with:collection startingAt:1.
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   123
        collection := newColl
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   124
    ] ifFalse:[
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   125
        collection grow:newSize
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   126
    ].
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   127
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   128
    "Modified: 19.8.1997 / 17:53:11 / cg"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   129
! !
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   130
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   131
!WriteStream methodsFor:'private accessing'!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   132
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   133
on:aCollection from:start to:last
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   134
    "create and return a new stream for writing onto aCollection, where
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   135
     writing is limited to the elements in the range start to last."
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   136
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   137
    super on:aCollection from:start to:last.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   138
    writeLimit := last.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   139
! !
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   140
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   141
!WriteStream methodsFor:'queries'!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   142
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   143
isWritable
1405
f4ab59f3888a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1398
diff changeset
   144
    "return true, if writing is supported by the recevier.
f4ab59f3888a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1398
diff changeset
   145
     Always return true here"
f4ab59f3888a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1398
diff changeset
   146
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   147
    ^ true
1405
f4ab59f3888a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1398
diff changeset
   148
f4ab59f3888a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1398
diff changeset
   149
    "Modified: 16.5.1996 / 14:44:49 / cg"
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
! !
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   151
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   152
!WriteStream methodsFor:'reading'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   153
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
next
10
claus
parents: 5
diff changeset
   155
    "catch read access to write stream - report an error"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
10
claus
parents: 5
diff changeset
   157
    self shouldNotImplement
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   158
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
a27a279701f8 Initial revision
claus
parents:
diff changeset
   160
peek
10
claus
parents: 5
diff changeset
   161
    "catch read access to write stream - report an error"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   162
10
claus
parents: 5
diff changeset
   163
    self shouldNotImplement
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   164
! !
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   165
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   166
!WriteStream methodsFor:'writing'!
369
claus
parents: 359
diff changeset
   167
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   168
next:count put:anObject
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   169
    "append anObject count times to the receiver.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   170
     Redefined to avoid count grows of the underlying collection -
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   171
     instead a single grow on the final size is performed."
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   172
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   173
    |final|
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   174
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   175
    (collection isNil or:[writeLimit notNil]) ifTrue:[
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   176
	^ super next:count put:anObject
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   177
    ].
369
claus
parents: 359
diff changeset
   178
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   179
    final := position + count - 1.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   180
    (final > collection size) ifTrue:[
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   181
	self growCollection:final
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   182
    ].
369
claus
parents: 359
diff changeset
   183
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   184
    position to:final do:[:index |
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   185
	collection at:index put:anObject.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   186
    ].
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   187
    position := position + count.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   188
    (position > readLimit) ifTrue:[readLimit := position - 1].
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   189
    ^ anObject
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   190
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   191
a27a279701f8 Initial revision
claus
parents:
diff changeset
   192
nextPut:anObject
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   193
    "append the argument, anObject to the stream.
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   194
     Specially tuned for appending to String, ByteArray and Array streams."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   195
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   196
%{  /* NOCONTEXT */
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   197
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   198
    REGISTER int pos;
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   199
    unsigned ch;
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   200
    OBJ coll;
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   201
    OBJ p, l, rL;
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   202
    int __readLimit = -1;
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   203
1136
898af060dfde underline cleanup
Claus Gittinger <cg@exept.de>
parents: 838
diff changeset
   204
    coll = __INST(collection);
898af060dfde underline cleanup
Claus Gittinger <cg@exept.de>
parents: 838
diff changeset
   205
    p = __INST(position);
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   206
293
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   207
    if (__isNonNilObject(coll) && __isSmallInteger(p)) {
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   208
        pos = __intVal(p);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   209
        l = __INST(writeLimit);
68
59faa75185ba *** empty log message ***
claus
parents: 63
diff changeset
   210
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   211
        if ((l == nil)
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   212
         || (__isSmallInteger(l) && (pos <= __intVal(l)))) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   213
            OBJ cls;
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   214
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   215
            cls = __qClass(coll);
252
claus
parents: 95
diff changeset
   216
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   217
            rL = __INST(readLimit);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   218
            if (__isSmallInteger(rL)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   219
                __readLimit = __intVal(rL);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   220
            }
293
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   221
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   222
            if (cls == @global(String)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   223
                if (__isCharacter(anObject) 
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   224
                 && (pos <= __stringSize(coll))) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   225
                    ch = __intVal(_characterVal(anObject));
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   226
                    if (ch <= 255) {
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   227
                        __StringInstPtr(coll)->s_element[pos-1] = ch;
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   228
                        __INST(position) = __MKSMALLINT(pos + 1);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   229
                        if ((__readLimit >= 0) && (pos >= __readLimit)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   230
                            __INST(readLimit) = __MKSMALLINT(pos);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   231
                        }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   232
                        RETURN ( anObject );
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   233
                    }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   234
                }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   235
            } else if (cls == @global(ByteArray)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   236
                if (__isSmallInteger(anObject) 
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   237
                 && ((ch = __intVal(anObject)) >= 0)
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   238
                 && (ch <= 255)
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   239
                 && (pos <= _byteArraySize(coll))) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   240
                    __ByteArrayInstPtr(coll)->ba_element[pos-1] = ch;
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   241
                    __INST(position) = __MKSMALLINT(pos + 1);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   242
                    if ((__readLimit >= 0) && (pos >= __readLimit)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   243
                        __INST(readLimit) = __MKSMALLINT(pos);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   244
                    }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   245
                    RETURN ( anObject );
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   246
                }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   247
            } else if (cls == @global(Array)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   248
                if (pos <= _arraySize(coll)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   249
                     __ArrayInstPtr(coll)->a_element[pos-1] = anObject;
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   250
                    __STORE(coll, anObject);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   251
                    __INST(position) = __MKSMALLINT(pos + 1);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   252
                    if ((__readLimit >= 0) && (pos >= __readLimit)) {
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   253
                        __INST(readLimit) = __MKSMALLINT(pos);
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   254
                    }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   255
                    RETURN ( anObject );
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   256
                }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   257
            }
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   258
        }
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   259
    }
260
cefb485445a7 *** empty log message ***
claus
parents: 252
diff changeset
   260
%}.
293
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   261
    (writeLimit isNil
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   262
    or:[position <= writeLimit]) ifTrue:[
1584
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   263
        (position > collection size) ifTrue:[self growCollection].
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   264
        collection at:position put:anObject.
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   265
        (position > readLimit) ifTrue:[readLimit := position].
cad4f2c515c7 nextPut: slightly modified
Claus Gittinger <cg@exept.de>
parents: 1541
diff changeset
   266
        position := position + 1.
838
67a53cceaab1 no global refs
Claus Gittinger <cg@exept.de>
parents: 701
diff changeset
   267
    ] ifFalse:[
1965
4ca38574df3b Fix typo.
Stefan Vogel <sv@exept.de>
parents: 1584
diff changeset
   268
        WriteErrorSignal raiseErrorString:'write beyond writeLimit'
293
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   269
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   270
    ^anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   271
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   272
a27a279701f8 Initial revision
claus
parents:
diff changeset
   273
nextPutAll:aCollection
63
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   274
    "append all elements of the argument, aCollection to the stream.
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   275
     Redefined to avoid count grows of the underlying collection -
1f0cdefb013f *** empty log message ***
claus
parents: 10
diff changeset
   276
     instead a single grow on the final size is performed."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   277
a27a279701f8 Initial revision
claus
parents:
diff changeset
   278
    |nMore final|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   279
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   280
    collection isNil ifTrue:[
260
cefb485445a7 *** empty log message ***
claus
parents: 252
diff changeset
   281
	^ super nextPutAll:aCollection
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   282
    ].
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   283
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   284
    nMore := aCollection size.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   285
    final := position + nMore - 1.
293
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   286
    (writeLimit notNil
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   287
    and:[final > writeLimit]) ifTrue:[
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   288
	final := writeLimit.
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   289
	nMore := final - position + 1
31df3850e98c *** empty log message ***
claus
parents: 261
diff changeset
   290
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   291
    (final > collection size) ifTrue:[
252
claus
parents: 95
diff changeset
   292
	self growCollection:final
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   293
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   294
    collection replaceFrom:position
252
claus
parents: 95
diff changeset
   295
			to:final
claus
parents: 95
diff changeset
   296
		      with:aCollection 
claus
parents: 95
diff changeset
   297
		startingAt:1.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   298
a27a279701f8 Initial revision
claus
parents:
diff changeset
   299
    position := position + nMore.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   300
    (position > readLimit) ifTrue:[readLimit := position - 1].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   301
    ^ aCollection
1539
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   302
!
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   303
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   304
nextPutAll:aCollection startingAt:pos1 to:pos2
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   305
    "append some elements of the argument, aCollection to the stream.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   306
     Redefined to avoid count grows of the underlying collection -
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   307
     instead a single grow on the final size is performed."
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   308
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   309
    |nMore final|
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   310
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   311
    collection isNil ifTrue:[
1541
6ed5feddc4ed checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1539
diff changeset
   312
        ^ super nextPutAll:aCollection startingAt:pos1 to:pos2
1539
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   313
    ].
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   314
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   315
    nMore := pos2 - pos1 + 1.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   316
    final := position + nMore - 1.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   317
    (writeLimit notNil
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   318
    and:[final > writeLimit]) ifTrue:[
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   319
        final := writeLimit.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   320
        nMore := final - position + 1
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   321
    ].
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   322
    (final > collection size) ifTrue:[
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   323
        self growCollection:final
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   324
    ].
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   325
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   326
    collection replaceFrom:position
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   327
                        to:final
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   328
                      with:aCollection 
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   329
                startingAt:pos1.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   330
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   331
    position := position + nMore.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   332
    (position > readLimit) ifTrue:[readLimit := position - 1].
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   333
    ^ aCollection
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   334
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   335
    "
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   336
     |s|
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   337
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   338
     s := '' writeStream.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   339
     s nextPutAll:'hello '.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   340
     s nextPutAll:'1234world012345' startingAt:5 to:9.
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   341
     s contents
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   342
    "
b1ac096a81e3 added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents: 1405
diff changeset
   343
1541
6ed5feddc4ed checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1539
diff changeset
   344
    "Modified: 12.7.1996 / 10:31:36 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   345
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   346
1965
4ca38574df3b Fix typo.
Stefan Vogel <sv@exept.de>
parents: 1584
diff changeset
   347
!WriteStream class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   348
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   349
version
2863
aec08a0c2c2e no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents: 2410
diff changeset
   350
    ^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.34 1997-08-19 16:08:22 cg Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   351
! !