Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
suddenly: all my files are usable again
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Aug 15, 2023
1 parent 5c40e78 commit aededf5
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CXXFLAGS = -std=c++14 -march=core2 -mtune=core2 \
SAN = -fsanitize=undefined,leak,address,integer
CXXFLAGS += $(SAN) -std=c++14 \
-DNIFWIND_VERSION="\"1.0\"" \
-I. -Isrc -Isrc/services -Isrc/models -Isrc/views $(FFD_CFLAGS) \
-I. -Isrc -Isrc/services -Isrc/models -Isrc/views -Isrc/qtisms $(FFD_CFLAGS) \
-I${QTDIR}/include -I${QTDIR}/include/QtCore -I${QTDIR}/include/QtWidgets \
-I${QTDIR}/include/QtOpenGL -I${QTDIR}/include/QtGui \
-DQT_OPENGL_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB \
Expand Down
6 changes: 6 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct QUnreliable {};
#include "nifwind.h"

#include "n_main_window.h"
#include "n_text_codec.h"

class foo final : public QOpenGLWidget, protected QOpenGLFunctions
{
Expand Down Expand Up @@ -80,6 +81,11 @@ class foo final : public QOpenGLWidget, protected QOpenGLFunctions

int main(int argc, char **argv)
{
// This is a temporary solution. The moment they break this, the "Qt" File
// IO related code shall become unreliable again.
(void)new NIFWIND_NS::NTextCodec; // a.k.a. register()
QTextCodec::setCodecForLocale (QTextCodec::codecForName ("DoNotTouchTC"));

QApplication app (argc, argv);

// test a file path containing non-unicode path names:
Expand Down
115 changes: 115 additions & 0 deletions src/qtisms/n_text_codec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**** BEGIN LICENSE BLOCK ****
BSD 3-Clause License
Copyright (c) 2023, the wind.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/

#ifndef _N_OPTIONS_H_
#define _N_OPTIONS_H_

#include "nifwind.h"
#include <QTextCodec>

NIFWIND_NAMESPACE

// Solution 1: To the Bridge.
//
// "Qt", you're not the people that will dictate the encoding of a File
// Path/Name; - That gets decided by the File System authors and its users.
// When I pass you a "const char *" - you touch it not; it is as simple as that.
// Wherever I'll interpret it as some encoding is up to me! - do you get that? -
// or shall I render it to you?!
//LATER - a 3D rendering of "do not decide my encoding instead of me"
//
// This code exists to solve a problem that shouldn't exist.
class NTextCodec final : public QTextCodec
{
public: inline QByteArray name() const override
{
static QByteArray n {"DoNotTouchTC"};
return n;
}
public: inline int mibEnum() const override { return -10000; }
// Courtesy of: qtbase/src/corelib/codecs/qisciicodec.cpp
// Packing these tightly results in an application that is unable to start:
// "__cxa_guard_acquire detected recursive initialization"
// e.g. the loops.
public: inline QByteArray convertFromUnicode(const QChar * uc, int len,
QTextCodec::ConverterState * cs) const override
{
static auto UTF8 = QTextCodec::codecForName ("UTF-8");
// courtesy of QTextCodec::canEncode()
/*QTextCodec::ConverterState state {};
state.flags = QTextCodec::ConversionFlag::ConvertInvalidToNull;
UTF8->fromUnicode (uc, len, &state);
// printf ("convertFromUnicode.invalidChars: %d" EOL, state.invalidChars);
if (0 == state.invalidChars)
return UTF8->fromUnicode (uc, len, cs);*/

QByteArray result {len, Qt::Uninitialized};
uchar * ch = reinterpret_cast<uchar *>(result.data ());
for (int i = 0; i < len; i++)
*ch++ = static_cast<uchar>(uc[i].unicode ());

// courtesy of QTextCodec::canEncode()
QTextCodec::ConverterState state {};
state.flags = QTextCodec::ConversionFlag::ConvertInvalidToNull;
UTF8->toUnicode (reinterpret_cast<char *>(result.data ()), len, &state);
// printf ("convertFromUnicode.invalidChars: %d" EOL, state.invalidChars);
if (0 == state.invalidChars)
return UTF8->fromUnicode (uc, len, cs);

return result;
}
QString convertToUnicode(const char * ch, int len,
QTextCodec::ConverterState * cs) const override
{
printf ("convertToUnicode(%s)" EOL, ch);
static auto UTF8 = QTextCodec::codecForName ("UTF-8");
// courtesy of QTextCodec::canEncode()
QTextCodec::ConverterState state {};
state.flags = QTextCodec::ConversionFlag::ConvertInvalidToNull;
UTF8->toUnicode (ch, len, &state);
printf ("convertToUnicode.invalidChars: %d" EOL, state.invalidChars);
if (0 == state.invalidChars)
return UTF8->toUnicode (ch, len, cs);

QString result {len, Qt::Uninitialized};
QChar * uc = result.data ();
for (int i = 0; i < len; i++)
*uc++ = static_cast<uchar>(ch[i]);
return result;
}
}; // NTextCodec

NAMESPACE_NIFWIND

#endif

0 comments on commit aededf5

Please sign in to comment.