hello,
i sent a Q to Neil Rhodes the author of Palm Developing book, but he didn't
answered, i hope u can, so here it is:
hello,
i bought your palm developing book and i have a Q: ( i hope u can help me )
about a pack and unpack function (pages 158 - 159 ) - here is the function:
1 static void PackCustomer(Customer *customer, VoidHand
customerDBEntry)
2{
// figure out necessary size
3 UInt length = 0;
4 CharPtr s;
5 UInt offset = 0;
6 length = sizeof (customer->customerID) + StrLen (customer->name) +
StrLen(customer->address) + StrLen(customer->city) +
StrLen (customer->phone) + 4; // 4 for string terminators
7 // resize the VoidHand
8 if (MemHandleResize(customerDBEntry, length) == 0)
9 { // copy the fields
10 s = MemHandleLock(customerDBEntry);
11 offset = 0;
12 DmWrite(s, offset, (CharPtr) &customer->customerID,
sizeof(customer->customerID));
13 offset += sizeof(customer->customerID);
14 DmStrCopy(s, offset, (CharPtr) customer->name);
15 offset += StrLen(customer->name) + 1;
16 DmStrCopy(s, offset, (CharPtr) customer->address);
17 offset += StrLen(customer->address) + 1;
18 DmStrCopy(s, offset, (CharPtr) customer->city);
19 offset += StrLen(customer->city) + 1;
20 DmStrCopy (s, offset, (CharPtr) customer->phone);
21 MemHandleUnlock(customerDBEntry);
22 }
23}
24// packedCustomer must remain locked while customer is in use
25 static void UnpackCustomer(Customer *customer,
const PackedCustomer
*packedCustomer)
26{
27 const char *s = packedCustomer->name;
28 customer->customerID = packedCustomer->customerID;
29 customer->name = s;
30 s += StrLen(s) + 1;
31 customer->address = s;
32 s += StrLen(s) + 1;
33 customer->city = s;
34 s += StrLen(s) + 1;
35 customer->phone = s;
36 s += StrLen(s) + 1;
37}
my Q:
when u wants to pack a customer u create a "pointer" and start to write the
fields of his struct to the memory, one after another. (line 10)
but as u can see u write the field customerID as CharPtr ( why is that ??? )
at the beginning and the rest of the field u write afterward. my Q for this
thing is why do u force it to be CharPtr?? when u write to a memory, is it
always have to be char type?? cann't i write numbers like int or float or
boolean??
my secound Q is:
the customer structure is -
typedef struct
{
SDWord customerID;
const char *name;
const char *address;
const char *city;
const char *phone;
} Customer;
and the pakedcustomer structure is -
typedef struct
{
SDWord customerID;
char name[1]; // actually may be longer than 1
} PackedCustomer;
first i want to ask why do u define the fields to be const??
in my app i have a push-button in one of the forms, and i save its result
into a boolean variable, so do i need to define it as:
const boolean *status;
???
and now for my main Q:
as i said before u, when u pack the customer, u write it into the memory
field after field with the ID field first ( line 12 ). in the unpack
function u use the unpacked structure (line 25) and i don't understand how u
can do what u do in line 28, how can u use the packedCustomer->customerID
field?? this field didn't get any value what so ever, as i said u wrote the
ID in to the memory first and here u don't even read in from the start of
the memory allocation, so how can u use it?? don't u need to read it like
all the other fields - from the memory one after another?? why do u need the
ID field in the unpacked structure if u don't use untill u unpacked a
customer??
i hope u got my Qs and that u could help me,
tnx in advance :-)
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/