<commit_before>//
// Copyright (C) 2013-2018 University of Amsterdam
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include "engine.h"
#include "timers.h"
#include "log.h"
#include <iostream>
#include <fstream>
#include <codecvt>
#include "otoolstuff.h"
#include "rbridge.h"
#include "utils.h"
#include "dirs.h"

#ifdef _WIN32
void openConsoleOutput(unsigned long slaveNo, unsigned parentPID)
{

		FreeConsole();

		// create a separate new console window
		AllocConsole();

		// attach the new console to this application's process
		AttachConsole(GetCurrentProcessId());

		static std::string title = "jaspEngine " + std::to_string(slaveNo) + " for parent JASP at PID " + std::to_string(parentPID);
		SetConsoleTitleA(title.c_str());

		// reopen the std I/O streams to redirect I/O to the new console
		freopen("CONOUT$", "w", stdout);
		freopen("CONOUT$", "w", stderr);
}
#endif


#ifdef _WIN32
int wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
	if(argc > 4)
	{
		unsigned long	slaveNo			= wcstoul(argv[1], NULL, 10),
						parentPID		= wcstoul(argv[2], NULL, 10);
		std::string		logFileBase		= Utils::wstringToString(argv[3]),
						logFileWhere	= Utils::wstringToString(argv[4]);


        if(argc > 5)
            Dirs::setReportingDir(Utils::wstringToString(argv[5]));
			
#else
int main(int argc, char *argv[])
{
	if(argc > 4)
	{
		unsigned long	slaveNo			= strtoul(argv[1], NULL, 10),
						parentPID		= strtoul(argv[2], NULL, 10);
		std::string		logFileBase		= argv[3],
						logFileWhere	= argv[4];


        if(argc > 5)
            Dirs::setReportingDir(argv[5]);

#endif

		Log::logFileNameBase = logFileBase;
		Log::initRedirects();
		Log::setLogFileName(logFileBase + " Engine " + std::to_string(slaveNo) + ".log");
		Log::setWhere(logTypeFromString(logFileWhere));
		Log::setEngineNo(slaveNo);

		Log::log() << "Log and possible redirects initialized!" << std::endl;
		Log::log() << "jaspEngine started and has slaveNo " << slaveNo << " and it's parent PID is " << parentPID << std::endl;
		Log::log() << "Current directory is: '" << std::filesystem::current_path().string() << "'" << std::endl;

		try
		{
			JASPTIMER_START(Engine Starting);
			Engine e(slaveNo, parentPID);
			JASPTIMER_STOP(Engine Starting);

			e.run();

		}
		catch (std::exception & e)
		{
			Log::log() << "Engine had an uncaught exception of: " << e.what() << std::endl;;
			throw e;
		}

		JASPTIMER_PRINTALL();

		Log::log() << "jaspEngine " << slaveNo << " child of " << parentPID << " stops." << std::endl;
		exit(0);
	}
	else if(argc == 2)
	{
		std::cout << "Engine started in R (Module) Library Fixer mode because it received a single argument: '" << argv[1] << "'." << std::endl;

		Engine e(0, 0); //It needs to start to make sure rbridge functions work

#ifdef _WIN32
		_moduleLibraryFixer(Utils::wstringToString(argv[1]), true, true);
#else
		_moduleLibraryFixer(argv[1], true, true);
#endif

		exit(0);
	}
#ifdef _WIN32
	else if(argc == 3)
	{
		std::string arg1(Utils::wstringToString(argv[1])), arg2(Utils::wstringToString(argv[2]));
		const std::string junctionCollectArg("--collectJunctions"), junctionRecreateArg("--recreateJunctions"), junctionRemoveArg("--removeJunctions");
		
		if(arg1 == junctionRemoveArg || arg1 == junctionRecreateArg) //Also remove the old modules if it already exists and we are asked to recreate them, because it might be the old ones (previous install)
		{
			std::string junctionsCreationLog("junctions-recreated-successfully.log");
			std::filesystem::path	junctionsCreationLogPath = Utils::osPath(junctionsCreationLog);
			
			if(exists(junctionsCreationLogPath))
				remove(junctionsCreationLogPath);

			std::string modulesFolder("Modules");
			std::cout << "Engine started to remove the Modules folder" << std::endl;
			std::filesystem::path	modulesPath	= Utils::osPath(modulesFolder);
			
			if(exists(modulesPath)) {<commit_msg>Fix engine main.cpp on windows<commit_after>//
// Copyright (C) 2013-2018 University of Amsterdam
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include "engine.h"
#include "timers.h"
#include "log.h"
#include <iostream>
#include <fstream>
#include <codecvt>
#include "otoolstuff.h"
#include "rbridge.h"
#include "utils.h"
#include "dirs.h"

#ifdef _WIN32
void openConsoleOutput(unsigned long slaveNo, unsigned parentPID)
{

		FreeConsole();

		// create a separate new console window
		AllocConsole();

		// attach the new console to this application's process
		AttachConsole(GetCurrentProcessId());

		static std::string title = "jaspEngine " + std::to_string(slaveNo) + " for parent JASP at PID " + std::to_string(parentPID);
		SetConsoleTitleA(title.c_str());

		// reopen the std I/O streams to redirect I/O to the new console
		freopen("CONOUT$", "w", stdout);
		freopen("CONOUT$", "w", stderr);
}
#endif


#ifdef _WIN32
int wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
	if(argc > 4)
	{
		unsigned long	slaveNo			= wcstoul(argv[1], NULL, 10),
						parentPID		= wcstoul(argv[2], NULL, 10);
		std::string		logFileBase		= Utils::wstringToString(argv[3]),
						logFileWhere	= Utils::wstringToString(argv[4]);


        if(argc > 5)
            Dirs::setReportingDir(Utils::wstringToString(argv[5]));
			
#else
int main(int argc, char *argv[])
{
	if(argc > 4)
	{
		unsigned long	slaveNo			= strtoul(argv[1], NULL, 10),
						parentPID		= strtoul(argv[2], NULL, 10);
		std::string		logFileBase		= argv[3],
						logFileWhere	= argv[4];


        if(argc > 5)
            Dirs::setReportingDir(argv[5]);

#endif

		Log::logFileNameBase = logFileBase;
		Log::initRedirects();
		Log::setLogFileName(logFileBase + " Engine " + std::to_string(slaveNo) + ".log");
		Log::setWhere(logTypeFromString(logFileWhere));
		Log::setEngineNo(slaveNo);

		Log::log() << "Log and possible redirects initialized!" << std::endl;
		Log::log() << "jaspEngine started and has slaveNo " << slaveNo << " and it's parent PID is " << parentPID << std::endl;
		Log::log() << "Current directory is: '" << std::filesystem::current_path().string() << "'" << std::endl;

		try
		{
			JASPTIMER_START(Engine Starting);
			Engine e(slaveNo, parentPID);
			JASPTIMER_STOP(Engine Starting);

			e.run();

		}
		catch (std::exception & e)
		{
			Log::log() << "Engine had an uncaught exception of: " << e.what() << std::endl;;
			throw e;
		}

		JASPTIMER_PRINTALL();

		Log::log() << "jaspEngine " << slaveNo << " child of " << parentPID << " stops." << std::endl;
		exit(0);
	}
	else if(argc == 2)
	{
		std::string singleArg =
#ifdef _WIN32
			Utils::wstringToString(argv[1]);
#else
			argv[1];
#endif
		std::cout << "Engine started in R (Module) Library Fixer mode because it received a single argument: '" << singleArg << "'." << std::endl;

		Engine e(0, 0); //It needs to start to make sure rbridge functions work

		_moduleLibraryFixer(singleArg, true, true);

		exit(0);
	}
#ifdef _WIN32
	else if(argc == 3)
	{
		std::string arg1(Utils::wstringToString(argv[1])), arg2(Utils::wstringToString(argv[2]));
		const std::string junctionCollectArg("--collectJunctions"), junctionRecreateArg("--recreateJunctions"), junctionRemoveArg("--removeJunctions");
		
		if(arg1 == junctionRemoveArg || arg1 == junctionRecreateArg) //Also remove the old modules if it already exists and we are asked to recreate them, because it might be the old ones (previous install)
		{
			std::string junctionsCreationLog("junctions-recreated-successfully.log");
			std::filesystem::path	junctionsCreationLogPath = Utils::osPath(junctionsCreationLog);
			
			if(exists(junctionsCreationLogPath))
				remove(junctionsCreationLogPath);

			std::string modulesFolder("Modules");
			std::cout << "Engine started to remove the Modules folder" << std::endl;
			std::filesystem::path	modulesPath	= Utils::osPath(modulesFolder);
			
			if(exists(modulesPath)) {
{'fraction_non_alphanumeric': 0.14399911180193184, 'fraction_numerical': 0.011879649161763072, 'mean_word_length': 4.243306169965075, 'pattern_counts': {'":': 0, '<': 115, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2588748', 'n_tokens_mistral': 3240, 'n_tokens_neox': 2890, 'n_words': 837}