Add BlendFileBlock.raw_data() and .as_string() functions

Add functions to interpret the data in a `BlendFileBlock` as either `bytes`
or `string`. This is used to obtain the contents of a `char*` (instead
of an embedded `char[N]` array).
This commit is contained in:
Sybren A. Stüvel
2023-05-16 16:01:38 +02:00
parent d2b353d1bf
commit 055457ab67
2 changed files with 21 additions and 0 deletions
@@ -610,6 +610,23 @@ class BlendFileBlock:
return value, field
return value
def raw_data(self) -> bytes:
"""Read low-level raw data of this datablock."""
self.bfile.fileobj.seek(self.file_offset, os.SEEK_SET)
return self.bfile.fileobj.read(self.size)
def as_string(self) -> str:
"""Interpret the bytes of this datablock as null-terminated utf8 string."""
the_bytes = self.raw_data()
try:
first_null = the_bytes.index(0)
except ValueError:
pass
else:
the_bytes = the_bytes[:first_null]
return the_bytes.decode()
def get_recursive_iter(
self,
path: dna.FieldPath,