Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Element2D uses Vector2D instead of Vector whenever possible

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