Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5279 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
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  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
157  while (newLineBegin < inputEnd )
158  {
159    newLineEnd = strchr(newLineBegin, '\n');
160    if (newLineEnd != NULL && *newLineEnd == '\n')
161      *newLineEnd = '\0';
162    else
163    {
164//       newLineEnd = newLineBegin + strlen(newLineBegin);
165      unsigned int len = strlen(newLineBegin);
166      strncpy(this->keepBufferArray, newLineBegin, len);
167      this->keepBufferArray[len] = '\0';
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 * 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
197  tIterator<char>* charIterator = this->buffer->getIterator();
198  char* tmpChar = charIterator->firstElement();
199  while(tmpChar != NULL)
200  {
201    printf(tmpChar);
202    tmpChar = charIterator->nextElement();
203  }
204  delete charIterator;
205}
Note: See TracBrowser for help on using the repository browser.