Giter VIP home page Giter VIP logo

Comments (3)

eqvinox avatar eqvinox commented on July 25, 2024
@0xed2b3b98b3eb515b;
struct Msg {
	data @0: Data;
}
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>

#include "data.capnp.h"

int main(int argc, char **argv)
{
	uint8_t data[] = { 0x12, 0x34, 0x00, 0x56, 0x78 };

	/* create capn + capn segment */
	struct capn c;
	capn_init_malloc(&c);
	capn_ptr cr = capn_root(&c);
	struct capn_segment *cs = cr.seg;

	struct Msg msg;

	/* capn_list8 & capn_data are essentially the same (they both just contain a single capn_ptr)
	 * usage is slightly convoluted sadly */
	capn_list8 list8 = capn_new_list8(cs, sizeof(data));
	capn_setv8(list8, 0, data, sizeof(data));
	msg.data.p = list8.p;

	/* encode & set as root */
	Msg_ptr pp = new_Msg(cs);
	write_Msg(&msg, pp);
	assert(capn_setp(capn_root(&c), 0, pp.p) == 0);

	uint8_t obuf[4096];
	size_t sz = capn_write_mem(&c, obuf, sizeof(obuf), 0);
	capn_free(&c);

	write(1, obuf, sz);
	return 0;
}
$ ./datatest | capnp decode data.capnp Msg
(data = "\x124\x00Vx")

from c-capnproto.

blowfish2003 avatar blowfish2003 commented on July 25, 2024

Thanks a lot equinox. The "capn_list8" part is the one I totally missed. I will make modifications and post the results.

from c-capnproto.

eqvinox avatar eqvinox commented on July 25, 2024

Added note in header, don't have a better place for documentation currently. -- closing

from c-capnproto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.