-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a41367
commit 27c9ee4
Showing
36 changed files
with
735 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
8 changes: 4 additions & 4 deletions
8
...ing/interpretcurrentdocumentasutf16be.cpp → src/encoding/interpret_as_utf_16_be.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
12 changes: 6 additions & 6 deletions
12
...ing/interpretcurrentdocumentasutf16le.cpp → src/encoding/interpret_as_utf_16_le.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
Oops, something went wrong.