Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5074 was 5074, checked in by bensch, 19 years ago

Shell: the shell outputs one Line now :)

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