M src/main.zig => src/main.zig +17 -4
@@ 1,11 1,27 @@
const std = @import("std");
const uwo = @import("uwo.zig");
+const MainError = error{
+ InvalidArgumentCount,
+};
+
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
- var image = try uwo.UwoImage.load(allocator, "test.uwo");
+ const args = try std.process.argsAlloc(allocator);
+ defer std.process.argsFree(allocator, args);
+
+ var image: uwo.UwoImage = undefined;
+ switch (args.len) {
+ 1 => image = try uwo.UwoImage.load(allocator, std.io.getStdIn().reader()),
+ 2 => {
+ var file = try std.fs.cwd().openFile(args[1], .{});
+ defer file.close();
+ image = try uwo.UwoImage.load(allocator, file.reader());
+ },
+ else => return MainError.InvalidArgumentCount,
+ }
defer image.deinit(allocator);
if (image.map_size > 0) {
@@ 21,13 37,10 @@ pub fn main() !void {
while (used[r] == max) {
r = rnd.random().int(u3);
}
- std.log.info("{}", .{r});
used[r] += 1;
image.map[i * 3 + 0] = if ((r & 1) != 0) 255 else 0;
image.map[i * 3 + 1] = if ((r & 2) != 0) 255 else 0;
image.map[i * 3 + 2] = if ((r & 4) != 0) 255 else 0;
- std.log.info("{}, {any}", .{ i, image.map[i * 3 .. i * 3 + 3] });
- std.log.info("{any}", .{image.map[0 .. image.map_size * 3]});
}
}
M src/uwo.zig => src/uwo.zig +3 -11
@@ 16,16 16,8 @@ pub const UwoImage = struct {
map: [256 * 3]u8,
data: []u8,
- pub fn load(allocator: Allocator, path: []const u8) !UwoImage {
- var file = try std.fs.cwd().openFile(path, .{});
- defer file.close();
-
- const file_size = (try file.stat()).size;
- if (file_size < header_len) {
- return UwoError.FileTooSmall;
- }
-
- const buf = try file.readToEndAlloc(allocator, file_size);
+ pub fn load(allocator: Allocator, reader: anytype) !UwoImage {
+ const buf = try reader.readAllAlloc(allocator, 1024 * 1024 * 1024 * 1024);
defer allocator.free(buf);
if (buf[0] != 'u' or buf[1] != 'w' or buf[2] != 'o') {
@@ 39,7 31,7 @@ pub const UwoImage = struct {
var buf_cursor: usize = header_len;
const expected_file_size = header_len + if (map_size > 0) size + 3 * map_size else 3 * size;
- if (expected_file_size != file_size) {
+ if (expected_file_size != buf.len) {
return UwoError.UnexpectedFileSize;
}