Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_buffer.cc @ 5212

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

orxonox/trunk: some little errors corrected via Valgrind

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