Changelog:
- Added methods to check for end of file
- `past_eof()` will return True if the current position is after the end of file.
- `eof()` will return True if the current position is at/ after the end of file.
- Allow trimming to 0 size
- `trim(0)` will now work.
- Fixed `read_bytes` returning a tuple
- Added basic struct-like implementation to allow classes to be read/written.
BinaryReader now has `read_struct` and `write_struct` methods. These methods are only used for calling the `__br_read__` and `__br_write__` methods of the given class/object, respectively. The class/object must inherit from `BrStruct`, a new class that contains the signatures of these methods.
Classes inheriting from `BrStruct` can read/write other `BrStruct` classes as well.
Example of a class inheriting from `BrStruct`:
py
from binary_reader import BinaryReader, BrStruct
class MyStruct(BrStruct):
def __br_read__(self, br):
no extra arguments are needed, but they can be given as *args in the
binary reader's read_struct and write_struct methods (check __br_write__ below)
self.value1 = br.read_int32()
self.value2 = br.read_float()
def __br_write__(self, br, some_value):
some_value is an additional argument that will be given to the binary reader's write_struct method
br.write_uint32(self.value1 + some_value)
br.write_float(self.value2)
Example usage of the new `read_struct` and `write_struct` methods:
py
will read and return 2 MyStruct objects as a tuple, after calling each one's __br_read__ method
(my_struct1, my_struct2) = br.read_struct(MyStruct, count=2)
will write my_struct1 by calling its __br_write__ method and passing 42 as the 'some_value' argument
br.write_struct(my_struct1, 42)