improve extension abi (dont rely on exceptions across dlls)
This commit is contained in:
parent
586e22e4a5
commit
39a0161989
@ -35,16 +35,13 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std
|
||||
InfoForExtension* miscInfoTraverser = miscInfoLinkedList;
|
||||
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->nextProperty = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
|
||||
std::shared_lock<std::shared_mutex> extenLock(extenMutex);
|
||||
try
|
||||
for (auto i : extensions)
|
||||
{
|
||||
for (auto i : extensions)
|
||||
{
|
||||
wchar_t* prev = sentenceBuffer;
|
||||
sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
|
||||
if (sentenceBuffer != prev) HeapFree(GetProcessHeap(), 0, prev);
|
||||
}
|
||||
wchar_t* nextBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
|
||||
if (nextBuffer == nullptr) { success = false; break; }
|
||||
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
||||
sentenceBuffer = nextBuffer;
|
||||
}
|
||||
catch (...) { success = false; }
|
||||
sentence = std::wstring(sentenceBuffer);
|
||||
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
||||
delete miscInfoLinkedList;
|
||||
|
@ -23,6 +23,8 @@ struct SentenceInfo
|
||||
}
|
||||
};
|
||||
|
||||
struct InvalidSentence {};
|
||||
|
||||
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
|
||||
|
||||
/**
|
||||
@ -31,20 +33,24 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
|
||||
* You should not modify this sentence. If you want Textractor to receive a modified sentence, copy it into your own buffer and return that.
|
||||
* Please allocate the buffer using HeapAlloc() and not new[] or malloc() or something else: Textractor uses HeapFree() to free it.
|
||||
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
||||
* Return value: pointer to sentence Textractor takes for future processing and display.
|
||||
* Return value: pointer to sentence Textractor takes for future processing and display. If nullptr, Textractor will 'eat' the sentence.
|
||||
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
|
||||
* Textractor will display the sentence after all extensions have had a chance to process and/or modify it.
|
||||
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
||||
*/
|
||||
extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentenceArr, const InfoForExtension* miscInfo)
|
||||
extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
std::wstring sentence(sentenceArr);
|
||||
if (ProcessSentence(sentence, SentenceInfo{ miscInfo }))
|
||||
try
|
||||
{
|
||||
// No need to worry about freeing this: Textractor does it for you.
|
||||
wchar_t* newSentence = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
|
||||
wcscpy_s(newSentence, sentence.size() + 1, sentence.c_str());
|
||||
return newSentence;
|
||||
std::wstring sentenceStr(sentence);
|
||||
if (ProcessSentence(sentenceStr, SentenceInfo{ miscInfo }))
|
||||
{
|
||||
// No need to worry about freeing this: Textractor does it for you.
|
||||
wchar_t* newSentence = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentenceStr.size() + 1) * sizeof(wchar_t));
|
||||
wcscpy_s(newSentence, sentenceStr.size() + 1, sentenceStr.c_str());
|
||||
return newSentence;
|
||||
}
|
||||
else return sentence;
|
||||
}
|
||||
else return sentenceArr;
|
||||
catch (InvalidSentence) { return nullptr; }
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ bool RemoveRepeatedChars(std::wstring& sentence)
|
||||
|
||||
bool RemoveCyclicRepeats(std::wstring& sentence)
|
||||
{
|
||||
if (sentence == L"") throw std::exception();
|
||||
if (sentence == L"") throw InvalidSentence();
|
||||
int junkLength = 0;
|
||||
wchar_t junk[2000] = {};
|
||||
while (wcsstr(sentence.c_str() + junkLength, junk))
|
||||
@ -47,7 +47,7 @@ bool RemoveRepeatedSentences(std::wstring& sentence, int64_t handle)
|
||||
static std::set<std::pair<int64_t, std::wstring>> seenSentences;
|
||||
static std::mutex m;
|
||||
std::lock_guard<std::mutex> l(m);
|
||||
if (seenSentences.count({ handle, sentence }) != 0) throw std::exception();
|
||||
if (seenSentences.count({ handle, sentence }) != 0) throw InvalidSentence();
|
||||
seenSentences.insert({ handle, sentence });
|
||||
return false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user