<commit_before>/*
 * PluginFrameworkImplWin.cpp
 *
 *  Created on: 23.03.2018
 *      Author: alwi0251
 */

#include "pluginFramework/impl/PluginFrameworkImplWin.hpp"

#ifdef NDEBUG
  #define DEBUG_MESSAGE(a) /**/
#else
  #define DEBUG_MESSAGE(a) a
#endif

#include <dirent.h>
#include <iostream>
#include <windows.h>

#include "pluginFramework/MDE4CPPPlugin.hpp"

typedef std::shared_ptr<MDE4CPPPlugin> (__stdcall *StartFunction)();
#define MAX_CHAR 260

PluginFrameworkImplWin::PluginFrameworkImplWin():
	PluginFrameworkImpl()
{
}

PluginFrameworkImplWin::~PluginFrameworkImplWin()
{
}

PluginFramework* PluginFrameworkImplWin::create()
{
	PluginFrameworkImplWin* instance = new PluginFrameworkImplWin();
	instance->initialize();
	return instance;
}

std::vector<std::string> PluginFrameworkImplWin::findAllAvailableLibraries()
{
	char folderBuffer[MAX_CHAR];
	char nameBuffer[MAX_CHAR];
	std::vector<std::string> libraries;

	m_debugMode = false;
	GetCurrentDirectory(MAX_CHAR, folderBuffer);
	GetModuleFileName(NULL, nameBuffer, MAX_CHAR);
	m_endingDebug = "d.dll";
	m_endingRelease = ".dll";
	m_endingString = "d.exe";

	std::string fileName(nameBuffer);
	std::string folderName(folderBuffer);

	if (fileName.length() >= m_endingString.length())
	{
		m_debugMode =  (0 == fileName.compare(fileName.length() - m_endingString.length(), m_endingString.length(), m_endingString));
	}

	DIR *dir;
	struct dirent *file;
	if((dir = opendir(folderName.c_str())) != NULL)
	{
		while((file = readdir(dir)) != NULL)
		{
			std::string libName = checkLibrary(file, folderName);
			if (!libName.empty())
			{
				libraries.push_back(libName);
			}
		}
		closedir (dir);
	}
	else
	{
		std::cerr << "Could not open directory " << folderName << " failed" << std::endl;
	}

	return libraries;
}

void PluginFrameworkImplWin::loadLibrary(std::string libraryPath)
{
	HINSTANCE hGetProcIDDLL = LoadLibrary(libraryPath.c_str());
	if(!hGetProcIDDLL)
	{
		std::cerr << "could not load the dynamic library, ErrorCode: " << GetLastError() << std::endl;
		return;
	}

	StartFunction startFunction = (StartFunction) GetProcAddress(hGetProcIDDLL, "_Z5startv");
	if(!startFunction)<commit_msg>[pluginFramework] add debug messages to PluginFrameworkImplWin::findAllAvailableLibraries<commit_after>/*
 * PluginFrameworkImplWin.cpp
 *
 *  Created on: 23.03.2018
 *      Author: alwi0251
 */

#include "pluginFramework/impl/PluginFrameworkImplWin.hpp"

#ifdef NDEBUG
  #define DEBUG_MESSAGE(a) /**/
#else
  #define DEBUG_MESSAGE(a) a
#endif

#include <dirent.h>
#include <iostream>
#include <windows.h>

#include "pluginFramework/MDE4CPPPlugin.hpp"

typedef std::shared_ptr<MDE4CPPPlugin> (__stdcall *StartFunction)();
#define MAX_CHAR 260

PluginFrameworkImplWin::PluginFrameworkImplWin():
	PluginFrameworkImpl()
{
}

PluginFrameworkImplWin::~PluginFrameworkImplWin()
{
}

PluginFramework* PluginFrameworkImplWin::create()
{
	PluginFrameworkImplWin* instance = new PluginFrameworkImplWin();
	instance->initialize();
	return instance;
}

std::vector<std::string> PluginFrameworkImplWin::findAllAvailableLibraries()
{
	char folderBuffer[MAX_CHAR];
	char nameBuffer[MAX_CHAR];
	std::vector<std::string> libraries;

	m_debugMode = false;
	GetCurrentDirectory(MAX_CHAR, folderBuffer);
	GetModuleFileName(NULL, nameBuffer, MAX_CHAR);
	m_endingDebug = "d.dll";
	m_endingRelease = ".dll";
	m_endingString = "d.exe";

	std::string fileName(nameBuffer);
	std::string folderName(folderBuffer);

	if (fileName.length() >= m_endingString.length())
	{
		m_debugMode =  (0 == fileName.compare(fileName.length() - m_endingString.length(), m_endingString.length(), m_endingString));
	}

	DIR *dir;
	struct dirent *file;
	if((dir = opendir(folderName.c_str())) != NULL)
	{
		DEBUG_MESSAGE(std::cout << "libraries to be loaded" << std::endl;)
		while((file = readdir(dir)) != NULL)
		{
			std::string libName = checkLibrary(file, folderName);
			if (!libName.empty())
			{
				DEBUG_MESSAGE(std::cout << "\t" << libName << std::endl;)
				libraries.push_back(libName);
			}
		}
		closedir (dir);
	}
	else
	{
		std::cerr << "Could not open directory " << folderName << " failed" << std::endl;
	}

	return libraries;
}

void PluginFrameworkImplWin::loadLibrary(std::string libraryPath)
{
	HINSTANCE hGetProcIDDLL = LoadLibrary(libraryPath.c_str());
	if(!hGetProcIDDLL)
	{
		std::cerr << "could not load the dynamic library, ErrorCode: " << GetLastError() << std::endl;
		return;
	}

	StartFunction startFunction = (StartFunction) GetProcAddress(hGetProcIDDLL, "_Z5startv");
	if(!startFunction)
{'fraction_non_alphanumeric': 0.14367436743674367, 'fraction_numerical': 0.008360836083608362, 'mean_word_length': 5.97239263803681, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 19, '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': '3020590', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1522, 'n_words': 282}