Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

File size: 4.4 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 "shell.h"
21
22#include "stdlibincl.h"
23
24using namespace std;
25
26
27/**
28 * standard constructor
29 */
30ShellBuffer::ShellBuffer ()
31{
32  ShellBuffer::singletonRef = this;
33  this->shell = NULL;
34
35  this->lineCount = 0;
36  this->keepBufferArray[0] = '\0';
37  this->bufferArray[0] = '\0';
38  this->keepBuffer = false;
39
40  this->setBufferSize(100);
41}
42
43ShellBuffer* ShellBuffer::singletonRef = NULL;
44
45/**
46 * standard deconstructor
47 */
48ShellBuffer::~ShellBuffer ()
49{
50  if (this->shell != NULL)
51    delete this->shell;
52
53  this->flush();
54
55  ShellBuffer::singletonRef = NULL;
56}
57
58/**
59 * registers the Shell to the Buffer
60 * @param shell the Shell to register.
61 */
62void ShellBuffer::registerShell(Shell* shell)
63{
64  if (this->shell == NULL)
65    this->shell = shell;
66  else
67    PRINTF(1)("already registered a Shell to the ShellBuffer\n");
68}
69
70/**
71 * unregisters the Shell from the Buffer
72 * @param shell the Shell to unregister.
73 */
74void ShellBuffer::unregisterShell(Shell* shell)
75{
76  if (this->shell == shell)
77    this->shell = NULL;
78  else
79    PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n");
80}
81
82/**
83 * deletes all the Buffers
84 */
85void ShellBuffer::flush()
86{
87  // delete all the Chars in the Buffers
88  list<char*>::iterator bufferLine;
89  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
90  {
91    delete[] (*bufferLine);
92  }
93  this->buffer.clear();
94}
95
96/**
97 * adds a new Line to the List of Buffers
98 * @param line the Line as in the first argument in printf
99 */
100bool ShellBuffer::addBufferLineStatic(const char* line, ...)
101{
102  va_list arguments;
103  va_start(arguments, line);
104
105#if DEBUG < 3
106  if (ShellBuffer::singletonRef == NULL)
107#endif
108
109    vprintf(line, arguments);
110#if DEBUG < 3
111  else
112#else
113  if (ShellBuffer::singletonRef != NULL)
114#endif
115    ShellBuffer::singletonRef->addBufferLine(line, arguments);
116  return true;
117}
118
119/**
120 * add a Line to the List of Buffers
121 * @param line
122 * @param arguments
123 *
124 * This function Adds one line to the buffer.
125 * and displays the line as the First Line of the display-buffer
126 */
127void ShellBuffer::addBufferLine(const char* line, va_list arguments)
128{
129  char* inputEnd;
130  char* newLineBegin;
131  char* newLineEnd;
132
133  // copy the output to the bufferArray
134  vsprintf(this->bufferArray, line, arguments);
135
136  // check if we have something left in the buffers
137  // if so, append the new String to this one.
138  if (unlikely(this->keepBuffer))
139  {
140    strcat(this->keepBufferArray, this->bufferArray);
141    inputEnd = this->keepBufferArray + strlen(this->bufferArray);
142    newLineBegin = this->keepBufferArray;
143    this->keepBuffer = false;
144  }
145  else
146  {
147    inputEnd = this->bufferArray + strlen(this->bufferArray);
148    newLineBegin = this->bufferArray;
149  }
150
151  // adding all the new Lines
152  while (newLineBegin < inputEnd)
153  {
154    newLineEnd = strchr(newLineBegin, '\n');
155    if (likely(newLineEnd != NULL && *newLineEnd == '\n'))
156      *newLineEnd = '\0';
157    else
158    {
159      unsigned int len = strlen(newLineBegin);
160      char* copyBuffer = new char[len+1];
161      strcpy(copyBuffer, newLineBegin);
162      strncpy(this->keepBufferArray, copyBuffer, len);
163      delete[] copyBuffer;
164      this->keepBufferArray[len] = '\0';
165      this->keepBuffer = true;
166      break;
167    }
168
169    char* addLine = new char[strlen(newLineBegin)+1];
170    strcpy(addLine, newLineBegin);
171
172    this->lineCount++;
173    this->buffer.push_back(addLine);
174    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
175      this->shell->printToDisplayBuffer(addLine);
176
177    if (this->buffer.size() > this->bufferSize)
178    {
179      delete[] this->buffer.front();
180      this->buffer.pop_front();
181    }
182
183    newLineBegin = newLineEnd+1;
184  }
185}
186
187/**
188 * displays some nice output from the Shell
189 */
190void ShellBuffer::debug() const
191{
192  PRINT(3)("Debugging output to console (not this shell)\n");
193
194  list<char*>::const_iterator bufferLine;
195  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
196    printf(*bufferLine);
197}
Note: See TracBrowser for help on using the repository browser.