Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/shell.cc @ 5092

Last change on this file since 5092 was 5092, checked in by bensch, 20 years ago

orxonox/trunk: less output, and shell also outputs to shell if debug-mode ≥3

File size: 6.7 KB
Line 
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: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell.h"
19
20#include "text_engine.h"
21#include "list.h"
22
23#include <stdarg.h>
24#include <stdio.h>
25
26using namespace std;
27
28
29/**
30 * standard constructor
31 */
32Shell::Shell ()
33{
34  this->setClassID(CL_SHELL, "Shell");
35  this->setName("Shell");
36
37  this->buffer = new tList<char>;
38
39  this->textSize = 10;
40
41  //this->bufferSize = 0;
42  this->bufferText = NULL;
43  this->setBufferSize(100);
44  this->setBufferDisplaySize(10);
45  this->setAbsCoor2D(2, 400);
46  this->setAbsDir2D(0);
47
48  this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 255, 0, 0);
49  this->inputLineText->setText(NULL);
50}
51
52Shell* Shell::singletonRef = NULL;
53
54/**
55 * standard deconstructor
56 */
57Shell::~Shell ()
58{
59  // delete what has to be deleted here
60  for (int i = 0; i < this->bufferDisplaySize; i++)
61    delete this->bufferText[i];
62  delete this->bufferText;
63  delete this->inputLineText;
64
65
66  Shell::singletonRef = NULL;
67}
68
69/**
70 * sets The count of Lines to display in the buffer.
71 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
72 */
73void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
74{
75  if (this->bufferText != NULL)
76  {
77    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
78      delete this->bufferText[i];
79    delete this->bufferText;
80  }
81
82  this->bufferText = new Text*[bufferDisplaySize];
83  for (unsigned int i = 0; i < bufferDisplaySize; i++)
84  {
85    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/earth.ttf", this->textSize, TEXT_DYNAMIC, 255, 0, 0);
86    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
87    this->bufferText[i]->setRelCoor2D(5, 12*i);
88    this->bufferText[i]->setText(NULL);
89    this->bufferText[i]->setParent2D(this);
90  }
91
92
93  this->bufferDisplaySize = bufferDisplaySize;
94}
95
96/**
97 * deletes all the Buffers
98 */
99void Shell::flushBuffers()
100{
101  // remove all chars from the BufferTexts.
102  if (this->bufferText)
103    for (int i; i < this->bufferDisplaySize; i++)
104    {
105      this->bufferText[i]->setText(NULL);
106    }
107
108
109  // delete all the Chars in the Buffers
110  tIterator<char>* charIterator = this->buffer->getIterator();
111  char* charElem = charIterator->nextElement();
112
113  while (charElem != NULL)
114  {
115    delete charElem;
116
117    charElem = charIterator->nextElement();
118  }
119  delete charIterator;
120}
121
122/**
123 * adds a new Line to the List of Buffers
124 * @param line the Line as in the first argument in printf
125 * @param args the arguments as a va_list
126 *
127 * @todo optimize
128 */
129bool Shell::addBufferLineStatic(const char* line, ...)
130{
131  va_list arguments;
132  va_start(arguments, line);
133
134#if DEBUG < 3
135  if (Shell::singletonRef == NULL)
136#endif
137
138  vprintf(line, arguments);
139#if DEBUG < 3
140  else
141#else
142  if (Shell::singletonRef != NULL)
143#endif
144    Shell::singletonRef->addBufferLine(line, arguments);
145  return true;
146}
147int curr = 0;
148
149/**
150 * add a Line to the List of Buffers
151 * @param line
152 * @param arguments
153 *
154 * This function Adds one line to the buffer.
155 * and displays the line as the First Line of the display-buffer
156 */
157void Shell::addBufferLine(const char* line, va_list arguments)
158{
159   vsprintf(this->bufferArray, line, arguments);
160
161   char* newLine = new char[strlen(this->bufferArray)+1];
162   strcpy(newLine, this->bufferArray);
163
164   this->buffer->add(newLine);
165
166   if (this->buffer->getSize() > this->bufferSize)
167   {
168     delete this->buffer->firstElement();
169     this->buffer->remove(this->buffer->firstElement());
170   }
171
172   if (likely(bufferText != NULL))
173   {
174     Text* moveText = this->bufferText[this->bufferDisplaySize-1];
175     for (int i = this->bufferDisplaySize-1; i > 0; i--)
176     {
177       this->bufferText[i] = this->bufferText[i-1];
178     }
179     this->bufferText[0] = moveText;
180   }
181   this->bufferText[0]->setText(newLine);
182   // this->bufferText->
183//  this->inputLineText->setText(newLine);
184}
185
186/**
187 * moves the buffer around lineCount lines upwards (negative values move down)
188 * @param lineCount the Count of lines to move upwards
189 *
190 * @todo
191 */
192void Shell::moveBuffer(int lineCount)
193{
194}
195
196/**
197 * @param lineNumber the n-th line from the bottom
198 * @returns the Buffer at Line lineNumber
199 */
200const char* Shell::getBufferLine(unsigned int lineNumber)
201{
202  tIterator<char>* charIterator = this->buffer->getIterator();
203  char* charElem = charIterator->nextElement();
204
205  int i = 1;
206  while (charElem != NULL)
207  {
208    if (i++ < lineNumber)
209    {
210      delete charIterator;
211      return charElem;
212    }
213
214    charElem = charIterator->nextElement();
215  }
216  delete charIterator;
217}
218
219
220/**
221 * deletes the InputLine
222 */
223void Shell::flushInputLine()
224{
225  if (likely(this->inputLine != NULL))
226  {
227    delete [] this->inputLine;
228  }
229  this->inputLine = new char[1];
230  *this->inputLine = '\0';
231
232}
233
234/**
235 * adds one character to the inputLine
236 * @param character the character to add to the inputLine
237 */
238void Shell::addCharacter(char character)
239{
240  char* addCharLine = new char[strlen(inputLine)+2];
241
242  sprintf(addCharLine, "%s%c", this->inputLine, character);
243  delete this->inputLine;
244  this->inputLine = addCharLine;
245}
246
247/**
248 * adds multiple Characters to thr inputLine
249 * @param characters a '\0' terminated char-array to add to the InputLine
250 */
251void Shell::addCharacters(const char* characters)
252{
253  char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1];
254
255  sprintf(addCharLine, "%s%s", this->inputLine, characters);
256  delete this->inputLine;
257  this->inputLine = addCharLine;
258}
259
260/**
261 * removes characterCount characters from the InputLine
262 * @param characterCount the count of Characters to remove from the input Line
263 */
264void Shell::removeCharacters(unsigned int characterCount)
265{
266  if (characterCount > strlen(this->inputLine))
267    characterCount = strlen(this->inputLine);
268
269  char* removeCharLine = new char[strlen(inputLine)-characterCount+1];
270
271  strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount);
272  delete this->inputLine;
273  this->inputLine = removeCharLine;
274}
275
276/**
277 * listens for some event
278 * @param event the Event happened
279 */
280void Shell::process(const Event &event)
281{
282//  if (event.type)
283
284}
285
286/**
287 * ticks the Shell for dt Seconds
288 * @param dt the elapsed time since the last tick();
289 */
290//void Shell::tick(float dt)
291//{
292//}
293
294/**
295 * displays the Shell
296 */
297void Shell::draw() const
298{
299}
300
301
302/**
303 * autocompletes the Shell's inputLine
304 * @returns true, if a result was found, false otherwise
305 */
306bool Shell::autoComplete()
307{
308
309}
310
311/**
312 * displays some nice output from the Shell
313 */
314void Shell::debug() const
315{
316
317}
Note: See TracBrowser for help on using the repository browser.