move onnewsentence into own file

This commit is contained in:
Akash Mozumdar 2018-10-11 14:29:11 -04:00
parent f7e3bbeb02
commit 199844987b
7 changed files with 39 additions and 37 deletions

View File

@ -1,8 +1,8 @@
cmake_policy(SET CMP0037 OLD) cmake_policy(SET CMP0037 OLD)
add_library(256_Remove\ Repetition SHARED removerepeat.cpp) add_library(256_Remove\ Repetition SHARED removerepeat.cpp extensionimpl.cpp)
add_library(512_Copy\ to\ Clipboard SHARED copyclipboard.cpp) add_library(512_Copy\ to\ Clipboard SHARED copyclipboard.cpp extensionimpl.cpp)
add_library(1024_Google\ Translate SHARED googletranslate.cpp) add_library(1024_Google\ Translate SHARED googletranslate.cpp extensionimpl.cpp)
add_library(2048_Extra\ Newlines SHARED extranewlines.cpp) add_library(2048_Extra\ Newlines SHARED extranewlines.cpp extensionimpl.cpp)
target_link_libraries(1024_Google\ Translate winhttp.lib) target_link_libraries(1024_Google\ Translate winhttp.lib)

View File

@ -1,4 +1,4 @@
#include "extensions.h" #include "extension.h"
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {

27
extensions/extension.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstdint>
#include <string>
struct InfoForExtension
{
const char* name;
int64_t value;
InfoForExtension* next;
};
struct SentenceInfo
{
const InfoForExtension* list;
// Traverse linked list to find info.
int64_t operator[](std::string propertyName)
{
for (auto i = list; i != nullptr; i = i->next) if (propertyName == i->name) return i->value;
throw;
}
};
struct SKIP {};
inline void Skip() { throw SKIP(); }

View File

@ -1,29 +1,4 @@
#pragma once #include "extension.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstdint>
#include <string>
struct InfoForExtension
{
const char* name;
int64_t value;
InfoForExtension* next;
};
struct SentenceInfo
{
const InfoForExtension* list;
// Traverse linked list to find info.
int64_t operator[](std::string propertyName)
{
for (auto i = list; i != nullptr; i = i->next) if (propertyName == i->name) return i->value;
throw;
}
};
struct InvalidSentence {};
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo); bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
@ -52,5 +27,5 @@ extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sen
} }
else return sentence; else return sentence;
} }
catch (InvalidSentence) { return nullptr; } catch (SKIP) { return nullptr; }
} }

View File

@ -1,4 +1,4 @@
#include "extensions.h" #include "extension.h"
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {

View File

@ -1,4 +1,4 @@
#include "extensions.h" #include "extension.h"
#include <winhttp.h> #include <winhttp.h>
#include <ctime> #include <ctime>
#include <regex> #include <regex>

View File

@ -1,4 +1,4 @@
#include "extensions.h" #include "extension.h"
#include <set> #include <set>
#include <mutex> #include <mutex>
@ -25,7 +25,7 @@ bool RemoveRepeatedChars(std::wstring& sentence)
bool RemoveCyclicRepeats(std::wstring& sentence) bool RemoveCyclicRepeats(std::wstring& sentence)
{ {
if (sentence == L"") throw InvalidSentence(); if (sentence == L"") Skip();
int junkLength = 0; int junkLength = 0;
wchar_t junk[2000] = {}; wchar_t junk[2000] = {};
while (wcsstr(sentence.c_str() + junkLength, junk)) 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::set<std::pair<int64_t, std::wstring>> seenSentences;
static std::mutex m; static std::mutex m;
std::lock_guard<std::mutex> l(m); std::lock_guard<std::mutex> l(m);
if (seenSentences.count({ handle, sentence }) != 0) throw InvalidSentence(); if (seenSentences.count({ handle, sentence }) != 0) Skip();
seenSentences.insert({ handle, sentence }); seenSentences.insert({ handle, sentence });
return false; return false;
} }