Textractor/GUI/extensions.cpp

50 lines
2.0 KiB
C++
Raw Normal View History

2018-07-26 22:42:21 -07:00
#include "extensions.h"
2018-08-21 22:43:30 -04:00
#include <shared_mutex>
2018-07-28 12:41:21 -07:00
#include <QDir>
2018-07-26 22:42:21 -07:00
2018-08-21 22:43:30 -04:00
std::shared_mutex extenMutex;
2018-07-26 22:42:21 -07:00
std::map<int, ExtensionFunction> extensions;
2018-07-28 12:41:21 -07:00
std::map<int, QString> LoadExtensions()
2018-07-26 22:42:21 -07:00
{
2018-07-29 18:24:46 -07:00
std::map<int, ExtensionFunction> newExtensions;
2018-07-28 12:41:21 -07:00
std::map<int, QString> extensionNames;
2018-08-19 17:55:50 -04:00
QStringList files = QDir().entryList();
for (auto file : files)
2018-08-25 17:08:45 -04:00
if (file.split("_").size() > 1 && file.split("_")[0].toInt() && file.endsWith(".dll"))
2018-08-19 17:55:50 -04:00
if (GetProcAddress(GetModuleHandleW(file.toStdWString().c_str()), "OnNewSentence") ||
GetProcAddress(LoadLibraryW(file.toStdWString().c_str()), "OnNewSentence"))
{
2018-08-25 17:08:45 -04:00
int extensionNumber = file.split("_")[0].toInt();
newExtensions[extensionNumber] = (ExtensionFunction)GetProcAddress(GetModuleHandleW(file.toStdWString().c_str()), "OnNewSentence");
file.chop(sizeof("dll"));
file.remove(0, file.indexOf("_") + 1);
extensionNames[extensionNumber] = file;
2018-08-19 17:55:50 -04:00
}
std::unique_lock<std::shared_mutex> extenLock(extenMutex);
2018-07-29 18:24:46 -07:00
extensions = newExtensions;
2018-07-26 22:42:21 -07:00
return extensionNames;
}
2018-09-20 22:32:47 -04:00
bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
2018-07-26 22:42:21 -07:00
{
2018-10-02 14:10:25 -04:00
bool success = true;
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
2018-09-22 15:45:54 -04:00
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
2018-07-29 14:36:45 -07:00
InfoForExtension* miscInfoLinkedList = new InfoForExtension;
InfoForExtension* miscInfoTraverser = miscInfoLinkedList;
2018-08-22 13:22:37 -04:00
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->nextProperty = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
2018-10-02 14:10:25 -04:00
std::shared_lock<std::shared_mutex> extenLock(extenMutex);
for (auto i : extensions)
2018-07-30 00:47:09 -07:00
{
wchar_t* nextBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
if (nextBuffer == nullptr) { success = false; break; }
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
sentenceBuffer = nextBuffer;
2018-07-30 00:47:09 -07:00
}
sentence = std::wstring(sentenceBuffer);
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
2018-10-02 14:10:25 -04:00
delete miscInfoLinkedList;
return success;
2018-07-26 22:42:21 -07:00
}