Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some very little todo's…, and Names in all files

File size: 4.6 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  vsprintf(this->bufferArray, line, arguments);
137
138  char* inputEnd;
139  char* newLineBegin;
140  char* newLineEnd;
141
142   // check if we have something left in the buffers
143  if (unlikely(this->keepBuffer))
144  {
145    strcat(this->keepBufferArray, this->bufferArray);
146    inputEnd = this->keepBufferArray + strlen(this->keepBufferArray);
147    newLineBegin = this->keepBufferArray;
148    this->keepBuffer = false;
149  }
150  else
151  {
152    inputEnd = this->bufferArray + strlen(this->bufferArray);
153    newLineBegin = this->bufferArray;
154  }
155
156   // adding all the new Lines
[5212]157  while (newLineBegin < inputEnd )
[5174]158  {
159    newLineEnd = strchr(newLineBegin, '\n');
160    if (newLineEnd != NULL && *newLineEnd == '\n')
161      *newLineEnd = '\0';
162    else
163    {
164//       newLineEnd = newLineBegin + strlen(newLineBegin);
[5215]165      unsigned int len = strlen(newLineBegin);
166      strncpy(this->keepBufferArray, newLineBegin, len);
167      this->keepBufferArray[len] = '\0';
[5174]168      this->keepBuffer = true;
169      break;
170    }
171
172    char* addLine = new char[strlen(newLineBegin)+1];
173    strcpy(addLine, newLineBegin);
174
[5176]175    this->lineCount++;
[5174]176    this->buffer->add(addLine);
[5206]177    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
178      this->shell->printToDisplayBuffer(addLine);
[5174]179
180    if (this->buffer->getSize() > this->bufferSize)
181    {
[5210]182      delete[] this->buffer->firstElement();
[5174]183      this->buffer->remove(this->buffer->firstElement());
184    }
185
186    newLineBegin = newLineEnd+1;
187  }
188}
189
190/**
[5177]191 * displays some nice output from the Shell
192 */
193void ShellBuffer::debug() const
194{
195  PRINT(3)("Debugging output to console (not this shell)\n");
196
[5246]197  tIterator<char>* charIterator = this->buffer->getIterator();
198  char* tmpChar = charIterator->firstElement();
[5177]199  while(tmpChar != NULL)
200  {
201    printf(tmpChar);
[5246]202    tmpChar = charIterator->nextElement();
[5177]203  }
[5246]204  delete charIterator;
[5177]205}
Note: See TracBrowser for help on using the repository browser.