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 | |
---|
36 | this->lineCount = 0; |
---|
37 | this->keepBufferArray[0] = '\0'; |
---|
38 | this->keepBuffer = false; |
---|
39 | this->buffer = new tList<char>; |
---|
40 | this->bufferIterator = this->buffer->getIterator(); |
---|
41 | |
---|
42 | this->setBufferSize(100); |
---|
43 | } |
---|
44 | |
---|
45 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
46 | |
---|
47 | /** |
---|
48 | * standard deconstructor |
---|
49 | */ |
---|
50 | ShellBuffer::~ShellBuffer () |
---|
51 | { |
---|
52 | if (Shell::isInstanciated()) |
---|
53 | delete Shell::getInstance(); |
---|
54 | |
---|
55 | this->flushBuffers(); |
---|
56 | delete bufferIterator; |
---|
57 | delete buffer; |
---|
58 | |
---|
59 | ShellBuffer::singletonRef = NULL; |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * deletes all the Buffers |
---|
64 | */ |
---|
65 | void ShellBuffer::flushBuffers() |
---|
66 | { |
---|
67 | // delete all the Chars in the Buffers |
---|
68 | char* charElem = bufferIterator->firstElement(); |
---|
69 | while (charElem != NULL) |
---|
70 | { |
---|
71 | delete charElem; |
---|
72 | charElem = bufferIterator->nextElement(); |
---|
73 | } |
---|
74 | delete this->bufferIterator; |
---|
75 | delete this->buffer; |
---|
76 | this->buffer = new tList<char>; |
---|
77 | this->bufferIterator = this->buffer->getIterator(); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * adds a new Line to the List of Buffers |
---|
82 | * @param line the Line as in the first argument in printf |
---|
83 | */ |
---|
84 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
85 | { |
---|
86 | va_list arguments; |
---|
87 | va_start(arguments, line); |
---|
88 | |
---|
89 | #if DEBUG < 3 |
---|
90 | if (ShellBuffer::singletonRef == NULL) |
---|
91 | #endif |
---|
92 | |
---|
93 | vprintf(line, arguments); |
---|
94 | #if DEBUG < 3 |
---|
95 | else |
---|
96 | #else |
---|
97 | if (ShellBuffer::singletonRef != NULL) |
---|
98 | #endif |
---|
99 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
100 | return true; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * add a Line to the List of Buffers |
---|
105 | * @param line |
---|
106 | * @param arguments |
---|
107 | * |
---|
108 | * This function Adds one line to the buffer. |
---|
109 | * and displays the line as the First Line of the display-buffer |
---|
110 | */ |
---|
111 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
112 | { |
---|
113 | vsprintf(this->bufferArray, line, arguments); |
---|
114 | |
---|
115 | char* inputEnd; |
---|
116 | char* newLineBegin; |
---|
117 | char* newLineEnd; |
---|
118 | |
---|
119 | // check if we have something left in the buffers |
---|
120 | if (unlikely(this->keepBuffer)) |
---|
121 | { |
---|
122 | strcat(this->keepBufferArray, this->bufferArray); |
---|
123 | inputEnd = this->keepBufferArray + strlen(this->keepBufferArray); |
---|
124 | newLineBegin = this->keepBufferArray; |
---|
125 | this->keepBuffer = false; |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
130 | newLineBegin = this->bufferArray; |
---|
131 | } |
---|
132 | |
---|
133 | // adding all the new Lines |
---|
134 | while (newLineBegin < inputEnd) |
---|
135 | { |
---|
136 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
137 | if (newLineEnd != NULL && *newLineEnd == '\n') |
---|
138 | *newLineEnd = '\0'; |
---|
139 | else |
---|
140 | { |
---|
141 | // newLineEnd = newLineBegin + strlen(newLineBegin); |
---|
142 | strcpy(this->keepBufferArray, newLineBegin); |
---|
143 | this->keepBuffer = true; |
---|
144 | break; |
---|
145 | } |
---|
146 | |
---|
147 | char* addLine = new char[strlen(newLineBegin)+1]; |
---|
148 | strcpy(addLine, newLineBegin); |
---|
149 | |
---|
150 | this->lineCount++; |
---|
151 | this->buffer->add(addLine); |
---|
152 | if (Shell::isInstanciated() && Shell::getInstance()->isActive()) |
---|
153 | Shell::getInstance()->printToDisplayBuffer(addLine); |
---|
154 | |
---|
155 | if (this->buffer->getSize() > this->bufferSize) |
---|
156 | { |
---|
157 | delete this->buffer->firstElement(); |
---|
158 | this->buffer->remove(this->buffer->firstElement()); |
---|
159 | } |
---|
160 | |
---|
161 | newLineBegin = newLineEnd+1; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * moves the buffer around lineCount lines upwards (negative values move down) |
---|
167 | * @param lineCount the Count of lines to move upwards |
---|
168 | * |
---|
169 | * @todo |
---|
170 | */ |
---|
171 | void ShellBuffer::moveBuffer(unsigned int lineCount) |
---|
172 | { |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * @param lineNumber the n-th line from the bottom |
---|
177 | * @returns the Buffer at Line lineNumber |
---|
178 | */ |
---|
179 | const char* ShellBuffer::getBufferLine(unsigned int lineNumber) |
---|
180 | { |
---|
181 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
182 | char* charElem = charIterator->firstElement(); |
---|
183 | |
---|
184 | int i = 1; |
---|
185 | while (charElem != NULL) |
---|
186 | { |
---|
187 | if (i++ < lineNumber) |
---|
188 | { |
---|
189 | delete charIterator; |
---|
190 | return charElem; |
---|
191 | } |
---|
192 | |
---|
193 | charElem = charIterator->nextElement(); |
---|
194 | } |
---|
195 | delete charIterator; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * displays some nice output from the Shell |
---|
200 | */ |
---|
201 | void ShellBuffer::debug() const |
---|
202 | { |
---|
203 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
204 | |
---|
205 | char* tmpChar = bufferIterator->firstElement(); |
---|
206 | while(tmpChar != NULL) |
---|
207 | { |
---|
208 | printf(tmpChar); |
---|
209 | tmpChar = bufferIterator->nextElement(); |
---|
210 | } |
---|
211 | } |
---|