Add Zig Cat Example (draft)

This commit is contained in:
Maciej Krzyżanowski 2024-09-03 08:27:16 +02:00
parent e394029a24
commit c26b902914
3 changed files with 54 additions and 0 deletions

2
zig-cat/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
zig-out/
.zig-cache/

26
zig-cat/build.zig Normal file
View File

@ -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);
}

26
zig-cat/src/main.zig Normal file
View File

@ -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", .{});
}
}
}