#Endian-Aware, Seekable File Reader

1 messages · Page 1 of 1 (latest)

stable jungle
#

I'm trying to write a library in Zig that reads file formats designed for the Xbox 360, which use both big-endian and little-endian values.
Ideally, I want to be able to seek the reader, change the endianness of the reader, and read byte-by-byte like so:

reader.seekTo(0);
var foo = reader.readByte();
reader.setEndian(Endian.big);
var bar = reader.readInt(u32);
// etc.

References:

If there's a better way, it would be nice to see an example that can at least handle two of the three. Doing it in a completely different way might be kind of hard to manage.
Searching the docs, I've found that I can either...

  • Use the File type, call seekTo on that, and read all the bytes of the file to a buffer array. This doesn't appear to expose a way to set the endian or read other types, leaving me to do all of that manually.
  • Or create a Reader from that File and call readByte or readInt(ReturnType, Endian, usize) on that. But this doesn't appear to expose seekTo.

Related: https://discord.com/channels/605571803288698900/1167211222911942666
I may be wrong but this didn't seem to solve my issue entirely either.

silent furnace
#

you seek on the file and then read from the associated reader

stable jungle
silent furnace
#

readers are attached to the file, they don't create like a new read head or anything

#

it's just a wrapper around the file's read function

stable jungle
#

ok cool

#

So just so I have it for later...

#
file.seekTo(0);
const reader = File.reader(file);
var foo = reader.readByte();
var bar = reader.readInt(u32, Endian.big, 4);
#

That should function exactly as I described in the original post?

#

I could write yet another wrapper around that to reduce the repetition, similar to Velocity's FileIO class. But that might just work.

silent furnace
#

mhm

#

tho u can just write .big, don't need to write Endian cus the type is already known :)

stable jungle
silent furnace
#

generally, if there's a known type, it can infer things :)

#

the only exception is errors i guess? but those are always error.Whatever so yknow

civic beacon
stable jungle
#

I still haven't been able to test it, but I think squirl's approach works best

civic beacon
#

it's basically the same approach, StreamSource just makes sources other than File more convenient

stable jungle
#

Neat