File indexing completed on 2025-01-19 04:23:26

0001 /*
0002     Copyright 2013 Christian Surlykke
0003 
0004     This program is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301  USA.
0018 */
0019 #include <QApplication>
0020 #include <QTextStream>
0021 #include <QDebug>
0022 
0023 #include "TerminalCharacterDecoder.h"
0024 #include "Emulation.h"
0025 #include "HistorySearch.h"
0026 
0027 HistorySearch::HistorySearch(EmulationPtr emulation, QRegExp regExp,
0028         bool forwards, int startColumn, int startLine,
0029         QObject* parent) :
0030 QObject(parent),
0031 m_emulation(emulation),
0032 m_regExp(regExp),
0033 m_forwards(forwards),
0034 m_startColumn(startColumn),
0035 m_startLine(startLine) {
0036 }
0037 
0038 HistorySearch::~HistorySearch() {
0039 }
0040 
0041 void HistorySearch::search() {
0042     bool found = false;
0043 
0044     if (! m_regExp.isEmpty())
0045     {
0046         if (m_forwards) {
0047             found = search(m_startColumn, m_startLine, -1, m_emulation->lineCount()) || search(0, 0, m_startColumn, m_startLine);
0048         } else {
0049             found = search(0, 0, m_startColumn, m_startLine) || search(m_startColumn, m_startLine, -1, m_emulation->lineCount());
0050         }
0051 
0052         if (found) {
0053             Q_EMIT matchFound(m_foundStartColumn, m_foundStartLine, m_foundEndColumn, m_foundEndLine);
0054         }
0055         else {
0056             Q_EMIT noMatchFound();
0057         }
0058     }
0059 
0060     deleteLater();
0061 }
0062 
0063 bool HistorySearch::search(int startColumn, int startLine, int endColumn, int endLine) {
0064     qDebug() << "search from" << startColumn << "," << startLine
0065             <<  "to" << endColumn << "," << endLine;
0066 
0067     int linesRead = 0;
0068     int linesToRead = endLine - startLine + 1;
0069 
0070     qDebug() << "linesToRead:" << linesToRead;
0071 
0072     // We read process history from (and including) startLine to (and including) endLine in
0073     // blocks of at most 10K lines so that we do not use unhealthy amounts of memory
0074     int blockSize;
0075     while ((blockSize = qMin(10000, linesToRead - linesRead)) > 0) {
0076 
0077         QString string;
0078         QTextStream searchStream(&string);
0079         PlainTextDecoder decoder;
0080         decoder.begin(&searchStream);
0081         decoder.setRecordLinePositions(true);
0082 
0083         // Calculate lines to read and read them
0084         int blockStartLine = m_forwards ? startLine + linesRead : endLine - linesRead - blockSize + 1;
0085         int chunkEndLine = blockStartLine + blockSize - 1;
0086         m_emulation->writeToStream(&decoder, blockStartLine, chunkEndLine);
0087 
0088         // We search between startColumn in the first line of the string and endColumn in the last
0089         // line of the string. First we calculate the position (in the string) of endColumn in the
0090         // last line of the string
0091         int endPosition;
0092 
0093         // The String that Emulator.writeToStream produces has a newline at the end, and so ends with an
0094         // empty line - we ignore that.
0095         int numberOfLinesInString = decoder.linePositions().size() - 1;
0096         if (numberOfLinesInString > 0 && endColumn > -1 )
0097         {
0098             endPosition = decoder.linePositions().at(numberOfLinesInString - 1) + endColumn;
0099         }
0100         else
0101         {
0102             endPosition = string.size();
0103         }
0104 
0105         // So now we can log for m_regExp in the string between startColumn and endPosition
0106         int matchStart;
0107         if (m_forwards)
0108         {
0109             matchStart = string.indexOf(m_regExp, startColumn);
0110             if (matchStart >= endPosition)
0111                 matchStart = -1;
0112         }
0113         else
0114         {
0115             matchStart = string.lastIndexOf(m_regExp, endPosition - 1);
0116             if (matchStart < startColumn)
0117                 matchStart = -1;
0118         }
0119 
0120         if (matchStart > -1)
0121         {
0122             int matchEnd = matchStart + m_regExp.matchedLength() - 1;
0123             qDebug() << "Found in string from" << matchStart << "to" << matchEnd;
0124 
0125             // Translate startPos and endPos to startColum, startLine, endColumn and endLine in history.
0126             int startLineNumberInString = findLineNumberInString(decoder.linePositions(), matchStart);
0127             m_foundStartColumn = matchStart - decoder.linePositions().at(startLineNumberInString);
0128             m_foundStartLine = startLineNumberInString + startLine + linesRead;
0129 
0130             int endLineNumberInString = findLineNumberInString(decoder.linePositions(), matchEnd);
0131             m_foundEndColumn = matchEnd - decoder.linePositions().at(endLineNumberInString);
0132             m_foundEndLine = endLineNumberInString + startLine + linesRead;
0133 
0134             qDebug() << "m_foundStartColumn" << m_foundStartColumn
0135                     << "m_foundStartLine" << m_foundEndLine
0136                     << "m_foundEndColumn" << m_foundEndColumn
0137                     << "m_foundEndLine" << m_foundEndLine;
0138 
0139             return true;
0140         }
0141 
0142 
0143         linesRead += blockSize;
0144     }
0145 
0146     qDebug() << "Not found";
0147     return false;
0148 }
0149 
0150 
0151 int HistorySearch::findLineNumberInString(QList<int> linePositions, int position) {
0152     int lineNum = 0;
0153     while (lineNum + 1 < linePositions.size() && linePositions[lineNum + 1] <= position)
0154         lineNum++;
0155 
0156     return lineNum;
0157 }