• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /* Same as test_encode1.c, except writes directly to stdout.
2   */
3  
4  #include <stdio.h>
5  #include <pb_encode.h>
6  #include "person.pb.h"
7  #include "test_helpers.h"
8  
9  /* This binds the pb_ostream_t into the stdout stream */
streamcallback(pb_ostream_t * stream,const uint8_t * buf,size_t count)10  bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
11  {
12      FILE *file = (FILE*) stream->state;
13      return fwrite(buf, 1, count, file) == count;
14  }
15  
main()16  int main()
17  {
18      /* Initialize the structure with constants */
19      Person person = {"Test Person 99", 99, true, "test@person.com",
20          3, {{"555-12345678", true, Person_PhoneType_MOBILE},
21              {"99-2342", false, 0},
22              {"1234-5678", true, Person_PhoneType_WORK},
23          }};
24  
25      /* Prepare the stream, output goes directly to stdout */
26      pb_ostream_t stream = {&streamcallback, NULL, SIZE_MAX, 0};
27      stream.state = stdout;
28      SET_BINARY_MODE(stdout);
29  
30      /* Now encode it and check if we succeeded. */
31      if (pb_encode(&stream, Person_fields, &person))
32      {
33          return 0; /* Success */
34      }
35      else
36      {
37          fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
38          return 1; /* Failure */
39      }
40  }
41