forked from ykky0/profanity-ether
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMode.cpp
More file actions
60 lines (51 loc) · 1.66 KB
/
Mode.cpp
File metadata and controls
60 lines (51 loc) · 1.66 KB
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
59
60
#include "Mode.hpp"
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
Mode::Mode() : score(0), prefixCount(0), suffixCount(0), matchingCount(0) {
}
static std::string::size_type hexValueNoException(char c) {
const std::string hex = "0123456789abcdef";
const std::string::size_type ret = hex.find(tolower(c));
return ret;
}
Mode Mode::matching(std::string input) {
Mode r;
std::vector<std::string> list;
std::string prefix = input.substr(0, 2);
if(input.size() == 42 && prefix == "0x") {
list.push_back(input.substr(2));
} else {
std::ifstream file(input);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
if(line.size() == 42) {
list.push_back(line.substr(2));
}
}
} else {
std::cout << "error: Failed to open matching file. :<" << std::endl;
}
}
if(list.size() > 0) {
r.matchingCount = list.size();
for( size_t j = 0; j < list.size(); j += 1) {
const std::string item = list[j];
for( size_t i = 0; i < item.size(); i += 2 ) {
const size_t indexHi = hexValueNoException(item[i]);
const size_t indexLo = (i + 1) < item.size() ? hexValueNoException(item[i+1]) : std::string::npos;
const unsigned long valHi = (indexHi == std::string::npos) ? 0 : indexHi << 4;
const unsigned long valLo = (indexLo == std::string::npos) ? 0 : indexLo;
const int maskHi = (indexHi == std::string::npos) ? 0 : 0xF << 4;
const int maskLo = (indexLo == std::string::npos) ? 0 : 0xF;
r.data1.push_back(maskHi | maskLo);
r.data2.push_back(valHi | valLo);
}
}
}
return r;
}