1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "textfileformat.h"
#include "fileutils.h"
#include "qtcassert.h"
#include "stringutils.h"
#include "utilstr.h"
#include <QDebug>
enum { debug = 0 };
namespace Utils {
QDebug operator<<(QDebug d, const TextFileFormat &format)
{
QDebug nsp = d.nospace();
nsp << "TextFileFormat: " << format.encoding().fullDisplayName()
<< " hasUtf8Bom=" << format.hasUtf8Bom
<< (format.lineTerminationMode == TextFileFormat::LFLineTerminator ? " LF" : " CRLF");
return d;
}
/*!
\class Utils::TextFileFormat
\inmodule QtCreator
\brief The TextFileFormat class describes the format of a text file and
provides autodetection.
The format comprises
\list
\li Encoding represented by a the name of a codec
\li Presence of an UTF8 Byte Order Marker (BOM)
\li Line feed storage convention
\endlist
The class also provides convenience functions to read text files and return them
as strings or string lists and to write out files.
*/
TextFileFormat::TextFileFormat() = default;
/*!
Detects the format of text \a data.
*/
void TextFileFormat::detectFromData(const QByteArray &data)
{
if (data.isEmpty())
return;
const int bytesRead = data.size();
const auto buf = reinterpret_cast<const unsigned char *>(data.constData());
// code taken from qtextstream
if (bytesRead >= 4 && ((buf[0] == 0xff && buf[1] == 0xfe && buf[2] == 0 && buf[3] == 0)
|| (buf[0] == 0 && buf[1] == 0 && buf[2] == 0xfe && buf[3] == 0xff))) {
m_encoding = TextEncoding::Utf32;
} else if (bytesRead >= 2 && ((buf[0] == 0xff && buf[1] == 0xfe)
|| (buf[0] == 0xfe && buf[1] == 0xff))) {
m_encoding = TextEncoding::Utf16;
} else if (bytesRead >= 3 && ((buf[0] == 0xef && buf[1] == 0xbb) && buf[2] == 0xbf)) {
m_encoding = TextEncoding::Utf8;
hasUtf8Bom = true;
}
// end code taken from qtextstream
const int newLinePos = data.indexOf('\n');
if (newLinePos == -1)
lineTerminationMode = NativeLineTerminator;
else if (newLinePos == 0)
lineTerminationMode = LFLineTerminator;
else
lineTerminationMode = data.at(newLinePos - 1) == '\r' ? CRLFLineTerminator : LFLineTerminator;
}
/*!
Returns a piece of text specified by \a data suitable as display for
an encoding error.
*/
QByteArray TextFileFormat::decodingErrorSample(const QByteArray &data)
{
const int p = data.indexOf('\n', 16384);
return p < 0 ? data : data.left(p);
}
TextEncoding TextFileFormat::encoding() const
{
return m_encoding;
}
void TextFileFormat::setEncoding(const TextEncoding &encoding)
{
m_encoding = encoding;
}
/*!
Returns \a data decoded to a string, \a target.
*/
bool TextFileFormat::decode(const QByteArray &data, QString *target) const
{
QTC_ASSERT(m_encoding.isValid(), return false);
QStringDecoder decoder(m_encoding.name());
*target = decoder.decode(data);
if (decoder.hasError())
return false;
if (lineTerminationMode == TextFileFormat::CRLFLineTerminator)
target->remove(QLatin1Char('\r'));
return true;
}
/*!
Reads a text file from \a filePath into a string, \a plainText using
\a defaultCodec and text file format \a format.
Returns whether decoding was possible without errors. If an errors occur
it is returned together with a decoding error sample.
\note This function does \e{not} use the codec set by \l setCodec. Instead
it detects the codec to be used from BOM read file contents.
If none is present, it falls back using the provided \a fallbackCodec.
If this still doesn't exist, it uses \l TextEncoding::encodingForLocale()
*/
TextFileFormat::ReadResult
TextFileFormat::readFile(const FilePath &filePath, const TextEncoding &fallbackEncoding)
{
QByteArray data;
try {
const Result<QByteArray> res = filePath.fileContents();
if (!res)
return {TextFileFormat::ReadIOError, res.error()};
data = *res;
} catch (const std::bad_alloc &) {
return {TextFileFormat::ReadMemoryAllocationError, Tr::tr("Out of memory.")};
}
detectFromData(data);
if (!m_encoding.isValid())
m_encoding = fallbackEncoding;
if (!m_encoding.isValid())
m_encoding = TextEncoding::encodingForLocale();
TextFileFormat::ReadResult result;
if (!decode(data, &result.content)) {
result.code = TextFileFormat::ReadEncodingError;
result.error = Tr::tr("An encoding error was encountered.");
result.decodingErrorSample = TextFileFormat::decodingErrorSample(data);
return result;
}
return result;
}
Result<> TextFileFormat::readFileUtf8(const FilePath &filePath,
const TextEncoding &fallbackEncoding,
QByteArray *plainText)
{
QByteArray data;
try {
const Result<QByteArray> res = filePath.fileContents();
if (!res)
return ResultError(res.error());
data = *res;
} catch (const std::bad_alloc &) {
return ResultError(Tr::tr("Out of memory."));
}
TextFileFormat format;
format.detectFromData(data);
if (!format.m_encoding.isValid())
format.m_encoding = fallbackEncoding;
if (!format.m_encoding.isValid())
format.m_encoding = TextEncoding::encodingForLocale();
QString target;
if (format.m_encoding.isUtf8() || !format.decode(data, &target)) {
if (format.hasUtf8Bom)
data.remove(0, 3);
if (format.lineTerminationMode == TextFileFormat::CRLFLineTerminator)
data.replace("\r\n", "\n");
*plainText = data;
} else {
*plainText = target.toUtf8();
}
return ResultOk;
}
/*!
Writes out a text file to \a filePath into a string, \a plainText.
Returns whether decoding was possible without errors.
*/
Result<> TextFileFormat::writeFile(const FilePath &filePath, QString plainText) const
{
QTC_ASSERT(m_encoding.isValid(), return ResultError("No codec"));
// Does the user want CRLF? If that is native,
// do not 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;
if (lineTerminationMode == CRLFLineTerminator)
plainText.replace(QLatin1Char('\n'), QLatin1String("\r\n"));
FileSaver saver(filePath, fileMode);
if (!saver.hasError()) {
if (hasUtf8Bom && m_encoding.isUtf8())
saver.write({"\xef\xbb\xbf", 3});
saver.write(m_encoding.encode(plainText));
}
const Result<> result = saver.finalize();
if (debug)
qDebug().nospace() << Q_FUNC_INFO << filePath << ' ' << *this << ' ' << plainText.size()
<< " bytes, returns " << result.has_value();
return result;
}
} // namespace Utils
|