From c26b90291466a3283440926318c414da487b4c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Krzy=C5=BCanowski?= Date: Tue, 3 Sep 2024 08:27:16 +0200 Subject: [PATCH] Add Zig Cat Example (draft) --- zig-cat/.gitignore | 2 ++ zig-cat/build.zig | 26 ++++++++++++++++++++++++++ zig-cat/src/main.zig | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 zig-cat/.gitignore create mode 100644 zig-cat/build.zig create mode 100644 zig-cat/src/main.zig diff --git a/zig-cat/.gitignore b/zig-cat/.gitignore new file mode 100644 index 0000000..dca1103 --- /dev/null +++ b/zig-cat/.gitignore @@ -0,0 +1,2 @@ +zig-out/ +.zig-cache/ diff --git a/zig-cat/build.zig b/zig-cat/build.zig new file mode 100644 index 0000000..888ed6d --- /dev/null +++ b/zig-cat/build.zig @@ -0,0 +1,26 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const exe = b.addExecutable(.{ + .name = "zig-cat", + .root_source_file = b.path("src/main.zig"), + .target = b.host, + }); + + b.installArtifact(exe); + + const run_exe = b.addRunArtifact(exe); + const run_step = b.step("run", "Run the application"); + + run_step.dependOn(&run_exe.step); + + const unit_tests = b.addTest(.{ + .root_source_file = b.path("src/main.zig"), + .target = b.host, + }); + + const test_step = b.step("test", "Run unit tests"); + const run_unit_tests = b.addRunArtifact(unit_tests); + + test_step.dependOn(&run_unit_tests.step); +} diff --git a/zig-cat/src/main.zig b/zig-cat/src/main.zig new file mode 100644 index 0000000..2c384d5 --- /dev/null +++ b/zig-cat/src/main.zig @@ -0,0 +1,26 @@ +const std = @import("std"); + +pub fn main() !void { + for (std.os.argv[1..]) |file_path_sent| { + const file_path = std.mem.span(file_path_sent); + const file = try std.fs.openFileAbsolute( + file_path, + .{ .mode = .read_only }, + ); + defer file.close(); + + while (true) { + const rw_result = file.reader().streamUntilDelimiter( + std.io.getStdOut().writer(), + '\n', + null, + ); + + if (rw_result) |_| {} else |_| { + break; + } + + std.debug.print("\n", .{}); + } + } +}