This is a C plugin for [Cap'n Proto](http://kentonv.github.io/capnproto), an efficient protocol for sharing data and capabilities.
Find a file
jose luis ferrer d9240320c1 compiler: Allocate all possible space for constants
When the c-capnp compiler runs, currently only takes the len of 1st
segment in the list as a capacity used in the generator for constants
definitions.

This works when the schema processing only generates 1 segment, or the
1st segment has 8192 bytes. There are cases where the fd returns
multiple segments an the first one has very low capacity (e.g. 96 or 80).
Hence, if more constants require to be allocated for the current schema
it will have misleading positions.

This commit takes a conservative approach by summing up all the lenghts
of capnproto segments obtained at the fd_init call of the compiler.
Those values are taken to set the memory allocation and the max
capacity for the segment utilized in the code generation.
2018-02-01 15:13:18 +01:00
ac tack on autoconf/automake build 2016-02-28 13:55:23 +01:00
compiler compiler: Allocate all possible space for constants 2018-02-01 15:13:18 +01:00
gtest@d225acc90b gtest: integrate as git submodule 2016-03-19 00:39:29 +01:00
lib Merge branch 'issue20' 2017-05-19 19:15:28 +02:00
m4 tack on autoconf/automake build 2016-02-28 13:55:23 +01:00
tests Add URL for original addressbook example; example to assert list length with capn_len(). 2017-04-02 23:59:05 +01:00
.gitignore Example usage in unit test. 2017-03-23 20:18:29 +00:00
.gitmodules gtest: integrate as git submodule 2016-03-19 00:39:29 +01:00
.travis.yml Travis CI: whatever. 2016-02-28 18:38:41 +01:00
c-capnproto.pc.in add & install pkgconfig file 2016-03-19 00:39:30 +01:00
configure.ac c-capnproto 0.3 release 2017-05-19 19:30:58 +02:00
COPYING tack on autoconf/automake build 2016-02-28 13:55:23 +01:00
Makefile.am Add comiler/c.capnp.h to Makefile.am noinst_HEADERS. 2017-03-27 03:33:34 +01:00
README.md Rewrite tests/example-test.cpp to use addressbook example from capnproto C++ project. 2017-03-27 03:30:37 +01:00

capnpc-c

This is a C plugin for Cap'n Proto, an efficient protocol for sharing data and capabilities.

This is only the code generator plugin, to properly make use of it you need to download, build and install capnpc and then build and install this project and then you can utilize it as:

capnpc compiler/test.capnp -oc

Build Status

Building on Linux

git clone https://github.com/opensourcerouting/c-capnproto
cd c-capnproto
git submodule update --init --recursive
autoreconf -f -i -s
./configure
make
make check

Usage

Generating C code from a .capnp schema file

The compiler directory contains the C language plugin (capnpc-c) for use with the capnp tool: https://capnproto.org/capnp-tool.html.

capnp will by default search $PATH for capnpc-c - if it's on your PATH, you can generate code for your schema as follows:

capnp compile -o c myschema.capnp

Otherwise, you can specify the path to the c plugin:

capnp compile -o ./capnpc-c myschema.capnp

capnp generates a C struct that corresponds to each capn proto struct, along with read/write functions that convert to/from capn proto form.

If you want accessor functions for struct members, use attribute fieldgetset in your .capnp file as follows:

using C = import "${c-capnproto}/compiler/c.capnp";

$C.fieldgetset;

struct MyStruct {}

Example C code

See the unit tests in tests/example-test.cpp. The example schema file is tests/addressbook.capnp. The tests are written in C++, but only use C features.

You need to compile these runtime library files and link them into your own project's binaries:

Your include path must contain the runtime library directory lib. Header file lib/capnp_c.h contains the public interfaces of the library.

Using make-based builds, make may try to compile ${x}.capnp from ${x}.capnp.c using its built-in rule for compiling ${y} from ${y}.c. You can either disable make's built-in compile rules or just this specific case with the no-op rule: %.capnp: ;.

For further reference, please see the other unit tests in tests, and header file lib/capnp_c.h.

The project quagga-capnproto uses c-capnproto and contains some good examples, as found with this github repository search:

  • Serialization in function bgp_notify_send() in file quagga-capnproto/bgpd/bgp_zmq.c
  • Deserialization in function qzc_callback() in file quagga-capnproto/lib/qzc.c

Status

This is a merge of 3 forks of James McKaskill's great work, which has been untouched for a while: