WriteStream.st
branchjv
changeset 18066 89d51443ba6f
parent 18057 8da7c39a6322
parent 15354 21ebb380b376
child 18071 009cf668b0ed
equal deleted inserted replaced
18065:1d9323e0a535 18066:89d51443ba6f
   118      (where things are written for the contents only) but may be incompatible
   118      (where things are written for the contents only) but may be incompatible
   119      with some applications. Time will show, if this is to be changed."
   119      with some applications. Time will show, if this is to be changed."
   120 
   120 
   121     |lastIndex|
   121     |lastIndex|
   122 
   122 
   123     lastIndex := position - ZeroPosition.
   123     lastIndex := position.
   124     collection size == lastIndex ifFalse:[
   124     collection size == lastIndex ifFalse:[
   125 	collection isFixedSize ifTrue:[
   125         collection isFixedSize ifTrue:[
   126 	    "
   126             "
   127 	     grow is expensive - return a copy.
   127              grow is expensive - return a copy.
   128 	     (is this what users of writeStream expect ?)
   128              (is this what users of writeStream expect ?)
   129 	    "
   129             "
   130 	    collection := collection copyFrom:1 to:lastIndex
   130             collection := collection copyFrom:1 to:lastIndex
   131 	] ifFalse:[
   131         ] ifFalse:[
   132 	    collection grow:lastIndex
   132             collection grow:lastIndex
   133 	]
   133         ]
   134     ].
   134     ].
   135     ^ collection
   135     ^ collection
   136 
   136 
   137     "Modified: / 19.2.1997 / 08:57:28 / stefan"
   137     "Modified: / 19.2.1997 / 08:57:28 / stefan"
   138     "Modified: / 30.10.1997 / 16:21:23 / cg"
   138     "Modified: / 30.10.1997 / 16:21:23 / cg"
   141 last
   141 last
   142     "return the last element - report an error if the stream is empty"
   142     "return the last element - report an error if the stream is empty"
   143 
   143 
   144     |position1Based|
   144     |position1Based|
   145 
   145 
   146     position1Based := position - ZeroPosition + 1.
   146     position1Based := position + 1.
   147     ^ collection at:(position1Based - 1).
   147     ^ collection at:(position1Based - 1).
   148 
   148 
   149     "
   149     "
   150      |s|
   150      |s|
   151 
   151 
   163     "return the last n elements as species of the underlying collection;
   163     "return the last n elements as species of the underlying collection;
   164      Report an error if the stream is empty"
   164      Report an error if the stream is empty"
   165 
   165 
   166     |position1Based|
   166     |position1Based|
   167 
   167 
   168     position1Based := position - ZeroPosition + 1.
   168     position1Based := position + 1.
   169     ^ collection copyFrom:(position1Based - n) to:(position1Based - 1).
   169     ^ collection copyFrom:(position1Based - n) to:(position1Based - 1).
   170 
   170 
   171     "
   171     "
   172      |s|
   172      |s|
   173 
   173 
   198 
   198 
   199 position0Based:index0Based
   199 position0Based:index0Based
   200     "redefined to allow positioning past the readLimit"
   200     "redefined to allow positioning past the readLimit"
   201 
   201 
   202     ((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError].
   202     ((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError].
   203     position := index0Based + ZeroPosition
   203     position := index0Based
   204 ! !
   204 ! !
   205 
   205 
   206 !WriteStream methodsFor:'private'!
   206 !WriteStream methodsFor:'private'!
   207 
   207 
   208 growCollection
   208 growCollection
   271 !
   271 !
   272 
   272 
   273 size
   273 size
   274     "return the current size"
   274     "return the current size"
   275 
   275 
   276     ^ position - ZeroPosition
   276     ^ position
   277 ! !
   277 ! !
   278 
   278 
   279 !WriteStream methodsFor:'reading'!
   279 !WriteStream methodsFor:'reading'!
   280 
   280 
   281 next
   281 next
   293 !WriteStream methodsFor:'testing'!
   293 !WriteStream methodsFor:'testing'!
   294 
   294 
   295 isEmpty
   295 isEmpty
   296     "return true, if the contents of the stream is empty"
   296     "return true, if the contents of the stream is empty"
   297 
   297 
   298     ^ self position0Based == ZeroPosition
   298     ^ self position0Based == 0
   299 
   299 
   300     "Created: 14.10.1997 / 20:44:37 / cg"
   300     "Created: 14.10.1997 / 20:44:37 / cg"
   301 ! !
   301 ! !
   302 
   302 
   303 !WriteStream methodsFor:'writing'!
   303 !WriteStream methodsFor:'writing'!
   312     (collection isNil or:[writeLimit notNil]) ifTrue:[
   312     (collection isNil or:[writeLimit notNil]) ifTrue:[
   313         super next:count put:anObject.
   313         super next:count put:anObject.
   314         ^ self.
   314         ^ self.
   315     ].
   315     ].
   316 
   316 
   317     position1Based := position - ZeroPosition + 1.
   317     position1Based := position + 1.
   318     final := position1Based + count - 1.
   318     final := position1Based + count - 1.
   319     (final > collection size) ifTrue:[
   319     (final > collection size) ifTrue:[
   320         self growCollection:final
   320         self growCollection:final
   321     ].
   321     ].
   322 
   322 
   323     position1Based to:final do:[:index |
   323     position1Based to:final do:[:index |
   324         collection at:index put:anObject.
   324         collection at:index put:anObject.
   325     ].
   325     ].
   326     position1Based := position1Based + count.
   326     position1Based := position1Based + count.
   327     (position1Based > readLimit) ifTrue:[readLimit := position1Based - 1].
   327     (position1Based > readLimit) ifTrue:[readLimit := position1Based - 1].
   328     position := position1Based - 1 + ZeroPosition.
   328     position := position1Based - 1.
   329     "/ ^ anObject -- return self
   329     "/ ^ anObject -- return self
   330 !
   330 !
   331 
   331 
   332 next:n putAll:aCollection startingAt:pos1
   332 next:n putAll:aCollection startingAt:pos1
   333     "append some elements of the argument, aCollection to the stream."
   333     "append some elements of the argument, aCollection to the stream."
   363     p = __INST(position);
   363     p = __INST(position);
   364 
   364 
   365     if (__isNonNilObject(coll) && __isSmallInteger(p)) {
   365     if (__isNonNilObject(coll) && __isSmallInteger(p)) {
   366         pos = __intVal(p);
   366         pos = __intVal(p);
   367         /* make 1-based */
   367         /* make 1-based */
   368         pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
   368         pos = pos + 1;
   369         wL = __INST(writeLimit);
   369         wL = __INST(writeLimit);
   370 
   370 
   371         if ((wL == nil)
   371         if ((wL == nil)
   372          || (__isSmallInteger(wL) && (pos <= __intVal(wL)))) {
   372          || (__isSmallInteger(wL) && (pos <= __intVal(wL)))) {
   373             OBJ cls;
   373             OBJ cls;
   421         }
   421         }
   422     }
   422     }
   423 #endif
   423 #endif
   424 %}.
   424 %}.
   425     (writeLimit isNil
   425     (writeLimit isNil
   426     or:[(position + 1 - ZeroPosition) <= writeLimit]) ifTrue:[
   426     or:[(position + 1) <= writeLimit]) ifTrue:[
   427         ((position + 1 - ZeroPosition) > collection size) ifTrue:[self growCollection].
   427         (position >= collection size) ifTrue:[self growCollection].
   428         collection at:(position + 1 - ZeroPosition) put:anObject.
   428         collection at:(position + 1) put:anObject.
   429         ((position + 1 - ZeroPosition) > readLimit) ifTrue:[readLimit := (position + 1 - ZeroPosition)].
   429         (position >= readLimit) ifTrue:[readLimit := (position + 1)].
   430         position := position + 1.
   430         position := position + 1.
   431     ] ifFalse:[
   431     ] ifFalse:[
   432         WriteError raiseErrorString:'write beyond writeLimit'
   432         WriteError raiseErrorString:'write beyond writeLimit'
   433     ].
   433     ].
   434     ^anObject
   434     ^anObject
   448     aCollection isSequenceable ifFalse:[
   448     aCollection isSequenceable ifFalse:[
   449         super nextPutAll:aCollection.
   449         super nextPutAll:aCollection.
   450         ^ self.
   450         ^ self.
   451     ].
   451     ].
   452 
   452 
   453     position1Based := position - ZeroPosition + 1.
   453     position1Based := position + 1.
   454 
   454 
   455     nMore := aCollection size.
   455     nMore := aCollection size.
   456     nMore == 0 ifTrue:[
   456     nMore == 0 ifTrue:[
   457         "/ for the programmer..
   457         "/ for the programmer..
   458         aCollection isCollection ifFalse:[
   458         aCollection isCollection ifFalse:[
   475         with:aCollection 
   475         with:aCollection 
   476         startingAt:1.
   476         startingAt:1.
   477 
   477 
   478     position1Based := position1Based + nMore.
   478     position1Based := position1Based + nMore.
   479     (position1Based > readLimit) ifTrue:[readLimit := position1Based - 1].
   479     (position1Based > readLimit) ifTrue:[readLimit := position1Based - 1].
   480     position := position1Based - 1 + ZeroPosition.
   480     position := position1Based - 1.
   481     "/ ^ aCollection -- self
   481     "/ ^ aCollection -- self
   482 
   482 
   483     "Modified: / 04-09-2011 / 20:03:32 / cg"
   483     "Modified: / 04-09-2011 / 20:03:32 / cg"
   484 !
   484 !
   485 
   485 
   492 
   492 
   493     collection isNil ifTrue:[
   493     collection isNil ifTrue:[
   494         ^ super nextPutAll:aCollection startingAt:pos1 to:pos2
   494         ^ super nextPutAll:aCollection startingAt:pos1 to:pos2
   495     ].
   495     ].
   496 
   496 
   497     position1Based := position - ZeroPosition + 1.
   497     position1Based := position + 1.
   498     nMore := pos2 - pos1 + 1.
   498     nMore := pos2 - pos1 + 1.
   499     final := position1Based + nMore - 1.
   499     final := position1Based + nMore - 1.
   500     (writeLimit notNil
   500     (writeLimit notNil
   501     and:[final > writeLimit]) ifTrue:[
   501     and:[final > writeLimit]) ifTrue:[
   502         final := writeLimit.
   502         final := writeLimit.
   511                       with:aCollection 
   511                       with:aCollection 
   512                 startingAt:pos1.
   512                 startingAt:pos1.
   513 
   513 
   514     position1Based := position1Based + nMore.
   514     position1Based := position1Based + nMore.
   515     (position1Based > readLimit) ifTrue:[readLimit := position1Based - 1].
   515     (position1Based > readLimit) ifTrue:[readLimit := position1Based - 1].
   516     position := position1Based - 1 + ZeroPosition.
   516     position := position1Based - 1.
   517     "/ ^ aCollection -- return self
   517     "/ ^ aCollection -- return self
   518 
   518 
   519     "
   519     "
   520      |s|
   520      |s|
   521 
   521 
   630 ! !
   630 ! !
   631 
   631 
   632 !WriteStream class methodsFor:'documentation'!
   632 !WriteStream class methodsFor:'documentation'!
   633 
   633 
   634 version
   634 version
   635     ^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.76 2013-04-26 14:11:10 cg Exp $'
   635     ^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.78 2013-06-03 18:39:07 cg Exp $'
   636 !
   636 !
   637 
   637 
   638 version_CVS
   638 version_CVS
   639     ^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.76 2013-04-26 14:11:10 cg Exp $'
   639     ^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.78 2013-06-03 18:39:07 cg Exp $'
   640 ! !
   640 ! !
   641 
   641