comparison libgui/src/m-editor/file-editor-tab.cc @ 19973:95a94c98c884

fix eol detection in editor which caused a crash for huge files * file-editor-tab.cc (detect_eol_mode): use QByteArray instead of char pointer, manually counting eol chars replaced by count method of QByteArray
author Torsten <ttl@justmail.de>
date Thu, 19 Feb 2015 13:26:40 +0100
parents 21015ca26566
children 35a8e1beac8d
comparison
equal deleted inserted replaced
19972:a010a65ace1f 19973:95a94c98c884
1347 } 1347 }
1348 1348
1349 QsciScintilla::EolMode 1349 QsciScintilla::EolMode
1350 file_editor_tab::detect_eol_mode () 1350 file_editor_tab::detect_eol_mode ()
1351 { 1351 {
1352 char *text = _edit_area->text ().toAscii ().data (); 1352 QByteArray text = _edit_area->text ().toAscii ();
1353 int text_size = _edit_area->text ().length (); 1353
1354 1354 QByteArray eol_lf = QByteArray (1,0x0a);
1355 char eol_lf = 0x0a; 1355 QByteArray eol_cr = QByteArray (1,0x0d);
1356 char eol_cr = 0x0d; 1356 QByteArray eol_crlf = eol_cr;
1357 1357 eol_crlf.append (eol_lf);
1358 int count_lf = 0; 1358
1359 int count_cr = 0; 1359 int count_crlf = text.count (eol_crlf);
1360 int count_crlf = 0; 1360 int count_lf = text.count (eol_lf) - count_crlf; // isolated lf
1361 1361 int count_cr = text.count (eol_cr) - count_crlf; // isolated cr;
1362 for (int i = 0; i < text_size; i++)
1363 {
1364 if (text[i] == eol_lf)
1365 {
1366 count_lf++;
1367 }
1368 else
1369 {
1370 if (text[i] == eol_cr)
1371 {
1372 if ((i < text_size -1) && text[i+1] == eol_lf)
1373 {
1374 count_crlf++;
1375 i++;
1376 }
1377 else
1378 count_cr++;
1379 }
1380 }
1381 }
1382 1362
1383 // get default from OS or from settings 1363 // get default from OS or from settings
1384 #if defined (Q_OS_WIN32) 1364 #if defined (Q_OS_WIN32)
1385 int os_eol_mode = QsciScintilla::EolWindows; 1365 int os_eol_mode = QsciScintilla::EolWindows;
1386 #elif defined (Q_OS_MAC) 1366 #elif defined (Q_OS_MAC)
1387 int os_eol_mode = QsciScintilla::EolMac; 1367 int os_eol_mode = QsciScintilla::EolMac;
1388 #else 1368 #else
1389 int os_eol_mode = QsciScintilla::EolUnix; 1369 int os_eol_mode = QsciScintilla::EolUnix;
1390 #endif 1370 #endif
1391 QSettings *settings = resource_manager::get_settings (); 1371 QSettings *settings = resource_manager::get_settings ();
1392 QsciScintilla::EolMode eol_mode = static_cast<QsciScintilla::EolMode> ( 1372 QsciScintilla::EolMode eol_mode = static_cast<QsciScintilla::EolMode> (
1393 settings->value("editor/default_eol_mode",os_eol_mode).toInt ()); 1373 settings->value("editor/default_eol_mode",os_eol_mode).toInt ());
1394 1374
1395 int count_max = 0; 1375 int count_max = 0;
1396 1376
1397 if (count_crlf > count_max) 1377 if (count_crlf > count_max)
1398 { 1378 {
1399 eol_mode = QsciScintilla::EolWindows; 1379 eol_mode = QsciScintilla::EolWindows;
1400 count_max = count_crlf; 1380 count_max = count_crlf;
1401 } 1381 }
1382 if (count_lf > count_max)
1383 {
1384 eol_mode = QsciScintilla::EolUnix;
1385 count_max = count_lf;
1386 }
1402 if (count_cr > count_max) 1387 if (count_cr > count_max)
1403 { 1388 {
1404 eol_mode = QsciScintilla::EolMac; 1389 eol_mode = QsciScintilla::EolMac;
1405 count_max = count_cr; 1390 count_max = count_cr;
1406 }
1407 if (count_lf > count_max)
1408 {
1409 eol_mode = QsciScintilla::EolUnix;
1410 count_max = count_lf;
1411 } 1391 }
1412 1392
1413 return eol_mode; 1393 return eol_mode;
1414 } 1394 }
1415 1395