Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more stl in Shell

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