Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7487 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
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
17
18#include "shell_buffer.h"
19#include "debug.h"
20#include "shell.h"
21
22namespace OrxShell
23{
24  /**
25   * @brief standard constructor
26   */
27  ShellBuffer::ShellBuffer ()
28  {
29    ShellBuffer::singletonRef = this;
30    this->shell = NULL;
31
32    this->lineCount = 0;
33    this->keepBufferArray[0] = '\0';
34    this->bufferArray[0] = '\0';
35    this->keepBuffer = false;
36
37    this->setBufferSize(100);
38  }
39
40  ShellBuffer* ShellBuffer::singletonRef = NULL;
41  SDL_mutex* ShellBuffer::bufferMutex = NULL;
42
43  /**
44   * @brief standard deconstructor
45   */
46  ShellBuffer::~ShellBuffer ()
47  {
48    if (this->shell != NULL)
49      delete this->shell;
50
51    this->flush();
52
53    if (ShellBuffer::bufferMutex != NULL)
54      SDL_DestroyMutex(ShellBuffer::bufferMutex);
55    ShellBuffer::bufferMutex = NULL;
56
57    ShellBuffer::singletonRef = NULL;
58  }
59
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  }
71
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  }
83
84  /**
85   * @brief deletes all the Buffers
86   */
87  void ShellBuffer::flush()
88  {
89    this->buffer.clear();
90  }
91
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);
100
101    if (ShellBuffer::bufferMutex == NULL)
102      ShellBuffer::bufferMutex = SDL_CreateMutex();
103
104    SDL_mutexP(ShellBuffer::bufferMutex);
105#if DEBUG < 3
106    if (ShellBuffer::singletonRef == NULL)
107#endif
108      vprintf(line, arguments);
109#if DEBUG < 3
110    else
111#else
112    if (ShellBuffer::singletonRef != NULL)
113#endif
114      ShellBuffer::singletonRef->addBufferLine(line, arguments);
115    SDL_mutexV(ShellBuffer::bufferMutex);
116    return true;
117  }
118
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;
132
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.
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    }
145    else
146    {
147      inputEnd = this->bufferArray + strlen(this->bufferArray);
148      newLineBegin = this->bufferArray;
149    }
150
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      }
168
169      this->lineCount++;
170      this->buffer.push_back(newLineBegin);
171      if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
172        this->shell->printToDisplayBuffer(newLineBegin);
173
174      if (this->buffer.size() > this->bufferSize)
175        this->buffer.pop_front();
176
177      newLineBegin = newLineEnd+1;
178    }
179  }
180
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");
187
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
193}
Note: See TracBrowser for help on using the repository browser.