Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/lib/shell/shell_buffer.cc @ 6746

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

orxonox/trunk: fixed issue with the overlaping char-arrays in the Shell's Buffer

File size: 4.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_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 */
31ShellBuffer::ShellBuffer ()
32{
33  ShellBuffer::singletonRef = this;
34  this->shell = NULL;
35
36  this->lineCount = 0;
37  this->keepBufferArray[0] = '\0';
38  this->bufferArray[0] = '\0';
39  this->keepBuffer = false;
40  this->buffer = new tList<char>;
41
42  this->setBufferSize(100);
43}
44
45ShellBuffer* ShellBuffer::singletonRef = NULL;
46
47/**
48 * standard deconstructor
49 */
50ShellBuffer::~ShellBuffer ()
51{
52  if (this->shell != NULL)
53    delete this->shell;
54
55  this->flush();
56  delete buffer;
57
58  ShellBuffer::singletonRef = NULL;
59}
60
61/**
62 * registers the Shell to the Buffer
63 * @param shell the Shell to register.
64 */
65void ShellBuffer::registerShell(Shell* shell)
66{
67  if (this->shell == NULL)
68    this->shell = shell;
69  else
70    PRINTF(1)("already registered a Shell to the ShellBuffer\n");
71}
72
73/**
74 * unregisters the Shell from the Buffer
75 * @param shell the Shell to unregister.
76 */
77void ShellBuffer::unregisterShell(Shell* shell)
78{
79  if (this->shell == shell)
80    this->shell = NULL;
81  else
82    PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n");
83}
84
85/**
86 * deletes all the Buffers
87 */
88void ShellBuffer::flush()
89{
90  // delete all the Chars in the Buffers
91  tIterator<char>* bufferIterator = this->buffer->getIterator();
92  char* charElem = bufferIterator->firstElement();
93  while (charElem != NULL)
94  {
95    delete[] charElem;
96    charElem = bufferIterator->nextElement();
97  }
98  delete bufferIterator;
99  delete this->buffer;
100  this->buffer = new tList<char>;
101}
102
103/**
104 * adds a new Line to the List of Buffers
105 * @param line the Line as in the first argument in printf
106 */
107bool ShellBuffer::addBufferLineStatic(const char* line, ...)
108{
109  va_list arguments;
110  va_start(arguments, line);
111
112#if DEBUG < 3
113  if (ShellBuffer::singletonRef == NULL)
114#endif
115
116    vprintf(line, arguments);
117#if DEBUG < 3
118  else
119#else
120  if (ShellBuffer::singletonRef != NULL)
121#endif
122    ShellBuffer::singletonRef->addBufferLine(line, arguments);
123  return true;
124}
125
126/**
127 * add a Line to the List of Buffers
128 * @param line
129 * @param arguments
130 *
131 * This function Adds one line to the buffer.
132 * and displays the line as the First Line of the display-buffer
133 */
134void ShellBuffer::addBufferLine(const char* line, va_list arguments)
135{
136  char* inputEnd;
137  char* newLineBegin;
138  char* newLineEnd;
139
140  // copy the output to the bufferArray
141  vsprintf(this->bufferArray, line, arguments);
142
143  // check if we have something left in the buffers
144  // if so, append the new String to this one.
145  if (unlikely(this->keepBuffer))
146  {
147    strcat(this->keepBufferArray, this->bufferArray);
148    inputEnd = this->keepBufferArray + strlen(this->bufferArray);
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 (likely(newLineEnd != NULL && *newLineEnd == '\n'))
163      *newLineEnd = '\0';
164    else
165    {
166      unsigned int len = strlen(newLineBegin);
167      char* copyBuffer = new char[len+1];
168      strcpy(copyBuffer, newLineBegin);
169      strncpy(this->keepBufferArray, copyBuffer, len);
170      delete[] copyBuffer;
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 * displays some nice output from the Shell
196 */
197void ShellBuffer::debug() const
198{
199  PRINT(3)("Debugging output to console (not this shell)\n");
200
201  tIterator<char>* charIterator = this->buffer->getIterator();
202  char* tmpChar = charIterator->firstElement();
203  while(tmpChar != NULL)
204  {
205    printf(tmpChar);
206    tmpChar = charIterator->nextElement();
207  }
208  delete charIterator;
209}
Note: See TracBrowser for help on using the repository browser.