protobuf-c-text
Library to generate & parse text format protobufs in C.
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups
protobuf-c-text Documentation

Description

Google protobufs are an efficient way to serialise and deserialise data to send across the wire or to store on disk. The Google compiler and related libraries provide implementations for C++, Java and Python. A very simple message definition language is used to generate parsers and generators for this binary format.

The Protobuf compiler and library for C provide access to this technology from within C code.

Besides the efficient binary protobuf format there is also a text mode format which is accessible to code generated by the Google protobuf compiler. While the text format has limitations - namely that it lacks some of the back/forwards-compatibility features of the binary format - it can be a useful debugging tool and a strict but human readable config file format.

The C protobuf implementation is very minimal and lacks this feature. This library supplements libprotobuf-c and provides functions to generate and consume text format protobuf. They will work fine with any ProtobufCMessage subtype generated by the protoc-c compiler.

See Also

Examples

Both examples use this .proto definition:

message Ponycopter {
  optional string hair_colour = 1;
  optional uint32 rotors = 2;
  optional uint32 awesomeness = 3;
}

Generating

#include <protobuf-c/protobuf-c.h>
#include "ponycopter.pb-c.h"

int main(int argc, char *argv[]) {
  int len;
  char *pc_bin, *pc_text;
  Ponycopter *pc;

  pc_bin = read_a_blob(argv[1], &len);
  pc = ponycopter__unpack(NULL, len, pc_bin);
  pc_text = protobuf_c_text_to_string((ProtobufCMessage *)pc, NULL);
  printf("Ponycopter: %s\n", pc_text);
}

Parsing

#include <protobuf-c/protobuf-c.h>
#include "ponycopter.pb-c.h"

int main(int argc, char *argv[]) {
  ProtobufCTextError tf_res;
  Ponycopter *pc;

  pc = (Ponycopter *)protobuf_c_text_from_file(
         &ponycopter__descriptor, argv[1], &tf_res, NULL);
}