<commit_before>#include <json2map.h>
#include "ctemplate.h"
#include "filemanager.h"
#include "date.h"
#include "translation.h"

void *ctemplate_parseJson(ctemplate_t *ctemplate, char *json);
char *ctemplate_executeModule(ctemplate_t *ctemplate, loader_module_t *module, char *json);
char ctemplate_isRecompilationNecessary(char *templatePath, char *sourcePath, char *libraryPath);
loader_module_t *ctemplate_moduleLoader(ctemplate_t *ctemplate, csafestring_t *templatePath, csafestring_t *sourcePath, csafestring_t *libraryPath);

ctemplate_t *ctemplate_init(char *templatePath, char *workingPath, ctemplate_functions_t *methods, bool recompile) {

	ctemplate_t *ctemplate = (ctemplate_t *) malloc(sizeof(ctemplate_t));


	ctemplate->templateBaseDir = safe_create(templatePath);
	if ( !safe_strcmp(ctemplate->templateBaseDir, "") ) {
		safe_strcpy(ctemplate->templateBaseDir, "./");
	} else if ( ctemplate->templateBaseDir->data[safe_strlen(ctemplate->templateBaseDir)] != '/' ) {
		safe_strchrappend(ctemplate->templateBaseDir, '/');
	}

	ctemplate->workingBaseDir = safe_create(workingPath);
	if ( !safe_strcmp(ctemplate->workingBaseDir, "") ) {
		safe_strcpy(ctemplate->workingBaseDir, "./");
	} else if ( ctemplate->workingBaseDir->data[safe_strlen(ctemplate->workingBaseDir)] != '/' ) {
		safe_strchrappend(ctemplate->workingBaseDir, '/');
	}

	ctemplate->mfunctions = methods;
	ctemplate->alwaysRecompile = recompile;
	ctemplate->modules = NULL;
	ctemplate->translation_modules = modules_init();

	return ctemplate;
}

char *ctemplate_executeTemplate(ctemplate_t *ctemplate, char *templateName, char *json) {

	if ( templateName == NULL ) {
		return NULL;
	}

	csafestring_t *templatePath = safe_clone(ctemplate->templateBaseDir);
	safe_strcat(templatePath, templateName);

	filemanager_fileinfo *templateInfo = filemanager_getStatus(templatePath->data);
	if ( filemanager_fileNotExists(templatePath->data) ) {
		free(templateInfo);
		free(templatePath);
		return NULL;
	}
	free(templateInfo);

	char *templateFile = filemanager_getFilename(templateName);
	csafestring_t *sourcePath = filemanager_calculateSourcePath(ctemplate, templateFile);
	csafestring_t *libraryPath = filemanager_calculateCompilationPath(ctemplate, templateFile);

	loader_module_t *module = ctemplate_moduleLoader(ctemplate, templatePath, sourcePath, libraryPath);<commit_msg>* destroy string correctly<commit_after>#include <json2map.h>
#include "ctemplate.h"
#include "filemanager.h"
#include "date.h"
#include "translation.h"

void *ctemplate_parseJson(ctemplate_t *ctemplate, char *json);
char *ctemplate_executeModule(ctemplate_t *ctemplate, loader_module_t *module, char *json);
char ctemplate_isRecompilationNecessary(char *templatePath, char *sourcePath, char *libraryPath);
loader_module_t *ctemplate_moduleLoader(ctemplate_t *ctemplate, csafestring_t *templatePath, csafestring_t *sourcePath, csafestring_t *libraryPath);

ctemplate_t *ctemplate_init(char *templatePath, char *workingPath, ctemplate_functions_t *methods, bool recompile) {

	ctemplate_t *ctemplate = (ctemplate_t *) malloc(sizeof(ctemplate_t));


	ctemplate->templateBaseDir = safe_create(templatePath);
	if ( !safe_strcmp(ctemplate->templateBaseDir, "") ) {
		safe_strcpy(ctemplate->templateBaseDir, "./");
	} else if ( ctemplate->templateBaseDir->data[safe_strlen(ctemplate->templateBaseDir)] != '/' ) {
		safe_strchrappend(ctemplate->templateBaseDir, '/');
	}

	ctemplate->workingBaseDir = safe_create(workingPath);
	if ( !safe_strcmp(ctemplate->workingBaseDir, "") ) {
		safe_strcpy(ctemplate->workingBaseDir, "./");
	} else if ( ctemplate->workingBaseDir->data[safe_strlen(ctemplate->workingBaseDir)] != '/' ) {
		safe_strchrappend(ctemplate->workingBaseDir, '/');
	}

	ctemplate->mfunctions = methods;
	ctemplate->alwaysRecompile = recompile;
	ctemplate->modules = NULL;
	ctemplate->translation_modules = modules_init();

	return ctemplate;
}

char *ctemplate_executeTemplate(ctemplate_t *ctemplate, char *templateName, char *json) {

	if ( templateName == NULL ) {
		return NULL;
	}

	csafestring_t *templatePath = safe_clone(ctemplate->templateBaseDir);
	safe_strcat(templatePath, templateName);

	filemanager_fileinfo *templateInfo = filemanager_getStatus(templatePath->data);
	if ( filemanager_fileNotExists(templatePath->data) ) {
		safe_destroy(templatePath);
		free(templateInfo);
		return NULL;
	}
	free(templateInfo);

	char *templateFile = filemanager_getFilename(templateName);
	csafestring_t *sourcePath = filemanager_calculateSourcePath(ctemplate, templateFile);
	csafestring_t *libraryPath = filemanager_calculateCompilationPath(ctemplate, templateFile);

	loader_module_t *module = ctemplate_moduleLoader(ctemplate, templatePath, sourcePath, libraryPath);
{'fraction_non_alphanumeric': 0.12148742869216142, 'fraction_numerical': 0.0004225649693640397, 'mean_word_length': 7.898496240601504, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '284978', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1587, 'n_words': 248}