Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/ex-06.copy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const TokenIter = zargs.TokenIter;
const ztype = @import("ztype");
const String = ztype.String;
const Open = ztype.Open;
const Read = ztype.Read;

pub fn main() !void {
const cmd = Command.new("copy").alias("cp")
Expand All @@ -14,7 +15,7 @@ pub fn main() !void {
.homepage("https://github.com/kioz-wang/zargs")
.arg(Arg.opt("verbose", bool).short('v').long("verbose").help("Show detail"))
.arg(Arg.optArg("max", usize).long("max").help("Max byte to copy").default(32 << 10))
.arg(Arg.posArg("source", Open(.file, .{})))
.arg(Arg.posArg("source", Read()))
.arg(Arg.posArg("target", Open(.fileCreate, .{})))
.config(.{ .style = .classic });

Expand All @@ -28,11 +29,11 @@ pub fn main() !void {
if (args.verbose) {
std.debug.print("Try copy {s} to {s} with max 0x{x} bytes\n", .{ args.source.s, args.target.s, args.max });
}
const content = try args.source.v.reader().any().readAllAlloc(allocator, args.max);
defer allocator.free(content);
try args.target.v.writer().any().writeAll(content);
// const content = try args.source.v.reader().any().readAllAlloc(allocator, args.max);
// defer allocator.free(content);
try args.target.v.writer().any().writeAll(args.source.v);
if (args.verbose) {
std.debug.print("Done with 0x{x} bytes\n", .{content.len});
std.debug.print("Done with 0x{x} bytes\n", .{args.source.v.len});
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/type/base.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const String = []const u8;
pub const LiteralString = [:0]const u8;
4 changes: 2 additions & 2 deletions src/type/checker.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const std = @import("std");
const testing = std.testing;

const String = []const u8;
const LiteralString = [:0]const u8;
const String = @import("base.zig").String;
const LiteralString = @import("base.zig").LiteralString;

pub fn isBase(T: type) bool {
if (T == String) return true;
Expand Down
8 changes: 4 additions & 4 deletions src/type/open.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");

const String = []const u8;
const LiteralString = [:0]const u8;
const String = @import("base.zig").String;
const LiteralString = @import("base.zig").LiteralString;

const OpenType = enum { file, fileCreate, dir, dirCreate };
fn OpenFlags(openType: OpenType) type {
Expand Down Expand Up @@ -50,11 +50,11 @@ fn OpenFn(openType: OpenType, lazy: bool, flags: OpenFlags(openType)) type {
},
else => {},
}
if (!lazy) _ = self.unlazy() catch return null;
if (!lazy) _ = self.unlazy(a_maybe) catch return null;

return self;
}
pub fn unlazy(self: *Self) !OpenValue(openType) {
pub fn unlazy(self: *Self, _: ?std.mem.Allocator) !OpenValue(openType) {
if (!self.@".is_inited") {
self.v = switch (openType) {
.file => try std.fs.cwd().openFile(self.s, flags),
Expand Down
53 changes: 53 additions & 0 deletions src/type/read.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const std = @import("std");

const String = @import("base.zig").String;
const LiteralString = @import("base.zig").LiteralString;

fn ReadFn(lazy: bool) type {
return struct {
const Self = @This();
v: String = undefined,
s: String = undefined,
@".is_inited": bool = false,
pub fn parse(s: String, a_maybe: ?std.mem.Allocator) ?Self {
var self: Self = .{};

self.s = blk: {
const s_alloc = a_maybe.?.alloc(u8, s.len) catch return null;
@memcpy(s_alloc, s);
break :blk s_alloc;
};

if (!lazy) _ = self.unlazy(a_maybe) catch return null;

return self;
}
pub fn unlazy(self: *Self, a_maybe: ?std.mem.Allocator) !String {
if (!self.@".is_inited") {
var fp = if (std.mem.eql(u8, self.s, "-")) std.io.getStdIn() else try std.fs.cwd().openFile(self.s, .{});
defer fp.close();
self.v = try fp.readToEndAlloc(a_maybe.?, 4 << 10);
self.@".is_inited" = true;
}
return self.v;
}
pub fn destroy(self: *Self, a_maybe: ?std.mem.Allocator) void {
if (self.@".is_inited") a_maybe.?.free(self.v);
a_maybe.?.free(self.s);
}
pub fn format(self: Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
try writer.print("Read({s})", .{self.s});
}
};
}

pub const Read = struct {
pub fn f() type {
return ReadFn(false);
}
}.f;
pub const ReadLazy = struct {
pub fn f() type {
return ReadFn(true);
}
}.f;
9 changes: 7 additions & 2 deletions src/type/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
//! const ... = ztype.zzz;
//! ```

pub const String = []const u8;
pub const LiteralString = [:0]const u8;
const base = @import("base.zig");
pub const String = base.String;
pub const LiteralString = base.LiteralString;

pub const checker = @import("checker.zig");

const open = @import("open.zig");
pub const Open = open.Open;
pub const OpenLazy = open.OpenLazy;

const read = @import("read.zig");
pub const Read = read.Read;
pub const ReadLazy = read.ReadLazy;

test {
@import("std").testing.refAllDecls(@This());
}
Loading