-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.cpp
More file actions
48 lines (44 loc) · 1.23 KB
/
decoder.cpp
File metadata and controls
48 lines (44 loc) · 1.23 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
#include "decoder.h"
Decoder::Decoder()
{
}
QString Decoder::decodeString(QString input)
{
QByteArray res = QByteArray();
QTextCodec *codec = QTextCodec::codecForName("Windows-1251");
// string inStr = input;
int i = 0;
while(i < input.length()) {
if (input.at(i) < '0' or input.at(i) > '9') {
res.append(input.at(i));
i++;
continue;
}
int q = input.mid(i, 3).toInt();
res.append(q % 300);
i += 3;
}
return codec->toUnicode(res);
}
bool Decoder::decodeFile(QString inFile, QString outFile)
{
if (!QFile::exists(inFile)) return false;
QFile inputFile(inFile);
if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
std::cout << "Input file not found at" + inFile.toStdString() << std::endl;
return false;
}
QFile outputFile(outFile);
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
std::cout << "Can't create output file at" + outFile.toStdString() << std::endl;
return false;
}
QTextStream out(&outputFile);
while (!inputFile.atEnd()) {
QByteArray line = inputFile.readLine();
out << decodeString(line);
}
outputFile.close();
}