From b0e0247b21bf2cfb3d4bdd633b80fd171129b445 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sun, 25 Jul 2021 21:08:02 +0100 Subject: [PATCH] Add ascii to the packed data --- examples/complete/Makefile | 2 +- examples/complete/pack.c | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/complete/Makefile b/examples/complete/Makefile index 23b87e0c..a7096825 100644 --- a/examples/complete/Makefile +++ b/examples/complete/Makefile @@ -9,7 +9,7 @@ all: $(PROG) $(PROG): $(CC) pack.c -o pack ./pack $(FILES_TO_EMBED) > fs.c - $(CC) -W -Wall -Wextra -O3 -g3 $(CFLAGS) -o $(PROG) $(SOURCES) + $(CC) -W -Wall -Wextra -Os -g3 $(CFLAGS) -o $(PROG) $(SOURCES) ROOT ?= $(realpath $(CURDIR)/../..) DOCKER ?= docker run -it --rm -e Tmp=. -e WINEDEBUG=-all -v $(ROOT):$(ROOT) -w $(CURDIR) mdashnet/vc98 wine diff --git a/examples/complete/pack.c b/examples/complete/pack.c index 7c5d1adc..c2561f42 100644 --- a/examples/complete/pack.c +++ b/examples/complete/pack.c @@ -24,7 +24,7 @@ static const char *code = "const char *unpack(const char *name, size_t *size) {\n" " const struct packed_file *p;\n" - " for (p = packed_files; p->name != NULL; p++) {\n" + " for (p = g_packed_files; p->name != NULL; p++) {\n" " if (strcmp(p->name, name) != 0) continue;\n" " if (size != NULL) *size = p->size - 1;\n" " return (const char *) p->data;\n" @@ -41,16 +41,22 @@ int main(int argc, char *argv[]) { for (i = 1; i < argc; i++) { FILE *fp = fopen(argv[i], "rb"); + char ascii[12]; if (fp == NULL) exit(EXIT_FAILURE); - printf("static const unsigned char v%d[] = {", i); + printf("static const unsigned char v%d[] = {\n", i); for (j = 0; (ch = fgetc(fp)) != EOF; j++) { - if ((j % 12) == 0) printf("%s", "\n "); + if (j == (int) sizeof(ascii)) { + printf(" // %.*s\n", j, ascii); + j = 0; + } + ascii[j] = (ch >= ' ' && ch <= '~' && ch != '\\') ? ch : '.'; printf(" %3u,", ch); } // Append zero byte at the end, to make text files appear in memory // as nul-terminated strings. - printf("%s", " 0x00\n};\n"); + // printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii); + printf(" 0 // %.*s\n};\n", j, ascii); fclose(fp); } @@ -58,7 +64,7 @@ int main(int argc, char *argv[]) { printf("%s", " const char *name;\n"); printf("%s", " const unsigned char *data;\n"); printf("%s", " size_t size;\n"); - printf("%s", "} packed_files[] = {\n"); + printf("%s", "} g_packed_files[] = {\n"); for (i = 1; i < argc; i++) { printf(" {\"%s\", v%d, sizeof(v%d) },\n", argv[i], i, i);