From c15a5b7a21df4613c3073b62ad32d4780e4f44fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucio=20Andr=C3=A9s=20Illanes=20Albornoz?= Date: Wed, 28 Aug 2019 10:29:28 +0200 Subject: [PATCH] MiRCART-python/SAUCEToAnsi.py: added (for spoke.) --- MiRCART-python/SAUCEToAnsi.py | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 MiRCART-python/SAUCEToAnsi.py diff --git a/MiRCART-python/SAUCEToAnsi.py b/MiRCART-python/SAUCEToAnsi.py new file mode 100755 index 0000000..6fe5dc3 --- /dev/null +++ b/MiRCART-python/SAUCEToAnsi.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# +# SAUCEToAnsi.py -- convert SAUCE-encoded ANSi to raw ANSI file (for spoke) +# Copyright (c) 2019 Lucio Andrés Illanes Albornoz +# This project is licensed under the terms of the MIT licence. +# + +import os, re, struct, sys + +def SAUCEToAnsi(inPathName, outPathName): + with open(inPathName, "rb") as inFile: + inFileStat = os.stat(inPathName) + inFile.seek(inFileStat.st_size - 128, 0) + inFile.seek(5 + 2 + 35 + 20 + 20 + 8 + 4, 1) + if (inFile.read(1) != b'\x01') \ + or (inFile.read(1) != b'\x01'): + print("error: only character based ANSi SAUCE files are supported.", file=sys.stderr) + return 1 + else: + width, height = struct.unpack("H", inFile.read(2))[0], struct.unpack("H", inFile.read(2))[0] + print(width) + with open(outPathName, "w+") as outFile: + inFile.seek(0, 0) + inFileData, row, rowChars = inFile.read(inFileStat.st_size - 128).decode("cp437"), "", 0 + inFileChar, inFileCharMax = 0, len(inFileData) + while True: + if inFileChar >= inFileCharMax: + break + else: + m = re.match('\x1b\[((?:\d{1,3};?)+)m', inFileData[inFileChar:]) + if m: + row += m[0]; inFileChar += len(m[0]); + else: + m = re.match('\x1b\[(\d+)C', inFileData[inFileChar:]) + if m: + row += m[0]; inFileChar += len(m[0]); rowChars += int(m[1]); + elif inFileData[inFileChar:inFileChar+2] == "\r\n": + inFileChar += 2; rowChars = width; + elif inFileData[inFileChar] == "\r" \ + or inFileData[inFileChar] == "\n": + inFileChar += 1; rowChars = width; + else: + row += inFileData[inFileChar]; inFileChar += 1; rowChars += 1; + if rowChars >= width: + print(row, file=outFile); row = ""; rowChars = 0; + +# +# Entry point +def main(*argv): + SAUCEToAnsi(argv[1], argv[2]) +if __name__ == "__main__": + if (len(sys.argv) - 1) != 2: + print("usage: {} ".format(sys.argv[0]), file=sys.stderr) + else: + main(*sys.argv) + +# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120