Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more stl in Shell

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