Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added new Files shell_completion_plugin for the new Plugin Structure.
Also created the first namespace: OrxShell

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