diff --git a/GUI/extensions.cpp b/GUI/extensions.cpp index 4fb6b64..883f454 100644 --- a/GUI/extensions.cpp +++ b/GUI/extensions.cpp @@ -35,16 +35,13 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_mapnextProperty = new InfoForExtension{ i.first.c_str(), i.second, nullptr }; std::shared_lock 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; diff --git a/extensions/extensions.h b/extensions/extensions.h index 2c61a87..f01f3dd 100644 --- a/extensions/extensions.h +++ b/extensions/extensions.h @@ -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; } } diff --git a/extensions/removerepeat.cpp b/extensions/removerepeat.cpp index c02919c..09996bc 100644 --- a/extensions/removerepeat.cpp +++ b/extensions/removerepeat.cpp @@ -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> seenSentences; static std::mutex m; std::lock_guard 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; }