dtob-sys 0.1.0

Raw FFI bindings to the dtob C library (encoder + decoder).
Documentation
#include "cli.h"

int main(int argc, char **argv)
{
    int indent = 2;
    int show_types = 0;
    const char *path = NULL;

    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "--flat") == 0) {
            indent = 0;
        } else if (strcmp(argv[i], "--indent") == 0) {
            if (++i >= argc) {
                fprintf(stderr, "error: --indent requires a value\n");
                return 1;
            }
            indent = atoi(argv[i]);
        } else if (strcmp(argv[i], "--types") == 0) {
            show_types = 1;
        } else if (argv[i][0] == '-') {
            fprintf(stderr, "unknown flag: %s\n", argv[i]);
            return 1;
        } else {
            path = argv[i];
        }
    }

    if (!path) {
        fprintf(stderr, "usage: dtob represent <file.dtob> [--indent N] [--flat] [--types]\n");
        return 1;
    }

    size_t len;
    uint8_t *buf = read_file(path, &len);
    if (!buf) return 1;

    DtobTypesHeader types;
    dtob_types_init(&types);
    DtobValue *root = dtob_decode_with_types(buf, len, &types);
    free(buf);

    if (!root) {
        fprintf(stderr, "error: failed to decode DTOB\n");
        return 1;
    }

    if (show_types)
        repr_types(&types);

    repr_value(root, indent, &types);

    for (size_t i = 0; i < types.count; i++)
        free(types.entries[i].name);
    dtob_free(root);
    return 0;
}