Commit c1225ea3 authored by Nikolai Kosjar's avatar Nikolai Kosjar

CppTools: Use editor manager's codec as fallback

...for reading not already opened files.

This partly reverts commit f7c68f6b. 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: default avatarOswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 2f040fc6
......@@ -304,6 +304,40 @@ TextFileFormat::ReadResult TextFileFormat::readFileUTF8(const QString &fileName,
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.
*/
......
......@@ -77,7 +77,9 @@ public:
QString *plainText, TextFileFormat *format, QString *errorString,
QByteArray *decodingErrorSample = 0);
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;
......
......@@ -2,11 +2,15 @@
#include "cppmodelmanager.h"
#include <coreplugin/editormanager/editormanager.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/textfileformat.h>
#include <QCoreApplication>
#include <QCryptographicHash>
#include <QTextCodec>
/*!
* \class CppTools::Internal::CppPreprocessor
......@@ -30,7 +34,8 @@ CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager,
m_modelManager(modelManager),
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing),
m_preprocess(this, &m_env),
m_revision(0)
m_revision(0),
m_defaultCodec(Core::EditorManager::defaultTextCodec())
{
m_preprocess.setKeepComments(true);
}
......@@ -41,7 +46,8 @@ CppPreprocessor::CppPreprocessor(QPointer<CppModelManager> modelManager, const S
m_modelManager(modelManager),
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing),
m_preprocess(this, &m_env),
m_revision(0)
m_revision(0),
m_defaultCodec(Core::EditorManager::defaultTextCodec())
{
m_preprocess.setKeepComments(true);
}
......@@ -182,9 +188,14 @@ void CppPreprocessor::getFileContents(const QString &absoluteFilePath,
return;
}
QString errStr;
if (contents)
Utils::TextFileFormat::readFileUTF8(absoluteFilePath, contents, &errStr);
if (contents) {
QString error;
if (Utils::TextFileFormat::readFileUTF8(absoluteFilePath, m_defaultCodec, contents, &error)
!= Utils::TextFileFormat::ReadSuccess) {
qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath),
qPrintable(error));
}
}
if (revision)
*revision = 0;
}
......
......@@ -9,6 +9,10 @@
#include <QHash>
#include <QPointer>
QT_BEGIN_NAMESPACE
class QTextCodec;
QT_END_NAMESPACE
namespace CppTools {
namespace Internal {
......@@ -75,6 +79,7 @@ protected:
virtual void sourceNeeded(unsigned line, const QString &fileName, IncludeType type);
private:
CppPreprocessor();
void addFrameworkPath(const QString &frameworkPath);
CPlusPlus::Snapshot m_snapshot;
......@@ -92,6 +97,7 @@ private:
QSet<QString> m_processed;
unsigned m_revision;
QHash<QString, QString> m_fileNameCache;
QTextCodec *m_defaultCodec;
};
} // namespace Internal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment