Skip to content

Commit

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

- Implemented:
- Encoding Menu -> Interpret as ... ISO-8859-1, ISO-8859-15

## 0.0.81

- Implemented:
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ set(PROJECT_SOURCES
src/encoding/interpret_as_koi8_u.h
src/encoding/interpret_as_koi8_r.cpp
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_9.cpp
src/encoding/interpret_as_iso_8859_9.h
src/encoding/interpret_as_iso_8859_8.cpp
Expand All @@ -175,6 +177,8 @@ set(PROJECT_SOURCES
src/encoding/interpret_as_iso_8859_3.h
src/encoding/interpret_as_iso_8859_2.cpp
src/encoding/interpret_as_iso_8859_2.h
src/encoding/interpret_as_iso_8859_1.cpp
src/encoding/interpret_as_iso_8859_1.h
${PROJECT_UI}
)

Expand Down
14 changes: 7 additions & 7 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-27T00:03:36. -->
<!-- Written by QtCreator 15.0.0, 2024-12-28T17:48:52. -->
<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_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DCMAKE_BUILD_TYPE:STRING=Debug
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}</value>
-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_GENERATOR:STRING=Ninja</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
4 changes: 3 additions & 1 deletion src/encoding/interpret_as_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ InterpreteAsDialog::InterpreteAsDialog(QWidget* parent)
"SCSU",
"KOI8-U",
"KOI8-R",
"ISO-8859-15",
"ISO-8859-9",
"ISO-8859-8",
"ISO-8859-7",
"ISO-8859-6",
"ISO-8859-5",
"ISO-8859-4",
"ISO-8859-3",
"ISO-8859-2"
"ISO-8859-2",
"ISO-8859-1"
});

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

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

// Decode ISO-8859-1 manually (1:1 mapping for 0xA0 to 0xFF)
QString Interpret_As_ISO_8859_1::decodeISO88591(const QByteArray& iso88591Data) {
QString result;
for (char byte : iso88591Data) {
uint8_t isoChar = static_cast<uint8_t>(byte);
result.append(QChar(isoChar)); // Direct 1:1 mapping
}
return result;
}

// Execute Interpretation
void Interpret_As_ISO_8859_1::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 iso88591Data = file.readAll();
file.close();

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

QString decodedText = decodeISO88591(iso88591Data);
editor->setPlainText(decodedText);

qDebug() << "[DEBUG] Decoding applied for file:" << filePath;
}
18 changes: 18 additions & 0 deletions src/encoding/interpret_as_iso_8859_1.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_1 {
public:
static Interpret_As_ISO_8859_1& instance();
void execute(QPlainTextEdit* editor);

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

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

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

// ISO-8859-15 to Unicode mapping table for differences from ISO-8859-1
const QMap<uint8_t, QChar> iso885915ToUnicode = {
{ 0xA4, QChar(0x20AC) }, // € - Euro Sign
{ 0xA6, QChar(0x0160) }, // Š - Latin Capital Letter S with Caron
{ 0xA8, QChar(0x0161) }, // š - Latin Small Letter S with Caron
{ 0xB4, QChar(0x017D) }, // Ž - Latin Capital Letter Z with Caron
{ 0xB8, QChar(0x017E) }, // ž - Latin Small Letter Z with Caron
{ 0xBC, QChar(0x0152) }, // Œ - Latin Capital Ligature OE
{ 0xBD, QChar(0x0153) }, // œ - Latin Small Ligature OE
{ 0xBE, QChar(0x0178) }, // Ÿ - Latin Capital Letter Y with Diaeresis
};

// Decode ISO-8859-15 manually
QString Interpret_As_ISO_8859_15::decodeISO885915(const QByteArray& iso885915Data) {
QString result;

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

return result;
}

// Execute Interpretation
void Interpret_As_ISO_8859_15::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 iso885915Data = file.readAll();
file.close();

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

QString decodedText = decodeISO885915(iso885915Data);
editor->setPlainText(decodedText);

qDebug() << "[DEBUG] ISO-8859-15 Decoding applied for file:" << filePath;
}
18 changes: 18 additions & 0 deletions src/encoding/interpret_as_iso_8859_15.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_15 {
public:
static Interpret_As_ISO_8859_15& instance();
void execute(QPlainTextEdit* editor);

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

QString decodeISO885915(const QByteArray& iso885915Data);
};
8 changes: 8 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "encoding/interpret_as_scsu.h"
#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_9.h"
#include "encoding/interpret_as_iso_8859_8.h"
#include "encoding/interpret_as_iso_8859_7.h"
Expand All @@ -50,6 +51,7 @@
#include "encoding/interpret_as_iso_8859_4.h"
#include "encoding/interpret_as_iso_8859_3.h"
#include "encoding/interpret_as_iso_8859_2.h"
#include "encoding/interpret_as_iso_8859_1.h"

MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent),
Expand Down Expand Up @@ -861,6 +863,9 @@ void MainWindow::on_actionInterpret_As_triggered()
Helpers::notImplemented(this);
Interpret_As_KOI8_R::instance().execute(editor);
}
if (selectedItem == "ISO-8859-15") {
Interpret_As_ISO_8859_15::instance().execute(editor);
}
if (selectedItem == "ISO-8859-9") {
Interpret_As_ISO_8859_9::instance().execute(editor);
}
Expand All @@ -886,6 +891,9 @@ void MainWindow::on_actionInterpret_As_triggered()
if (selectedItem == "ISO-8859-2") {
Interpret_As_ISO_8859_2::instance().execute(editor);
}
if (selectedItem == "ISO-8859-1") {
Interpret_As_ISO_8859_1::instance().execute(editor);
}
}
}

Expand Down

0 comments on commit a42ad7d

Please sign in to comment.