Utils: do not use QIODevice::Text open mode when saving text documents

The mode replaces "\n" with "\r\n" in the byte array after encoding the
text, resulting in file contents that cannot be decoded.

Change-Id: I8010df56f28a479d516b8bcb887749905fd162ce
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
David Schulz
2019-08-26 13:21:46 +02:00
parent 2aca0c1b28
commit a21df46481

View File

@@ -302,14 +302,11 @@ bool TextFileFormat::writeFile(const QString &fileName, QString plainText, QStri
QTC_ASSERT(codec, return false); QTC_ASSERT(codec, return false);
// Does the user want CRLF? If that is native, // Does the user want CRLF? If that is native,
// let QFile do the work, else manually add. // do net let QFile do the work, because it replaces the line ending after the text was encoded,
// and this could lead to undecodable file contents.
QIODevice::OpenMode fileMode = QIODevice::NotOpen; QIODevice::OpenMode fileMode = QIODevice::NotOpen;
if (lineTerminationMode == CRLFLineTerminator) { if (lineTerminationMode == CRLFLineTerminator)
if (NativeLineTerminator == CRLFLineTerminator) plainText.replace(QLatin1Char('\n'), QLatin1String("\r\n"));
fileMode |= QIODevice::Text;
else
plainText.replace(QLatin1Char('\n'), QLatin1String("\r\n"));
}
FileSaver saver(fileName, fileMode); FileSaver saver(fileName, fileMode);
if (!saver.hasError()) { if (!saver.hasError()) {