From 1be35411cf23c1b8cc909c2f2e1d1030e2f5015a Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Thu, 19 Sep 2024 06:53:00 +0200 Subject: Added embedding of files into compiled binary with Zig --- README.md | 1 + zig-embed/Makefile | 5 +++++ zig-embed/main.zig | 14 ++++++++++++++ zig-embed/max.txt | 7 +++++++ 4 files changed, 27 insertions(+) create mode 100644 zig-embed/Makefile create mode 100644 zig-embed/main.zig create mode 100644 zig-embed/max.txt diff --git a/README.md b/README.md index b4a1632..fe1b931 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ running it. | [zig-wad](./zig-wad) | zig-0.11.0 | Reads doom.wad and extracts the identification header. | | [zig-os-props](./zig-os-props) | zig-0.11.0 | Detects properties of the target operating system. | | [zig-tlv-encoding](./zig-tlv-encoding) | zig-0.13.0 | Naive implementation of TLV encoding in Zig. | +| [zig-embed](./zig-embed) | zig-0.13.0 | Embedding external resources in compiled binary. | ## License diff --git a/zig-embed/Makefile b/zig-embed/Makefile new file mode 100644 index 0000000..d78c528 --- /dev/null +++ b/zig-embed/Makefile @@ -0,0 +1,5 @@ +default: + zig run main.zig + +build: + zig build-exe main.zig diff --git a/zig-embed/main.zig b/zig-embed/main.zig new file mode 100644 index 0000000..a1089b9 --- /dev/null +++ b/zig-embed/main.zig @@ -0,0 +1,14 @@ +const std = @import("std"); + +pub fn main() !void { + const data: []const u8 = @embedFile("max.txt"); + + // Print the whole file out. + std.debug.print("{s}\n", .{data}); + + // Loop over each byte. + for (data) |c| { + std.debug.print("0x{X:0>2} ", .{c}); + } + std.debug.print("\n", .{}); +} diff --git a/zig-embed/max.txt b/zig-embed/max.txt new file mode 100644 index 0000000..5eedd45 --- /dev/null +++ b/zig-embed/max.txt @@ -0,0 +1,7 @@ +"What you've just said is one of the most insanely idiotic things I +have ever heard. At no point in your rambling, incoherent response +were you even close to anything that could be considered a rational +thought. Everyone in this room is now dumber for having listened to +it." + +— Josh Mostel as Principal Max Anderson in Billy Madison (1995) -- cgit v1.2.3