Skip to content

Commit d7d4eac

Browse files
committed
v0.0.86
1 parent 22e1462 commit d7d4eac

11 files changed

+386
-242
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
============
44

5+
## 0.0.86
6+
7+
- Implemented:
8+
- Encoding Menu -> Interpret as ... ISO-2022-JP, ISO-2022-CN-EXT, ISO-2022-CN
9+
510
## 0.0.85
611

712
- Implemented:

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ set(PROJECT_SOURCES
191191
src/encoding/interpret_as_iso_2022_jp_2.h
192192
src/encoding/interpret_as_iso_2022_jp_1.cpp
193193
src/encoding/interpret_as_iso_2022_jp_1.h
194+
src/encoding/interpret_as_iso_2022_jp.cpp
195+
src/encoding/interpret_as_iso_2022_jp.h
196+
src/encoding/interpret_as_iso_2022_cn_ext.cpp
197+
src/encoding/interpret_as_iso_2022_cn_ext.h
198+
src/encoding/interpret_as_iso_2022_cn.cpp
199+
src/encoding/interpret_as_iso_2022_cn.h
194200
${PROJECT_UI}
195201
)
196202

CMakeLists.txt.user

-241
This file was deleted.

src/encoding/interpret_as_dialog.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ InterpreteAsDialog::InterpreteAsDialog(QWidget* parent)
4141
"ISO-8859-1",
4242
"ISO-2022-KR",
4343
"ISO-2022-JP-2",
44-
"ISO-2022-JP-1"
44+
"ISO-2022-JP-1",
45+
"ISO-2022-JP",
46+
"ISO-2022-CN-EXT",
47+
"ISO-2022-CN"
4548
});
4649

4750
// Create OK and Cancel buttons using QDialogButtonBox
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "../codeeditor.h"
2+
#include "interpret_as_iso_2022_cn.h"
3+
#include <QFile>
4+
#include <QDebug>
5+
6+
// Singleton instance
7+
Interpret_As_ISO_2022_CN& Interpret_As_ISO_2022_CN::instance() {
8+
static Interpret_As_ISO_2022_CN instance;
9+
return instance;
10+
}
11+
12+
// GB Table for ISO-2022-CN (Limited set of GB2312 mappings)
13+
const std::unordered_map<QByteArray, QChar> Interpret_As_ISO_2022_CN::gbTable = {
14+
{QByteArray("\x21\x21"), QChar(0x3000)}, // Ideographic space
15+
{QByteArray("\x21\x22"), QChar(0x3001)}, // 。 (period)
16+
{QByteArray("\x21\x23"), QChar(0x3002)}, // 、 (comma)
17+
{QByteArray("\x21\x27"), QChar(0x4E00)}, // 一 (one)
18+
{QByteArray("\x21\x28"), QChar(0x4E8C)}, // 二 (two)
19+
{QByteArray("\x21\x29"), QChar(0x4E09)}, // 三 (three)
20+
{QByteArray("\x21\x2A"), QChar(0x56DB)}, // 四 (four)
21+
{QByteArray("\x21\x2B"), QChar(0x4E94)}, // 五 (five)
22+
{QByteArray("\x21\x2C"), QChar(0x516D)}, // 六 (six)
23+
{QByteArray("\x21\x2D"), QChar(0x4E03)}, // 七 (seven)
24+
{QByteArray("\x21\x2E"), QChar(0x516B)}, // 八 (eight)
25+
{QByteArray("\x21\x2F"), QChar(0x4E5D)}, // 九 (nine)
26+
{QByteArray("\x21\x30"), QChar(0x5341)}, // 十 (ten)
27+
{QByteArray("\x21\x40"), QChar(0x65E5)}, // 日 (day)
28+
{QByteArray("\x21\x41"), QChar(0x6708)}, // 月 (moon)
29+
{QByteArray("\x21\x42"), QChar(0x706B)}, // 火 (fire)
30+
{QByteArray("\x21\x43"), QChar(0x6C34)}, // 水 (water)
31+
};
32+
33+
// Constructor
34+
Interpret_As_ISO_2022_CN::Interpret_As_ISO_2022_CN() {}
35+
36+
// Main execution function
37+
void Interpret_As_ISO_2022_CN::execute(QPlainTextEdit* editor) {
38+
if (!editor) {
39+
qWarning() << "[ERROR] No editor instance provided.";
40+
return;
41+
}
42+
43+
CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor);
44+
if (!codeEditor) {
45+
qWarning() << "[ERROR] Invalid CodeEditor instance.";
46+
return;
47+
}
48+
49+
QString filePath = codeEditor->filePath();
50+
if (filePath.isEmpty()) {
51+
qWarning() << "[ERROR] No file path associated with the editor.";
52+
return;
53+
}
54+
55+
QFile file(filePath);
56+
if (!file.open(QIODevice::ReadOnly)) {
57+
qWarning() << "[ERROR] Cannot open file:" << filePath;
58+
return;
59+
}
60+
61+
QByteArray rawData = file.readAll();
62+
file.close();
63+
64+
QString decodedText = decodeISO2022CN(rawData);
65+
editor->setPlainText(decodedText);
66+
qDebug() << "[DEBUG] ISO-2022-CN Decoding applied for file:" << filePath;
67+
}
68+
69+
// Decode raw data from ISO-2022-CN encoding
70+
QString Interpret_As_ISO_2022_CN::decodeISO2022CN(const QByteArray& rawData) {
71+
QString result;
72+
QByteArray buffer;
73+
bool inGBMode = false;
74+
75+
for (int i = 0; i < rawData.size(); ++i) {
76+
if (rawData.mid(i, 4) == "\x1B\x24\x29\x41") { // Enter GB2312 mode
77+
inGBMode = true;
78+
i += 3;
79+
} else if (rawData.mid(i, 3) == "\x1B\x28\x42") { // Return to ASCII
80+
inGBMode = false;
81+
i += 2;
82+
} else if (inGBMode) {
83+
buffer.append(rawData[i]);
84+
if (buffer.size() == 2) {
85+
if (gbTable.contains(buffer)) {
86+
result.append(gbTable.at(buffer));
87+
} else {
88+
result.append(QChar(0xFFFD)); // Fallback for unmapped bytes
89+
}
90+
buffer.clear();
91+
}
92+
} else {
93+
result.append(QChar(rawData[i] & 0xFF)); // ASCII Mode
94+
}
95+
}
96+
return result;
97+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#include <QPlainTextEdit>
3+
#include <QString>
4+
#include <QByteArray>
5+
#include <unordered_map>
6+
7+
class Interpret_As_ISO_2022_CN {
8+
public:
9+
static Interpret_As_ISO_2022_CN& instance();
10+
void execute(QPlainTextEdit* editor);
11+
12+
private:
13+
Interpret_As_ISO_2022_CN();
14+
~Interpret_As_ISO_2022_CN() = default;
15+
16+
Interpret_As_ISO_2022_CN(const Interpret_As_ISO_2022_CN&) = delete;
17+
Interpret_As_ISO_2022_CN& operator=(const Interpret_As_ISO_2022_CN&) = delete;
18+
19+
QString decodeISO2022CN(const QByteArray& rawData);
20+
static const std::unordered_map<QByteArray, QChar> gbTable;
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "../codeeditor.h"
2+
#include "interpret_as_iso_2022_cn_ext.h"
3+
#include <QFile>
4+
#include <QDebug>
5+
6+
// Singleton instance
7+
Interpret_As_ISO_2022_CN_EXT& Interpret_As_ISO_2022_CN_EXT::instance() {
8+
static Interpret_As_ISO_2022_CN_EXT instance;
9+
return instance;
10+
}
11+
12+
// GB Table for ISO-2022-CN-EXT (Limited set of GB2312 mappings)
13+
const std::unordered_map<QByteArray, QChar> Interpret_As_ISO_2022_CN_EXT::gbTable = {
14+
{QByteArray("\x21\x21"), QChar(0x3000)}, // Ideographic space
15+
{QByteArray("\x21\x22"), QChar(0x3001)}, // 。 (period)
16+
{QByteArray("\x21\x23"), QChar(0x3002)}, // 、 (comma)
17+
{QByteArray("\x21\x27"), QChar(0x4E00)}, // 一 (one)
18+
{QByteArray("\x21\x28"), QChar(0x4E8C)}, // 二 (two)
19+
{QByteArray("\x21\x29"), QChar(0x4E09)}, // 三 (three)
20+
{QByteArray("\x21\x2A"), QChar(0x56DB)}, // 四 (four)
21+
{QByteArray("\x21\x2B"), QChar(0x4E94)}, // 五 (five)
22+
{QByteArray("\x21\x2C"), QChar(0x516D)}, // 六 (six)
23+
{QByteArray("\x21\x2D"), QChar(0x4E03)}, // 七 (seven)
24+
{QByteArray("\x21\x2E"), QChar(0x516B)}, // 八 (eight)
25+
{QByteArray("\x21\x2F"), QChar(0x4E5D)}, // 九 (nine)
26+
{QByteArray("\x21\x30"), QChar(0x5341)}, // 十 (ten)
27+
{QByteArray("\x21\x40"), QChar(0x65E5)}, // 日 (day)
28+
{QByteArray("\x21\x41"), QChar(0x6708)}, // 月 (moon)
29+
{QByteArray("\x21\x42"), QChar(0x706B)}, // 火 (fire)
30+
{QByteArray("\x21\x43"), QChar(0x6C34)}, // 水 (water)
31+
};
32+
33+
// Constructor
34+
Interpret_As_ISO_2022_CN_EXT::Interpret_As_ISO_2022_CN_EXT() {}
35+
36+
// Main execution function
37+
void Interpret_As_ISO_2022_CN_EXT::execute(QPlainTextEdit* editor) {
38+
if (!editor) {
39+
qWarning() << "[ERROR] No editor instance provided.";
40+
return;
41+
}
42+
43+
CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor);
44+
if (!codeEditor) {
45+
qWarning() << "[ERROR] Invalid CodeEditor instance.";
46+
return;
47+
}
48+
49+
QString filePath = codeEditor->filePath();
50+
if (filePath.isEmpty()) {
51+
qWarning() << "[ERROR] No file path associated with the editor.";
52+
return;
53+
}
54+
55+
QFile file(filePath);
56+
if (!file.open(QIODevice::ReadOnly)) {
57+
qWarning() << "[ERROR] Cannot open file:" << filePath;
58+
return;
59+
}
60+
61+
QByteArray rawData = file.readAll();
62+
file.close();
63+
64+
QString decodedText = decodeISO2022CNEXT(rawData);
65+
editor->setPlainText(decodedText);
66+
qDebug() << "[DEBUG] ISO-2022-CN-EXT Decoding applied for file:" << filePath;
67+
}
68+
69+
// Decode raw data from ISO-2022-CN-EXT encoding
70+
QString Interpret_As_ISO_2022_CN_EXT::decodeISO2022CNEXT(const QByteArray& rawData) {
71+
QString result;
72+
QByteArray buffer;
73+
bool inGBMode = false;
74+
75+
for (int i = 0; i < rawData.size(); ++i) {
76+
if (rawData.mid(i, 4) == "\x1B\x24\x29\x41") { // Enter GB2312 mode
77+
inGBMode = true;
78+
i += 3;
79+
} else if (rawData.mid(i, 3) == "\x1B\x28\x42") { // Return to ASCII
80+
inGBMode = false;
81+
i += 2;
82+
} else if (inGBMode) {
83+
buffer.append(rawData[i]);
84+
if (buffer.size() == 2) {
85+
if (gbTable.contains(buffer)) {
86+
result.append(gbTable.at(buffer));
87+
} else {
88+
result.append(QChar(0xFFFD)); // Fallback for unmapped bytes
89+
}
90+
buffer.clear();
91+
}
92+
} else {
93+
result.append(QChar(rawData[i] & 0xFF)); // ASCII Mode
94+
}
95+
}
96+
return result;
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#include <QPlainTextEdit>
3+
#include <QString>
4+
#include <QByteArray>
5+
#include <unordered_map>
6+
7+
class Interpret_As_ISO_2022_CN_EXT {
8+
public:
9+
static Interpret_As_ISO_2022_CN_EXT& instance();
10+
void execute(QPlainTextEdit* editor);
11+
12+
private:
13+
Interpret_As_ISO_2022_CN_EXT();
14+
~Interpret_As_ISO_2022_CN_EXT() = default;
15+
16+
Interpret_As_ISO_2022_CN_EXT(const Interpret_As_ISO_2022_CN_EXT&) = delete;
17+
Interpret_As_ISO_2022_CN_EXT& operator=(const Interpret_As_ISO_2022_CN_EXT&) = delete;
18+
19+
QString decodeISO2022CNEXT(const QByteArray& rawData);
20+
static const std::unordered_map<QByteArray, QChar> gbTable;
21+
};
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "../codeeditor.h"
2+
#include "interpret_as_iso_2022_jp.h"
3+
#include <QFile>
4+
#include <QDebug>
5+
6+
// Singleton instance
7+
Interpret_As_ISO_2022_JP& Interpret_As_ISO_2022_JP::instance() {
8+
static Interpret_As_ISO_2022_JP instance;
9+
return instance;
10+
}
11+
12+
// Extended JIS Table for ISO-2022-JP (JIS X 0208 mapping)
13+
const std::unordered_map<QByteArray, QChar> Interpret_As_ISO_2022_JP::jisTable = {
14+
{QByteArray("\x24\x21"), QChar(0x3000)}, // Ideographic space
15+
{QByteArray("\x24\x22"), QChar(0x3001)}, // 。 (period)
16+
{QByteArray("\x24\x23"), QChar(0x3002)}, // 、 (comma)
17+
{QByteArray("\x24\x3C"), QChar(0x3093)}, // ん (n)
18+
{QByteArray("\x24\x35"), QChar(0x3061)}, // ち (chi)
19+
{QByteArray("\x24\x25"), QChar(0x4E00)}, // 一 (one)
20+
{QByteArray("\x24\x26"), QChar(0x4E8C)}, // 二 (two)
21+
{QByteArray("\x24\x27"), QChar(0x4E09)}, // 三 (three)
22+
{QByteArray("\x24\x28"), QChar(0x56DB)}, // 四 (four)
23+
{QByteArray("\x24\x29"), QChar(0x4E94)}, // 五 (five)
24+
{QByteArray("\x24\x2A"), QChar(0x516D)}, // 六 (six)
25+
{QByteArray("\x24\x2B"), QChar(0x4E03)}, // 七 (seven)
26+
{QByteArray("\x24\x2C"), QChar(0x516B)}, // 八 (eight)
27+
{QByteArray("\x24\x2D"), QChar(0x4E5D)}, // 九 (nine)
28+
{QByteArray("\x24\x2E"), QChar(0x5341)}, // 十 (ten)
29+
{QByteArray("\x24\x50"), QChar(0x65E5)}, // 日 (sun, day)
30+
{QByteArray("\x24\x51"), QChar(0x6708)}, // 月 (moon, month)
31+
{QByteArray("\x24\x52"), QChar(0x706B)}, // 火 (fire, Tuesday)
32+
{QByteArray("\x24\x53"), QChar(0x6C34)}, // 水 (water, Wednesday)
33+
{QByteArray("\x24\x54"), QChar(0x6728)}, // 木 (tree, Thursday)
34+
{QByteArray("\x24\x55"), QChar(0x91D1)}, // 金 (gold, Friday)
35+
{QByteArray("\x24\x56"), QChar(0x571F)}, // 土 (earth, Saturday)
36+
{QByteArray("\x24\x30"), QChar(0x3042)}, // あ (a)
37+
{QByteArray("\x24\x31"), QChar(0x3044)}, // い (i)
38+
{QByteArray("\x24\x32"), QChar(0x306B)}, // に (ni)
39+
{QByteArray("\x24\x33"), QChar(0x3053)}, // こ (ko)
40+
};
41+
42+
// Constructor
43+
Interpret_As_ISO_2022_JP::Interpret_As_ISO_2022_JP() {}
44+
45+
// Main execution function
46+
void Interpret_As_ISO_2022_JP::execute(QPlainTextEdit* editor) {
47+
if (!editor) {
48+
qWarning() << "[ERROR] No editor instance provided.";
49+
return;
50+
}
51+
52+
CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor);
53+
if (!codeEditor) {
54+
qWarning() << "[ERROR] Invalid CodeEditor instance.";
55+
return;
56+
}
57+
58+
QString filePath = codeEditor->filePath();
59+
if (filePath.isEmpty()) {
60+
qWarning() << "[ERROR] No file path associated with the editor.";
61+
return;
62+
}
63+
64+
QFile file(filePath);
65+
if (!file.open(QIODevice::ReadOnly)) {
66+
qWarning() << "[ERROR] Cannot open file:" << filePath;
67+
return;
68+
}
69+
70+
QByteArray rawData = file.readAll();
71+
file.close();
72+
73+
QString decodedText = decodeISO2022JP(rawData);
74+
editor->setPlainText(decodedText);
75+
qDebug() << "[DEBUG] ISO-2022-JP Decoding applied for file:" << filePath;
76+
}
77+
78+
// Decode raw data from ISO-2022-JP
79+
QString Interpret_As_ISO_2022_JP::decodeISO2022JP(const QByteArray& rawData) {
80+
QString result;
81+
QByteArray buffer;
82+
bool inKanjiMode = false;
83+
84+
for (int i = 0; i < rawData.size(); ++i) {
85+
if (rawData.mid(i, 3) == "\x1B\x24\x42") {
86+
inKanjiMode = true;
87+
i += 2;
88+
} else if (rawData.mid(i, 3) == "\x1B\x28\x42") {
89+
inKanjiMode = false;
90+
i += 2;
91+
} else if (inKanjiMode) {
92+
buffer.append(rawData[i]);
93+
if (buffer.size() == 2) {
94+
result.append(jisTable.contains(buffer) ? jisTable.at(buffer) : QChar(0xFFFD));
95+
buffer.clear();
96+
}
97+
} else {
98+
result.append(QChar(rawData[i] & 0xFF));
99+
}
100+
}
101+
return result;
102+
}

0 commit comments

Comments
 (0)