Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to disable qtextcodec usage #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common.pri
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
exists(config.pri):infile(config.pri, SOLUTIONS_LIBRARY, yes): CONFIG += quazip-uselib
exists(config.pri):infile(config.pri, USE_TEXTCODEC, yes): {
DEFINES += USE_TEXTCODEC
CONFIG += use-textcodec
}
TEMPLATE += fakelib
QUAZIP_LIBNAME = $$qtLibraryTarget(quazip-head)
TEMPLATE -= fakelib
Expand Down
53 changes: 48 additions & 5 deletions quazip/quazip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class QuaZipPrivate {
/// The pointer to the corresponding QuaZip instance.
QuaZip *q;
/// The codec for file names (used when UTF-8 is not enabled).
QTextCodec *fileNameCodec;
QTextCodec *fileNameCodec = NULL;
/// The codec for comments (used when UTF-8 is not enabled).
QTextCodec *commentCodec;
QTextCodec *commentCodec = NULL;
/// The archive file name.
QString zipName;
/// The device to access the archive.
Expand Down Expand Up @@ -79,16 +79,22 @@ class QuaZipPrivate {
inline QTextCodec *getDefaultFileNameCodec()
{
if (defaultFileNameCodec == NULL) {
#if defined( USE_TEXTCODEC )
return QTextCodec::codecForLocale();
#else
return NULL;
#endif
} else {
return defaultFileNameCodec;
}
}
/// The constructor for the corresponding QuaZip constructor.
inline QuaZipPrivate(QuaZip *q):
q(q),
#if defined( USE_TEXTCODEC )
fileNameCodec(getDefaultFileNameCodec()),
commentCodec(QTextCodec::codecForLocale()),
#endif
ioDevice(NULL),
mode(QuaZip::mdNotOpen),
hasCurrentFile_f(false),
Expand All @@ -107,8 +113,10 @@ class QuaZipPrivate {
/// The constructor for the corresponding QuaZip constructor.
inline QuaZipPrivate(QuaZip *q, const QString &zipName):
q(q),
#if defined( USE_TEXTCODEC )
fileNameCodec(getDefaultFileNameCodec()),
commentCodec(QTextCodec::codecForLocale()),
#endif
zipName(zipName),
ioDevice(NULL),
mode(QuaZip::mdNotOpen),
Expand All @@ -128,8 +136,10 @@ class QuaZipPrivate {
/// The constructor for the corresponding QuaZip constructor.
inline QuaZipPrivate(QuaZip *q, QIODevice *ioDevice):
q(q),
#if defined( USE_TEXTCODEC )
fileNameCodec(getDefaultFileNameCodec()),
commentCodec(QTextCodec::codecForLocale()),
#endif
ioDevice(ioDevice),
mode(QuaZip::mdNotOpen),
hasCurrentFile_f(false),
Expand Down Expand Up @@ -359,7 +369,11 @@ void QuaZip::close()
case mdAdd:
p->zipError=zipClose(p->zipFile_f, p->comment.isNull() ? NULL : isUtf8Enabled()
? p->comment.toUtf8().constData()
#if defined( USE_TEXTCODEC )
: p->commentCodec->fromUnicode(p->comment).constData());
#else
: p->comment.toUtf8().constData());
#endif
break;
default:
qWarning("QuaZip::close(): unknown mode: %d", (int)p->mode);
Expand Down Expand Up @@ -425,9 +439,13 @@ QString QuaZip::getComment()const
if((fakeThis->p->zipError=unzGetGlobalComment(p->unzFile_f, comment.data(), comment.size())) < 0)
return QString();
fakeThis->p->zipError = UNZ_OK;
#if defined( USE_TEXTCODEC )
unsigned flags = 0;
return (unzGetFileFlags(p->unzFile_f, &flags) == UNZ_OK) && (flags & UNZ_ENCODING_UTF8)
? QString::fromUtf8(comment) : p->commentCodec->toUnicode(comment);
#else
return QString::fromUtf8(comment);
#endif
}

bool QuaZip::setCurrentFile(const QString& fileName, CaseSensitivity cs)
Expand Down Expand Up @@ -562,8 +580,13 @@ bool QuaZip::getCurrentFileInfo(QuaZipFileInfo64 *info)const
info->diskNumberStart=info_z.disk_num_start;
info->internalAttr=info_z.internal_fa;
info->externalAttr=info_z.external_fa;
#if defined( USE_TEXTCODEC )
info->name=(info->flags & UNZ_ENCODING_UTF8) ? QString::fromUtf8(fileName) : p->fileNameCodec->toUnicode(fileName);
info->comment=(info->flags & UNZ_ENCODING_UTF8) ? QString::fromUtf8(comment) : p->commentCodec->toUnicode(comment);
#else
info->name=QString::fromUtf8(fileName);
info->comment=QString::fromUtf8(comment);
#endif
info->extra=extra;
info->dateTime=QDateTime(
QDate(info_z.tmu_date.tm_year, info_z.tmu_date.tm_mon+1, info_z.tmu_date.tm_mday),
Expand All @@ -588,8 +611,16 @@ QString QuaZip::getCurrentFileName()const
NULL, 0, NULL, 0))!=UNZ_OK)
return QString();
fileName.resize(file_info.size_filename);
QString result = (file_info.flag & UNZ_ENCODING_UTF8)
? QString::fromUtf8(fileName) : p->fileNameCodec->toUnicode(fileName);
QString result;
if (file_info.flag & UNZ_ENCODING_UTF8)
result = QString::fromUtf8(fileName);
else
#if defined( USE_TEXTCODEC )
result = p->fileNameCodec->toUnicode(fileName);
#else
result = QString::fromUtf8(fileName);
#endif

if (result.isEmpty())
return result;
// Add to directory map
Expand All @@ -604,7 +635,11 @@ void QuaZip::setFileNameCodec(QTextCodec *fileNameCodec)

void QuaZip::setFileNameCodec(const char *fileNameCodecName)
{
p->fileNameCodec=QTextCodec::codecForName(fileNameCodecName);
#if defined( USE_TEXTCODEC )
p->fileNameCodec=QTextCodec::codecForName(fileNameCodecName);
#else
Q_UNUSED(fileNameCodecName);
#endif
}

void QuaZip::setOsCode(uint osCode)
Expand All @@ -629,7 +664,11 @@ void QuaZip::setCommentCodec(QTextCodec *commentCodec)

void QuaZip::setCommentCodec(const char *commentCodecName)
{
#if defined( USE_TEXTCODEC )
p->commentCodec=QTextCodec::codecForName(commentCodecName);
#else
Q_UNUSED(commentCodecName);
#endif
}

QTextCodec *QuaZip::getCommentCodec()const
Expand Down Expand Up @@ -802,7 +841,11 @@ void QuaZip::setDefaultFileNameCodec(QTextCodec *codec)

void QuaZip::setDefaultFileNameCodec(const char *codecName)
{
#if defined( USE_TEXTCODEC )
setDefaultFileNameCodec(QTextCodec::codecForName(codecName));
#else
Q_UNUSED(codecName);
#endif
}

void QuaZip::setDefaultOsCode(uint osCode)
Expand Down
4 changes: 4 additions & 0 deletions quazip/quazip.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ quazip/(un)zip.h files for details, basically it's zlib license.

#include <QString>
#include <QStringList>
#if defined( USE_TEXTCODEC )
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
# include <QtCore5Compat/QTextCodec>
#else
# include <QtCore/QTextCodec>
#endif
#else
class QTextCodec;
#endif

#include "zip.h"
#include "unzip.h"
Expand Down
4 changes: 3 additions & 1 deletion quazip/quazip.pri
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ win32:INCLUDEPATH += $(MEMSOURCE_LIB)/qt/src/3rdparty/zlib
}

greaterThan( QT_MAJOR_VERSION, 5 ) {
QT += core5compat
use-textcodec:{
QT += core5compat
}
}

unix:LIBS += -lz
Expand Down
8 changes: 8 additions & 0 deletions quazip/quazipfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,23 @@ bool QuaZipFile::open(OpenMode mode, const QuaZipNewInfo& info,
else
zipClearFlags(p->zip->getZipFile(), ZIP_WRITE_DATA_DESCRIPTOR);
p->setZipError(zipOpenNewFileInZip4_64(p->zip->getZipFile(),
#if defined( USE_TEXTCODEC )
p->zip->isUtf8Enabled()
? info.name.toUtf8().constData()
: p->zip->getFileNameCodec()->fromUnicode(info.name).constData(),
#else
info.name.toUtf8().constData(),
#endif
&info_z,
info.extraLocal.constData(), info.extraLocal.length(),
info.extraGlobal.constData(), info.extraGlobal.length(),
#if defined( USE_TEXTCODEC )
p->zip->isUtf8Enabled()
? info.comment.toUtf8().constData()
: p->zip->getCommentCodec()->fromUnicode(info.comment).constData(),
#else
info.comment.toUtf8().constData(),
#endif
method, level, (int)raw,
windowBits, memLevel, strategy,
password, (uLong)crc,
Expand Down