File indexing completed on 2024-06-23 05:48:55

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "extractstringsjob.hpp"
0010 
0011 // Okteta core
0012 #include <Okteta/AbstractByteArrayModel>
0013 #include <Okteta/CharCodec>
0014 #include <Okteta/Character>
0015 // Qt
0016 #include <QCoreApplication>
0017 
0018 namespace Kasten {
0019 
0020 static constexpr int ExtractStringBlockSize = 100000;
0021 
0022 void ExtractStringsJob::exec()
0023 {
0024 
0025     // reset
0026     mContainedStringList->clear();
0027 
0028     // check
0029     if (!mByteArrayModel || !mSelection.isValid()) {
0030         deleteLater(); // TODO: could be reused on next operation
0031 
0032         return;
0033     }
0034 
0035     bool stringStarted = false;
0036     Okteta::Address stringStart = mSelection.start();
0037     QString string;
0038 
0039     Okteta::Address i = mSelection.start();
0040     Okteta::Address blockEnd = mSelection.start();
0041     while (i <= mSelection.end()) {
0042         blockEnd += ExtractStringBlockSize;
0043         if (blockEnd > mSelection.end()) {
0044             blockEnd = mSelection.end();
0045         }
0046 
0047         for (; i <= blockEnd; ++i) {
0048             const Okteta::Character decodedChar = mCharCodec->decode(mByteArrayModel->byte(i));
0049             // TODO: Zeilenumbrüche ausnehmen
0050             const bool isStringChar = (!decodedChar.isUndefined() &&
0051                                        (decodedChar.isLetterOrNumber() || decodedChar.isSpace() || decodedChar.isPunct()));
0052 
0053             if (isStringChar) {
0054                 if (!stringStarted) {
0055                     stringStart = i;
0056                     stringStarted = true;
0057                     string.clear();
0058                 }
0059                 string.append(decodedChar);
0060             } else {
0061                 if (stringStarted) {
0062                     if (i - stringStart >= mMinLength) {
0063                         mContainedStringList->append(ContainedString(string, stringStart));
0064                     }
0065                     stringStarted = false;
0066                 }
0067             }
0068         }
0069 
0070         QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
0071     }
0072 
0073     // last string not ended?
0074     if (stringStarted) {
0075         if (i - stringStart >= mMinLength) {
0076             mContainedStringList->append(ContainedString(string, stringStart));
0077         }
0078         stringStarted = false;
0079     }
0080 
0081     deleteLater(); // TODO: could be reused on next operation
0082 }
0083 
0084 }
0085 
0086 #include "moc_extractstringsjob.cpp"