Skip to content

Commit

Permalink
V0.0.74
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand committed Dec 19, 2024
1 parent 7a41367 commit 27c9ee4
Show file tree
Hide file tree
Showing 36 changed files with 735 additions and 507 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.74

- Implemented:
- Encoding Menu -> Interpret as ... UTF-7, 32LE, 32BE, UTF32, UTF-16, US-ASCII, TSCII

## 0.0.73

- Implemented:
Expand Down
44 changes: 27 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ set(PROJECT_UI
src/systemreplace/systemreplacedialog.ui
src/systemsearchresultdialog.ui
src/aboutdialog.ui
src/encoding/interpreteasdialog.ui
src/encoding/interpret_as_dialog.ui
)

set(CMAKE_AUTOUIC_SEARCH_PATHS src)
Expand Down Expand Up @@ -118,24 +118,34 @@ set(PROJECT_SOURCES
src/view/wordwrap.h
src/view/toggletoformertab.cpp
src/view/toggletoformertab.h
src/encoding/interpretcurrentdocumentasutf8.cpp
src/encoding/interpretcurrentdocumentasutf8.h
src/encoding/interpretcurrentdocumentasutf8withoutbom.cpp
src/encoding/interpretcurrentdocumentasutf8withoutbom.h
src/encoding/interpretcurrentdocumentasutf16be.cpp
src/encoding/interpretcurrentdocumentasutf16be.h
src/encoding/interpretcurrentdocumentasutf16le.cpp
src/encoding/interpretcurrentdocumentasutf16le.h
src/encoding/interpreteasdialog.cpp
src/encoding/interpreteasdialog.h
src/encoding/interpreteasutf8.cpp
src/encoding/interpreteasutf8.h
src/encoding/interpreteasutf7.cpp
src/encoding/interpreteasutf7.h
src/encoding/interpret_as_utf_8.cpp
src/encoding/interpret_as_utf_8.h
src/encoding/interpret_as_utf_8_without_bom.cpp
src/encoding/interpret_as_utf_8_without_bom.h
src/encoding/interpret_as_utf_16_be.cpp
src/encoding/interpret_as_utf_16_be.h
src/encoding/interpret_as_utf_16_le.cpp
src/encoding/interpret_as_utf_16_le.h
src/encoding/interpret_as_dialog.cpp
src/encoding/interpret_as_dialog.h
src/encoding/interpret_as_utf_8.cpp
src/encoding/interpret_as_utf_8.h
src/encoding/interpret_as_utf_7.cpp
src/encoding/interpret_as_utf_7.h
src/encoding/utf7handler.cpp
src/encoding/utf7handler.h
src/encoding/interpretcurrentdocumentasutf32le.cpp
src/encoding/interpretcurrentdocumentasutf32le.h
src/encoding/interpret_as_utf_32_le.cpp
src/encoding/interpret_as_utf_32_le.h
src/encoding/interpret_as_utf_32_be.cpp
src/encoding/interpret_as_utf_32_be.h
src/encoding/interpret_as_utf_32.cpp
src/encoding/interpret_as_utf_32.h
src/encoding/interpret_as_utf_16.cpp
src/encoding/interpret_as_utf_16.h
src/encoding/interpret_as_us_ascii.cpp
src/encoding/interpret_as_us_ascii.h
src/encoding/interprete_as_tscii.cpp
src/encoding/interprete_as_tscii.h
${PROJECT_UI}
)

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "interpreteasdialog.h"
#include "interpret_as_dialog.h"
#include <QComboBox>
#include <QVBoxLayout>
#include <QDialogButtonBox>
Expand All @@ -13,7 +13,14 @@ InterpreteAsDialog::InterpreteAsDialog(QWidget* parent)
comboBox->addItems({
"UTF-8",
"UTF-7",
"UTF-32LE"
"UTF-32LE",
"UTF-32BE",
"UTF-32",
"UTF-16LE",
"UTF16BE",
"UTF-16",
"US-ASCII",
"TSCII"
});

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

Interpret_As_US_ASCII& Interpret_As_US_ASCII::instance() {
static Interpret_As_US_ASCII instance;
return instance;
}

Interpret_As_US_ASCII::Interpret_As_US_ASCII() = default;
Interpret_As_US_ASCII::~Interpret_As_US_ASCII() = default;

void Interpret_As_US_ASCII::execute(QPlainTextEdit* editor) {
if (!editor) {
qDebug() << "[ERROR] No QPlainTextEdit instance provided.";
return;
}

// Assume the document is already loaded in the editor
QString filePath = editor->property("filePath").toString(); // Adjust if you store filePath differently
if (filePath.isEmpty()) {
qDebug() << "[ERROR] No file path associated with the editor.";
return;
}

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

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

qDebug() << "[DEBUG] Raw file content (Hex):" << fileContent.toHex();

QString decodedText = decodeUsAscii(fileContent);
if (decodedText.isEmpty()) {
qDebug() << "[ERROR] Decoding US-ASCII content failed.";
return;
}

editor->setPlainText(decodedText);
qDebug() << "[DEBUG] Updated editor content with decoded US-ASCII text.";
}

QString Interpret_As_US_ASCII::decodeUsAscii(const QByteArray& input) {
QString result;
for (char c : input) { // Process as `char`
if (static_cast<unsigned char>(c) <= 127) { // Explicit cast to avoid warnings
result += QChar(c); // Convert to QChar
} else {
qWarning() << "[WARNING] Non-ASCII character encountered:" << static_cast<int>(c);
result += QChar('?'); // Replace non-ASCII characters with a placeholder
}
}
return result;
}
27 changes: 27 additions & 0 deletions src/encoding/interpret_as_us_ascii.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <QString>
#include <QPlainTextEdit>
#include <QFile>
#include <QDebug>

class Interpret_As_US_ASCII {
public:
// Singleton access
static Interpret_As_US_ASCII& instance();

// Interpret file content as US-ASCII and update the editor
void execute(QPlainTextEdit* editor);

private:
// Private constructor and destructor
Interpret_As_US_ASCII();
~Interpret_As_US_ASCII();

// Disable copy and assignment
Interpret_As_US_ASCII(const Interpret_As_US_ASCII&) = delete;
Interpret_As_US_ASCII& operator=(const Interpret_As_US_ASCII&) = delete;

// Helper function to decode file content
QString decodeUsAscii(const QByteArray& fileContent);
};
69 changes: 69 additions & 0 deletions src/encoding/interpret_as_utf_16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "interpret_as_utf_16.h"
#include <QString>
#include <QDebug>
#include <QFile>
#include <QPlainTextEdit>

Interpret_As_UTF_16& Interpret_As_UTF_16::instance() {
static Interpret_As_UTF_16 instance;
return instance;
}

Interpret_As_UTF_16::Interpret_As_UTF_16() = default;
Interpret_As_UTF_16::~Interpret_As_UTF_16() = default;

void Interpret_As_UTF_16::execute(QPlainTextEdit* editor) {
if (!editor) {
qDebug() << "[ERROR] No QPlainTextEdit instance provided.";
return;
}

// Assume the document is already loaded in the editor
QString filePath = editor->property("filePath").toString(); // Adjust if you store filePath differently
if (filePath.isEmpty()) {
qDebug() << "[ERROR] No file path associated with the editor.";
return;
}

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

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

qDebug() << "[DEBUG] Raw file content (Hex):" << fileContent.toHex();

QString decodedText = decodeUtf16(fileContent);
if (decodedText.isEmpty()) {
qDebug() << "[ERROR] Decoding UTF-16 content failed.";
return;
}

editor->setPlainText(decodedText);
qDebug() << "[DEBUG] Updated editor content with decoded UTF-16 text.";
}

QString Interpret_As_UTF_16::decodeUtf16(const QByteArray& fileContent) {
// Check for BOM (Byte Order Mark) to determine endianness
if (fileContent.startsWith("\xFF\xFE")) {
qDebug() << "[DEBUG] Detected BOM: UTF-16LE";
return QString::fromUtf16(reinterpret_cast<const char16_t*>(fileContent.constData() + 2),
(fileContent.size() - 2) / 2);
} else if (fileContent.startsWith("\xFE\xFF")) {
qDebug() << "[DEBUG] Detected BOM: UTF-16BE";
QByteArray swappedContent = fileContent.mid(2);
for (int i = 0; i < swappedContent.size(); i += 2) {
std::swap(swappedContent[i], swappedContent[i + 1]);
}
return QString::fromUtf16(reinterpret_cast<const char16_t*>(swappedContent.constData()),
swappedContent.size() / 2);
}

// If no BOM is present, default to UTF-16LE
qDebug() << "[DEBUG] No BOM detected. Assuming UTF-16LE.";
return QString::fromUtf16(reinterpret_cast<const char16_t*>(fileContent.constData()),
fileContent.size() / 2);
}
27 changes: 27 additions & 0 deletions src/encoding/interpret_as_utf_16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <QString>
#include <QPlainTextEdit>
#include <QFile>
#include <QDebug>

class Interpret_As_UTF_16 {
public:
// Singleton access
static Interpret_As_UTF_16& instance();

// Interpret file content as UTF-16 and update the editor
void execute(QPlainTextEdit* editor);

private:
// Private constructor and destructor
Interpret_As_UTF_16();
~Interpret_As_UTF_16();

// Disable copy and assignment
Interpret_As_UTF_16(const Interpret_As_UTF_16&) = delete;
Interpret_As_UTF_16& operator=(const Interpret_As_UTF_16&) = delete;

// Helper function to decode file content
QString decodeUtf16(const QByteArray& fileContent);
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "interpretcurrentdocumentasutf16be.h"
#include "interpret_as_utf_16_be.h"
#include "../codeeditor.h"
#include <QDebug>

InterpretCurrentDocumentAsUTF16BE& InterpretCurrentDocumentAsUTF16BE::instance()
InterpretAsUTF16BE& InterpretAsUTF16BE::instance()
{
static InterpretCurrentDocumentAsUTF16BE instance;
static InterpretAsUTF16BE instance;
return instance;
}

void InterpretCurrentDocumentAsUTF16BE::execute(CodeEditor* editor)
void InterpretAsUTF16BE::execute(CodeEditor* editor)
{
if (!editor) {
qDebug() << "Error: No CodeEditor instance provided.";
Expand Down
25 changes: 25 additions & 0 deletions src/encoding/interpret_as_utf_16_be.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <QString>

class CodeEditor; // Forward declaration

class InterpretAsUTF16BE
{
public:
// Singleton instance access
static InterpretAsUTF16BE& instance();

// Method to interpret the current document as UTF-16BE
void execute(CodeEditor* editor);

private:
// Private constructor for singleton
InterpretAsUTF16BE() = default;
~InterpretAsUTF16BE() = default;

// Delete copy constructor and assignment operator
InterpretAsUTF16BE(const InterpretAsUTF16BE&) = delete;
InterpretAsUTF16BE& operator=(const InterpretAsUTF16BE&) = delete;
};

Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include "interpretcurrentdocumentasutf16le.h"
#include "interpret_as_utf_16_le.h"
#include "../codeeditor.h"
#include <QDebug>

InterpretCurrentDocumentAsUTF16LE& InterpretCurrentDocumentAsUTF16LE::instance()
InterpretAsUTF16LE& InterpretAsUTF16LE::instance()
{
static InterpretCurrentDocumentAsUTF16LE instance;
static InterpretAsUTF16LE instance;
return instance;
}

InterpretCurrentDocumentAsUTF16LE::InterpretCurrentDocumentAsUTF16LE() = default;
InterpretAsUTF16LE::InterpretAsUTF16LE() = default;

InterpretCurrentDocumentAsUTF16LE::~InterpretCurrentDocumentAsUTF16LE() = default;
InterpretAsUTF16LE::~InterpretAsUTF16LE() = default;

void InterpretCurrentDocumentAsUTF16LE::execute(CodeEditor* editor)
void InterpretAsUTF16LE::execute(CodeEditor* editor)
{
if (!editor) {
qDebug() << "Error: No CodeEditor instance provided.";
Expand Down
19 changes: 19 additions & 0 deletions src/encoding/interpret_as_utf_16_le.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <QString>

class CodeEditor;

class InterpretAsUTF16LE
{
public:
static InterpretAsUTF16LE& instance();
void execute(CodeEditor* editor);

private:
InterpretAsUTF16LE(); // Private constructor
~InterpretAsUTF16LE();
InterpretAsUTF16LE(const InterpretAsUTF16LE&) = delete;
InterpretAsUTF16LE& operator=(const InterpretAsUTF16LE&) = delete;
};

Loading

0 comments on commit 27c9ee4

Please sign in to comment.