Re: How to make a NOT-EVEN length struct?

2005-05-27 Thread Chris Tutty
From: Logan Shaw [EMAIL PROTECTED]
 Chrix Bell wrote:
  I have make a struct with two offsets and a take-place field
  looks like this:
  struct whatever{
  unsigned Aoffset;
  unsigned Boffset;
  char FirstField;
  } 
  I want to use this to pack the record size,
 
 What does pack the record size mean?
 
Since the original message described writing two fields my guess 
would be to pack string data from multiple fields into a database 
record without unnecessary padding.  This is hardly a new concept
for Palm OS databases :-)

I tend to create the struct ending with a char[2] to indicate that this
is string data rather than an individual char and, more importantly,
comment the struct to indicate to any programmer that follows you
that the record data entends past the end of the struct. 

  data would be write from
  FirstField, write A, then from (FirstField+ Boffset), write B.
 
 What are A and B?  Are the some sort of data you want to write
 into a database?  Are they variables?  Are they instances of
 struct whatever?
 
Doesn't matter.  The concepts are the same regardless of the 
objects - find the size of the first and second, create a buffer 
big enough for the data and any management overhead, write 
the first record to the buffer at offset zero (or just past any record
header), find the size of the second and write at an offset just 
past the end of the first block.  Then (if you want to use the technique 
this poster seems to be following) write the offset for the second
record to the header at the start of the buffer and close the record.

The problem is that you don't write B at (FirstField+ Boffset).  
Generally each field is written at the stored offset matching it
so A would be stored at Aoffset (strictly redundant because
you know it starts at the offset of the FirstField struct member),
then Boffset is calculated as Aoffset + (sizeof(A) or StrLen(A))  
+ 1 (depending on whether you're storing a struct or a string and 
a nasty trap for beginnners) and B is stored at Boffset.  Continue
as required.  The calculation mistake is probably why you're 
getting an out of bound error.

This has been done before so try to find some code that 
implements this and copy it.

  I am wondering if there are some other kind of skills can make
  the record's length not a fixed one.
 
 If by the record you mean the struct, then it is impossible.
 In C, a struct's size is determined at compile time.  
Strictly correct, but slightly misleading...

 If you need
 to write a data structure whose length differs depending on what it
 contains, you will need to write it by hand, one byte at a time.
 (Or with DmWrite() or MemMove() or similar.)
 
I've seen this technique of a using a struct to describe the fixed 
elements and appending strings to the end of it fairly often in
Palm OS apps - I'm sure at least one of the sample apps does 
things this way so it's not only not impossible, it's fairly standard
for Palm OS.  The confusion is that you've got to understand
that the struct is just a guide that simplifies accessing the start
of the record - the actual data extends beyond it so, as you say,
you've got to be careful about deciding whether you're going to
write the struct as a block and then append data or write the 
contents of the struct member by member (generally safer).

If you're only storing strings you actually don't need the offsets
as long as you make sure that each string is null-terminated in the buffer.
You can then read the first string at offset zero and the second
at offset StrLen(firststring) + 1).

Storing the offsets can still be useful if you're writing many strings
and want to quickly extract one without reading the preceeding
strings.

Chris Tutty

-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: How to make a NOT-EVEN length struct?

2005-05-27 Thread Robert Moynihan

Chris Tutty wrote:

I've seen this technique of a using a struct to describe the fixed 
elements and appending strings to the end of it fairly often in
Palm OS apps - I'm sure at least one of the sample apps does 
things this way so it's not only not impossible, it's fairly standard

for Palm OS.

I would say; precisely right.  I use this technique often, where I write 
the record in 2 parts; the 1st is a fixed structure, with a little 
internal padding to allow for slight structure changes in the future, 
and the 2nd part is a variable length data buffer, or series of 
buffers.  I used to do it element by element, tagging each element with 
a specific identifier, but that took a lot of coding.  Bundling the 
majority of the stuff into a strucutre is just so much tidier and quicker


Bob.


--
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


How to make a NOT-EVEN length struct?

2005-05-25 Thread Chrix Bell
I have make a struct with two offsets and a take-place field
looks like this:
struct whatever{
unsigned Aoffset;
unsigned Boffset;
char FirstField;
} 
I want to use this to pack the record size, data would be write from 
FirstField, write A, then from (FirstField+ Boffset), write B. And I use 
dmWrite() to fill data into this struct, but returned out of bound. 
I am wondering if there are some other kind of skills can make the record's 
length not a fixed one.
Could any one help me? 
-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: How to make a NOT-EVEN length struct?

2005-05-25 Thread Logan Shaw

Chrix Bell wrote:

I have make a struct with two offsets and a take-place field
looks like this:
struct whatever{
unsigned Aoffset;
unsigned Boffset;
char FirstField;
} 





I want to use this to pack the record size,


What does pack the record size mean?


data would be write from
FirstField, write A, then from (FirstField+ Boffset), write B.


What are A and B?  Are the some sort of data you want to write
into a database?  Are they variables?  Are they instances of
struct whatever?


And I use dmWrite() to fill data into this struct, but returned
out of bound. 


This could easily happen if you created an instance of the struct
with MemPtrNew() or on the stack.  DmWrite() only should be used
to write into the storage heap, which usually means writing directly
into a record within a database.


I am wondering if there are some other kind of skills can make
the record's length not a fixed one.


If by the record you mean the struct, then it is impossible.
In C, a struct's size is determined at compile time.  If you need
to write a data structure whose length differs depending on what it
contains, you will need to write it by hand, one byte at a time.
(Or with DmWrite() or MemMove() or similar.)


By the way, you might get more useful answers if you explain what
you're trying to do, i.e. what problem you're trying to solve.
I can't really tell based on your message what you're trying to
accomplish with this struct you've defined.

  - Logan

--
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/