Skip to content

Commit

Permalink
v0.0.91
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand committed Jan 8, 2025
1 parent e66501d commit 4b0bbe7
Show file tree
Hide file tree
Showing 13 changed files with 535 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.91

- Implemented:
- Encoding Menu -> Interpret as ... IBM-857, IBM-855, IBM-852, IBM-850

## 0.0.90

- Implemented:
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ set(PROJECT_SOURCES
src/encoding/interpret_as_ibm_861.h
src/encoding/interpret_as_ibm_860.cpp
src/encoding/interpret_as_ibm_860.h
src/encoding/interpret_as_ibm_857.cpp
src/encoding/interpret_as_ibm_857.h
src/encoding/interpret_as_ibm_855.cpp
src/encoding/interpret_as_ibm_855.h
src/encoding/interpret_as_ibm_852.cpp
src/encoding/interpret_as_ibm_852.h
src/encoding/interpret_as_ibm_850.cpp
src/encoding/interpret_as_ibm_850.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, 2025-01-08T13:24:09. -->
<!-- Written by QtCreator 15.0.0, 2025-01-08T21:08:54. -->
<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}
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_BUILD_TYPE:STRING=Debug</value>
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}</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
6 changes: 5 additions & 1 deletion src/encoding/interpret_as_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ InterpreteAsDialog::InterpreteAsDialog(QWidget* parent)
"IBM-863",
"IBM-862",
"IBM-861",
"IBM-860"
"IBM-860",
"IBM-857",
"IBM-855",
"IBM-852",
"IBM-850"
});

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

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

// EBCDIC to Unicode mapping for IBM-850 (Latin-1)
const std::unordered_map<unsigned char, QChar> Interpret_As_IBM_850::ebcdicTable = {
{0x80, QChar(0x00C7)}, // Ç
{0x81, QChar(0x00FC)}, // ü
{0x82, QChar(0x00E9)}, // é
{0x83, QChar(0x00E2)}, // â
{0x84, QChar(0x00E4)}, // ä
{0x85, QChar(0x00E0)}, // à
{0x86, QChar(0x00E5)}, // å
{0x87, QChar(0x00E7)}, // ç
{0x88, QChar(0x00EA)}, // ê
{0x89, QChar(0x00EB)}, // ë
{0x8A, QChar(0x00E8)}, // è
{0x8B, QChar(0x00EF)}, // ï
{0x8C, QChar(0x00EE)}, // î
{0x8D, QChar(0x00EC)}, // ì
{0x8E, QChar(0x00C4)}, // Ä
{0x8F, QChar(0x00C5)}, // Å
{0x90, QChar(0x00C9)}, // É
{0x91, QChar(0x00E6)}, // æ
{0x92, QChar(0x00C6)}, // Æ
{0x93, QChar(0x00F4)}, // ô
{0x94, QChar(0x00F6)}, // ö
{0x95, QChar(0x00F2)}, // ò
{0x96, QChar(0x00FB)}, // û
{0x97, QChar(0x00F9)}, // ù
{0x98, QChar(0x00FF)}, // ÿ
{0x99, QChar(0x00D6)}, // Ö
{0x9A, QChar(0x00DC)}, // Ü
{0x9B, QChar(0x00A2)}, // ¢
{0x9C, QChar(0x00A3)}, // £
{0x9D, QChar(0x00A5)}, // ¥
{0x9E, QChar(0x20A7)}, //
{0x9F, QChar(0x0192)}, // ƒ
{0xA0, QChar(0x00E1)}, // á
{0xA1, QChar(0x00ED)}, // í
{0xA2, QChar(0x00F3)}, // ó
{0xA3, QChar(0x00FA)}, // ú
{0xA4, QChar(0x00F1)}, // ñ
{0xA5, QChar(0x00D1)}, // Ñ
{0xA6, QChar(0x00AA)}, // ª
{0xA7, QChar(0x00BA)}, // º
};

// Constructor
Interpret_As_IBM_850::Interpret_As_IBM_850() {}

// Main execution function
void Interpret_As_IBM_850::execute(QPlainTextEdit* editor) {
if (!editor) {
qWarning() << "[ERROR] No editor instance provided.";
return;
}

CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor);
if (!codeEditor) {
qWarning() << "[ERROR] Invalid CodeEditor instance.";
return;
}

QString filePath = codeEditor->filePath();
if (filePath.isEmpty()) {
qWarning() << "[ERROR] No file path associated with the editor.";
return;
}

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

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

QString decodedText = decodeIBM850(rawData);
editor->setPlainText(decodedText);
qDebug() << "[DEBUG] IBM-850 Decoding applied for file:" << filePath;
}

// Decode raw data from IBM-850 encoding
QString Interpret_As_IBM_850::decodeIBM850(const QByteArray& rawData) {
QString result;

for (unsigned char byte : rawData) {
if (ebcdicTable.contains(byte)) {
result.append(ebcdicTable.at(byte));
} else {
result.append(QChar(0xFFFD)); // Fallback for unmapped characters
}
}
return result;
}
22 changes: 22 additions & 0 deletions src/encoding/interpret_as_ibm_850.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

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

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

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

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

QString decodeIBM850(const QByteArray& rawData);
static const std::unordered_map<unsigned char, QChar> ebcdicTable;
};
104 changes: 104 additions & 0 deletions src/encoding/interpret_as_ibm_852.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "../codeeditor.h"
#include "interpret_as_ibm_852.h"
#include <QFile>
#include <QDebug>

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

// EBCDIC to Unicode mapping for IBM-852 (Central European)
const std::unordered_map<unsigned char, QChar> Interpret_As_IBM_852::ebcdicTable = {
{0x80, QChar(0x0104)}, // Ą
{0x81, QChar(0x00FC)}, // ü
{0x82, QChar(0x00E9)}, // é
{0x83, QChar(0x0101)}, // ā
{0x84, QChar(0x00E4)}, // ä
{0x85, QChar(0x0123)}, // ģ
{0x86, QChar(0x00E5)}, // å
{0x87, QChar(0x0107)}, // ć
{0x88, QChar(0x0142)}, // ł
{0x89, QChar(0x0113)}, // ē
{0x8A, QChar(0x010D)}, // č
{0x8B, QChar(0x0119)}, // ę
{0x8C, QChar(0x0117)}, // ė
{0x8D, QChar(0x0106)}, // Ć
{0x8E, QChar(0x00C4)}, // Ä
{0x8F, QChar(0x00C5)}, // Å
{0x90, QChar(0x0141)}, // Ł
{0x91, QChar(0x00E6)}, // æ
{0x92, QChar(0x00C6)}, // Æ
{0x93, QChar(0x0112)}, // Ē
{0x94, QChar(0x00F6)}, // ö
{0x95, QChar(0x00F3)}, // ó
{0x96, QChar(0x0144)}, // ń
{0x97, QChar(0x0143)}, // Ń
{0x98, QChar(0x00D6)}, // Ö
{0x99, QChar(0x00DC)}, // Ü
{0x9A, QChar(0x00A2)}, // ¢
{0x9B, QChar(0x015A)}, // Ś
{0x9C, QChar(0x015B)}, // ś
{0x9D, QChar(0x00D3)}, // Ó
{0x9E, QChar(0x0179)}, // Ź
{0x9F, QChar(0x017A)}, // ź
{0xA0, QChar(0x00E1)}, // á
{0xA1, QChar(0x00ED)}, // í
{0xA2, QChar(0x00F3)}, // ó
{0xA3, QChar(0x00FA)}, // ú
{0xA4, QChar(0x00F1)}, // ñ
{0xA5, QChar(0x00D1)}, // Ñ
{0xA6, QChar(0x015E)}, // Ş
{0xA7, QChar(0x015F)}, // ş
};

// Constructor
Interpret_As_IBM_852::Interpret_As_IBM_852() {}

// Main execution function
void Interpret_As_IBM_852::execute(QPlainTextEdit* editor) {
if (!editor) {
qWarning() << "[ERROR] No editor instance provided.";
return;
}

CodeEditor* codeEditor = qobject_cast<CodeEditor*>(editor);
if (!codeEditor) {
qWarning() << "[ERROR] Invalid CodeEditor instance.";
return;
}

QString filePath = codeEditor->filePath();
if (filePath.isEmpty()) {
qWarning() << "[ERROR] No file path associated with the editor.";
return;
}

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

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

QString decodedText = decodeIBM852(rawData);
editor->setPlainText(decodedText);
qDebug() << "[DEBUG] IBM-852 Decoding applied for file:" << filePath;
}

// Decode raw data from IBM-852 encoding
QString Interpret_As_IBM_852::decodeIBM852(const QByteArray& rawData) {
QString result;

for (unsigned char byte : rawData) {
if (ebcdicTable.contains(byte)) {
result.append(ebcdicTable.at(byte));
} else {
result.append(QChar(0xFFFD)); // Fallback for unmapped characters
}
}
return result;
}
22 changes: 22 additions & 0 deletions src/encoding/interpret_as_ibm_852.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

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

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

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

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

QString decodeIBM852(const QByteArray& rawData);
static const std::unordered_map<unsigned char, QChar> ebcdicTable;
};
Loading

0 comments on commit 4b0bbe7

Please sign in to comment.