From 90f503f7c0d7c479ee93c1364259d10e344c5312 Mon Sep 17 00:00:00 2001 From: kayomn Date: Tue, 25 Oct 2022 00:40:14 +0100 Subject: [PATCH] More work on OAR packing utility --- src/core/io.zig | 19 ++++++++++++++- src/oar/main.zig | 62 ++++++++++++++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/core/io.zig b/src/core/io.zig index c04fab1..c6dd1cb 100644 --- a/src/core/io.zig +++ b/src/core/io.zig @@ -187,6 +187,23 @@ test "Spliterating text" { /// pub const Writer = meta.Function(@sizeOf(usize), []const u8, usize); +/// +/// Returns `true` if `elements` starts with the characters in `with`, otherwise `false`. +/// +pub fn begins(comptime Element: type, elements: []const Element, with: []const Element) bool { + if (elements.len < with.len) return false; + + return equals(Element, elements[0 .. with.len], with); +} + +test "Check memory begins with" { + const bytes_sequence = &.{69, 42}; + const testing = std.testing; + + try testing.expect(begins(u8, &.{69, 42, 0, 89}, bytes_sequence)); + try testing.expect(!begins(u8, &.{69, 89, 42, 0}, bytes_sequence)); +} + /// /// Returns `true` if `this` is the same length and contains the same data as `that`, otherwise /// `false`. @@ -203,7 +220,7 @@ pub fn equals(comptime Element: type, this: []const Element, that: []const Eleme return true; } -test "Memory buffers equal" { +test "Check memory is equals" { const bytes_sequence = &.{69, 42, 0}; const testing = std.testing; diff --git a/src/oar/main.zig b/src/oar/main.zig index 9573ba7..99066ae 100644 --- a/src/oar/main.zig +++ b/src/oar/main.zig @@ -76,7 +76,7 @@ pub const Path = extern struct { /// Returns `true` if `this_path` is equal to `that_path, otherwise `false`. /// pub fn equals(this_path: Path, that_path: Path) bool { - return core.io.equalsBytes(this_path.buffer[0 ..this_path. + return core.io.equals(u8, this_path.buffer[0 ..this_path. length], that_path.buffer[0 .. that_path.length]); } @@ -145,44 +145,54 @@ pub const Path = extern struct { pub const seperator = '/'; }; +test "Path" { + const testing = std.testing; + const empty_path = Path.empty; + + try testing.expectEqual(empty_path.length, 0); + try testing.expect(empty_path.equals(Path.empty)); + + const joined_component_path = try Path.joined(&.{"path", "to/my", "/file"}); + const joined_normalized_path = try Path.joined(&.{"path/to/my/file"}); + + try testing.expectEqual(empty_path.length, joined_normalized_path.length); + try testing.expect(joined_component_path.equals(joined_normalized_path)); +} + /// /// Starts the **O**na **Ar**chive packer utility. /// -pub fn main() !u8 { +pub fn main() u8 { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - defer _ = gpa.deinit(); + defer std.debug.assert(!gpa.deinit()); - const process = std.process; const allocator = gpa.allocator(); - const args = try process.argsAlloc(allocator); + const out_writer = std.io.getStdOut().writer(); + const process = std.process; + + const args = process.argsAlloc(allocator) catch { + try out_writer.print("Failed to allocate args memory", .{}); + + return 1; + }; defer process.argsFree(allocator, args); - const outWriter = std.io.getStdOut().writer(); + if (args.len < 2) { + try out_writer.print("Usage: oar [OPTION]... [FILE]...", .{}); + try out_writer.print("Options and arguments", .{}); - if (args.len > 1) { - const command = args[1]; - const io = core.io; - - if (io.equalsBytes(command, "pack")) { - return 0; - } - - if (io.equalsBytes(command, "unpack")) { - return 0; - } - - try outWriter.print("Unknown command: {s}", .{command}); - - return 1; + return 0; } - try outWriter.print("{s}", .{args[0]}); + const arg = std.mem.sliceTo(args[1], 0); + + if (core.io.begins(arg, "--create")) { + return 0; + } + + try out_writer.print("Unrecognized command-line option `{s}`", .{arg}); return 0; } - -test { - -}