April 24, 2024, 03:18:38 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Associative Array question

Started by Haim, October 14, 2006, 10:13:30 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Haim

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

Parker

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 )

Haim

Thank you.
I was thinking along the same lines but your explanation clarified it.
Thanks again,

Haim

Jerry Muelver

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}

Parker

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.