More work on OAR packing utility
continuous-integration/drone/pr Build is failing Details
continuous-integration/drone/push Build is failing Details

This commit is contained in:
kayomn 2022-10-25 00:40:14 +01:00
parent a2abd9d42d
commit 90f503f7c0
2 changed files with 54 additions and 27 deletions

View File

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

View File

@ -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 > 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;
}
try outWriter.print("{s}", .{args[0]});
if (args.len < 2) {
try out_writer.print("Usage: oar [OPTION]... [FILE]...", .{});
try out_writer.print("Options and arguments", .{});
return 0;
}
test {
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;
}