View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

How to Save and Load using SIXX

There are number of ways to do saving and loading in smalltalk.
You can do saving using XML, but it's more likely a text-based file.
What my group founded as the easiest to use was SIXX.
You can import it from the cincom repository. The package is called "SIXX".

This will be the tutorial of how to use "SIXX."

Saving your classes

I found that the strength of SIXX comes from the ability to save classes.

First, Suppose that we have created a Honda class. Under Honda class, we have number of Car classes. Let's say we have, Car classes named "Accord", "Civic", and "S2000". Each of these Car classes has different Engine, Transmission, and Doors.
Okay. This should be enough to explain SIXX's capability.
So, right now, we have a Honda class. Under it, we have three different Car classes, each with different specs.

Now, let's write some code for saving.

	"saves the application."
	|sws dialog filename |
        "This part of the code is for familiar 'Save File As' operation"
	dialog := SaveFileDialog new.
	      acceptButtonLabel: 'Save Project File';
                "Here, determines the file extection for saved file using SIXX"
		addFileFilter: 'Honda Project File (*.pdp)' pattern: '*.pdp';
		defaultExtension: 'pdp';
		fileCondition: #any;
		windowTitle: 'Save Project File As:'.
        "Catching exceptions"
	filename := ( dialog open )
		ifTrue: [dialog selection]
		ifFalse: [nil].

        "you must have this part to do saving."
	sws := SixxWriteStream newFileNamed: filename.
	( filename isNil ) ifFalse: [sws nextPut: Honda.]. "Saving the Honda class"
	sws close.

Apply the code above to a GUI component. Click on it, then you will see the usual saving procedure. Selecting a foder and a filename.
Do those processes. Name the file, "foo.pdp".
In the code above, we saved Honda class to "foo.pdp" using SixxWriteStream and its nextPut method.
When you save Honda class, any methods, variables, and subclasses (basically everything in it) will be saved to "foo.pdp"
Basically, you can save a whole junk of stuff into a file using three lines of code. Just make sure you have everything in a class, and save that class.

Also, you can save multiple classes into a file. Say, you want to save Toyota class.
Then, all you have to do is, add "sws nextPut: Toyota."
 	( filename isNil ) ifFalse: [sws nextPut: Honda. sws nextPut: Toyota]. "Saving the Honda class"

So, basic saving is done.

Loading your saved classes
Loading is very similar to saving procedure. You will open a file using the friendly Windows "open a file" procedure. Then, you will have to read the file using SixxReadStream. Then, restore everything.
See the code below.

	"load the saved project"
	|srs savedstuff dialog filename aHonda aToyota|
        "again, code for the familiar "open a file" dialog."
	dialog := OpenFileDialog new.
		acceptButtonLabel: 'Load Project File';
		addFileFilter: 'Car Manufaturer's Project File (*.pdp)' pattern: '*.pdp';
		defaultExtension: 'pdp';
		fileCondition: #mustBeOld;
		windowTitle: 'Open Project File:'.
	filename := ( dialog open )
		ifTrue: [dialog selection]
		ifFalse: [nil].
	( filename isNil ) ifFalse: [
                "use SixxReadStream to read the file."
		srs := SixxReadStream readOnlyFileNamed: filename.
		savedstuff := srs contents.
		srs close.
	 	aHonda := (savedstuff at: 1). "restores saved Honda"
                aToyota := (savedstuff at: 2). "restores saved Toyota"

You have to be careful when you load stuff. "srs" field contains the loaded stuff. To restore classes, you need to call the contents of "srs". We stored this contents to "savedstuff" field.
"savedstuff" field contains multiple classes in it. We saved Honda and then Toyota.
So, "savedstuff at: 1" contains Honda, and "savedstuff at: 2" contains Toyota.
You have to be aware of the order you saved your classes.

Then, the loading is done! Just have the method applied in a GUI component.

This tutorial covers only basic stuff.
But, my group found that codes above are enough to cover milestone requirements. But, it is up to you for how you want your things saved and loaded.

Links to this Page