On Sat, Feb 16, 2013 at 12:51 PM, Paul O'Fallon <[email protected]>wrote:
> Hello! At the risk of revealing my atrophied C skills, I have a question > about the recv.c Messenger example which contains this block of code: > > size_t buffsize = 1024; > char buffer[buffsize]; > pn_data_t *body = pn_message_body(message); > pn_data_format(body, buffer, &buffsize); > > > I have a similar block of code in my own project (adapted from the recv > example). I imagine ideally I would allocate a buffer equal to the size of > the message body. However, if I attempt to determine the size of the > message body by calling pn_data_size(body) immediately after > pn_message_body(), I *always* get back the value of "1". Is this the > correct way to determine the size of a message body, or is there an overall > better approach for getting the data out of the message body? > > Thanks in advance for your help! The pn_data_size() function is actually returning the number of nodes within the data object, not a size in bytes. The pn_data_format function doesn't really know in advance how much space it will need in order to serialize the contents of a data object. This can be difficult to predict given things like utf8 content with multibyte sequences and what not. It will however return PN_OVERFLOW if the buffer is to small, so you can write a loop that checks for this return size and doubles the scratch buffer. --Rafael
