Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model_old/src/lib/shell/shell_buffer.cc @ 8462

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

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

File size: 4.5 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_
17
18#include "shell_buffer.h"
19#include "debug.h"
20#include "shell.h"
21
22#include "stdlibincl.h"
23
24using namespace std;
25
26
27/**
28 * @brief standard constructor
29 */
30ShellBuffer::ShellBuffer ()
31{
32  ShellBuffer::singletonRef = this;
33  this->shell = NULL;
34
35  this->lineCount = 0;
36  this->keepBufferArray[0] = '\0';
37  this->bufferArray[0] = '\0';
38  this->keepBuffer = false;
39
40  this->setBufferSize(100);
41}
42
43ShellBuffer* ShellBuffer::singletonRef = NULL;
44SDL_mutex* ShellBuffer::bufferMutex = NULL;
45
46/**
47 * @brief standard deconstructor
48 */
49ShellBuffer::~ShellBuffer ()
50{
51  if (this->shell != NULL)
52    delete this->shell;
53
54  this->flush();
55
56  if (ShellBuffer::bufferMutex != NULL)
57    SDL_DestroyMutex(ShellBuffer::bufferMutex);
58  ShellBuffer::bufferMutex = NULL;
59
60  ShellBuffer::singletonRef = NULL;
61}
62
63/**
64 * @brief registers the Shell to the Buffer
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/**
76 * @brief unregisters the Shell from the Buffer
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/**
88 * @brief deletes all the Buffers
89 */
90void ShellBuffer::flush()
91{
92  this->buffer.clear();
93}
94
95/**
96 * @brief adds a new Line to the List of Buffers
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
104  if (ShellBuffer::bufferMutex == NULL)
105    ShellBuffer::bufferMutex = SDL_CreateMutex();
106
107  SDL_mutexP(ShellBuffer::bufferMutex);
108#if DEBUG < 3
109  if (ShellBuffer::singletonRef == NULL)
110#endif
111    vprintf(line, arguments);
112#if DEBUG < 3
113  else
114#else
115  if (ShellBuffer::singletonRef != NULL)
116#endif
117    ShellBuffer::singletonRef->addBufferLine(line, arguments);
118  SDL_mutexV(ShellBuffer::bufferMutex);
119  return true;
120}
121
122/**
123 * @brief add a Line to the List of Buffers
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
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.
141  if (unlikely(this->keepBuffer))
142  {
143    strcat(this->keepBufferArray, this->bufferArray);
144    inputEnd = this->keepBufferArray + strlen(this->bufferArray);
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
154  // adding all the new Lines
155  while (newLineBegin < inputEnd)
156  {
157    newLineEnd = strchr(newLineBegin, '\n');
158    if (likely(newLineEnd != NULL && *newLineEnd == '\n'))
159      *newLineEnd = '\0';
160    else
161    {
162      unsigned int len = strlen(newLineBegin);
163      char* copyBuffer = new char[len+1];
164      strcpy(copyBuffer, newLineBegin);
165      strncpy(this->keepBufferArray, copyBuffer, len);
166      delete[] copyBuffer;
167      this->keepBufferArray[len] = '\0';
168      this->keepBuffer = true;
169      break;
170    }
171
172    this->lineCount++;
173    this->buffer.push_back(newLineBegin);
174    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
175      this->shell->printToDisplayBuffer(newLineBegin);
176
177    if (this->buffer.size() > this->bufferSize)
178      this->buffer.pop_front();
179
180    newLineBegin = newLineEnd+1;
181  }
182}
183
184/**
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
191  list<std::string>::const_iterator bufferLine;
192  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
193    printf((*bufferLine).c_str());
194}
Note: See TracBrowser for help on using the repository browser.