Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5609 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
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5246]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5173]18#include "shell_buffer.h"
[5174]19#include "debug.h"
20#include "list.h"
[5175]21#include "shell.h"
[1853]22
[5174]23#include "stdlibincl.h"
24
[1856]25using namespace std;
[1853]26
[1856]27
[3245]28/**
[4838]29 * standard constructor
[5254]30 */
[5173]31ShellBuffer::ShellBuffer ()
[3365]32{
[5175]33  ShellBuffer::singletonRef = this;
[5206]34  this->shell = NULL;
[5175]35
36  this->lineCount = 0;
[5174]37  this->keepBufferArray[0] = '\0';
[5215]38  this->bufferArray[0] = '\0';
[5174]39  this->keepBuffer = false;
40  this->buffer = new tList<char>;
[4320]41
[5177]42  this->setBufferSize(100);
[3365]43}
[1853]44
[5174]45ShellBuffer* ShellBuffer::singletonRef = NULL;
[1853]46
[3245]47/**
[4838]48 * standard deconstructor
[5254]49 */
[5173]50ShellBuffer::~ShellBuffer ()
[3543]51{
[5206]52  if (this->shell != NULL)
53    delete this->shell;
[5174]54
[5246]55  this->flush();
[5174]56  delete buffer;
57
58  ShellBuffer::singletonRef = NULL;
[3543]59}
[5174]60
61/**
[5206]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/**
[5174]86 * deletes all the Buffers
87 */
[5246]88void ShellBuffer::flush()
[5174]89{
90  // delete all the Chars in the Buffers
[5246]91  tIterator<char>* bufferIterator = this->buffer->getIterator();
[5175]92  char* charElem = bufferIterator->firstElement();
[5174]93  while (charElem != NULL)
94  {
[5210]95    delete[] charElem;
[5175]96    charElem = bufferIterator->nextElement();
[5174]97  }
[5246]98  delete bufferIterator;
[5174]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
[5183]116    vprintf(line, arguments);
[5174]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
[5290]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.
[5174]145  if (unlikely(this->keepBuffer))
146  {
147    strcat(this->keepBufferArray, this->bufferArray);
[5290]148    inputEnd = this->keepBufferArray + strlen(this->bufferArray);
[5174]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
[5290]158  // adding all the new Lines
159  while (newLineBegin < inputEnd)
[5174]160  {
161    newLineEnd = strchr(newLineBegin, '\n');
[5290]162    if (likely(newLineEnd != NULL && *newLineEnd == '\n'))
[5174]163      *newLineEnd = '\0';
164    else
165    {
[5215]166      unsigned int len = strlen(newLineBegin);
[5290]167      char* copyBuffer = new char[len+1];
168      strcpy(copyBuffer, newLineBegin);
169      strncpy(this->keepBufferArray, copyBuffer, len);
170      delete[] copyBuffer;
[5215]171      this->keepBufferArray[len] = '\0';
[5174]172      this->keepBuffer = true;
173      break;
174    }
175
176    char* addLine = new char[strlen(newLineBegin)+1];
177    strcpy(addLine, newLineBegin);
178
[5176]179    this->lineCount++;
[5174]180    this->buffer->add(addLine);
[5206]181    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
182      this->shell->printToDisplayBuffer(addLine);
[5174]183
184    if (this->buffer->getSize() > this->bufferSize)
185    {
[5210]186      delete[] this->buffer->firstElement();
[5174]187      this->buffer->remove(this->buffer->firstElement());
188    }
189
190    newLineBegin = newLineEnd+1;
191  }
192}
193
194/**
[5177]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
[5246]201  tIterator<char>* charIterator = this->buffer->getIterator();
202  char* tmpChar = charIterator->firstElement();
[5177]203  while(tmpChar != NULL)
204  {
205    printf(tmpChar);
[5246]206    tmpChar = charIterator->nextElement();
[5177]207  }
[5246]208  delete charIterator;
[5177]209}
Note: See TracBrowser for help on using the repository browser.