move file parsing to load time

This commit is contained in:
Akash Mozumdar 2021-11-06 13:53:51 -06:00
parent 2316f59415
commit 90b58da867
3 changed files with 38 additions and 40 deletions

View File

@ -55,7 +55,7 @@ namespace
void Unload(int index)
{
std::scoped_lock writeLock(extenMutex);
std::scoped_lock lock(extenMutex);
FreeLibrary(GetModuleHandleW((extensions.at(index).name + L".xdll").c_str()));
extensions.erase(extensions.begin() + index);
}

View File

@ -14,6 +14,7 @@ add_library(Extra\ Window MODULE extrawindow.cpp extensionimpl.cpp)
add_library(Google\ Translate MODULE googletranslate.cpp translatewrapper.cpp network.cpp extensionimpl.cpp)
add_library(Lua MODULE lua.cpp extensionimpl.cpp)
add_library(Regex\ Filter MODULE regexfilter.cpp extensionimpl.cpp)
add_library(Regex\ Replacer MODULE regexreplacer.cpp extensionimpl.cpp)
add_library(Remove\ Repeated\ Characters MODULE removerepeatchar.cpp extensionimpl.cpp)
add_library(Remove\ Repeated\ Phrases MODULE removerepeatphrase.cpp extensionimpl.cpp)
add_library(Remove\ Repeated\ Phrases\ 2 MODULE removerepeatphrase2.cpp extensionimpl.cpp)
@ -21,7 +22,6 @@ add_library(Remove\ 30\ Repeated\ Sentences MODULE removerepeatsentence.cpp exte
add_library(Replacer MODULE replacer.cpp extensionimpl.cpp)
add_library(Styler MODULE styler.cpp extensionimpl.cpp)
add_library(Thread\ Linker MODULE threadlinker.cpp extensionimpl.cpp)
add_library(Regex\ Replacer MODULE regexreplacer.cpp extensionimpl.cpp)
target_precompile_headers(Bing\ Translate REUSE_FROM pch)
target_precompile_headers(Copy\ to\ Clipboard REUSE_FROM pch)
@ -34,6 +34,7 @@ target_precompile_headers(Extra\ Window REUSE_FROM pch)
target_precompile_headers(Google\ Translate REUSE_FROM pch)
target_precompile_headers(Lua REUSE_FROM pch)
target_precompile_headers(Regex\ Filter REUSE_FROM pch)
target_precompile_headers(Regex\ Replacer REUSE_FROM pch)
target_precompile_headers(Remove\ Repeated\ Characters REUSE_FROM pch)
target_precompile_headers(Remove\ Repeated\ Phrases REUSE_FROM pch)
target_precompile_headers(Remove\ Repeated\ Phrases\ 2 REUSE_FROM pch)
@ -41,7 +42,6 @@ target_precompile_headers(Remove\ 30\ Repeated\ Sentences REUSE_FROM pch)
target_precompile_headers(Replacer REUSE_FROM pch)
target_precompile_headers(Styler REUSE_FROM pch)
target_precompile_headers(Thread\ Linker REUSE_FROM pch)
target_precompile_headers(Regex\ Replacer REUSE_FROM pch)
target_link_libraries(Bing\ Translate winhttp Qt5::Widgets)
target_link_libraries(DeepL\ Translate winhttp Qt5::Widgets)

View File

@ -5,10 +5,37 @@
extern const wchar_t* REGEX_REPLACER_INSTRUCTIONS;
const char* REGEX_REPLACEMENTS_SAVE_FILE = "SavedRegexReplacements.txt";
const char* REPLACE_SAVE_FILE = "SavedRegexReplacements.txt";
std::optional<std::wregex> regex;
std::atomic<std::filesystem::file_time_type> replaceFileLastWrite = {};
concurrency::reader_writer_lock m;
std::vector<std::tuple<std::wregex, std::wstring, std::regex_constants::match_flag_type>> replacements;
void UpdateReplacements()
{
try
{
if (replaceFileLastWrite.exchange(std::filesystem::last_write_time(REPLACE_SAVE_FILE)) == std::filesystem::last_write_time(REPLACE_SAVE_FILE)) return;
std::scoped_lock lock(m);
replacements.clear();
std::ifstream stream(REPLACE_SAVE_FILE, std::ios::binary);
BlockMarkupIterator savedFilters(stream, Array<std::wstring_view>{ L"|REGEX|", L"|BECOMES|", L"|MODIFIER|" });
while (auto read = savedFilters.Next())
{
const auto& [regex, replacement, modifier] = read.value();
try
{
replacements.emplace_back(
std::wregex(regex, modifier.find(L'i') == std::string::npos ? std::regex::ECMAScript : std::regex::icase),
replacement,
modifier.find(L'g') == std::string::npos ? std::regex_constants::format_first_only : std::regex_constants::format_default
);
}
catch (std::regex_error) {}
}
}
catch (std::filesystem::filesystem_error) { replaceFileLastWrite.store({}); }
}
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
@ -16,12 +43,13 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
{
case DLL_PROCESS_ATTACH:
{
if (!std::ifstream(REGEX_REPLACEMENTS_SAVE_FILE).good())
UpdateReplacements();
if (replacements.empty())
{
auto file = std::ofstream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary) << "\xff\xfe";
auto file = std::ofstream(REPLACE_SAVE_FILE, std::ios::binary) << "\xff\xfe";
for (auto ch : std::wstring_view(REGEX_REPLACER_INSTRUCTIONS))
file << (ch == L'\n' ? std::string_view("\r\0\n", 4) : std::string_view((char*)&ch, 2));
_spawnlp(_P_DETACH, "notepad", "notepad", REGEX_REPLACEMENTS_SAVE_FILE, NULL); // show file to user
_spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_SAVE_FILE, NULL); // show file to user
}
}
break;
@ -35,39 +63,9 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
if (!sentenceInfo["current select"] || sentenceInfo["text number"] == 0) return false;
std::regex::flag_type mod;
std::regex_constants::match_flag_type flag;
UpdateReplacements();
std::ifstream stream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary);
BlockMarkupIterator savedFilters(stream, Array<std::wstring_view>{ L"|REGEX|", L"|BECOMES|", L"|MODIFIER|" });
concurrency::reader_writer_lock::scoped_lock_read readLock(m);
while (auto read = savedFilters.Next()) {
const auto& [regex, replacement, modifier] = read.value();
if (modifier == L"g")
{
mod = std::regex::ECMAScript;
flag = std::regex_constants::format_default;
}
else if (modifier == L"gi" || modifier == L"ig")
{
mod = std::regex::icase;
flag = std::regex_constants::format_default;
}
else if (modifier == L"i")
{
mod = std::regex::icase;
flag = std::regex_constants::format_first_only;
}
else
{
mod = std::regex::ECMAScript;
flag = std::regex_constants::format_first_only;
}
try { ::regex = std::wregex(regex, mod); }
catch (std::regex_error) { continue; }
sentence = std::regex_replace(sentence, ::regex.value(), replacement, flag);
}
for (const auto& [regex, replacement, flags] : replacements) sentence = std::regex_replace(sentence, regex, replacement, flags);
return true;
}