import os

def read_lines_after_bytes(file_path, bytes_to_search):
    lines = []
    with open(file_path, "rb") as file:
        content = file.read()
        for byte in bytes_to_search:
            index = content.find(byte)
            while index != -1:
                next_null_index = content.find(b'\x00', index)
                if next_null_index != -1:
                    try:
                        line = content[index + 1:next_null_index].decode('utf-8')
                        line = line.replace(chr(0), '\\n')
                        lines.append(line)
                    except UnicodeDecodeError:
                        pass
                    index = content.find(byte, next_null_index)
                else:
                    break
    return lines

def read_lines_after_byte_178(file_path):
    lines = []
    with open(file_path, "rb") as file:
        content = file.read()
        index = content.find(b'\xB2')  # byte 178
        while index != -1:
            index += 1
            next_null_index = content.find(b'\x00', index)
            if next_null_index != -1:
                try:
                    line = content[index:next_null_index].decode('utf-8')
                    line = line.replace(chr(0), '\\n')
                    lines.append(line)
                except UnicodeDecodeError:
                    pass
                index = content.find(b'\xB2', next_null_index)
            else:
                break
    return lines

def read_lines_after_byte_179(file_path):
    lines = []
    with open(file_path, "rb") as file:
        content = file.read()
        index = content.find(b'\xB3')  # byte 179
        while index != -1:
            index += 1
            next_null_index = content.find(b'\x00', index)
            if next_null_index != -1:
                try:
                    line = content[index:next_null_index].decode('utf-8')
                    line = line.replace(chr(0), '\\n')
                    lines.append(line)
                except UnicodeDecodeError:
                    pass
                index = content.find(b'\xB3', next_null_index)
            else:
                break
    return lines

def read_lines_after_byte_180(file_path):
    lines = []
    with open(file_path, "rb") as file:
        content = file.read()
        index = content.find(b'\xB4')  # byte 180
        while index != -1:
            index += 1
            next_null_index = min(
                (i for seq in [b'\x00\x00', b'\x00\x8d', b'\x00\xb8']
                 if (i := content.find(seq, index)) != -1),
                default=-1
            )
            if next_null_index != -1:
                try:
                    line = content[index:next_null_index].decode('utf-8')
                    line = line.replace(chr(0), '\\n')
                    lines.append(line)
                except UnicodeDecodeError:
                    pass
                index = content.find(b'\xB4', next_null_index)
            else:
                break
    return lines

def read_lines_after_byte_191(file_path):
    lines = []
    with open(file_path, "rb") as file:
        content = file.read()
        index = content.find(b'\xBF')  # byte 191
        while index != -1:
            index += 1
            next_null_index = content.find(b'\x00', index)
            if next_null_index != -1:
                try:
                    line = content[index:next_null_index].decode('utf-8')
                    line = line.replace(chr(0), '\\n')
                    lines.append(line)
                except UnicodeDecodeError:
                    pass
                index = content.find(b'\xBF', next_null_index)
            else:
                break
    return lines

def read_lines_after_byte_202(file_path):
    lines = []
    with open(file_path, "rb") as file:
        content = file.read()
        index = content.find(b'\xCA')  # byte 202
        while index != -1:
            index += 1
            next_null_index = content.find(b'\x00', index)
            if next_null_index != -1:
                try:
                    line = content[index:next_null_index].decode('utf-8')
                    line = line.replace(chr(0), '\\n')
                    lines.append(line)
                except UnicodeDecodeError:
                    pass
                index = content.find(b'\xCA', next_null_index)
            else:
                break
    return lines

def process_raw_files(directory):
    for filename in os.listdir(directory):
        if filename.endswith(".raw"):
            file_path = os.path.join(directory, filename)
            lines = read_lines_after_byte_178(file_path)
            lines += read_lines_after_byte_179(file_path)
            lines += read_lines_after_byte_180(file_path)
            lines += read_lines_after_byte_191(file_path)
            lines += read_lines_after_byte_202(file_path)
            lines += read_lines_after_bytes(file_path, [b'\x89', b'\x80'])

            if lines:
                with open("output_.txt", "a", encoding="utf-8") as output_file:
                    output_file.write(f"File: {filename}\n")
                    output_file.write("\n".join(lines))
                    output_file.write("\n\n")

# Run processing
process_raw_files("text")