import re
import glob, os
from pathlib import Path
from os.path import exists
import math
import shutil
import sys
cfiles = next(os.walk('./components'))[2]
cpucomps = []
for f in cfiles:
if f.startswith("h_"):
cpucomps.append(f[:-2])
cpucomps.sort()
pfiles = next(os.walk('./preprocessors'))[2]
cpupres = []
for f in pfiles:
if f.startswith("h_"):
cpupres.append(f[:-2])
cpupres.sort()
if len(sys.argv) != 3:
print("Usage: {} \"[preprocessor_name ...]\" \"component_name [component_name ...]\"".format(sys.argv[0]))
print("\nAvailable components are:")
for c in cpucomps:
print(c[2:], end = ' ')
print("\n\nAvailable preprocessors are: ")
for p in cpupres:
print(p[2:], end = ' ')
print("\n\n")
sys.exit()
else:
preprocessor_names_str = sys.argv[1].strip('"')
preprocessor_names = preprocessor_names_str.split()
component_names_str = sys.argv[2].strip('"')
component_names = component_names_str.split()
preprocessor_params = {}
pattern = r'\(([^)]+)\)'
for name in preprocessor_names:
match = re.search(pattern, name)
if match:
values = match.group(1).split(',')
paramc = len(values)
paramv = values
else:
paramc = 0
paramv = []
preprocessor_params[name] = {"paramc": paramc, "paramv": paramv}
shutil.copyfile('compressor-framework.cpp', 'compressor-standalone.cpp')
shutil.copyfile('decompressor-framework.cpp', 'decompressor-standalone.cpp')
comp_list = []
for f in component_names:
comp_list.append("h_" + f)
pre_list = []
for preprocessor in preprocessor_params.keys():
preprocessor_name = re.sub(r'\(.*\)', '', preprocessor).strip()
pre_list.append("h_" + preprocessor_name)
for f in comp_list:
if f not in cpucomps:
print(f, ": Component does not exist. Try again.")
quit()
for f in pre_list:
if f not in cpupres:
print(f, "Preprocessor does nor exist. Try again.")
quit()
comp_uni = []
for x in comp_list:
if x not in comp_uni:
comp_uni.append(x)
pre_uni = []
for x in pre_list:
if x not in pre_uni:
pre_uni.append(x)
with open("compressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##include-beg##[\s\S]*##include-end##", contents)
str_to_add = ''
for c in pre_uni:
str_to_add += "#include \"preprocessors/" + str(c) + ".h\"\n"
for c in comp_uni:
str_to_add += "#include \"components/" + str(c) + ".h\"\n"
contents = contents[:m.span()[0]] + "##include-beg##*/\n" + str_to_add + "/*##include-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
with open("decompressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##include-beg##[\s\S]*##include-end##", contents)
str_to_add = ''
for c in pre_uni:
str_to_add += "#include \"preprocessors/" + str(c) + ".h\"\n"
for c in comp_uni:
str_to_add += "#include \"components/" + str(c) + ".h\"\n"
contents = contents[:m.span()[0]] + "##include-beg##*/\n" + str_to_add + "/*##include-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
with open("compressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##print-beg##[\s\S]*##print-end##", contents)
str_to_add = ' printf(\"CPU LC 1.2 Algorithm:'
for c in pre_uni:
str_to_add += " " + str(c[2:])
for c in comp_uni:
str_to_add += " " + str(c[2:])
str_to_add += "\\n\");\n"
contents = contents[:m.span()[0]] + "##print-beg##*/\n" + str_to_add + "/*##print-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
with open("decompressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##print-beg##[\s\S]*##print-end##", contents)
str_to_add = ' printf(\"CPU LC 1.2 Algorithm:'
for c in pre_uni:
str_to_add += " " + str(c[2:])
for c in comp_uni:
str_to_add += " " + str(c[2:])
str_to_add += "\\n\");\n"
contents = contents[:m.span()[0]] + "##print-beg##*/\n" + str_to_add + "/*##print-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
with open("compressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##comp-encoder-beg##[\s\S]*##comp-encoder-end##", contents)
str_to_add = ''
for c in comp_list:
c = c[2:]
str_to_add += " if (good) {\n std::swap(in, out);\n good = h_" + str(c) + "(csize, in, out);\n }\n"
contents = contents[:m.span()[0]] + "##comp-encoder-beg##*/\n" + str_to_add + " /*##comp-encoder-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
with open("compressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##pre-encoder-beg##[\s\S]*##pre-encoder-end##", contents)
str_to_add = ''
first_iteration = True
for d in preprocessor_params.keys():
preprocessor_params_c = preprocessor_params[d]
paramc = preprocessor_params_c["paramc"]
paramv = preprocessor_params_c["paramv"]
c = re.sub(r'\(.*\)', '', d).strip()
if first_iteration:
params = "double paramv[] = {" + ", ".join(map(str, paramv)) + "};"
first_iteration = False
else:
params = "paramv[] = {" + ", ".join(map(str, paramv)) + "};"
str_to_add += f" {params}\n"
str_to_add += f" h_{c}(hpreencsize, hpreencdata, {paramc}, paramv);\n"
contents = contents[:m.span()[0]] + "##pre-encoder-beg##*/\n" + str_to_add + " /*##pre-encoder-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
comp_list.reverse()
with open("decompressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##pre-decoder-beg##[\s\S]*##pre-decoder-end##", contents)
str_to_add = ''
first_iteration = True
for d in reversed(preprocessor_params.keys()):
preprocessor_params_c = preprocessor_params[d]
paramc = preprocessor_params_c["paramc"]
paramv = preprocessor_params_c["paramv"]
c = re.sub(r'\(.*\)', '', d).strip()
if first_iteration:
params = "double paramv[] = {" + ", ".join(map(str, paramv)) + "};"
first_iteration = False
else:
params = "paramv[] = {" + ", ".join(map(str, paramv)) + "};"
str_to_add += f" {params}\n"
str_to_add += f" h_i{c}(hdecsize, hdecoded, {paramc}, paramv);\n"
contents = contents[:m.span()[0]] + "##pre-decoder-beg##*/\n" + str_to_add + " /*##pre-decoder-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
with open("decompressor-standalone.cpp", "r+") as f:
contents = f.read()
m = re.search(r"##comp-decoder-beg##[\s\S]*##comp-decoder-end##", contents)
str_to_add = ''
for c in comp_list:
c = c[2:]
str_to_add += " std::swap(in, out);\n h_i" + str(c) + "(csize, in, out);\n"
contents = contents[:m.span()[0]] + "##comp-decoder-beg##*/\n" + str_to_add + " /*##comp-decoder-end##" + contents[m.span()[1]:]
f.seek(0)
f.truncate()
f.write(contents)
enc_file_path = "compressor-standalone.cpp"
with open(enc_file_path, "r") as file:
cpp_code = file.readlines()
pattern = r'/\*##[^#]+##\*/'
cpp_code = [line for line in cpp_code if not re.match(pattern, line.strip())]
with open(enc_file_path, "w") as file:
file.write("".join(cpp_code))
dec_file_path = "decompressor-standalone.cpp"
with open(dec_file_path, "r") as file:
cpp_code = file.readlines()
pattern = r'/\*##[^#]+##\*/'
cpp_code = [line for line in cpp_code if not re.match(pattern, line.strip())]
with open(dec_file_path, "w") as file:
file.write("".join(cpp_code))
print("Compile encoder with:\ng++ -O3 -march=native -fopenmp -mno-fma -ffp-contract=off -I. -std=c++17 -o compress compressor-standalone.cpp")
print("\nRun encoder with:\n./compress input_file_name compressed_file_name [y]")
print("\nCompile decoder with:\ng++ -O3 -march=native -fopenmp -mno-fma -ffp-contract=off -I. -std=c++17 -o decompress decompressor-standalone.cpp")
print("\nRun decoder with:\n./decompress compressed_file_name decompressed_file_name [y]\n")