Skip to content

Commit

Permalink
v0.0.83
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand committed Dec 28, 2024
1 parent a42ad7d commit a6fa74a
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 5 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.83

- Implemented:
- Encoding Menu -> Interpret as ... ISO-8859-14, ISO-8859-13

## 0.0.82

- Implemented:
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ set(PROJECT_SOURCES
src/encoding/interpret_as_koi8_r.h
src/encoding/interpret_as_iso_8859_15.cpp
src/encoding/interpret_as_iso_8859_15.h
src/encoding/interpret_as_iso_8859_14.cpp
src/encoding/interpret_as_iso_8859_14.h
src/encoding/interpret_as_iso_8859_13.cpp
src/encoding/interpret_as_iso_8859_13.h
src/encoding/interpret_as_iso_8859_9.cpp
src/encoding/interpret_as_iso_8859_9.h
src/encoding/interpret_as_iso_8859_8.cpp
Expand Down
10 changes: 5 additions & 5 deletions CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 15.0.0, 2024-12-28T17:48:52. -->
<!-- Written by QtCreator 15.0.0, 2024-12-29T01:20:53. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -103,14 +103,14 @@
<value type="int" key="CMake.Configure.BaseEnvironment">2</value>
<value type="bool" key="CMake.Configure.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="CMake.Configure.UserEnvironmentChanges"/>
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake
-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_GENERATOR:STRING=Ninja</value>
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}</value>
<value type="QString" key="CMake.Source.Directory">/data/Code/Qt/Notepad--</value>
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/data/Code/Qt/Notepad--/build/Desktop_Qt_6_8_1-Debug</value>
Expand Down
2 changes: 2 additions & 0 deletions src/encoding/interpret_as_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ InterpreteAsDialog::InterpreteAsDialog(QWidget* parent)
"KOI8-U",
"KOI8-R",
"ISO-8859-15",
"ISO-8859-14",
"ISO-8859-13",
"ISO-8859-9",
"ISO-8859-8",
"ISO-8859-7",
Expand Down
84 changes: 84 additions & 0 deletions src/encoding/interpret_as_iso_8859_13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "interpret_as_iso_8859_13.h"
#include <QFile>
#include <QDebug>
#include <QPlainTextEdit>
#include "../codeeditor.h"

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

// ISO-8859-13 to Unicode mapping table (partial, extend as needed)
const QMap<uint8_t, QChar> iso885913ToUnicode = {
{ 0xA1, QChar(0x0104) }, // Ą
{ 0xA2, QChar(0x012E) }, // Į
{ 0xA3, QChar(0x0100) }, // Ā
{ 0xA5, QChar(0x0106) }, // Ć
{ 0xA6, QChar(0x0118) }, // Ę
{ 0xA8, QChar(0x0112) }, // Ē
{ 0xAA, QChar(0x0116) }, // Ė
{ 0xAF, QChar(0x0122) }, // Ģ
{ 0xB3, QChar(0x0101) }, // ā
{ 0xB4, QChar(0x0107) }, // ć
{ 0xB6, QChar(0x0119) }, // ę
{ 0xB8, QChar(0x0113) }, // ē
{ 0xBB, QChar(0x0117) }, // ė
{ 0xBF, QChar(0x0123) }, // ģ
{ 0xC5, QChar(0x0160) }, // Š
{ 0xC6, QChar(0x012A) }, // Ī
{ 0xC9, QChar(0x014C) }, // Ō
{ 0xD3, QChar(0x0161) }, // š
{ 0xD6, QChar(0x012B) }, // ī
{ 0xD9, QChar(0x014D) }, // ō
};

// Decode ISO-8859-13 manually
QString Interpret_As_ISO_8859_13::decodeISO885913(const QByteArray& iso885913Data) {
QString result;

for (char byte : iso885913Data) {
uint8_t isoChar = static_cast<uint8_t>(byte);
if (isoChar < 0x80) {
result.append(QChar(isoChar)); // ASCII characters (Direct mapping)
} else {
// Map ISO-8859-13 specific characters, fallback to original byte
result.append(iso885913ToUnicode.value(isoChar, QChar(isoChar)));
}
}

return result;
}

// Execute Interpretation
void Interpret_As_ISO_8859_13::execute(QPlainTextEdit* editor) {
if (!editor) {
qWarning() << "[ERROR] No editor instance.";
return;
}

CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor);
if (!codeEditor) {
qWarning() << "[ERROR] Editor is not a CodeEditor.";
return;
}

QString filePath = codeEditor->filePath();
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "[ERROR] Cannot open file: " << filePath;
return;
}

QByteArray iso885913Data = file.readAll();
file.close();

// Debugging: Print Raw Bytes
qDebug() << "[DEBUG] Raw Bytes (Hex):" << iso885913Data.toHex();

QString decodedText = decodeISO885913(iso885913Data);
editor->setPlainText(decodedText);

qDebug() << "[DEBUG] ISO-8859-13 Decoding applied for file:" << filePath;
}
18 changes: 18 additions & 0 deletions src/encoding/interpret_as_iso_8859_13.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <QString>
#include <QPlainTextEdit>

class Interpret_As_ISO_8859_13 {
public:
static Interpret_As_ISO_8859_13& instance();
void execute(QPlainTextEdit* editor);

private:
Interpret_As_ISO_8859_13() = default;
~Interpret_As_ISO_8859_13() = default;
Interpret_As_ISO_8859_13(const Interpret_As_ISO_8859_13&) = delete;
Interpret_As_ISO_8859_13& operator=(const Interpret_As_ISO_8859_13&) = delete;

QString decodeISO885913(const QByteArray& iso885913Data);
};
83 changes: 83 additions & 0 deletions src/encoding/interpret_as_iso_8859_14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "interpret_as_iso_8859_14.h"
#include <QFile>
#include <QDebug>
#include <QPlainTextEdit>
#include "../codeeditor.h"

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

// ISO-8859-14 to Unicode mapping table (partial, extend as needed)
const QMap<uint8_t, QChar> iso885914ToUnicode = {
{ 0xA1, QChar(0x1E02) }, //
{ 0xA2, QChar(0x1E03) }, //
{ 0xA3, QChar(0x010A) }, // Ċ
{ 0xA4, QChar(0x010B) }, // ċ
{ 0xA5, QChar(0x1E0A) }, //
{ 0xA6, QChar(0x1E09) }, //
{ 0xA7, QChar(0x1E1E) }, //
{ 0xA8, QChar(0x1E1F) }, //
{ 0xA9, QChar(0x0120) }, // Ġ
{ 0xAA, QChar(0x0121) }, // ġ
{ 0xAB, QChar(0x1E40) }, //
{ 0xAC, QChar(0x1E41) }, //
{ 0xAD, QChar(0x1E56) }, //
{ 0xAE, QChar(0x1E57) }, //
{ 0xAF, QChar(0x1E60) }, //
{ 0xB0, QChar(0x1E61) }, //
{ 0xB1, QChar(0x1E6A) }, //
{ 0xB2, QChar(0x1E6B) }, //
{ 0xBB, QChar(0x00BB) }, // »
};

// Decode ISO-8859-14 manually
QString Interpret_As_ISO_8859_14::decodeISO885914(const QByteArray& iso885914Data) {
QString result;

for (char byte : iso885914Data) {
uint8_t isoChar = static_cast<uint8_t>(byte);
if (isoChar < 0x80) {
result.append(QChar(isoChar)); // ASCII characters (Direct mapping)
} else {
// Map ISO-8859-14 specific characters, fallback to original byte
result.append(iso885914ToUnicode.value(isoChar, QChar(isoChar)));
}
}

return result;
}

// Execute Interpretation
void Interpret_As_ISO_8859_14::execute(QPlainTextEdit* editor) {
if (!editor) {
qWarning() << "[ERROR] No editor instance.";
return;
}

CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor);
if (!codeEditor) {
qWarning() << "[ERROR] Editor is not a CodeEditor.";
return;
}

QString filePath = codeEditor->filePath();
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "[ERROR] Cannot open file: " << filePath;
return;
}

QByteArray iso885914Data = file.readAll();
file.close();

// Debugging: Print Raw Bytes
qDebug() << "[DEBUG] Raw Bytes (Hex):" << iso885914Data.toHex();

QString decodedText = decodeISO885914(iso885914Data);
editor->setPlainText(decodedText);

qDebug() << "[DEBUG] ISO-8859-14 Decoding applied for file:" << filePath;
}
18 changes: 18 additions & 0 deletions src/encoding/interpret_as_iso_8859_14.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <QString>
#include <QPlainTextEdit>

class Interpret_As_ISO_8859_14 {
public:
static Interpret_As_ISO_8859_14& instance();
void execute(QPlainTextEdit* editor);

private:
Interpret_As_ISO_8859_14() = default;
~Interpret_As_ISO_8859_14() = default;
Interpret_As_ISO_8859_14(const Interpret_As_ISO_8859_14&) = delete;
Interpret_As_ISO_8859_14& operator=(const Interpret_As_ISO_8859_14&) = delete;

QString decodeISO885914(const QByteArray& iso885914Data);
};
8 changes: 8 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include "encoding/interpret_as_koi8_u.h"
#include "encoding/interpret_as_koi8_r.h"
#include "encoding/interpret_as_iso_8859_15.h"
#include "encoding/interpret_as_iso_8859_14.h"
#include "encoding/interpret_as_iso_8859_13.h"
#include "encoding/interpret_as_iso_8859_9.h"
#include "encoding/interpret_as_iso_8859_8.h"
#include "encoding/interpret_as_iso_8859_7.h"
Expand Down Expand Up @@ -866,6 +868,12 @@ void MainWindow::on_actionInterpret_As_triggered()
if (selectedItem == "ISO-8859-15") {
Interpret_As_ISO_8859_15::instance().execute(editor);
}
if (selectedItem == "ISO-8859-14") {
Interpret_As_ISO_8859_14::instance().execute(editor);
}
if (selectedItem == "ISO-8859-13") {
Interpret_As_ISO_8859_13::instance().execute(editor);
}
if (selectedItem == "ISO-8859-9") {
Interpret_As_ISO_8859_9::instance().execute(editor);
}
Expand Down

0 comments on commit a6fa74a

Please sign in to comment.