A bit more work on metadata reader
This commit is contained in:
+35
-1
@@ -1,11 +1,45 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const io = std.io;
|
||||||
|
|
||||||
const MetadataHeader = packed struct {
|
const MetadataHeader = packed struct {
|
||||||
compressed: bool,
|
not_compressed: bool,
|
||||||
size: u15,
|
size: u15,
|
||||||
};
|
};
|
||||||
|
|
||||||
const MetadataReader = struct {
|
const MetadataReader = struct {
|
||||||
rdr: std.io.AnyReader,
|
rdr: std.io.AnyReader,
|
||||||
|
alloc: std.heap.Allocator,
|
||||||
curBlock: []const u8,
|
curBlock: []const u8,
|
||||||
|
curOffset: u16,
|
||||||
|
|
||||||
|
pub fn init(rdr: io.AnyReader, alloc: std.heap.Allocator) !MetadataReader {
|
||||||
|
const out = .{
|
||||||
|
.rdr = rdr,
|
||||||
|
.alloc = alloc,
|
||||||
|
.curBlock = undefined,
|
||||||
|
.curOffset = 0,
|
||||||
|
};
|
||||||
|
try out.readNextBlock();
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
pub fn any(self: MetadataReader) !io.AnyReader {
|
||||||
|
return .{
|
||||||
|
.context = @ptrCast(&self),
|
||||||
|
.readFn = typeErasedReadFn,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn readNextBlock(self: MetadataReader) !void {
|
||||||
|
if (self.curBlock != undefined) {
|
||||||
|
self.alloc.free(self.curBlock);
|
||||||
|
}
|
||||||
|
self.curOffset = 0;
|
||||||
|
const hdr = try self.rdr.readStruct(MetadataHeader);
|
||||||
|
const buf = try self.alloc.alloc(u8, hdr.size);
|
||||||
|
if (hdr.not_compressed) {
|
||||||
|
self.curBlock = buf;
|
||||||
|
} else {
|
||||||
|
//TODO: decompress
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ pub fn newReader(filename: []const u8) !Reader {
|
|||||||
errdefer alloc.deinit();
|
errdefer alloc.deinit();
|
||||||
const super = try file.reader().readStruct(Superblock);
|
const super = try file.reader().readStruct(Superblock);
|
||||||
try super.valid();
|
try super.valid();
|
||||||
|
|
||||||
return Reader{
|
return Reader{
|
||||||
.super = super,
|
.super = super,
|
||||||
.rdr = file,
|
.rdr = file,
|
||||||
|
|||||||
Reference in New Issue
Block a user