-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcharacter_mapper.cpp
More file actions
114 lines (99 loc) · 2.63 KB
/
character_mapper.cpp
File metadata and controls
114 lines (99 loc) · 2.63 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <QFile>
#include <QRegExp>
#include "character_mapper.h"
#include "debug.h"
bool character_mapper::load_map(QString map_file_path)
{
if(map == nullptr){
map = new QMap<unsigned char, unsigned char>();
}else{
map->clear();
}
QFile map_file(map_file_path);
if(!map_file.open(QIODevice::ReadOnly | QIODevice::Text)){
return (map_state = false);
}
while(!map_file.atEnd()){
QString line = map_file.readLine().trimmed();
int seperator = line.remove(QRegExp("//.*")).lastIndexOf('=');
if(line.isEmpty()){
continue;
}
QString right = line.right(line.length()-seperator-1).trimmed();
QString left = line.left(seperator).trimmed().leftJustified(1, ' ');
bool status;
unsigned char value = right.toInt(&status, 16);
if(seperator == -1 || left.length() != 1 || right.length() != 2 || !status){
return (map_state = false);
}
map->insert(left.at(0).toLatin1(), value);
}
return (map_state = true);
}
void character_mapper::save_map(QString map_file_path)
{
if(map == nullptr){
return;
}
QFile map_file(map_file_path);
if(!map_file.open(QIODevice::WriteOnly | QIODevice::Text)){
return;
}
const QList<unsigned char> keys = map->keys();
const QList<unsigned char> values = map->values();
int count = keys.count();
for(int i = 0; i < count; i++){
QString line = QString((char)keys[i]) + "=" + QString::number(values[i], 16) + "\n";
map_file.write(line.toLatin1());
}
}
void character_mapper::set_map(QMap<unsigned char, unsigned char> *dialog_mapper)
{
if(map != nullptr && map != dialog_mapper){
delete map;
}
map = dialog_mapper;
map_state = true;
}
unsigned char character_mapper::decode(unsigned char input)
{
if(map != nullptr && map->contains(input) && map_state){
return map->value(input);
}
return input;
}
QByteArray character_mapper::decode(QByteArray input)
{
if(map != nullptr && map_state){
for(int i = input.length() - 1; i > -1; i--){
unsigned char current = input.at(i);
input[i] = map->contains(current) ? map->value(current) : current;
}
}
return input;
}
unsigned char character_mapper::encode(unsigned char input)
{
if(map != nullptr && map->key(input) && map_state){
return map->key(input);
}
return input;
}
QByteArray character_mapper::encode(QByteArray input)
{
if(map != nullptr && map_state){
for(int i = input.length() - 1; i > -1; i--){
unsigned char current = input.at(i);
input[i] = map->key(current) ? map->key(current) : current;
}
}
return input;
}
void character_mapper::delete_active_map()
{
if(map != nullptr){
delete map;
}
}
QMap<unsigned char, unsigned char> *character_mapper::map = nullptr;
bool character_mapper::map_state = false;