merge code

This commit is contained in:
Rongsong Shen 2025-04-21 13:13:34 +08:00
parent 1089ab8441
commit 5d64af4841
5 changed files with 2510 additions and 1522 deletions

View file

@ -55,3 +55,15 @@ annotation extraheader @0xbadb496d09cf4612 (file): Text;
annotation extendedattribute @0xd187bca5c6844c24 (file): Text; annotation extendedattribute @0xd187bca5c6844c24 (file): Text;
# add an extended attribute to each generated function # add an extended attribute to each generated function
annotation codecgen @0xcccaac86283e2609 (file): Void;
# generate codec(encode/decode) to each type
annotation mapname @0xb9edf6fc2d8972b8 (*): Text;
# the mapped type name which will be encoded
annotation maplistcount @0xb6ea49eb8a9b0f9e (field): Text;
# the mapped list count field which will be encoded
annotation mapuniontag @0xdce06d41858f91ac (union, struct): Text;
# the mapped tag (which) of union

File diff suppressed because it is too large Load diff

28
examples/book/book.capnp Normal file
View file

@ -0,0 +1,28 @@
@0xf9ffb48dde27c0e6;
using C = import "/c.capnp";
$C.fieldgetset;
$C.codecgen;
$C.extraheader("#include <book.h>");
struct Chapter $C.mapname("chapter_t") {
caption @0: Text;
start @1: UInt32;
end @2: UInt32;
}
struct Publish $C.mapname("publish_t") {
isbn @0: UInt64;
year @1: UInt32;
}
struct Book $C.mapname("book_t") {
title @0: Text;
authors @1: List(Text) $C.mapname("authors") $C.maplistcount("n_authors");
chapters @5: List(Chapter) $C.maplistcount("n_chapters");
publish @6: Publish;
magic1 @2: List(UInt32) $C.maplistcount("n_magic1");
acquire :union $C.mapuniontag("acquire_method") {
buy @3: Text;
donation @4: Text;
}
}

35
examples/book/book.h Normal file
View file

@ -0,0 +1,35 @@
#if !defined(_BOOK_H_)
#define _BOOK_H_ 1
#include <stdint.h>
#include <string.h>
typedef struct {
char *caption;
int32_t start;
int32_t end;
} chapter_t;
typedef struct {
uint64_t isbn;
uint32_t year;
} publish_t;
typedef struct {
char *title;
int n_authors;
char **authors;
int n_chapters;
chapter_t *chapters;
publish_t publish;
int n_magic1;
uint32_t *magic1;
int acquire_method;
union {
char *buy;
char *donation;
} acquire;
} book_t;
#endif

128
examples/book/test.c Normal file
View file

@ -0,0 +1,128 @@
#include <stdio.h>
#include <stdlib.h>
#include "book.capnp.h"
#include "book.h"
static void usage(char *app) {
fprintf(stderr, "usage: %s encode | %s decode\n",
app);
}
int encode() {
struct capn c;
book_t book;
char *title = "Book title";
char *authors[2] = {
"author1",
"author2"
};
uint32_t magic1[2] = {
1101,1012
};
chapter_t chapters[3] = {
{.caption ="Chapter1",
.start =1,
.end=99},
{.caption = "Chapter2",
.start = 100,
.end = 150},
{.caption = "Chapter3",
.start = 151,
.end=199}
};
publish_t publish = {
.isbn = 335677,
.year =2001
};
struct capn_segment *cs;
struct Book b;
Book_ptr p;
book.title = title;
book.n_authors = 2;
book.authors = authors;
book.n_chapters = 3;
book.chapters = &chapters[0];
memcpy(&(book.publish), &publish, sizeof(publish));
book.n_magic1 = 2;
book.magic1 = &magic1[0];
book.acquire_method = Book_acquire_buy;
book.acquire.buy = "bought from Xinhua book store";
capn_init_malloc(&c);
cs = capn_root(&c).seg;
encode_Book_ptr(cs, &p, &book);
capn_setp(capn_root(&c), 0, p.p);
capn_write_fd(&c, write, 1, 0);
capn_free(&c);
return 0;
}
int decode() {
struct capn c;
Book_ptr p;
book_t book;
int i;
capn_init_fp(&c, stdin, 0);
p.p = capn_getp(capn_root(&c), 0, 1);
decode_Book_ptr(&book, p);
printf("title: %s\n", book.title);
printf("authors(%d):\n", book.n_authors);
for(i = 0; i < book.n_authors; i ++) {
printf("\t%s\n", book.authors[i]);
}
printf("chapters(%d):\n", book.n_chapters);
for(i = 0; i < book.n_chapters; i ++) {
printf("\tcaption: %s\n", book.chapters[i].caption);
printf("\tfrom %d to %d\n",
book.chapters[i].start,
book.chapters[i].end);
}
printf("ISBN: %lu year: %u\n",
book.publish.isbn,
book.publish.year);
printf("magic1:\n");
for(i = 0; i < book.n_magic1; i ++) {
printf("\t%d\n", book.magic1[i]);
}
if (book.acquire_method == Book_acquire_buy) {
printf("%s\n", book.acquire.buy);
}
else {
printf("%s\n", book.acquire.donation);
}
return 0;
}
int main(int argc,char *argv[]) {
if (argc != 2) {
usage(argv[0]);
return -1;
}
if ( strcmp(argv[1],"encode") == 0) {
encode();
}
else {
decode();
}
return 0;
}