@@ -50,6 +50,7 @@ LiteEditorFile::LiteEditorFile(LiteApi::IApplication *app, QObject *parent)
50
50
m_codec = QTextCodec::codecForName (" utf-8" );
51
51
m_hasDecodingError = false ;
52
52
m_bReadOnly = false ;
53
+ m_bBinary = false ;
53
54
m_hasUtf8Bom = false ;
54
55
m_lineTerminatorMode = NativeLineTerminator;
55
56
}
@@ -67,6 +68,11 @@ bool LiteEditorFile::isReadOnly() const
67
68
return m_bReadOnly;
68
69
}
69
70
71
+ bool LiteEditorFile::isBinary () const
72
+ {
73
+ return m_bBinary;
74
+ }
75
+
70
76
bool LiteEditorFile::saveText (const QString &fileName, const QString &text)
71
77
{
72
78
QFile file (fileName);
@@ -140,82 +146,81 @@ bool LiteEditorFile::loadFileHelper(const QString &fileName, const QString &mime
140
146
141
147
QByteArray buf = file.readAll ();
142
148
m_hasDecodingError = false ;
149
+ m_bBinary = false ;
143
150
144
151
if (HasBinaryData (buf,32 )) {
145
- m_liteApp->appendLog (" LiteEditor" ," Binary file not open in the text editor! " +fileName,true );
146
- m_hasDecodingError = true ;
147
- // outText = "error load binary file!!!";
148
- return false ;
149
- }
150
-
151
- if (bCheckCodec) {
152
- m_codec = QTextCodec::codecForName (" UTF-8" );
153
- m_hasUtf8Bom = false ;
154
-
155
- if (mimeType == " text/html" || mimeType == " text/xml" ) {
156
- m_codec = QTextCodec::codecForHtml (buf,QTextCodec::codecForName (" utf-8" ));
157
- } else {
158
- LiteApi::IMimeType *im = m_liteApp->mimeTypeManager ()->findMimeType (mimeType);
159
- if (im) {
160
- QString codecName = im->codec ();
161
- if (!codecName.isEmpty ()) {
162
- m_codec = QTextCodec::codecForName (codecName.toLatin1 ());
163
- }
164
- }
165
- int bytesRead = buf.size ();
166
- QTextCodec *codec = m_codec;
167
- // code taken from qtextstream
168
- if (bytesRead >= 4 && ((uchar (buf[0 ]) == 0xff && uchar (buf[1 ]) == 0xfe && uchar (buf[2 ]) == 0 && uchar (buf[3 ]) == 0 )
169
- || (uchar (buf[0 ]) == 0 && uchar (buf[1 ]) == 0 && uchar (buf[2 ]) == 0xfe && uchar (buf[3 ]) == 0xff ))) {
170
- codec = QTextCodec::codecForName (" UTF-32" );
171
- } else if (bytesRead >= 2 && ((uchar (buf[0 ]) == 0xff && uchar (buf[1 ]) == 0xfe )
172
- || (uchar (buf[0 ]) == 0xfe && uchar (buf[1 ]) == 0xff ))) {
173
- codec = QTextCodec::codecForName (" UTF-16" );
174
- } else if (bytesRead >= 3 && uchar (buf[0 ]) == 0xef && uchar (buf[1 ]) == 0xbb && uchar (buf[2 ])== 0xbf ) {
175
- codec = QTextCodec::codecForName (" UTF-8" );
176
- buf.remove (0 ,3 );
177
- m_hasUtf8Bom = true ;
178
- } else if (!codec){
179
- codec = QTextCodec::codecForLocale ();
180
- }
181
- // end code taken from qtextstream
182
- m_codec = codec;
183
- }
184
- }
185
-
186
- QTextCodec::ConverterState state;
187
- outText = m_codec->toUnicode (buf,buf.size (),&state);
188
- if (state.invalidChars > 0 || state.remainingChars > 0 ) {
189
- m_hasDecodingError = true ;
190
- }
191
- if (m_hasDecodingError && bCheckCodec) {
152
+ m_liteApp->appendLog (" LiteEditor" ," Open binary file in the text editor! " +fileName,true );
153
+ m_bBinary = true ;
192
154
QByteArray testName = m_libucd.parse (buf);
193
155
if (!testName.isEmpty ()) {
194
156
QTextCodec *c = QTextCodec::codecForName (testName);
195
157
if (c && (c->mibEnum () != m_codec->mibEnum ()) ) {
196
- QTextCodec::ConverterState testState;
197
- QString testText = c->toUnicode (buf,buf.size (),&testState);
198
- if (testState.invalidChars == 0 && testState.remainingChars == 0 ) {
199
- m_hasDecodingError = false ;
200
- m_codec = c;
201
- outText = testText;
158
+ m_codec = c;
159
+ }
160
+ }
161
+ QTextCodec::ConverterState state;
162
+ outText = m_codec->toUnicode (buf,buf.size (),&state);
163
+ if (state.invalidChars > 0 || state.remainingChars > 0 ) {
164
+ m_hasDecodingError = true ;
165
+ }
166
+ } else {
167
+ if (bCheckCodec) {
168
+ m_codec = QTextCodec::codecForName (" UTF-8" );
169
+ m_hasUtf8Bom = false ;
170
+
171
+ if (mimeType == " text/html" || mimeType == " text/xml" ) {
172
+ m_codec = QTextCodec::codecForHtml (buf,QTextCodec::codecForName (" utf-8" ));
173
+ } else {
174
+ LiteApi::IMimeType *im = m_liteApp->mimeTypeManager ()->findMimeType (mimeType);
175
+ if (im) {
176
+ QString codecName = im->codec ();
177
+ if (!codecName.isEmpty ()) {
178
+ m_codec = QTextCodec::codecForName (codecName.toLatin1 ());
179
+ }
180
+ }
181
+ int bytesRead = buf.size ();
182
+ QTextCodec *codec = m_codec;
183
+ // code taken from qtextstream
184
+ if (bytesRead >= 4 && ((uchar (buf[0 ]) == 0xff && uchar (buf[1 ]) == 0xfe && uchar (buf[2 ]) == 0 && uchar (buf[3 ]) == 0 )
185
+ || (uchar (buf[0 ]) == 0 && uchar (buf[1 ]) == 0 && uchar (buf[2 ]) == 0xfe && uchar (buf[3 ]) == 0xff ))) {
186
+ codec = QTextCodec::codecForName (" UTF-32" );
187
+ } else if (bytesRead >= 2 && ((uchar (buf[0 ]) == 0xff && uchar (buf[1 ]) == 0xfe )
188
+ || (uchar (buf[0 ]) == 0xfe && uchar (buf[1 ]) == 0xff ))) {
189
+ codec = QTextCodec::codecForName (" UTF-16" );
190
+ } else if (bytesRead >= 3 && uchar (buf[0 ]) == 0xef && uchar (buf[1 ]) == 0xbb && uchar (buf[2 ])== 0xbf ) {
191
+ codec = QTextCodec::codecForName (" UTF-8" );
192
+ buf.remove (0 ,3 );
193
+ m_hasUtf8Bom = true ;
194
+ } else if (!codec){
195
+ codec = QTextCodec::codecForLocale ();
196
+ }
197
+ // end code taken from qtextstream
198
+ m_codec = codec;
199
+ }
200
+ }
201
+
202
+ QTextCodec::ConverterState state;
203
+ outText = m_codec->toUnicode (buf,buf.size (),&state);
204
+ if (state.invalidChars > 0 || state.remainingChars > 0 ) {
205
+ m_hasDecodingError = true ;
206
+ }
207
+
208
+ if (m_hasDecodingError && bCheckCodec) {
209
+ QByteArray testName = m_libucd.parse (buf);
210
+ if (!testName.isEmpty ()) {
211
+ QTextCodec *c = QTextCodec::codecForName (testName);
212
+ if (c && (c->mibEnum () != m_codec->mibEnum ()) ) {
213
+ QTextCodec::ConverterState testState;
214
+ QString testText = c->toUnicode (buf,buf.size (),&testState);
215
+ if (testState.invalidChars == 0 && testState.remainingChars == 0 ) {
216
+ m_hasDecodingError = false ;
217
+ m_codec = c;
218
+ outText = testText;
219
+ }
202
220
}
203
221
}
204
222
}
205
223
}
206
- /*
207
- QByteArray verifyBuf = m_codec->fromUnicode(text); // slow
208
- // the minSize trick lets us ignore unicode headers
209
- int minSize = qMin(verifyBuf.size(), buf.size());
210
- m_hasDecodingError = (minSize < buf.size()- 4
211
- || memcmp(verifyBuf.constData() + verifyBuf.size() - minSize,
212
- buf.constData() + buf.size() - minSize, minSize));
213
- */
214
- /*
215
- if (text.length()*2+4 < buf.length()) {
216
- m_hasDecodingError = true;
217
- }
218
- */
219
224
220
225
int lf = outText.indexOf (' \n ' );
221
226
if (lf < 0 ) {
0 commit comments