forked from qt-creator/qt-creator
CppTools: Use editor manager's codec as fallback
...for reading not already opened files.
This partly reverts commit f7c68f6. In case TextFileFormat::detect()
fails, the user configurable editor manager's codec is used instead of
QTextCodec::codecForLocale().
Adds also a qWarning() to easier detect encoding errors.
Task-number: QTCREATORBUG-10378
Change-Id: I0fa4e6b898ed090d85414ce2a001f11b115a42d3
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
@@ -304,6 +304,40 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const QString &fileName,
|
|||||||
return TextFileFormat::ReadSuccess;
|
return TextFileFormat::ReadSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const QString &fileName,
|
||||||
|
const QTextCodec *defaultCodec,
|
||||||
|
QByteArray *plainText, QString *errorString)
|
||||||
|
{
|
||||||
|
QByteArray data;
|
||||||
|
try {
|
||||||
|
Utils::FileReader reader;
|
||||||
|
if (!reader.fetch(fileName, errorString))
|
||||||
|
return Utils::TextFileFormat::ReadIOError;
|
||||||
|
data = reader.data();
|
||||||
|
} catch (const std::bad_alloc &) {
|
||||||
|
*errorString = QCoreApplication::translate("Utils::TextFileFormat", "Out of memory.");
|
||||||
|
return Utils::TextFileFormat::ReadMemoryAllocationError;
|
||||||
|
}
|
||||||
|
|
||||||
|
Utils::TextFileFormat format = Utils::TextFileFormat::detect(data);
|
||||||
|
if (!format.codec)
|
||||||
|
format.codec = defaultCodec ? defaultCodec : QTextCodec::codecForLocale();
|
||||||
|
if (format.codec->name() == "UTF-8") {
|
||||||
|
if (format.hasUtf8Bom)
|
||||||
|
data.remove(0, 3);
|
||||||
|
*plainText = data;
|
||||||
|
return Utils::TextFileFormat::ReadSuccess;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString target;
|
||||||
|
if (!format.decode(data, &target)) {
|
||||||
|
*errorString = QCoreApplication::translate("Utils::TextFileFormat", "An encoding error was encountered.");
|
||||||
|
return Utils::TextFileFormat::ReadEncodingError;
|
||||||
|
}
|
||||||
|
*plainText = target.toUtf8();
|
||||||
|
return Utils::TextFileFormat::ReadSuccess;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Writes out a text file.
|
Writes out a text file.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -77,7 +77,9 @@ public:
|
|||||||
QString *plainText, TextFileFormat *format, QString *errorString,
|
QString *plainText, TextFileFormat *format, QString *errorString,
|
||||||
QByteArray *decodingErrorSample = 0);
|
QByteArray *decodingErrorSample = 0);
|
||||||
static ReadResult readFileUTF8(const QString &fileName, QByteArray *plainText,
|
static ReadResult readFileUTF8(const QString &fileName, QByteArray *plainText,
|
||||||
QString *errorString);
|
QString *errorString); // TODO: Remove this version.
|
||||||
|
static ReadResult readFileUTF8(const QString &fileName, const QTextCodec *defaultCodec,
|
||||||
|
QByteArray *plainText, QString *errorString);
|
||||||
|
|
||||||
bool writeFile(const QString &fileName, QString plainText, QString *errorString) const;
|
bool writeFile(const QString &fileName, QString plainText, QString *errorString) const;
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,15 @@
|
|||||||
|
|
||||||
#include "cppmodelmanager.h"
|
#include "cppmodelmanager.h"
|
||||||
|
|
||||||
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
|
|
||||||
|
#include <utils/fileutils.h>
|
||||||
#include <utils/hostosinfo.h>
|
#include <utils/hostosinfo.h>
|
||||||
#include <utils/textfileformat.h>
|
#include <utils/textfileformat.h>
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
|
#include <QTextCodec>
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \class CppTools::Internal::CppPreprocessor
|
* \class CppTools::Internal::CppPreprocessor
|
||||||
@@ -30,7 +34,8 @@ CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager,
|
|||||||
m_modelManager(modelManager),
|
m_modelManager(modelManager),
|
||||||
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing),
|
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing),
|
||||||
m_preprocess(this, &m_env),
|
m_preprocess(this, &m_env),
|
||||||
m_revision(0)
|
m_revision(0),
|
||||||
|
m_defaultCodec(Core::EditorManager::defaultTextCodec())
|
||||||
{
|
{
|
||||||
m_preprocess.setKeepComments(true);
|
m_preprocess.setKeepComments(true);
|
||||||
}
|
}
|
||||||
@@ -41,7 +46,8 @@ CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager, const S
|
|||||||
m_modelManager(modelManager),
|
m_modelManager(modelManager),
|
||||||
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing),
|
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing),
|
||||||
m_preprocess(this, &m_env),
|
m_preprocess(this, &m_env),
|
||||||
m_revision(0)
|
m_revision(0),
|
||||||
|
m_defaultCodec(Core::EditorManager::defaultTextCodec())
|
||||||
{
|
{
|
||||||
m_preprocess.setKeepComments(true);
|
m_preprocess.setKeepComments(true);
|
||||||
}
|
}
|
||||||
@@ -182,9 +188,14 @@ void CppPreprocessor::getFileContents(const QString &absoluteFilePath,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString errStr;
|
if (contents) {
|
||||||
if (contents)
|
QString error;
|
||||||
Utils::TextFileFormat::readFileUTF8(absoluteFilePath, contents, &errStr);
|
if (Utils::TextFileFormat::readFileUTF8(absoluteFilePath, m_defaultCodec, contents, &error)
|
||||||
|
!= Utils::TextFileFormat::ReadSuccess) {
|
||||||
|
qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath),
|
||||||
|
qPrintable(error));
|
||||||
|
}
|
||||||
|
}
|
||||||
if (revision)
|
if (revision)
|
||||||
*revision = 0;
|
*revision = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,10 @@
|
|||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
class QTextCodec;
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
namespace CppTools {
|
namespace CppTools {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
@@ -75,6 +79,7 @@ protected:
|
|||||||
virtual void sourceNeeded(unsigned line, const QString &fileName, IncludeType type);
|
virtual void sourceNeeded(unsigned line, const QString &fileName, IncludeType type);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
CppPreprocessor();
|
||||||
void addFrameworkPath(const QString &frameworkPath);
|
void addFrameworkPath(const QString &frameworkPath);
|
||||||
|
|
||||||
CPlusPlus::Snapshot m_snapshot;
|
CPlusPlus::Snapshot m_snapshot;
|
||||||
@@ -92,6 +97,7 @@ private:
|
|||||||
QSet<QString> m_processed;
|
QSet<QString> m_processed;
|
||||||
unsigned m_revision;
|
unsigned m_revision;
|
||||||
QHash<QString, QString> m_fileNameCache;
|
QHash<QString, QString> m_fileNameCache;
|
||||||
|
QTextCodec *m_defaultCodec;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
Reference in New Issue
Block a user