summaryrefslogtreecommitdiffstats
path: root/pb_encode.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2014-04-05 11:26:39 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2014-04-05 11:26:39 +0300
commitb63e582bdb34b4727a8eb551fc72ee0476047a46 (patch)
tree37e19df37e51132052c54ce9b46d1cc9b16b0698 /pb_encode.c
parente5b855fec5d2977971f96d817728c7a3ee8077b2 (diff)
Add a convenience function pb_get_encoded_size()
There is minimal size penalty from this, and it is probably much more intuitive to use than PB_OSTREAM_SIZING itself. This has been suggested before also, but I ended up refusing it back them. Reconsidering it now, I see that an intuitive API is much better than any amount of documentation explaining a non-intuitive API. Update issue 16 Status: FixedInGit
Diffstat (limited to 'pb_encode.c')
-rw-r--r--pb_encode.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/pb_encode.c b/pb_encode.c
index 1eb9473a..dc5a2734 100644
--- a/pb_encode.c
+++ b/pb_encode.c
@@ -378,6 +378,17 @@ bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const
return pb_encode_submessage(stream, fields, src_struct);
}
+bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct)
+{
+ pb_ostream_t stream = PB_OSTREAM_SIZING;
+
+ if (!pb_encode(&stream, fields, src_struct))
+ return false;
+
+ *size = stream.bytes_written;
+ return true;
+}
+
/********************
* Helper functions *
********************/