merge code
This commit is contained in:
parent
1089ab8441
commit
5d64af4841
5 changed files with 2510 additions and 1522 deletions
128
examples/book/test.c
Normal file
128
examples/book/test.c
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue