Packing & unpacking are the only way to do this. Usually, you write a pair
of functions, pack & unpack, that deal with the details. Pack would be
something like this (without error checking):
// pass in a pointer to a serverData and a pointer to a UInt, returns packed
record and sets UInt to size of packed record
CharPtr Pack(serverData *data, UInt *size) {
UInt mySize, charLen;
CharPtr packedRecord, copyLocation;
// compute size of 3 sdwords, 3 strings, and 3 nulls
mySize = sizeof(SDWord)*3 +
StrLen(label)+StrLen(server)+StrLen(url)+3;
// allocate a handle in memory to a packed record
packedRecord= copyLocation = MemPtrNew(mySize);
// Copy in SDWords
MemMove(copyLocation , &data->serverID, sizeof(SDWord));
copyLocation += sizeof(SDWord);
MemMove(copyLocation , &data->port, sizeof(SDWord));
copyLocation += sizeof(SDWord);
MemMove(copyLocation, &data->category, sizeof(SDWord));
copyLocation += sizeof(SDWord);
MemMove(copyLocation, data->label, charLen = StrLen(data.label)+1);
// copy string *and null terminator*
copyLocation += charLen;
MemMove(copyLocation, data->server, charLen =
StrLen(data.server)+1); // copy string *and null terminator*
copyLocation += charLen;
MemMove(copyLocation, data->url, charLen = StrLen(data.url)+1); //
copy string *and null terminator*
return packedRecord;
}
Or, you could just allocate a database record in Pack, and use DmWrite()
calls to write the data in.
Unpack looks pretty much the same in reverse:
serverData *Unpack(CharPtr packedRecord) {
serverData *data;
UInt charLen;
data = MemPtrNew(sizeof(serverData);
MemMove(&data->serverID, packedRecord, sizeof(SDWord);
packedRecord += sizeof(SDWord);
MemMove(&data->port, packedRecord, sizeof(SDWord);
packedRecord += sizeof(SDWord);
MemMove(&data->category, packedRecord, sizeof(SDWord);
packedRecord += sizeof(SDWord);
charLen = StrLen(packedRecord);
data->label = MemPtrNew(charLen)+1;
MemMove(data->label, packedRecord, charLen+1);
packedRecord += (charLen+1);
charLen = StrLen(packedRecord);
data->server= MemPtrNew(charLen)+1;
MemMove(data->server, packedRecord, charLen+1);
packedRecord += (charLen+1);
charLen = StrLen(packedRecord);
data->url= MemPtrNew(charLen)+1;
MemMove(data->url, packedRecord, charLen+1);
}
You're usually better off doing the packing and unpacking one element at a
time, using MemMove(), so you don't run into any issues with data alignment
or struct padding.
John Schettino
Palm OS Programming For Dummies: http://schettino.tripod.com
-----Original Message-----
From: Patrick Robert Aland [mailto:[EMAIL PROTECTED]]
Sent: Sunday, March 12, 2000 5:10 PM
To: Palm Developer Forum
Subject: Better way to store structure
My application has a database that is made up of structures as follows:
typedef struct
{
SDWord serverID;
SDWord port;
SDWord category;
char *label;
char *server;
char *url;
}serverData;
The O'Reilly book uses a similar data structure and packs the char *'s
into a single char [1] and the unpacks them when they are needed. Is their
a better way to do this?
I tried just doing a single write but because of the Char's it only
recorded the address in memory of the string and not the string.
Thanks.
--patrick
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palm.com/devzone/mailinglists.html
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palm.com/devzone/mailinglists.html