Add support for writing bytes

This commit is contained in:
2014-10-14 11:21:30 +02:00
parent f1f6033618
commit 28c28578cf

View File

@@ -112,12 +112,26 @@ def closeBlendFile(afile):
# Write a string to the file. # Write a string to the file.
###################################################### ######################################################
def WriteString(handle, astring, fieldlen): def WriteString(handle, astring, fieldlen):
assert(isinstance(astring, str))
stringw = "" stringw = ""
if len(astring) >= fieldlen: if len(astring) >= fieldlen:
stringw = astring[0:fieldlen] stringw = astring[0:fieldlen]
else: else:
stringw = astring + '\0' stringw = astring + '\0'
handle.write(stringw.encode()) handle.write(stringw.encode('utf-8'))
def WriteBytes(handle, astring, fieldlen):
assert(isinstance(astring, (bytes, bytearray)))
stringw = b''
if len(astring) >= fieldlen:
stringw = astring[0:fieldlen]
else:
stringw = astring + b'\0'
print(stringw)
print(handle)
handle.write(stringw)
###################################################### ######################################################
# ReadString reads a String of given length from a file handle # ReadString reads a String of given length from a file handle
@@ -605,7 +619,10 @@ class DNAStructure:
ftype = field[0] ftype = field[0]
if len(rest) == 0: if len(rest) == 0:
if ftype[0] == "char": if ftype[0] == "char":
if type(value) is str:
return WriteString(handle, value, fname.array_size) return WriteString(handle, value, fname.array_size)
else:
return WriteBytes(handle, value, fname.array_size)
else: else:
return ftype[2].field_set(header, handle, rest, value) return ftype[2].field_set(header, handle, rest, value)
else: else: