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()`)
|
||||
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(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const file = try std.fs.cwd().openFile(filename, .{});
|
||||
defer file.close();
|
||||
// try day01part1(allocator, false);
|
||||
try day01part1(allocator, true);
|
||||
}
|
||||
|
||||
var buf_reader = std.io.bufferedReader(file.reader());
|
||||
const reader = buf_reader.reader();
|
||||
fn day01part1(allocator: std.mem.Allocator, comptime is_test: bool) !void {
|
||||
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);
|
||||
defer line.deinit();
|
||||
|
||||
const writer = line.writer();
|
||||
while (reader.streamUntilDelimiter(writer, '\n', null)) {
|
||||
defer line.clearRetainingCapacity();
|
||||
|
||||
print(":D {s}\n", .{line.items});
|
||||
} else |err| switch (err) {
|
||||
error.EndOfStream => {},
|
||||
while (true) {
|
||||
const bytes = line_reader.next() catch |err| switch (err) {
|
||||
error.EndOfStream => {
|
||||
break;
|
||||
},
|
||||
else => return err,
|
||||
};
|
||||
|
||||
print("Bytes: {s}\n", .{bytes});
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,6 +37,42 @@ fn file_name(comptime is_test: bool, comptime day: []const u8) []const u8 {
|
||||
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" {
|
||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
||||
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
||||
|
Loading…
Reference in New Issue
Block a user