-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathOJAdapter.cpp
58 lines (58 loc) · 1.72 KB
/
OJAdapter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "OJAdapter.hpp"
#include <iostream>
#include <vector>
static std::vector<OJAdapterInfo*>& getRef() {
static std::vector<OJAdapterInfo*> ref;
return ref;
}
OJAdapterInfo::OJAdapterInfo(
const std::string& name, const std::regex& pattern,
const std::function<bool(const fs::path&)>& func,
int priority)
: mName(name), mPattern(pattern), mFunc(func),
mPriority(priority) {
getRef().push_back(this);
}
bool OJAdapterInfo::match(const fs::path& exec) const {
return std::regex_match(exec.stem().string(),
mPattern);
}
bool OJAdapterInfo::call(const fs::path& exec) const {
std::cout << "Using \033[33m" << mName << "\033[0m"
<< std::endl;
return mFunc(exec);
}
std::string OJAdapterInfo::name() const {
return mName;
}
bool OJAdapterInfo::
operator<(const OJAdapterInfo& rhs) const {
return mPriority < rhs.mPriority;
}
bool runDefault(const fs::path& exec);
void autoRun(const fs::path& exec) {
std::vector<OJAdapterInfo*>& ref = getRef();
std::sort(ref.rbegin(), ref.rend(),
[](const OJAdapterInfo* a,
const OJAdapterInfo* b) {
return *a < *b;
});
line("Supported OJAdapter");
for(auto poa : ref)
std::cout << poa->name() << std::endl;
line("");
for(auto poa : ref) {
if(poa->match(exec)) {
if(!poa->call(exec)) {
std::cout
<< "\033[35mJudge failed , using "
"DefaultJudger\033[0m"
<< std::endl;
runDefault(exec);
}
return;
}
}
std::cout << "\033[35mNo suitable OJAdapter\033[0m"
<< std::endl;
}