1 | /* |
---|
2 | Copyright 2013 Christian Surlykke |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU General Public License as published by |
---|
6 | the Free Software Foundation; either version 2 of the License, or |
---|
7 | (at your option) any later version. |
---|
8 | |
---|
9 | This program is distributed in the hope that it will be useful, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
17 | 02110-1301 USA. |
---|
18 | */ |
---|
19 | #include <QApplication> |
---|
20 | #include <QTextStream> |
---|
21 | #include <QDebug> |
---|
22 | |
---|
23 | #include "TerminalCharacterDecoder.h" |
---|
24 | #include "Emulation.h" |
---|
25 | #include "HistorySearch.h" |
---|
26 | |
---|
27 | HistorySearch::HistorySearch(EmulationPtr emulation, const QRegExp& regExp, |
---|
28 | bool forwards, int startColumn, int startLine, |
---|
29 | QObject* parent) : |
---|
30 | QObject(parent), |
---|
31 | m_emulation(emulation), |
---|
32 | m_regExp(regExp), |
---|
33 | m_forwards(forwards), |
---|
34 | m_startColumn(startColumn), |
---|
35 | m_startLine(startLine) { |
---|
36 | } |
---|
37 | |
---|
38 | HistorySearch::~HistorySearch() { |
---|
39 | } |
---|
40 | |
---|
41 | void HistorySearch::search() { |
---|
42 | bool found = false; |
---|
43 | |
---|
44 | if (! m_regExp.isEmpty()) |
---|
45 | { |
---|
46 | if (m_forwards) { |
---|
47 | found = search(m_startColumn, m_startLine, -1, m_emulation->lineCount()) || search(0, 0, m_startColumn, m_startLine); |
---|
48 | } else { |
---|
49 | found = search(0, 0, m_startColumn, m_startLine) || search(m_startColumn, m_startLine, -1, m_emulation->lineCount()); |
---|
50 | } |
---|
51 | |
---|
52 | if (found) { |
---|
53 | emit matchFound(m_foundStartColumn, m_foundStartLine, m_foundEndColumn, m_foundEndLine); |
---|
54 | } |
---|
55 | else { |
---|
56 | emit noMatchFound(); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | deleteLater(); |
---|
61 | } |
---|
62 | |
---|
63 | bool HistorySearch::search(int startColumn, int startLine, int endColumn, int endLine) { |
---|
64 | qDebug() << "search from" << startColumn << "," << startLine |
---|
65 | << "to" << endColumn << "," << endLine; |
---|
66 | |
---|
67 | int linesRead = 0; |
---|
68 | int linesToRead = endLine - startLine + 1; |
---|
69 | |
---|
70 | qDebug() << "linesToRead:" << linesToRead; |
---|
71 | |
---|
72 | // We read process history from (and including) startLine to (and including) endLine in |
---|
73 | // blocks of at most 10K lines so that we do not use unhealthy amounts of memory |
---|
74 | int blockSize; |
---|
75 | while ((blockSize = qMin(10000, linesToRead - linesRead)) > 0) { |
---|
76 | |
---|
77 | QString string; |
---|
78 | QTextStream searchStream(&string); |
---|
79 | PlainTextDecoder decoder; |
---|
80 | decoder.begin(&searchStream); |
---|
81 | decoder.setRecordLinePositions(true); |
---|
82 | |
---|
83 | // Calculate lines to read and read them |
---|
84 | int blockStartLine = m_forwards ? startLine + linesRead : endLine - linesRead - blockSize + 1; |
---|
85 | int chunkEndLine = blockStartLine + blockSize - 1; |
---|
86 | m_emulation->writeToStream(&decoder, blockStartLine, chunkEndLine); |
---|
87 | |
---|
88 | // We search between startColumn in the first line of the string and endColumn in the last |
---|
89 | // line of the string. First we calculate the position (in the string) of endColumn in the |
---|
90 | // last line of the string |
---|
91 | int endPosition; |
---|
92 | |
---|
93 | // The String that Emulator.writeToStream produces has a newline at the end, and so ends with an |
---|
94 | // empty line - we ignore that. |
---|
95 | int numberOfLinesInString = decoder.linePositions().size() - 1; |
---|
96 | if (numberOfLinesInString > 0 && endColumn > -1 ) |
---|
97 | { |
---|
98 | endPosition = decoder.linePositions().at(numberOfLinesInString - 1) + endColumn; |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | endPosition = string.size(); |
---|
103 | } |
---|
104 | |
---|
105 | // So now we can log for m_regExp in the string between startColumn and endPosition |
---|
106 | int matchStart; |
---|
107 | if (m_forwards) |
---|
108 | { |
---|
109 | matchStart = string.indexOf(m_regExp, startColumn); |
---|
110 | if (matchStart >= endPosition) |
---|
111 | matchStart = -1; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | matchStart = string.lastIndexOf(m_regExp, endPosition - 1); |
---|
116 | if (matchStart < startColumn) |
---|
117 | matchStart = -1; |
---|
118 | } |
---|
119 | |
---|
120 | if (matchStart > -1) |
---|
121 | { |
---|
122 | int matchEnd = matchStart + m_regExp.matchedLength() - 1; |
---|
123 | qDebug() << "Found in string from" << matchStart << "to" << matchEnd; |
---|
124 | |
---|
125 | // Translate startPos and endPos to startColum, startLine, endColumn and endLine in history. |
---|
126 | int startLineNumberInString = findLineNumberInString(decoder.linePositions(), matchStart); |
---|
127 | m_foundStartColumn = matchStart - decoder.linePositions().at(startLineNumberInString); |
---|
128 | m_foundStartLine = startLineNumberInString + startLine + linesRead; |
---|
129 | |
---|
130 | int endLineNumberInString = findLineNumberInString(decoder.linePositions(), matchEnd); |
---|
131 | m_foundEndColumn = matchEnd - decoder.linePositions().at(endLineNumberInString); |
---|
132 | m_foundEndLine = endLineNumberInString + startLine + linesRead; |
---|
133 | |
---|
134 | qDebug() << "m_foundStartColumn" << m_foundStartColumn |
---|
135 | << "m_foundStartLine" << m_foundEndLine |
---|
136 | << "m_foundEndColumn" << m_foundEndColumn |
---|
137 | << "m_foundEndLine" << m_foundEndLine; |
---|
138 | |
---|
139 | return true; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | linesRead += blockSize; |
---|
144 | } |
---|
145 | |
---|
146 | qDebug() << "Not found"; |
---|
147 | return false; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | int HistorySearch::findLineNumberInString(QList<int> linePositions, int position) { |
---|
152 | int lineNum = 0; |
---|
153 | while (lineNum + 1 < linePositions.size() && linePositions[lineNum + 1] <= position) |
---|
154 | lineNum++; |
---|
155 | |
---|
156 | return lineNum; |
---|
157 | } |
---|