Skip to content

Commit

Permalink
Qt6: remove or replace QTextCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Sep 30, 2022
1 parent 10e9e3b commit f9f2013
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 38 deletions.
88 changes: 70 additions & 18 deletions src/Base/InputSource.cpp
Expand Up @@ -23,6 +23,15 @@

#include "PreCompiled.h"

#include <qglobal.h>
#if QT_VERSION < 0x060000
#include <QTextCodec>
#else
#include <QByteArray>
#include <QStringDecoder>
#include <QStringEncoder>
#endif

#include "InputSource.h"
#include "XMLTools.h"

Expand All @@ -36,11 +45,68 @@ using namespace std;
// ---------------------------------------------------------------------------
// StdInputStream: Constructors and Destructor
// ---------------------------------------------------------------------------

#if QT_VERSION < 0x060000
struct StdInputStream::TextCodec
{
QTextCodec::ConverterState state;
TextCodec() {
state.flags |= QTextCodec::IgnoreHeader;
state.flags |= QTextCodec::ConvertInvalidToNull;
}

void validateBytes(XMLByte* const toFill, std::streamsize len) {
QTextCodec *textCodec = QTextCodec::codecForName("UTF-8");
if (textCodec) {
const QString text = textCodec->toUnicode(reinterpret_cast<char *>(toFill), static_cast<int>(len), &state);
if (state.invalidChars > 0) {
// In case invalid characters were found decode back to 'utf-8' and replace
// them with '?'
// First, Qt replaces invalid characters with '\0' (see ConvertInvalidToNull)
// but Xerces doesn't like this because it handles this as termination. Thus,
// we have to go through the array and replace '\0' with '?'.
std::streamsize pos = 0;
QByteArray ba = textCodec->fromUnicode(text);
for (int i=0; i<ba.length(); i++, pos++) {
if (pos < len && ba[i] == '\0') {
toFill[i] = '?';
}
}
}
}
}
};
#else
struct StdInputStream::TextCodec
{
void validateBytes(XMLByte* const toFill, std::streamsize len) {
QByteArray encodedString(reinterpret_cast<char *>(toFill), static_cast<int>(len));
auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
QString text = toUtf16(encodedString);
if (toUtf16.hasError()) {
// In case invalid characters were found decode back to 'utf-8' and replace
// them with '?'
// First, Qt replaces invalid characters with '\0'
// but Xerces doesn't like this because it handles this as termination. Thus,
// we have to go through the array and replace '\0' with '?'.
std::streamsize pos = 0;
auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
QByteArray ba = fromUtf16(text);
for (int i=0; i<ba.length(); i++, pos++) {
if (pos < len && ba[i] == '\0') {
toFill[i] = '?';
}
}
}
}
};
#endif

StdInputStream::StdInputStream( std::istream& Stream, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* const manager )
: stream(Stream), fMemoryManager(manager)
: stream(Stream)
, fMemoryManager(manager)
, codec(new TextCodec)
{
state.flags |= QTextCodec::IgnoreHeader;
state.flags |= QTextCodec::ConvertInvalidToNull;
}


Expand All @@ -65,21 +131,7 @@ XMLSize_t StdInputStream::readBytes(XMLByte* const toFill, const XMLSize_t maxT
stream.read(reinterpret_cast<char *>(toFill), static_cast<std::streamsize>(maxToRead));
std::streamsize len = stream.gcount();

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
const QString text = codec->toUnicode(reinterpret_cast<char *>(toFill), static_cast<int>(len), &state);
if (state.invalidChars > 0) {
// In case invalid characters were found decode back to 'utf-8' and replace
// them with '?'
// First, Qt replaces invalid characters with '\0' (see ConvertInvalidToNull)
// but Xerces doesn't like this because it handles this as termination. Thus,
// we have to go through the array and replace '\0' with '?'.
std::streamsize pos = 0;
QByteArray ba = codec->fromUnicode(text);
for (int i=0; i<ba.length(); i++, pos++) {
if (pos < len && ba[i] == '\0')
toFill[i] = '?';
}
}
codec->validateBytes(toFill, len);

return static_cast<XMLSize_t>(len);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Base/InputSource.h
Expand Up @@ -24,10 +24,10 @@
#define BASE_IINPUTSOURCE_H

#include <iosfwd>
#include <memory>

#include <xercesc/util/BinInputStream.hpp>
#include <xercesc/sax/InputSource.hpp>
#include <QTextCodec>
#ifndef FC_GLOBAL_H
#include <FCGlobal.h>
#endif
Expand Down Expand Up @@ -70,7 +70,8 @@ private :
// -----------------------------------------------------------------------
std::istream &stream;
XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* const fMemoryManager;
QTextCodec::ConverterState state;
struct TextCodec;
std::unique_ptr<TextCodec> codec;
};


Expand Down
4 changes: 4 additions & 0 deletions src/Gui/EditorView.cpp
Expand Up @@ -37,7 +37,9 @@
# include <QPrintPreviewDialog>
# include <QSpacerItem>
# include <QStyle>
# if QT_VERSION < 0x060000
# include <QTextCodec>
# endif
# include <QTextCursor>
# include <QTextDocument>
# include <QTextStream>
Expand Down Expand Up @@ -523,7 +525,9 @@ bool EditorView::saveFile()
if (!file.open(QFile::WriteOnly))
return false;
QTextStream ts(&file);
#if QT_VERSION < 0x060000
ts.setCodec(QTextCodec::codecForName("UTF-8"));
#endif
ts << d->textEdit->document()->toPlainText();
file.close();
d->textEdit->document()->setModified(false);
Expand Down
1 change: 0 additions & 1 deletion src/Gui/QtAll.h
Expand Up @@ -45,7 +45,6 @@
#include <QSet>
#include <QSignalMapper>
#include <QTemporaryFile>
#include <qtextcodec.h>
#include <qtextstream.h>
#include <qthread.h>
#include <qthreadpool.h>
Expand Down
15 changes: 0 additions & 15 deletions src/Main/MainGui.cpp
Expand Up @@ -42,7 +42,6 @@
#include <QApplication>
#include <QLocale>
#include <QMessageBox>
#include <QTextCodec>

// FreeCAD header
#include <App/Application.h>
Expand Down Expand Up @@ -96,20 +95,6 @@ class Redirection
FILE* file;
};

#if defined (FC_OS_LINUX) || defined(FC_OS_BSD)
QString myDecoderFunc(const QByteArray &localFileName)
{
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
return codec->toUnicode(localFileName);
}

QByteArray myEncoderFunc(const QString &fileName)
{
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
return codec->fromUnicode(fileName);
}
#endif

int main( int argc, char ** argv )
{
#if defined (FC_OS_LINUX) || defined(FC_OS_BSD)
Expand Down
1 change: 0 additions & 1 deletion src/Mod/TechDraw/Gui/QGSPage.cpp
Expand Up @@ -40,7 +40,6 @@
#include <QTextStream>
#include <QFile>
#include <QLabel>
#include <QTextCodec>
#include <cmath>
#endif

Expand Down
1 change: 0 additions & 1 deletion src/Mod/TechDraw/Gui/QGVPage.cpp
Expand Up @@ -43,7 +43,6 @@
#include <QTextStream>
#include <QFile>
#include <QLabel>
#include <QTextCodec>
#endif

#include <App/Application.h>
Expand Down

0 comments on commit f9f2013

Please sign in to comment.