Core: Properly consider text codec when rewriting header files

The code blindly assumed the current locale's codec when reading files,
potentially breaking the content.

Fixes: QTCREATORBUG-28164
Change-Id: I390b3d4e615e5ca853faef2c6159988cc6246337
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-11-24 17:38:36 +01:00
parent 9dc5a9ec00
commit ac760f8e4a

View File

@@ -3,14 +3,16 @@
#include "fileutils.h" #include "fileutils.h"
#include <coreplugin/coreconstants.h> #include "coreconstants.h"
#include <coreplugin/documentmanager.h> #include "documentmanager.h"
#include <coreplugin/foldernavigationwidget.h> #include "editormanager/editormanager.h"
#include <coreplugin/icore.h> #include "foldernavigationwidget.h"
#include <coreplugin/iversioncontrol.h> #include "icore.h"
#include <coreplugin/messagemanager.h> #include "iversioncontrol.h"
#include <coreplugin/navigationwidget.h> #include "messagemanager.h"
#include <coreplugin/vcsmanager.h> #include "navigationwidget.h"
#include "vcsmanager.h"
#include <utils/commandline.h> #include <utils/commandline.h>
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
@@ -251,12 +253,9 @@ bool FileUtils::renameFile(const FilePath &orgFilePath, const FilePath &newFileP
if (!result) // The moving via vcs failed or the vcs does not support moving, fall back if (!result) // The moving via vcs failed or the vcs does not support moving, fall back
result = orgFilePath.renameFile(newFilePath); result = orgFilePath.renameFile(newFilePath);
if (result) { if (result) {
// yeah we moved, tell the filemanager about it
DocumentManager::renamedFile(orgFilePath, newFilePath); DocumentManager::renamedFile(orgFilePath, newFilePath);
}
if (result)
updateHeaderFileGuardIfApplicable(orgFilePath, newFilePath, handleGuards); updateHeaderFileGuardIfApplicable(orgFilePath, newFilePath, handleGuards);
}
return result; return result;
} }
@@ -291,11 +290,16 @@ bool FileUtils::updateHeaderFileGuardAfterRename(const QString &headerPath,
int guardStartLine = -1; int guardStartLine = -1;
int guardCloseLine = -1; int guardCloseLine = -1;
QByteArray data = headerFile.readAll(); const QByteArray data = headerFile.readAll();
headerFile.close(); headerFile.close();
const auto headerFileTextFormat = Utils::TextFileFormat::detect(data); auto headerFileTextFormat = Utils::TextFileFormat::detect(data);
QTextStream inStream(&data); if (!headerFileTextFormat.codec)
headerFileTextFormat.codec = EditorManager::defaultTextCodec();
QString stringContent;
if (!headerFileTextFormat.decode(data, &stringContent))
return false;
QTextStream inStream(&stringContent);
int lineCounter = 0; int lineCounter = 0;
QString line; QString line;
while (!inStream.atEnd()) { while (!inStream.atEnd()) {
@@ -398,10 +402,7 @@ bool FileUtils::updateHeaderFileGuardAfterRename(const QString &headerPath,
} }
lineCounter++; lineCounter++;
} }
const QTextCodec *textCodec = (headerFileTextFormat.codec == nullptr) tmpHeader.write(headerFileTextFormat.codec->fromUnicode(outString));
? QTextCodec::codecForName("UTF-8")
: headerFileTextFormat.codec;
tmpHeader.write(textCodec->fromUnicode(outString));
tmpHeader.close(); tmpHeader.close();
} else { } else {
// if opening the temp file failed report error // if opening the temp file failed report error