diff --git a/src/lib/args.zig b/src/lib/args.zig new file mode 100644 index 0000000..a206d61 --- /dev/null +++ b/src/lib/args.zig @@ -0,0 +1,16 @@ +const std = @import("std"); +const process = std.process; +const print = std.debug.print; + +pub fn getFirstArg() [:0]const u8 { + var args = process.args(); + defer args.deinit(); + + // Skip the first argument since it's the name of the program. + _ = args.skip(); + + return args.next() orelse { + print("Provide at least one input\n", .{}); + process.exit(1); + }; +} diff --git a/src/lib/files.zig b/src/lib/files.zig new file mode 100644 index 0000000..5d124c0 --- /dev/null +++ b/src/lib/files.zig @@ -0,0 +1,13 @@ +const std = @import("std"); +const fs = std.fs; +const File = std.fs.File; + +pub fn openForReading(rel_path: []const u8) !File { + var abs_path_buff: [fs.max_path_bytes]u8 = undefined; + const abs_path = try fs.realpath(rel_path, &abs_path_buff); + return try fs.openFileAbsolute(abs_path, File.OpenFlags{ .mode = .read_only }); +} + +pub fn readLine(file: File, buf: []u8) ![]u8 { + return try file.reader().readUntilDelimiter(buf, '\n'); +}