#!/usr/bin/python3

import re

import sys

if (len(sys.argv) < 2):
	print('No argument given')
	exit()


nips = sys.argv[1]

input_file = open(nips + '_inputdata.txt', 'r')
in_data = input_file.read();

# convert double to uint8_t
in_data = re.sub(r'(\d)\.0+e\+00', r'\1,', in_data)
in_data = re.sub(r'(\d)\.(\d)0+e\+01', r'\1\2,', in_data)
in_data = re.sub(r'(\d)\.(\d)(\d)0+e\+02', r'\1\2\3,', in_data)
in_data = in_data.replace(";", "")

# remove last comma
in_data = in_data[:-2]
# count samples
input_sample_cnt = len(in_data.split(","))
#####################################################################

ref_file = open(nips + '_outputdata.txt', 'r')
ref_data = ref_file.read()
ref_data = re.sub(r'\n', r' / ln2,\n', ref_data)
ref_data = ref_data[:-2]
ref_sample_cnt = len(ref_data.split(","))

# create cpp file
f = open("../xspn_data.cpp", "w")
cpp_file = "#include <array>\n"
cpp_file += "#include <cmath>\n"
cpp_file += "#include <bits/stdint-uintn.h>\n\n"
# The results in the outputdata.txt file are not directly what comes out of the PE but the natural logarithm of it.
cpp_file += "constexpr auto ln2 = std::log(2);\n"
cpp_file += "std::array<uint8_t, " + str(input_sample_cnt) + "> input_data = {\n" + str(in_data) + "}; \n\n"
cpp_file += "std::array<double, " + str(ref_sample_cnt) + "> ref_data = {\n" + str(ref_data)+ "}; \n"

f.write(cpp_file)
f.close()
