sample c code for encoding/decoding "repeated string" type (without max_count or max_size) ??

2,996 views
Skip to first unread message

nel...@iname.com

unread,
Mar 17, 2014, 8:06:55 PM3/17/14
to nan...@googlegroups.com
Do you have or know of some sample c source code, available to browse on the web, that encodes and decodes "repeated string" type? If so, please post a URL here. Otherwise I'll have to figure it out by trial and error. :-/

Thanks.

Petteri Aimonen

unread,
Mar 18, 2014, 1:35:52 AM3/18/14
to nan...@googlegroups.com
Hi,

> Do you have or know of some sample c source code, available to browse on
> the web, that encodes and decodes "repeated string" type? If so, please
> post a URL here.

See encode_repeatedstring() here:
http://code.google.com/p/nanopb/source/browse/tests/callbacks/encode_callbacks.c#47

For decoding, you can look at print_string() here:
http://code.google.com/p/nanopb/source/browse/tests/callbacks/decode_callbacks.c#10

Note that when decoding repeated fields using callbacks, the callback will
be called multiple times.

--
Petteri

nel...@iname.com

unread,
Mar 18, 2014, 2:27:45 PM3/18/14
to nan...@googlegroups.com
Petteri, Thanks for your quick reply. I was surprised to see that the decode callback doesn't get any explicit notification of the number of strings that will come from the stream. I guess I will have to add that to the message explicitly.

Do you also have an example of repeated bytes?

Thanks again.

Petteri Aimonen

unread,
Mar 18, 2014, 2:45:04 PM3/18/14
to nan...@googlegroups.com
Hi,

> Petteri, Thanks for your quick reply. I was surprised to see that the
> decode callback doesn't get any explicit notification of the number of
> strings that will come from the stream. I guess I will have to add
> that to the message explicitly.

The protobuf format doesn't give the number of fields separately. Of
course if you have the whole message in a memory buffer, it is easy to
count them, but if you are reading from stream it is not possible.

Also field order in protobuf is unspecified, so it may be quite
troublesome to ensure that you receive the "number of strings" field
before the actual strings.

What are you going to do with the strings? And where is the message
coming from?

> Do you also have an example of repeated bytes?

It is exactly same as strings. The encoded data is exactly the same
with bytes and strings fields, difference is only in datatypes in
structures when not using callbacks.

--
Petteri

nel...@iname.com

unread,
Mar 18, 2014, 7:10:12 PM3/18/14
to nan...@googlegroups.com
On Tuesday, March 18, 2014 11:45:04 AM UTC-7, Petteri Aimonen wrote:
> Also field order in protobuf is unspecified, so it may be quite
> troublesome to ensure that you receive the "number of strings" field
> before the actual strings.

That's good to know.

> [Repeated bytes] is exactly same as strings. The encoded data is exactly the


> same with bytes and strings fields, difference is only in datatypes in
> structures when not using callbacks.

So, the difference is not in the over-the-wire encoding, but only in the decoded (unencoded) form. That's good to know.

Thanks for the helpful answers.

Reply all
Reply to author
Forward
0 new messages