Skip to content

Instantly share code, notes, and snippets.

@1sudo
Last active March 5, 2021 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1sudo/688bac2dd87b26c752145899aa88de3c to your computer and use it in GitHub Desktop.
Save 1sudo/688bac2dd87b26c752145899aa88de3c to your computer and use it in GitHub Desktop.
class STFWriter:
def __init__(self):
self.b = bytearray()
self.row_count = None
self.character_count = None
self.fname = None
def parse_bytes(self, value, num_bytes=None):
if num_bytes is not None:
x = value.to_bytes(num_bytes, 'little')
self.b.extend(x)
elif value <= 255:
self.b.extend([value])
elif (value > 255) and (value <= 65535):
x = value.to_bytes(2, 'little')
self.b.extend(x)
elif (value > 65535) and (value <= 16777215):
x = value.to_bytes(3, 'little')
self.b.extend(x)
elif (value > 16777215) and (value <= 335544319):
x = value.to_bytes(4, 'little')
self.b.extend(x)
else:
x = value.to_bytes(8, 'little')
self.b.extend(x)
def save_data(self, data, filepath=None):
# [205, 171] STF file (ABCD)
self.b.extend([205, 171, 0, 0, 0, 0, 0, 0, 0])
# row count
self.row_count = len(data)
self.parse_bytes(self.row_count, 4)
print("Row Count for value: {}".format(self.row_count))
for i in range(self.row_count):
# row number
self.parse_bytes(i + 1, 4)
self.b.extend([255, 255, 255, 255])
# character count
self.character_count = len(data[i][1])
self.parse_bytes(self.character_count, 4)
print("Character count for value: {}".format(self.character_count))
for i in data[i][1]:
self.parse_bytes(ord(i))
self.b.extend([0])
for i in range(self.row_count):
# row number
self.parse_bytes(i + 1, 4)
# character count
self.character_count = len(data[i][0])
self.parse_bytes(self.character_count, 4)
for i in data[i][0]:
self.parse_bytes(ord(i))
if filepath is not None:
output_file = open(filepath, "wb")
output_file.write(self.b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment