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
|
/*
Copyright 2013 Christian Surlykke
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
#include <QApplication>
#include <QTextStream>
#include <QDebug>
#include "TerminalCharacterDecoder.h"
#include "Emulation.h"
#include "HistorySearch.h"
HistorySearch::HistorySearch(EmulationPtr emulation, const QRegExp& regExp,
bool forwards, int startColumn, int startLine,
QObject* parent) :
QObject(parent),
m_emulation(emulation),
m_regExp(regExp),
m_forwards(forwards),
m_startColumn(startColumn),
m_startLine(startLine) {
}
HistorySearch::~HistorySearch() {
}
void HistorySearch::search() {
bool found = false;
if (! m_regExp.isEmpty())
{
if (m_forwards) {
found = search(m_startColumn, m_startLine, -1, m_emulation->lineCount()) || search(0, 0, m_startColumn, m_startLine);
} else {
found = search(0, 0, m_startColumn, m_startLine) || search(m_startColumn, m_startLine, -1, m_emulation->lineCount());
}
if (found) {
emit matchFound(m_foundStartColumn, m_foundStartLine, m_foundEndColumn, m_foundEndLine);
}
else {
emit noMatchFound();
}
}
deleteLater();
}
bool HistorySearch::search(int startColumn, int startLine, int endColumn, int endLine) {
qDebug() << "search from" << startColumn << "," << startLine
<< "to" << endColumn << "," << endLine;
int linesRead = 0;
int linesToRead = endLine - startLine + 1;
qDebug() << "linesToRead:" << linesToRead;
// We read process history from (and including) startLine to (and including) endLine in
// blocks of at most 10K lines so that we do not use unhealthy amounts of memory
int blockSize;
while ((blockSize = qMin(10000, linesToRead - linesRead)) > 0) {
QString string;
QTextStream searchStream(&string);
PlainTextDecoder decoder;
decoder.begin(&searchStream);
decoder.setRecordLinePositions(true);
// Calculate lines to read and read them
int blockStartLine = m_forwards ? startLine + linesRead : endLine - linesRead - blockSize + 1;
int chunkEndLine = blockStartLine + blockSize - 1;
m_emulation->writeToStream(&decoder, blockStartLine, chunkEndLine);
// We search between startColumn in the first line of the string and endColumn in the last
// line of the string. First we calculate the position (in the string) of endColumn in the
// last line of the string
int endPosition;
// The String that Emulator.writeToStream produces has a newline at the end, and so ends with an
// empty line - we ignore that.
int numberOfLinesInString = decoder.linePositions().size() - 1;
if (numberOfLinesInString > 0 && endColumn > -1 )
{
endPosition = decoder.linePositions().at(numberOfLinesInString - 1) + endColumn;
}
else
{
endPosition = string.size();
}
// So now we can log for m_regExp in the string between startColumn and endPosition
int matchStart;
if (m_forwards)
{
matchStart = string.indexOf(m_regExp, startColumn);
if (matchStart >= endPosition)
matchStart = -1;
}
else
{
matchStart = string.lastIndexOf(m_regExp, endPosition - 1);
if (matchStart < startColumn)
matchStart = -1;
}
if (matchStart > -1)
{
int matchEnd = matchStart + m_regExp.matchedLength() - 1;
qDebug() << "Found in string from" << matchStart << "to" << matchEnd;
// Translate startPos and endPos to startColum, startLine, endColumn and endLine in history.
int startLineNumberInString = findLineNumberInString(decoder.linePositions(), matchStart);
m_foundStartColumn = matchStart - decoder.linePositions().at(startLineNumberInString);
m_foundStartLine = startLineNumberInString + startLine + linesRead;
int endLineNumberInString = findLineNumberInString(decoder.linePositions(), matchEnd);
m_foundEndColumn = matchEnd - decoder.linePositions().at(endLineNumberInString);
m_foundEndLine = endLineNumberInString + startLine + linesRead;
qDebug() << "m_foundStartColumn" << m_foundStartColumn
<< "m_foundStartLine" << m_foundEndLine
<< "m_foundEndColumn" << m_foundEndColumn
<< "m_foundEndLine" << m_foundEndLine;
return true;
}
linesRead += blockSize;
}
qDebug() << "Not found";
return false;
}
int HistorySearch::findLineNumberInString(QList<int> linePositions, int position) {
int lineNum = 0;
while (lineNum + 1 < linePositions.size() && linePositions[lineNum + 1] <= position)
lineNum++;
return lineNum;
}
|