From fb97037243ea0a956dbc99f9ad25df5840471bfb Mon Sep 17 00:00:00 2001 From: Mid <> Date: Sat, 4 Jan 2025 10:45:25 +0200 Subject: [PATCH] Writer example --- README.md | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba8fb99..c809036 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,45 @@ The library is meant to be embedded directly into another project. ## Writing example - TBA + #include"eebie/writer.h" + + #include + #include + + static void *on_alloc(EBMLWriter *w, void *p, size_t sz) { + return realloc(p, sz); + } + + static size_t on_write(EBMLWriter *w, const void *p, size_t sz) { + FILE *fp = w->ud; + + return fwrite(p, 1, sz, fp); + } + + int main() { + EBMLWriter w; + ebml_writer_init(&w); + + FILE *output = fopen("test.ebml", "wb"); + w.ud = output; + + w.eventWrite = on_write; + w.eventAlloc = on_alloc; + + ebml_writer_push(&w, 0x1A45DFA3); + ebml_writer_put(&w, 0x4286, EBML_UNSIGNED_INTEGER, (EBMLPrimitive) {.uInt = 1}); + ebml_writer_put(&w, 0x4282, EBML_STRING, (EBMLPrimitive) {.string = "excrement"}); + ebml_writer_pop(&w); + + // Unknown-sized element must not have corresponding pop + ebml_writer_push_ind(&w, 0x18538067); + + ebml_writer_put(&w, 0x4123, EBML_STRING, (EBMLPrimitive) {.string = "yodel"}); + + while(!ebml_writer_flush(&w)); + + fclose(output); + } ## `schemagen`