Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Font for the Shell… this one is really readeable, and from StarTrek

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