Textractor_test/extensions/replacer.cpp

150 lines
4.3 KiB
C++
Raw Normal View History

2019-01-24 05:11:14 +08:00
#include "extension.h"
#include <cwctype>
#include <fstream>
2019-01-24 21:32:21 +08:00
#include <atomic>
#include <filesystem>
#include <process.h>
2019-01-24 05:11:14 +08:00
2019-02-28 00:33:17 +08:00
extern const wchar_t* REPLACER_INSTRUCTIONS;
2019-02-19 12:12:12 +08:00
constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt";
2019-01-24 21:32:21 +08:00
std::atomic<std::filesystem::file_time_type> replaceFileLastWrite;
2019-02-28 14:40:40 +08:00
std::shared_mutex m;
2019-01-24 05:11:14 +08:00
struct
{
public:
void Put(std::wstring original, std::wstring replacement)
{
Node* current = &root;
2019-02-18 08:14:49 +08:00
for (auto ch : original)
if (Ignore(ch));
else if (auto& next = current->next[ch]) current = next.get();
2019-01-24 05:11:14 +08:00
else current = (next = std::make_unique<Node>()).get();
2019-01-27 21:49:08 +08:00
if (current != &root) current->value = replacement;
2019-01-24 05:11:14 +08:00
}
2019-04-23 01:35:40 +08:00
std::pair<int, std::optional<std::wstring>> Lookup(const std::wstring& text) const
2019-01-24 05:11:14 +08:00
{
std::pair<int, std::optional<std::wstring>> result = {};
2019-01-24 05:11:14 +08:00
int length = 0;
2019-04-23 01:35:40 +08:00
const Node* current = &root;
2019-02-18 08:14:49 +08:00
for (auto ch : text)
{
if (Ignore(ch))
{
length += 1;
}
2019-02-28 14:40:40 +08:00
else if (current->next.count(ch) != 0)
{
2019-02-28 14:40:40 +08:00
auto& next = current->next.at(ch);
length += 1;
current = next.get();
if (current->value) result = { length, current->value };
}
2019-01-24 05:11:14 +08:00
else break;
}
return result;
2019-01-24 05:11:14 +08:00
}
private:
2019-02-18 08:14:49 +08:00
static bool Ignore(wchar_t ch)
2019-01-24 05:11:14 +08:00
{
2019-02-18 08:14:49 +08:00
return ch <= 0x20 || std::iswspace(ch);
2019-01-24 05:11:14 +08:00
}
struct Node
{
std::unordered_map<wchar_t, std::unique_ptr<Node>> next;
2019-02-21 11:14:32 +08:00
std::optional<std::wstring> value;
2019-01-24 05:11:14 +08:00
} root;
} replacementTrie;
2019-01-24 21:32:21 +08:00
int Parse(const std::wstring& file)
2019-01-24 05:11:14 +08:00
{
2019-02-28 14:40:40 +08:00
std::lock_guard l(m);
2019-01-24 21:32:21 +08:00
replacementTrie = {};
2019-02-21 11:14:32 +08:00
int count = 0;
2019-01-24 05:11:14 +08:00
size_t end = 0;
while (true)
{
size_t original = file.find(L"|ORIG|", end);
size_t becomes = file.find(L"|BECOMES|", original);
2019-01-24 21:32:21 +08:00
if ((end = file.find(L"|END|", becomes)) == std::wstring::npos) break;
replacementTrie.Put(file.substr(original + 6, becomes - original - 6), file.substr(becomes + 9, end - becomes - 9));
2019-02-21 11:14:32 +08:00
count += 1;
2019-01-24 05:11:14 +08:00
}
2019-02-21 11:14:32 +08:00
return count;
2019-01-24 21:32:21 +08:00
}
bool Replace(std::wstring& sentence)
{
2019-02-28 14:40:40 +08:00
std::shared_lock l(m);
2019-01-24 21:32:21 +08:00
for (int i = 0; i < sentence.size(); ++i)
2019-02-21 11:14:32 +08:00
{
auto[length, replacement] = replacementTrie.Lookup(sentence.substr(i));
if (replacement)
{
sentence.replace(i, length, replacement.value());
i += replacement.value().size() - 1; // iterate to end of replacement
}
}
2019-01-24 21:32:21 +08:00
return true;
2019-01-24 05:11:14 +08:00
}
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
2019-01-24 21:32:21 +08:00
static HANDLE replacementFile; // not actually used to read/write, just to ensure it exists
2019-01-24 05:11:14 +08:00
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
2019-01-24 21:32:21 +08:00
replacementFile = CreateFileA(REPLACE_SAVE_FILE, FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {});
if (Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))) == 0)
{
std::ofstream(REPLACE_SAVE_FILE, std::ios::out | std::ios::binary | std::ios::trunc).write((char*)REPLACER_INSTRUCTIONS, wcslen(REPLACER_INSTRUCTIONS) * sizeof(wchar_t));
_spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_SAVE_FILE, NULL); // show file to user
}
replaceFileLastWrite = std::filesystem::last_write_time(REPLACE_SAVE_FILE);
2019-01-24 05:11:14 +08:00
}
break;
case DLL_PROCESS_DETACH:
{
2019-01-24 21:32:21 +08:00
CloseHandle(replacementFile);
2019-01-24 05:11:14 +08:00
}
break;
}
return TRUE;
}
bool ProcessSentence(std::wstring& sentence, SentenceInfo)
{
2019-01-24 21:32:21 +08:00
static_assert(std::has_unique_object_representations_v<decltype(replaceFileLastWrite)::value_type>);
if (!replaceFileLastWrite.compare_exchange_strong(std::filesystem::last_write_time(REPLACE_SAVE_FILE), std::filesystem::last_write_time(REPLACE_SAVE_FILE)))
{
std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {});
Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t)));
}
return Replace(sentence);
2019-01-24 05:11:14 +08:00
}
2019-02-28 14:40:40 +08:00
TEST_SYNC(
2019-01-24 05:11:14 +08:00
{
2019-01-24 21:32:21 +08:00
assert(Parse(LR"(|ORIG| さよなら|BECOMES|goodbye|END|
2019-01-24 05:11:14 +08:00
|ORIG||BECOMES|idiot|END|
2019-01-24 21:32:21 +08:00
|ORIG| |BECOMES|hello|END|)") == 3);
std::wstring replaced = LR"(blahblah 
2019-01-24 05:11:14 +08:00
)";
2019-01-24 21:32:21 +08:00
Replace(replaced);
assert(replaced.find(L"さよなら") == std::wstring::npos &&
replaced.find(L"バカ") == std::wstring::npos &&
replaced.find(L"こんにちは") == std::wstring::npos &&
replaced.find(L"goodbye") != std::wstring::npos &&
replaced.find(L"idiot") != std::wstring::npos &&
replaced.find(L"hello") != std::wstring::npos
2019-01-24 21:32:21 +08:00
);
2019-01-24 05:11:14 +08:00
}
);