1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: ... |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "shell_buffer.h" |
---|
19 | #include "debug.h" |
---|
20 | #include "list.h" |
---|
21 | #include "shell.h" |
---|
22 | |
---|
23 | #include "stdlibincl.h" |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | |
---|
28 | /** |
---|
29 | * standard constructor |
---|
30 | * @todo this constructor is not jet implemented - do it |
---|
31 | */ |
---|
32 | ShellBuffer::ShellBuffer () |
---|
33 | { |
---|
34 | ShellBuffer::singletonRef = this; |
---|
35 | this->shell = NULL; |
---|
36 | |
---|
37 | this->lineCount = 0; |
---|
38 | this->keepBufferArray[0] = '\0'; |
---|
39 | this->bufferArray[0] = '\0'; |
---|
40 | this->keepBuffer = false; |
---|
41 | this->buffer = new tList<char>; |
---|
42 | this->bufferIterator = this->buffer->getIterator(); |
---|
43 | |
---|
44 | this->setBufferSize(100); |
---|
45 | } |
---|
46 | |
---|
47 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
48 | |
---|
49 | /** |
---|
50 | * standard deconstructor |
---|
51 | */ |
---|
52 | ShellBuffer::~ShellBuffer () |
---|
53 | { |
---|
54 | if (this->shell != NULL) |
---|
55 | delete this->shell; |
---|
56 | |
---|
57 | this->flushBuffers(); |
---|
58 | delete bufferIterator; |
---|
59 | delete buffer; |
---|
60 | |
---|
61 | ShellBuffer::singletonRef = NULL; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * registers the Shell to the Buffer |
---|
66 | * @param shell the Shell to register. |
---|
67 | */ |
---|
68 | void ShellBuffer::registerShell(Shell* shell) |
---|
69 | { |
---|
70 | if (this->shell == NULL) |
---|
71 | this->shell = shell; |
---|
72 | else |
---|
73 | PRINTF(1)("already registered a Shell to the ShellBuffer\n"); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * unregisters the Shell from the Buffer |
---|
78 | * @param shell the Shell to unregister. |
---|
79 | */ |
---|
80 | void ShellBuffer::unregisterShell(Shell* shell) |
---|
81 | { |
---|
82 | if (this->shell == shell) |
---|
83 | this->shell = NULL; |
---|
84 | else |
---|
85 | PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n"); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * deletes all the Buffers |
---|
90 | */ |
---|
91 | void ShellBuffer::flushBuffers() |
---|
92 | { |
---|
93 | // delete all the Chars in the Buffers |
---|
94 | char* charElem = bufferIterator->firstElement(); |
---|
95 | while (charElem != NULL) |
---|
96 | { |
---|
97 | delete[] charElem; |
---|
98 | charElem = bufferIterator->nextElement(); |
---|
99 | } |
---|
100 | delete this->bufferIterator; |
---|
101 | delete this->buffer; |
---|
102 | this->buffer = new tList<char>; |
---|
103 | this->bufferIterator = this->buffer->getIterator(); |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * adds a new Line to the List of Buffers |
---|
109 | * @param line the Line as in the first argument in printf |
---|
110 | */ |
---|
111 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
112 | { |
---|
113 | va_list arguments; |
---|
114 | va_start(arguments, line); |
---|
115 | |
---|
116 | #if DEBUG < 3 |
---|
117 | if (ShellBuffer::singletonRef == NULL) |
---|
118 | #endif |
---|
119 | |
---|
120 | vprintf(line, arguments); |
---|
121 | #if DEBUG < 3 |
---|
122 | else |
---|
123 | #else |
---|
124 | if (ShellBuffer::singletonRef != NULL) |
---|
125 | #endif |
---|
126 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * add a Line to the List of Buffers |
---|
132 | * @param line |
---|
133 | * @param arguments |
---|
134 | * |
---|
135 | * This function Adds one line to the buffer. |
---|
136 | * and displays the line as the First Line of the display-buffer |
---|
137 | */ |
---|
138 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
139 | { |
---|
140 | vsprintf(this->bufferArray, line, arguments); |
---|
141 | |
---|
142 | char* inputEnd; |
---|
143 | char* newLineBegin; |
---|
144 | char* newLineEnd; |
---|
145 | |
---|
146 | // check if we have something left in the buffers |
---|
147 | if (unlikely(this->keepBuffer)) |
---|
148 | { |
---|
149 | strcat(this->keepBufferArray, this->bufferArray); |
---|
150 | inputEnd = this->keepBufferArray + strlen(this->keepBufferArray); |
---|
151 | newLineBegin = this->keepBufferArray; |
---|
152 | this->keepBuffer = false; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
157 | newLineBegin = this->bufferArray; |
---|
158 | } |
---|
159 | |
---|
160 | // adding all the new Lines |
---|
161 | while (newLineBegin < inputEnd ) |
---|
162 | { |
---|
163 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
164 | if (newLineEnd != NULL && *newLineEnd == '\n') |
---|
165 | *newLineEnd = '\0'; |
---|
166 | else |
---|
167 | { |
---|
168 | // newLineEnd = newLineBegin + strlen(newLineBegin); |
---|
169 | unsigned int len = strlen(newLineBegin); |
---|
170 | strncpy(this->keepBufferArray, newLineBegin, len); |
---|
171 | this->keepBufferArray[len] = '\0'; |
---|
172 | this->keepBuffer = true; |
---|
173 | break; |
---|
174 | } |
---|
175 | |
---|
176 | char* addLine = new char[strlen(newLineBegin)+1]; |
---|
177 | strcpy(addLine, newLineBegin); |
---|
178 | |
---|
179 | this->lineCount++; |
---|
180 | this->buffer->add(addLine); |
---|
181 | if (likely (this->shell != NULL) && unlikely (this->shell->isActive())) |
---|
182 | this->shell->printToDisplayBuffer(addLine); |
---|
183 | |
---|
184 | if (this->buffer->getSize() > this->bufferSize) |
---|
185 | { |
---|
186 | delete[] this->buffer->firstElement(); |
---|
187 | this->buffer->remove(this->buffer->firstElement()); |
---|
188 | } |
---|
189 | |
---|
190 | newLineBegin = newLineEnd+1; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * moves the buffer around lineCount lines upwards (negative values move down) |
---|
196 | * @param lineCount the Count of lines to move upwards |
---|
197 | * |
---|
198 | * @todo |
---|
199 | */ |
---|
200 | void ShellBuffer::moveBuffer(unsigned int lineCount) |
---|
201 | { |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * @param lineNumber the n-th line from the bottom |
---|
206 | * @returns the Buffer at Line lineNumber |
---|
207 | */ |
---|
208 | const char* ShellBuffer::getBufferLine(unsigned int lineNumber) |
---|
209 | { |
---|
210 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
211 | char* charElem = charIterator->firstElement(); |
---|
212 | |
---|
213 | int i = 1; |
---|
214 | while (charElem != NULL) |
---|
215 | { |
---|
216 | if (i++ < lineNumber) |
---|
217 | { |
---|
218 | delete charIterator; |
---|
219 | return charElem; |
---|
220 | } |
---|
221 | |
---|
222 | charElem = charIterator->nextElement(); |
---|
223 | } |
---|
224 | delete charIterator; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * displays some nice output from the Shell |
---|
229 | */ |
---|
230 | void ShellBuffer::debug() const |
---|
231 | { |
---|
232 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
233 | |
---|
234 | char* tmpChar = bufferIterator->firstElement(); |
---|
235 | while(tmpChar != NULL) |
---|
236 | { |
---|
237 | printf(tmpChar); |
---|
238 | tmpChar = bufferIterator->nextElement(); |
---|
239 | } |
---|
240 | } |
---|