failed attempt to generalize reading file line by line
This commit is contained in:
parent
a8ee9f637e
commit
d37ce56eef
70
src/main.zig
70
src/main.zig
@ -6,32 +6,28 @@ pub fn main() !void {
|
|||||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||||
|
|
||||||
try day01part1(file_name(false, "01"));
|
|
||||||
try day01part1(file_name(true, "01"));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn day01part1(filename: []const u8) !void {
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
const file = try std.fs.cwd().openFile(filename, .{});
|
// try day01part1(allocator, false);
|
||||||
defer file.close();
|
try day01part1(allocator, true);
|
||||||
|
}
|
||||||
|
|
||||||
var buf_reader = std.io.bufferedReader(file.reader());
|
fn day01part1(allocator: std.mem.Allocator, comptime is_test: bool) !void {
|
||||||
const reader = buf_reader.reader();
|
const filename = file_name(is_test, "01");
|
||||||
|
const line_reader = try LineReader.init(allocator, filename);
|
||||||
|
errdefer line_reader.deinit();
|
||||||
|
|
||||||
var line = std.ArrayList(u8).init(allocator);
|
while (true) {
|
||||||
defer line.deinit();
|
const bytes = line_reader.next() catch |err| switch (err) {
|
||||||
|
error.EndOfStream => {
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
|
||||||
const writer = line.writer();
|
print("Bytes: {s}\n", .{bytes});
|
||||||
while (reader.streamUntilDelimiter(writer, '\n', null)) {
|
|
||||||
defer line.clearRetainingCapacity();
|
|
||||||
|
|
||||||
print(":D {s}\n", .{line.items});
|
|
||||||
} else |err| switch (err) {
|
|
||||||
error.EndOfStream => {},
|
|
||||||
else => return err,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +37,42 @@ fn file_name(comptime is_test: bool, comptime day: []const u8) []const u8 {
|
|||||||
return "inputs" ++ input_suffix ++ day;
|
return "inputs" ++ input_suffix ++ day;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const U8ArrayList = std.ArrayList(u8);
|
||||||
|
|
||||||
|
const LineReader = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
file: *std.fs.File,
|
||||||
|
line: *U8ArrayList,
|
||||||
|
|
||||||
|
pub fn init(allocator: std.mem.Allocator, filename: []const u8) !LineReader {
|
||||||
|
var file = try std.fs.cwd().openFile(filename, .{ .read = true });
|
||||||
|
var line = std.ArrayList(u8).init(allocator);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.allocator = allocator,
|
||||||
|
.file = &file,
|
||||||
|
.line = &line,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next(self: LineReader) ![]u8 {
|
||||||
|
// self.line.clearRetainingCapacity();
|
||||||
|
var line = std.ArrayList(u8).init(self.allocator);
|
||||||
|
errdefer line.deinit();
|
||||||
|
|
||||||
|
const reader = self.file.reader();
|
||||||
|
const writer = line.writer();
|
||||||
|
|
||||||
|
try reader.streamUntilDelimiter(writer, '\n', null);
|
||||||
|
return self.line.items;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: LineReader) void {
|
||||||
|
self.file.close();
|
||||||
|
self.line.deinit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
test "simple test" {
|
test "simple test" {
|
||||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
var list = std.ArrayList(i32).init(std.testing.allocator);
|
||||||
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
||||||
|
Loading…
Reference in New Issue
Block a user