6663b6c9
adorian
projet complet av...
|
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#include "console_store.h"
#include <assert.h>
#include <string.h>
namespace Code {
ConsoleStore::ConsoleStore() :
m_history{0}
{
}
void ConsoleStore::clear() {
assert(k_historySize > 0);
m_history[0] = 0;
}
void ConsoleStore::startNewSession() {
if (k_historySize < 1) {
return;
}
m_history[0] = makePrevious(m_history[0]);
for (int i = 0; i < k_historySize - 1; i++) {
if (m_history[i] == 0) {
if (m_history[i+1] == 0) {
return ;
}
m_history[i+1] = makePrevious(m_history[i+1]);
}
}
}
ConsoleLine ConsoleStore::lineAtIndex(int i) const {
assert(i >= 0 && i < numberOfLines());
int currentLineIndex = 0;
for (int j=0; j<k_historySize; j++) {
if (m_history[j] == 0) {
currentLineIndex++;
j++;
}
if (currentLineIndex == i) {
return ConsoleLine(lineTypeForMarker(m_history[j]), m_history+j+1);
}
}
assert(false);
return ConsoleLine();
}
int ConsoleStore::numberOfLines() const {
if (m_history[0] == 0) {
return 0;
}
int result = 0;
for (int i = 0; i < k_historySize - 1; i++) {
if (m_history[i] == 0) {
result++;
if (m_history[i+1] == 0) {
return result;
}
}
}
assert(false);
return 0;
}
void ConsoleStore::pushCommand(const char * text, size_t length) {
push(CurrentSessionCommandMarker, text, length);
}
void ConsoleStore::pushResult(const char * text, size_t length) {
push(CurrentSessionResultMarker, text, length);
}
void ConsoleStore::deleteLastLineIfEmpty() {
ConsoleLine lastLine = lineAtIndex(numberOfLines()-1);
char lastLineFirstChar = lastLine.text()[0];
if (lastLineFirstChar == 0 || lastLineFirstChar == '\n') {
deleteLastLine();
}
}
int ConsoleStore::deleteCommandAndResultsAtIndex(int index) {
int numberOfLinesAtStart = numberOfLines();
assert(index >= 0 && index < numberOfLinesAtStart);
int indexOfLineToDelete = index;
while (indexOfLineToDelete < numberOfLinesAtStart - 1) {
if (lineAtIndex(indexOfLineToDelete + 1).isCommand()) {
break;
}
indexOfLineToDelete++;
}
ConsoleLine lineToDelete = lineAtIndex(indexOfLineToDelete);
while (indexOfLineToDelete > 0 && !lineAtIndex(indexOfLineToDelete).isCommand()) {
deleteLineAtIndex(indexOfLineToDelete);
indexOfLineToDelete--;
lineToDelete = lineAtIndex(indexOfLineToDelete);
}
deleteLineAtIndex(indexOfLineToDelete);
return indexOfLineToDelete;
}
void ConsoleStore::push(const char marker, const char * text, size_t length) {
size_t textLength = length;
if (ConsoleLine::sizeOfConsoleLine(length) > k_historySize - 1) {
textLength = k_historySize - 1 - 1 - 1; // Marker, null termination and null marker.
}
int i = indexOfNullMarker();
// If needed, make room for the text we want to push.
while (i + ConsoleLine::sizeOfConsoleLine(textLength) > k_historySize - 1) {
deleteFirstLine();
i = indexOfNullMarker();
}
m_history[i] = marker;
strlcpy(&m_history[i+1], text, textLength+1);
m_history[i+1+textLength+1] = 0;
}
ConsoleLine::Type ConsoleStore::lineTypeForMarker(char marker) const {
assert(marker == CurrentSessionCommandMarker || marker == CurrentSessionResultMarker || marker == PreviousSessionCommandMarker || marker == PreviousSessionResultMarker);
return static_cast<ConsoleLine::Type>(marker-1);
}
int ConsoleStore::indexOfNullMarker() const {
if (m_history[0] == 0) {
return 0;
}
for (int i=0; i<k_historySize; i++) {
if (m_history[i] == 0 && m_history[i+1] == 0) {
return (i+1);
}
}
assert(false);
return 0;
}
void ConsoleStore::deleteLineAtIndex(int index) {
assert(index >=0 && index < numberOfLines());
int currentLineIndex = 0;
for (int i = 0; i < k_historySize - 1; i++) {
if (m_history[i] == 0) {
currentLineIndex++;
continue;
}
if (currentLineIndex == index) {
int nextLineStart = i;
while (m_history[nextLineStart] != 0 && nextLineStart < k_historySize - 2) {
nextLineStart++;
}
nextLineStart++;
if (nextLineStart > k_historySize - 1) {
return;
}
memmove(&m_history[i], &m_history[nextLineStart], (k_historySize - 1) - nextLineStart + 1);
return;
}
}
}
void ConsoleStore::deleteFirstLine() {
if (m_history[0] == 0) {
return;
}
int secondLineMarkerIndex = 1;
while (m_history[secondLineMarkerIndex] != 0) {
secondLineMarkerIndex++;
}
secondLineMarkerIndex++;
for (int i=0; i<k_historySize - secondLineMarkerIndex; i++) {
m_history[i] = m_history[secondLineMarkerIndex+i];
}
}
void ConsoleStore::deleteLastLine() {
int lineCount = numberOfLines();
if (lineCount < 0) {
return;
}
if (lineCount == 1) {
deleteFirstLine();
return;
}
int currentLineIndex = 1;
int lastLineMarkerIndex = 0;
for (int i=0; i<k_historySize; i++) {
if (m_history[i] == 0) {
currentLineIndex++;
if (currentLineIndex == lineCount) {
lastLineMarkerIndex = i+1;
break;
}
}
}
m_history[lastLineMarkerIndex] = 0;
}
}
|