cembed

convert files to C char arrays
git clone git://git.rr3.xyz/cembed
Log | Files | Refs | LICENSE

commit 16f90fe070d32fa56ba72f69ed285ad60251028c
parent 3052308aaa824356317c61cd4ecc494dc8848dcf
Author: Robert Russell <robert@rr3.xyz>
Date:   Mon, 11 Nov 2024 19:57:06 -0800

Remove multiple resource functionality

Diffstat:
Mcembed.c | 117+++++++++++++++++++++-----------------------------------------------------------
1 file changed, 30 insertions(+), 87 deletions(-)

diff --git a/cembed.c b/cembed.c @@ -1,62 +1,9 @@ #include <errno.h> #include <rcx/all.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> -typedef struct resource Resource; - -struct resource { - char *decl; - char *name; - usize size; - char *data; -}; - -char *argv0; - -void -print_usage(FILE *file) { - fprintf(file, "usage: %s [-h|--help] DECL0:NAME0=PATH0 DECL1:NAME1=PATH1 ...\n", argv0); -} - -Resource -parse_resource(char *arg) { - char *p = arg; - - char *colon = strchr(p, ':'); - if (!colon) goto fail; - *colon = '\0'; - char *decl = p; - p = colon + 1; - - char *equals = strchr(p, '='); - if (!equals) goto fail; - *equals = '\0'; - char *name = p; - p = equals + 1; - - char *path = p; - - usize size; - void *data = r_vmem_open(&size, 0, path, 0, "r"); - if (!data) { - fprintf(stderr, "r_vmem_open: %s\n", strerror(errno)); - exit(1); - } - - return (Resource){ - .decl = decl, - .name = name, - .size = size, - .data = data, - }; - -fail: - fprintf(stderr, "invalid resource: \"%s\"\n", arg); - print_usage(stderr); - exit(1); -} +#define USAGE "usage: %s [-h|--help] NAME PATH\n" usize format_char(char *buf, char c) { @@ -91,50 +38,46 @@ format_char(char *buf, char c) { return 4; } -void -print_resource(Resource rsc) { - printf("%s%schar %s[] =\n\t\"", rsc.decl, rsc.decl[0] ? " " : "", rsc.name); - usize len = 0; - for (usize i = 0; i < rsc.size; i++) { - if (len >= 72) { - printf("\"\n\t\""); - len = 0; - } - char buf[5]; - len += format_char(buf, rsc.data[i]); - fputs(buf, stdout); - } - printf("\";\n"); -} - -// TODO: Accept only one resource. The user must concatenate externally. -// This also makes adding decl prefixes possible externally, so we can get -// rid of that functionality. - int main(int argc, char **argv) { - argv0 = argv[0]; - // TODO: Replace this with a proper option parsing from RCX (does not // exist at the time of writing). for (usize i = 1; i < argc; i++) { if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - print_usage(stdout); - exit(0); + printf(USAGE, argv[0]); + return 0; } } - - int nrscs = argc - 1; - REQUIRE(nrscs >= 0); - Resource *rscs = r_eallocn(nrscs, sizeof *rscs); - for (usize i = 0; i < nrscs; i++) - rscs[i] = parse_resource(argv[i + 1]); + if (argc != 3) { + fprintf(stderr, USAGE, argv[0]); + return 1; + } + + char *name = argv[1]; + char *path = argv[2]; - for (usize i = 0; i < nrscs; i++) { - if (i > 0) printf("\n"); - print_resource(rscs[i]); + usize size; + char *data = r_vmem_open(&size, 0, path, 0, "r"); + if (!data) { + fprintf(stderr, "r_vmem_open: %s\n", strerror(errno)); + return 1; + } + + printf("char %s[] =\n\t\"", name); + usize line_len = 0; + for (usize i = 0; i < size; i++) { + if (line_len >= 72) { + printf("\"\n\t\""); + line_len = 0; + } + + char buf[5]; + line_len += format_char(buf, data[i]); + + fputs(buf, stdout); } + printf("\";\n"); return 0; }