55[ ![ Release Status] ( https://img.shields.io/github/release/squidfunk/protobluff.svg )] ( https://github.com/squidfunk/protobluff/releases/latest )
66[ ![ License] ( https://img.shields.io/github/license/squidfunk/protobluff.svg )] ( https://github.com/squidfunk/protobluff/blob/master/LICENSE )
77
8- protobluff is an extremely lightweight Protocol Buffers implementation for C.
8+ protobluff is a modular Protocol Buffers implementation for C.
99
1010## Theory of Operation
1111
@@ -19,14 +19,14 @@ number of scattered allocations which in turn is not very cache-friendly.
1919
2020protobluff follows a different approach. It entirely skips the necessary
2121decoding and encoding steps when reading or writing values from messages,
22- as it directly operates on the encoded binary . New values can be incrementally
22+ as it directly operates on the encoded data . New values can be incrementally
2323read or written, memory management is centralized and handled by the underlying
24- binary . If no alterations that change the size of the underlying binary are
25- expected, the binary can be used in * zero-copy mode* , omitting all dynamic
24+ journal . If no alterations that change the size of the underlying journal are
25+ expected, the journal can be used in * zero-copy mode* , omitting all dynamic
2626allocations.
2727
2828Updates on fixed-sized wire types on little-endian machines can be carried out
29- in-place using raw-pointers to the underlying binary . These include the native
29+ in-place using raw-pointers to the underlying data . These include the native
3030Protocol Buffers types ` fixed(32|64) ` , ` sfixed(32|64) ` , ` float ` and ` double `
3131(see the [ Protocol Buffers Encoding Guide] [ ] for more information). Strings may
3232also be accessed through raw-pointers, however writing a string of different
@@ -100,53 +100,65 @@ Here's a usage example taken from the original description of the Google
100100Protocol Buffers library and adapted to protobluff:
101101
102102``` c
103- /* Create an empty binary to assemble a new person message */
104- pb_binary_t binary = pb_binary_create_empty ();
103+ /* Create an empty journal to assemble a new person message */
104+ pb_journal_t journal = pb_journal_create_empty ();
105105
106106/* Create a person message */
107- pb_message_t person = person_create(&binary );
107+ pb_message_t person = person_create(&journal );
108108
109109/* Define the values we want to set */
110- pb_string_t name = pb_string_init (" John Doe" ),
111- email =
pb_string_init (
" [email protected] " ),
112- home = pb_string_init (" +1-541-754-3010" ),
113- mobile = pb_string_init (" +1-541-293-8228" );
110+ pb_string_t name = pb_string_init_from_chars (" John Doe" ),
111+ email =
pb_string_init_from_chars (
" [email protected] " ),
112+ home = pb_string_init_from_chars (" +1-541-754-3010" ),
113+ mobile = pb_string_init_from_chars (" +1-541-293-8228" );
114114int32_t id = 1234 ;
115115
116116/* Set values on person message and check return codes */
117117pb_error_t error = PB_ERROR_NONE;
118118do {
119119 if ((error = person_put_name(&person, &name)) ||
120- (error = person_put_email (&person, &email )) ||
121- (error = person_put_id (&person, &id )))
120+ (error = person_put_id (&person, &id )) ||
121+ (error = person_put_email (&person, &email )))
122122 break;
123123
124124 /* Set home number * /
125125 pb_message_t phone1 = person_create_phone(&person);
126- if ((error = person_phonenumber_put_type_home(&phone1)) ||
127- (error = person_phonenumber_put_number(&phone1, &home)))
128- break;
129-
130- /* Set mobile number * /
131- pb_message_t phone2 = person_create_phone(&person);
132- if ((error = person_phonenumber_put_type_mobile(&phone2)) ||
133- (error = person_phonenumber_put_number(&phone2, &mobile)))
134- break;
135-
136- /* All values were set successfully, the binary is ready to be persisted,
137- sent or whatever - no marshalling necessary. The raw message data and size
138- can be directly obtained through the binary * /
139- const uint8_t * data = pb_binary_data(&binary);
140- const size_t size = pb_binary_size(&binary);
126+ if (!(error = person_phonenumber_put_number(&phone1, &home)) &&
127+ !(error = person_phonenumber_put_type_home(&phone1))) {
128+
129+ /* Set mobile number */
130+ pb_message_t phone2 = person_create_phone(&person);
131+ if (!(error = person_phonenumber_put_number(&phone2, &mobile)) &&
132+ !(error = person_phonenumber_put_type_mobile(&phone2))) {
133+
134+ /* Dump the journal */
135+ pb_journal_dump (&journal);
136+
137+ /* The encoded message can be accessed as follows */
138+ // const uint8_t *data = pb_journal_data(&journal);
139+ // const size_t size = pb_journal_size(&journal);
140+ }
141+ person_phonenumber_destroy (&phone2);
142+ }
143+ person_phonenumber_destroy (&phone1);
141144} while (0 );
142145
146+ /* Print error, if any */
147+ if (error)
148+ fprintf (stderr, "ERROR: %s\n", pb_error_string(error));
149+
143150/* Cleanup and invalidate * /
144151person_destroy(&person);
145152
146- /* Free all allocated memory * /
147- pb_binary_destroy(&binary);
153+ /* Free all allocated memory and return * /
154+ pb_journal_destroy(&journal);
155+ return error
156+ ? EXIT_FAILURE
157+ : EXIT_SUCCESS;
148158```
149159
160+ See the examples directory for more information.
161+
150162## Linking
151163
152164### Manually
@@ -157,8 +169,8 @@ library. Therefore, the following compiler and linker flags must be obtained
157169and added to your build toolchain:
158170
159171``` sh
160- pkg-config --cflags libprotobluff # Add output to compiler flags
161- pkg-config --libs libprotobluff # Add output to linker flags
172+ pkg-config --cflags protobluff # Add output to compiler flags
173+ pkg-config --libs protobluff # Add output to linker flags
162174```
163175
164176### Autotools
@@ -169,7 +181,7 @@ the compiler flags into the variable `protobluff_CFLAGS` and the linker flags
169181into the variable ` protobluff_LDFLAGS ` :
170182
171183``` makefile
172- PKG_CHECK_MODULES([protobluff], [libprotobluff ])
184+ PKG_CHECK_MODULES([protobluff], [protobluff ])
173185```
174186
175187## Features
@@ -198,9 +210,8 @@ PKG_CHECK_MODULES([protobluff], [libprotobluff])
198210### Roadmap
199211
2002121 . Oneofs
201- 2 . Streaming API
202- 3 . Packed fields
203- 4 . Services
213+ 2 . Packed fields
214+ 3 . Services
204215
205216## License
206217
0 commit comments