Skip to content

Commit

Permalink
v0.0.72
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand committed Dec 15, 2024
1 parent 17bfa1c commit 5e830c5
Show file tree
Hide file tree
Showing 18 changed files with 522 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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

## 0.0.72

- Implemented:
- Encoding Menu -> Interpret as UTF-8
- Encoding Menu -> Interpret as UTF-8 without BOM
- Encoding Menu -> Interpret as UTF-16BE (UCS2 Big Endian)
- Encoding Menu -> Interpret as UTF-16LE (UCS2 Little Endian)
- Encoding Menu -> Interpret as ...

## 0.0.71

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

set(CMAKE_AUTOUIC_SEARCH_PATHS src)
Expand Down Expand Up @@ -117,6 +118,18 @@ 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
${PROJECT_UI}
)

Expand Down
60 changes: 60 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf16be.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "interpretcurrentdocumentasutf16be.h"
#include "../codeeditor.h"
#include <QDebug>

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

void InterpretCurrentDocumentAsUTF16BE::execute(CodeEditor* editor)
{
if (!editor) {
qDebug() << "Error: No CodeEditor instance provided.";
return;
}

// Get the current text from the editor
QString text = editor->toPlainText();

// Log the original text
qDebug() << "Original text:" << text;

// Convert the text to UTF-16BE
QByteArray utf16beData;
const char16_t* utf16Data = reinterpret_cast<const char16_t*>(text.utf16());
qsizetype utf16Length = text.size();

for (qsizetype i = 0; i < utf16Length; ++i) {
char16_t codeUnit = utf16Data[i];

// Convert to big-endian format
utf16beData.append(static_cast<char>((codeUnit >> 8) & 0xFF)); // High byte
utf16beData.append(static_cast<char>(codeUnit & 0xFF)); // Low byte
}

// Log the UTF-16BE data
qDebug() << "UTF-16BE data:" << utf16beData.toHex();

// Correctly interpret UTF-16BE as QString
QByteArray tempData;
for (qsizetype i = 0; i < utf16beData.size(); i += 2) {
// Convert back to native-endian format for QString::fromUtf16
tempData.append(utf16beData[i + 1]); // Low byte
tempData.append(utf16beData[i]); // High byte
}

// Interpret the text using native-endian conversion
QString interpretedText = QString::fromUtf16(
reinterpret_cast<const char16_t*>(tempData.constData()),
tempData.size() / 2);

// Log the interpreted text
qDebug() << "Interpreted text:" << interpretedText;

// Update the editor content
editor->setPlainText(interpretedText);

qDebug() << "Document successfully reinterpreted as UTF-16BE.";
}
25 changes: 25 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf16be.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <QString>

class CodeEditor; // Forward declaration

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

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

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

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

36 changes: 36 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf16le.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "interpretcurrentdocumentasutf16le.h"
#include "../codeeditor.h"
#include <QDebug>

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

InterpretCurrentDocumentAsUTF16LE::InterpretCurrentDocumentAsUTF16LE() = default;

InterpretCurrentDocumentAsUTF16LE::~InterpretCurrentDocumentAsUTF16LE() = default;

void InterpretCurrentDocumentAsUTF16LE::execute(CodeEditor* editor)
{
if (!editor) {
qDebug() << "Error: No CodeEditor instance provided.";
return;
}

// Get the raw content of the document
QByteArray rawData = editor->toPlainText().toUtf8(); // Ensure raw data
qDebug() << "Original Raw Data (Hex):" << rawData.toHex();

// Reinterpret as UTF-16LE
QString interpretedText = QString::fromUtf16(
reinterpret_cast<const char16_t*>(rawData.constData()), rawData.size() / 2);

qDebug() << "Interpreted UTF-16LE Text:" << interpretedText;

// Update the editor content
editor->setPlainText(interpretedText);

qDebug() << "Document reinterpreted as UTF-16LE.";
}
19 changes: 19 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf16le.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <QString>

class CodeEditor;

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

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

30 changes: 30 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "interpretcurrentdocumentasutf8.h"
#include "../codeeditor.h"
#include <QDebug>

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

// Execute the reinterpretation of the current document as UTF-8
void InterpretCurrentDocumentAsUTF8::execute(CodeEditor* editor)
{
if (!editor) {
qDebug() << "Error: No CodeEditor instance provided.";
return;
}

// Retrieve the raw text from the CodeEditor
QByteArray rawData = editor->toPlainText().toUtf8();

// Reinterpret the content as UTF-8
QString utf8Text = QString::fromUtf8(rawData);

// Update the editor content with the reinterpreted UTF-8 text
editor->setPlainText(utf8Text);

qDebug() << "Document successfully reinterpreted as UTF-8.";
}
23 changes: 23 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <QString>

class CodeEditor; // Forward declaration

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

// Method to reinterpret the content of a CodeEditor as UTF-8
void execute(CodeEditor* editor);

private:
InterpretCurrentDocumentAsUTF8() = default; // Private constructor for singleton
~InterpretCurrentDocumentAsUTF8() = default; // Destructor
InterpretCurrentDocumentAsUTF8(const InterpretCurrentDocumentAsUTF8&) = delete; // No copy
InterpretCurrentDocumentAsUTF8& operator=(const InterpretCurrentDocumentAsUTF8&) = delete; // No assignment
};


33 changes: 33 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf8withoutbom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "interpretcurrentdocumentasutf8withoutbom.h"
#include "../codeeditor.h"
#include <QDebug>

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

void InterpretCurrentDocumentAsUTF8WithoutBOM::execute(CodeEditor* editor)
{
if (!editor) {
qDebug() << "Error: No CodeEditor instance provided.";
return;
}

// Get the current text as raw bytes
QByteArray rawData = editor->toPlainText().toUtf8();

// Remove BOM if present
if (rawData.startsWith("\xEF\xBB\xBF")) {
rawData.remove(0, 3); // Remove the first three bytes of BOM
}

// Convert back to QString
QString utf8Text = QString::fromUtf8(rawData);

// Update the editor content with the UTF-8 without BOM
editor->setPlainText(utf8Text);

qDebug() << "Document successfully reinterpreted as UTF-8 without BOM.";
}
24 changes: 24 additions & 0 deletions src/encoding/interpretcurrentdocumentasutf8withoutbom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <QString>

class CodeEditor; // Forward declaration

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

// Method to interpret the current document as UTF-8 without BOM
void execute(CodeEditor* editor);

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

// Delete copy constructor and assignment operator
InterpretCurrentDocumentAsUTF8WithoutBOM(const InterpretCurrentDocumentAsUTF8WithoutBOM&) = delete;
InterpretCurrentDocumentAsUTF8WithoutBOM& operator=(const InterpretCurrentDocumentAsUTF8WithoutBOM&) = delete;
};
36 changes: 36 additions & 0 deletions src/encoding/interpreteasdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "interpreteasdialog.h"
#include <QComboBox>
#include <QVBoxLayout>
#include <QDialogButtonBox>

InterpreteAsDialog::InterpreteAsDialog(QWidget* parent)
: QDialog(parent)
{
setWindowTitle("Interpret As");

// Create and populate the combobox
comboBox = new QComboBox(this);
comboBox->addItems({
"UTF-8",
"UTF-7"
});

// Create OK and Cancel buttons using QDialogButtonBox
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);

// Set layout for the dialog
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(comboBox);
layout->addWidget(buttonBox);

// Connect button signals to accept/reject the dialog
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

QString InterpreteAsDialog::getSelectedItem() const
{
return comboBox->currentText();
}


16 changes: 16 additions & 0 deletions src/encoding/interpreteasdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <QDialog>
#include <QString>

class QComboBox;

class InterpreteAsDialog : public QDialog
{
public:
explicit InterpreteAsDialog(QWidget* parent = nullptr);
QString getSelectedItem() const;

private:
QComboBox* comboBox; // Pointer to the combobox
};
Loading

0 comments on commit 5e830c5

Please sign in to comment.