EndOfStream an Error???

Stewart MacLean stingray at paradise.net.nz
Wed May 24 23:55:14 UTC 2000


For what it's worth here's my take on this (sorry to be late to the party)...

Comming from an old fashioned COBOL environment I use (and still use) a method called JSP (Jackson Structured Programming) which is based around treating file input/output as datastreams. This technique is equally applicable when doing stream based programming.

Serial files have a physical end of file marker. The recognition of this physical end of file marker is dependent on the input/output software and is incorportated in the read operation itself. Different languages handle the at end condition in different ways. 

To quote from Jacksons Principles of Program Design:

"Whatever the facilities, or obsticles, provided by the input-ouput software, we must adopt a technique which satisfies these requirements:

(1) - the read operation (aStream next) must remain closed with respect to the flow of control; that is, processing must continue at the point in the text following the read statement, whether or not the end-of-file has been reached;

(2) - the end-of-file record must be effectively recognizable as a possible value of "next record", and the test for the value must be capable of being made anywhere in the program and not only during or immediately after the read operation.

... there is no reason why the records of a serial file should not be bit-strings, able to take all possible values; (3) there is then no unique value available to designate the end of file."

Interpreting this in light of the discussion:

1) aStream next should not raise an exception (it's not closed with respect to the flow of control)

2) we already have a mechanism for establishing if we're at the end anywhere in the program; aStream atEnd

3) nil should not be used as a unique value to designate the end of file; the stream itself should be tested for atEnd, or maybe we could have a distinguished singleton end-of-stream object, which is always returned by next when the stream is at end?

By not having an exception raised when attempting to read the next element would mean that one could code something like this:

[aStream atEnd | (aStream next = 'terminating value')] whileFalse: [...]
(always be evaluated and if atEnd, kaboom!)

rather than having to code defensively like this:

[aStream atEnd or: [aStream next = 'terminating value']] whileFalse: [...]

(the or: argument is not evaluated if atEnd, safe)

To get around this I sometimes append my own end-of-file "marker" to a stream before processing it. An example follows below.

This probably sounds very old fashioned, but some of our "modern problems" have been solved before!

Hope this helps,

Stewart


mapTopologicalNodesAndBranches

	"Map (form links to and from) the corresponding TopologicalNodes and 
	Branches of the switch level network to those of the nodal level network. 
	Done by collating the substations of the switch level network (derived from 
	the network definition file) with those of the nodal network (derived from the 
	IEEE State Estimator file) and forming to/from links for each pair of corresponding 
	topological nodes. 
	Uses collating pattern and appends a dummy Substation to handle 
	end of stream processing.
	The nominal voltage of the switch level substation is used (as opposed to the 
	voltage level name) and this is truncated to map with the integer voltage used 
	within the IEEE State Estimator file."

	| streamSwitch streamNodal subSwitch subNodal 
	key readSwitch readNodal highValues |

	highValues := 
		(Entity new 
			baseRoleClass: Station 
			name: (String with: (Character value: 255)))
				addSubstationOfClass: Substation voltage:  9999.					
	streamSwitch := ReadStream on: 
		(switchLevelNetwork substationsSortedByNameTruncatedNominalVoltage 
				add: highValues; yourself).
	streamNodal := ReadStream on: 
		(nodalLevelNetwork substationsSortedByNameTruncatedNominalVoltage 
			add: highValues; yourself).
	readSwitch := [subSwitch := streamSwitch next].
	readNodal := [subNodal := streamNodal next].
	readSwitch value.
	readNodal value.
	key := 
		subSwitch nameTruncatedNominalVoltage  
		<= subNodal nameTruncatedNominalVoltage 
			ifTrue: [subSwitch nameTruncatedNominalVoltage ] 
			ifFalse: [subNodal nameTruncatedNominalVoltage ].
	[(subSwitch = highValues  
	and: [subNodal = highValues]) not] whileTrue: [
		subSwitch nameTruncatedNominalVoltage  
		= subNodal nameTruncatedNominalVoltage ifTrue: [
			self mapTopologicalNodesOf: subSwitch with: subNodal.
			readSwitch value.
			readNodal value] ifFalse: [
		subSwitch nameTruncatedNominalVoltage = key ifTrue: [
			readSwitch value] ifFalse: [
		subNodal nameTruncatedNominalVoltage = key ifTrue: [
			readNodal value] ifFalse: []]].
		key := 
			subSwitch nameTruncatedNominalVoltage  
			<= subNodal nameTruncatedNominalVoltage 
				ifTrue: [subSwitch nameTruncatedNominalVoltage] 
				ifFalse: [subNodal nameTruncatedNominalVoltage]].
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 3579 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20000525/4c339f59/attachment.bin


More information about the Squeak-dev mailing list