Reinventing the Wheel

With an environment as large as VA Smalltalk, it can be a challenge to be aware of all the capabilities already implemented somewhere in the bowels of the system.  I usually figure that if a programming task seems incredibly tedious or common-place, there’s probably already a way of doing it in Smalltalk.

One of the more common requirements I run into for applications is the ability to retrieve configuration information from an external location.  Typically, this is an ini file.  Every VA Smalltalk image has an ini file associated with it, so it’s a good place to store that configuration information, but, all too often, someone has done a lot more work than they need to in order to read the file.

An ini file consists of a number of stanzas containing key-value pairs; for instance:

[Blog Data]
title=’Nothing About Elk’
tagline=’Banging the rocks together…’
author=’Tom Koschate’

It’s actually possible to read this information from the application ini file without writing any new capabilities into the system.

EmSystemConfiguration>>#iniFileGetContentsArray: will answer a dictionary for a given stanza.  For the example above, executing System iniFileGetContentsArray: ‘Blog Data’ would answer a dictionary with three associations corresponding to the key/value pairs in the ‘Blog Data’ stanza.  If the stanza weren’t there, it would answer an empty dictionary.

The method #iniFileGetContentsArray: is a private method, but it’s used in so many places by VA Smalltalk itself, that you can be reasonably confident that it will be around unchanged for a while.  You might want to wrap it a little more nicely, but it’s available on all supported operating systems.

As a final note, if you change the contents of the ini file during your development, you’ll want to execute EmSystemConfiguration>>#primitiveIniFileFlushCache to force the system to read the updated information.  Normally the file is read only when the image starts.


1 Response to “Reinventing the Wheel”

  1. 1 Marten Feldtmann 2011-04-13 at 09:44

    There is a limit of a maximal length of a line in such ini files (255 characters ?) – I had a discussion about that in the Instantiations forum. Therefore be aware of this limitation !

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: