paring binary files - basic understanding question of the OO Way

goran.krampe at bluefish.se goran.krampe at bluefish.se
Fri Aug 6 12:48:14 UTC 2004


Hi!

Guenther Schmidt <gue.schmidt at web.de> wrote:
> Hi,
> 
> I'm trying to write a parser for binary files (excel) and wow! I've 
> already figured out how to use streams ;-).
> 
> Now comes a problem with basic understanding of OO.
> 
> As I read from the stream byte by byte how can I 'parse' the byte i.e. 
> give it it's meaning or create the proper object?
> 
> Does it realy come down to if byte = xxx ... else ... else ...? I mean 
> this doesn't seem very OO to me.

When simple "data" (bytes, Strings, numbers etc) enter an OO system it
typically gets a bit ugly, hard to avoid. Somewhere there has to be an
interpretation and thus *somehow* there must be a choice made. Now - it
is often "nicer" to use some lookup Dictionary or something to do the
mapping. Like for example:

| table object byte |
byte _ 10.
table _ Dictionary new
		at: 10 put: ['ten'];
		at: 13 put: ['thirteen'];
		yourself.
object _ (table at: byte) value.
object

...which can also be written like:

| table object byte |
byte _ 10.
table _ {10->['ten']. 13->['thirteen']} as: Dictionary
object _ (table at: byte) value.
object

...but no matter how you cut it, it still is a "switch" statement.

Other more OO-ish approaches probably involve some kind of multi level
dispatching. For example you could create a tree structure of objects
and then use the input (in your example - your bytes) to traverse the
tree and dispatch on each node etc.

Also note that the class hierarchy can be used this way too - in SM I
actually traverse the subclasses of SMInstaller on the class side to
find which one is capable of installing a certain package release.

And you can also use #perform: to do "dirtier" stuff. :) Like taking a
String, converting it to a Symbol and do "SomeObject perform: aSymbol"
and thus get a dispatch that way.

But again, whichever way you turn it gets a bit uglyish.

> Please help.
> 
> Regards
> 
> Guenther

regards, Göran



More information about the Squeak-dev mailing list