Skip to content

Commit

Permalink
v0.0.76
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand committed Dec 21, 2024
1 parent 57632c7 commit e7263f0
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

============

## 0.0.76

- Implemented:
- Encoding Menu -> Interpret as ... KOI8-U (needs fundamental change in file loading)

## 0.0.75

- Implemented:
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ set(PROJECT_SOURCES
src/encoding/interpret_as_shift_jts.h
src/encoding/interpret_as_scsu.cpp
src/encoding/interpret_as_scsu.h
src/encoding/interpret_as_koi8_u.cpp
src/encoding/interpret_as_koi8_u.h
${PROJECT_UI}
)

Expand Down
241 changes: 241 additions & 0 deletions CMakeLists.txt.user

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/encoding/interpret_as_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ InterpreteAsDialog::InterpreteAsDialog(QWidget* parent)
"TSCII",
"TIS-620",
"SHIFT-JTS",
"SCSU"
"SCSU",
"KOI8-U"
});

// Create OK and Cancel buttons using QDialogButtonBox
Expand Down
58 changes: 58 additions & 0 deletions src/encoding/interpret_as_koi8_u.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "interpret_as_koi8_u.h"
#include <QFile>
#include <QDebug>

// Singleton instance
Interpret_As_KOI8_U& Interpret_As_KOI8_U::instance() {
static Interpret_As_KOI8_U instance;
return instance;
}

// KOI8-U to Unicode mapping
QChar Interpret_As_KOI8_U::koi8uToUnicode(uint8_t koi8uChar) {
// ASCII range (0x00-0x7F remains unchanged)
if (koi8uChar < 0x80) {
return QChar(koi8uChar);
}

// Mapping for KOI8-U specific characters (0x80-0xFF)
static const uint16_t koi8uToUnicodeTable[128] = {
0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, // 0x80-0x87
0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590, // 0x88-0x8F
// Add the remaining KOI8-U mappings (refer to the KOI8-U standard table)
// ...
0xFFFD // Placeholder for unmapped characters
};

return QChar(koi8uToUnicodeTable[koi8uChar - 0x80]);
}

// Decode the KOI8-U data
QString Interpret_As_KOI8_U::decodeKOI8U(const QByteArray& koi8uData) {
QString result;

for (char byte : koi8uData) {
QChar unicodeChar = koi8uToUnicode(static_cast<uint8_t>(byte));
result.append(unicodeChar);
}

return result;
}

// Execute the KOI8-U interpretation
void Interpret_As_KOI8_U::execute(QPlainTextEdit* editor) {
if (!editor) {
qWarning() << "[ERROR] No editor instance provided.";
return;
}

// Access the raw QByteArray from the editor's document
QTextDocument* doc = editor->document();
QByteArray rawBytes = doc->toRawText().toLocal8Bit();

// Decode KOI8-U from raw bytes
QString decodedText = decodeKOI8U(rawBytes);

// Replace the text in the editor with the decoded text
editor->setPlainText(decodedText);
}
25 changes: 25 additions & 0 deletions src/encoding/interpret_as_koi8_u.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <QString>
#include <QByteArray>
#include <QPlainTextEdit>

class Interpret_As_KOI8_U {
public:
static Interpret_As_KOI8_U& instance();

// Main decoding method
QString decodeKOI8U(const QByteArray& koi8uData);

// Executes the KOI8-U interpretation for a given editor
void execute(QPlainTextEdit* editor);

private:
Interpret_As_KOI8_U() = default;
~Interpret_As_KOI8_U() = default;

Interpret_As_KOI8_U(const Interpret_As_KOI8_U&) = delete;
Interpret_As_KOI8_U& operator=(const Interpret_As_KOI8_U&) = delete;

QChar koi8uToUnicode(uint8_t koi8uChar);
};
2 changes: 1 addition & 1 deletion src/fileloaderworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void FileLoaderWorker::startLoading() {

const qint64 chunkSize = 16 * 1024; // Use 16KB chunks for finer updates
QTextStream in(&file);
in.setEncoding(QStringConverter::Utf8);
in.setEncoding(QStringConverter::Utf8); // FIXME: Load in QByteArray buffer too (for encodings)

qint64 bytesRead = 0;
int lastReportedProgress = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "encoding/interpret_as_tis_620.h"
#include "encoding/interpret_as_shift_jts.h"
#include "encoding/interpret_as_scsu.h"
#include "encoding/interpret_as_koi8_u.h"

MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent),
Expand Down Expand Up @@ -846,6 +847,10 @@ void MainWindow::on_actionInterpret_As_triggered()
if (selectedItem == "SCSU") {
Interpret_As_SCSU::instance().execute(editor);
}
if (selectedItem == "KOI8-U") {
Helpers::notImplemented(this);
Interpret_As_KOI8_U::instance().execute(editor);
}
}
}

Expand Down

0 comments on commit e7263f0

Please sign in to comment.