Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minimalized the ShellBuffer

File size: 4.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_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->bufferArray[0] = '\0';
40  this->keepBuffer = false;
41  this->buffer = new tList<char>;
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->flush();
57  delete buffer;
58
59  ShellBuffer::singletonRef = NULL;
60}
61
62/**
63 * registers the Shell to the Buffer
64 * @param shell the Shell to register.
65 */
66void ShellBuffer::registerShell(Shell* shell)
67{
68  if (this->shell == NULL)
69    this->shell = shell;
70  else
71    PRINTF(1)("already registered a Shell to the ShellBuffer\n");
72}
73
74/**
75 * unregisters the Shell from the Buffer
76 * @param shell the Shell to unregister.
77 */
78void ShellBuffer::unregisterShell(Shell* shell)
79{
80  if (this->shell == shell)
81    this->shell = NULL;
82  else
83    PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n");
84}
85
86/**
87 * deletes all the Buffers
88 */
89void ShellBuffer::flush()
90{
91  // delete all the Chars in the Buffers
92  tIterator<char>* bufferIterator = this->buffer->getIterator();
93  char* charElem = bufferIterator->firstElement();
94  while (charElem != NULL)
95  {
96    delete[] charElem;
97    charElem = bufferIterator->nextElement();
98  }
99  delete bufferIterator;
100  delete this->buffer;
101  this->buffer = new tList<char>;
102}
103
104/**
105 * adds a new Line to the List of Buffers
106 * @param line the Line as in the first argument in printf
107 */
108bool ShellBuffer::addBufferLineStatic(const char* line, ...)
109{
110  va_list arguments;
111  va_start(arguments, line);
112
113#if DEBUG < 3
114  if (ShellBuffer::singletonRef == NULL)
115#endif
116
117    vprintf(line, arguments);
118#if DEBUG < 3
119  else
120#else
121  if (ShellBuffer::singletonRef != NULL)
122#endif
123    ShellBuffer::singletonRef->addBufferLine(line, arguments);
124  return true;
125}
126
127/**
128 * add a Line to the List of Buffers
129 * @param line
130 * @param arguments
131 *
132 * This function Adds one line to the buffer.
133 * and displays the line as the First Line of the display-buffer
134 */
135void ShellBuffer::addBufferLine(const char* line, va_list arguments)
136{
137  vsprintf(this->bufferArray, line, arguments);
138
139  char* inputEnd;
140  char* newLineBegin;
141  char* newLineEnd;
142
143   // check if we have something left in the buffers
144  if (unlikely(this->keepBuffer))
145  {
146    strcat(this->keepBufferArray, this->bufferArray);
147    inputEnd = this->keepBufferArray + strlen(this->keepBufferArray);
148    newLineBegin = this->keepBufferArray;
149    this->keepBuffer = false;
150  }
151  else
152  {
153    inputEnd = this->bufferArray + strlen(this->bufferArray);
154    newLineBegin = this->bufferArray;
155  }
156
157   // adding all the new Lines
158  while (newLineBegin < inputEnd )
159  {
160    newLineEnd = strchr(newLineBegin, '\n');
161    if (newLineEnd != NULL && *newLineEnd == '\n')
162      *newLineEnd = '\0';
163    else
164    {
165//       newLineEnd = newLineBegin + strlen(newLineBegin);
166      unsigned int len = strlen(newLineBegin);
167      strncpy(this->keepBufferArray, newLineBegin, len);
168      this->keepBufferArray[len] = '\0';
169      this->keepBuffer = true;
170      break;
171    }
172
173    char* addLine = new char[strlen(newLineBegin)+1];
174    strcpy(addLine, newLineBegin);
175
176    this->lineCount++;
177    this->buffer->add(addLine);
178    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
179      this->shell->printToDisplayBuffer(addLine);
180
181    if (this->buffer->getSize() > this->bufferSize)
182    {
183      delete[] this->buffer->firstElement();
184      this->buffer->remove(this->buffer->firstElement());
185    }
186
187    newLineBegin = newLineEnd+1;
188  }
189}
190
191/**
192 * displays some nice output from the Shell
193 */
194void ShellBuffer::debug() const
195{
196  PRINT(3)("Debugging output to console (not this shell)\n");
197
198  tIterator<char>* charIterator = this->buffer->getIterator();
199  char* tmpChar = charIterator->firstElement();
200  while(tmpChar != NULL)
201  {
202    printf(tmpChar);
203    tmpChar = charIterator->nextElement();
204  }
205  delete charIterator;
206}
Note: See TracBrowser for help on using the repository browser.