Skip to content

Instantly share code, notes, and snippets.

@LemonHaze420
Created November 24, 2024 21:55
Show Gist options
  • Save LemonHaze420/4d4d2eb20ac31cc73864a7e035f99071 to your computer and use it in GitHub Desktop.
Save LemonHaze420/4d4d2eb20ac31cc73864a7e035f99071 to your computer and use it in GitHub Desktop.
split up output
import os
import re
import sys
def split_c_file(input_file, output_dir):
os.makedirs(output_dir, exist_ok=True)
marker_pattern = re.compile(r"//----- \(0000000[0-9A-Fa-f]+\) ----------------------------------------------------")
function_decl_keyword = "Function declarations"
data_decl_keyword = "Data declarations"
try:
with open(input_file, 'r', encoding='utf-8', errors='ignore') as file:
lines = file.readlines()
except Exception as e:
print(f"Error reading file {input_file}: {e}")
sys.exit(1)
functions = []
current_function = []
remainder_lines = []
in_function = False
in_function_decl_block = False
function_declarations = []
for line in lines:
if marker_pattern.match(line):
if current_function:
functions.append(current_function)
current_function = []
in_function = True
if function_decl_keyword in line and line.strip().startswith("//"):
in_function_decl_block = True
continue
elif data_decl_keyword in line and line.strip().startswith("//") and in_function_decl_block:
in_function_decl_block = False
continue
if in_function_decl_block:
function_declarations.append(line)
continue
if in_function:
current_function.append(line)
else:
remainder_lines.append(line)
if current_function:
functions.append(current_function)
for i, function_lines in enumerate(functions):
output_file = os.path.join(output_dir, f"function_{i + 1}.c")
with open(output_file, 'w', encoding='utf-8') as f:
includes = "#include \"types.h\"\n#include \"data.h\"\n#include \"functions.h\"\n\n"
function_lines.insert(0, includes)
f.writelines(function_lines)
functions_header_path = os.path.join(output_dir, "functions.h")
with open(functions_header_path, 'w', encoding='utf-8') as func_header:
if function_declarations:
includes = "#pragma once\n#include \"types.h\"\n\n"
function_declarations.insert(0, includes)
func_header.writelines(function_declarations)
data_header_path = os.path.join(output_dir, "data.h")
with open(data_header_path, 'w', encoding='utf-8') as data_header:
includes = "#pragma once\n#include \"types.h\"\n\n"
remainder_lines.insert(0, includes)
data_header.writelines(remainder_lines)
print(f"Successfully split {len(functions)} functions into {output_dir}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python split.py <input_c_file> <output_directory>")
sys.exit(1)
input_file = sys.argv[1]
output_dir = sys.argv[2]
split_c_file(input_file, output_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment