Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6784 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
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"
[5175]20#include "shell.h"
[1853]21
[5174]22#include "stdlibincl.h"
23
[1856]24using namespace std;
[1853]25
[1856]26
[3245]27/**
[4838]28 * standard constructor
[5254]29 */
[5173]30ShellBuffer::ShellBuffer ()
[3365]31{
[5175]32  ShellBuffer::singletonRef = this;
[5206]33  this->shell = NULL;
[5175]34
35  this->lineCount = 0;
[5174]36  this->keepBufferArray[0] = '\0';
[5215]37  this->bufferArray[0] = '\0';
[5174]38  this->keepBuffer = false;
[4320]39
[5177]40  this->setBufferSize(100);
[3365]41}
[1853]42
[5174]43ShellBuffer* ShellBuffer::singletonRef = NULL;
[1853]44
[3245]45/**
[4838]46 * standard deconstructor
[5254]47 */
[5173]48ShellBuffer::~ShellBuffer ()
[3543]49{
[5206]50  if (this->shell != NULL)
51    delete this->shell;
[5174]52
[5246]53  this->flush();
[5174]54
55  ShellBuffer::singletonRef = NULL;
[3543]56}
[5174]57
58/**
[5206]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/**
[5174]83 * deletes all the Buffers
84 */
[5246]85void ShellBuffer::flush()
[5174]86{
87  // delete all the Chars in the Buffers
[5781]88  list<char*>::iterator bufferLine;
89  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
[5174]90  {
[5781]91    delete[] (*bufferLine);
[5174]92  }
[5783]93  this->buffer.clear();
[5174]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
[5183]109    vprintf(line, arguments);
[5174]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
[5290]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.
[5174]138  if (unlikely(this->keepBuffer))
139  {
140    strcat(this->keepBufferArray, this->bufferArray);
[5290]141    inputEnd = this->keepBufferArray + strlen(this->bufferArray);
[5174]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
[5290]151  // adding all the new Lines
152  while (newLineBegin < inputEnd)
[5174]153  {
154    newLineEnd = strchr(newLineBegin, '\n');
[5290]155    if (likely(newLineEnd != NULL && *newLineEnd == '\n'))
[5174]156      *newLineEnd = '\0';
157    else
158    {
[5215]159      unsigned int len = strlen(newLineBegin);
[5290]160      char* copyBuffer = new char[len+1];
161      strcpy(copyBuffer, newLineBegin);
162      strncpy(this->keepBufferArray, copyBuffer, len);
163      delete[] copyBuffer;
[5215]164      this->keepBufferArray[len] = '\0';
[5174]165      this->keepBuffer = true;
166      break;
167    }
168
169    char* addLine = new char[strlen(newLineBegin)+1];
170    strcpy(addLine, newLineBegin);
171
[5176]172    this->lineCount++;
[5781]173    this->buffer.push_back(addLine);
[5206]174    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
175      this->shell->printToDisplayBuffer(addLine);
[5174]176
[5781]177    if (this->buffer.size() > this->bufferSize)
[5174]178    {
[5781]179      delete[] this->buffer.front();
180      this->buffer.pop_front();
[5174]181    }
182
183    newLineBegin = newLineEnd+1;
184  }
185}
186
187/**
[5177]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
[5781]194  list<char*>::const_iterator bufferLine;
195  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
196    printf(*bufferLine);
[5177]197}
Note: See TracBrowser for help on using the repository browser.