IonicWind Software

Aurora Compiler => General Discussion => Topic started by: Haim on October 14, 2006, 10:13:30 AM

Title: Associative Array question
Post by: Haim on October 14, 2006, 10:13:30 AM
I was trying to learn the topic of Cdictionary and Associative arrays in Aurora.
Once I have an associative pointer array in memory, how can I save it to a disk file and be able to restore it from that file?

Haim
Title: Re: Associative Array question
Post by: Parker on October 14, 2006, 12:28:18 PM
You have to iterate through yourself. When I write information to a file like this, I usually use Pascal-style strings - that is, first I write an integer telling how long the string is, and then the string itself. That speeds up reading from the file because I don't have to check for NULL each time. So for a CStringAssoc, you might have the following dictionary:

KeyValue
mycompilerAurora
yourcompilerGCC

To write this to a file, you would do something like this:
int keylen = len( key ), vallen = len( value );
write( file, keylen, sizeof( int ) );
write( file, key, keylen );
write( file, vallen, sizeof( int ) );
write( file, value, vallen );
// Repeat for each key/value pair


To read from a file:
int keylen, vallen;
string *key, *value;
read( file, keylen, sizeof( int ) );
key = new( byte, keylen + 1 );
read( file, key, keylen );
read( file, vallen, sizeof( int ) );
value = new( byte, vallen + 1 );
read( file, value, vallen );
// Add to dictionary, repeat until eof( file )
Title: Re: Associative Array question
Post by: Haim on October 14, 2006, 10:30:39 PM
Thank you.
I was thinking along the same lines but your explanation clarified it.
Thanks again,

Haim
Title: Re: Associative Array question
Post by: Jerry Muelver on October 16, 2006, 10:59:42 AM
I usually convert the hash into a string, with delimiters, and save it that way. To reload, read the string in, and split key|value pairs off. If values are multi-line, convert crlf to a marker (chr$(3) is a favorite), and convert back to crlf on loading.

{key|value}{mycompiler|Aurora}{yourcompiler|GCC}{pie|apple}{ice cream|Blue Bunny Rabbit Tracks}
Title: Re: Associative Array question
Post by: Parker on October 16, 2006, 07:33:01 PM
The reason I don't do it that way is you can technically have any ascii character in a string. The way I described allows you to even have NULLs in the string (which some languages allow). You could escape things, but it's easier not to. Not saying anything's wrong with that approach though.