Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellBuffer is now ThreadSafe for printing to it
I must say: SDL_Thread's are way easier than simple pThreads

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_
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 * 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 * 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 * 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 * 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 * deletes all the Buffers
89 */
90void ShellBuffer::flush()
91{
92  // delete all the Chars in the Buffers
93  list<char*>::iterator bufferLine;
94  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
95  {
96    delete[] (*bufferLine);
97  }
98  this->buffer.clear();
99}
100
101/**
102 * adds a new Line to the List of Buffers
103 * @param line the Line as in the first argument in printf
104 */
105bool ShellBuffer::addBufferLineStatic(const char* line, ...)
106{
107  va_list arguments;
108  va_start(arguments, line);
109
110  if (ShellBuffer::bufferMutex == NULL)
111    ShellBuffer::bufferMutex = SDL_CreateMutex();
112
113  SDL_mutexP(ShellBuffer::bufferMutex);
114#if DEBUG < 3
115  if (ShellBuffer::singletonRef == NULL)
116#endif
117    vprintf(line, arguments);
118#if DEBUG < 3
119  else
120#else
121  if (ShellBuffer::singletonRef != NULL)
122#endif
123    ShellBuffer::singletonRef->addBufferLine(line, arguments);
124  SDL_mutexV(ShellBuffer::bufferMutex);
125  return true;
126}
127
128/**
129 * add a Line to the List of Buffers
130 * @param line
131 * @param arguments
132 *
133 * This function Adds one line to the buffer.
134 * and displays the line as the First Line of the display-buffer
135 */
136void ShellBuffer::addBufferLine(const char* line, va_list arguments)
137{
138  char* inputEnd;
139  char* newLineBegin;
140  char* newLineEnd;
141
142  // copy the output to the bufferArray
143  vsprintf(this->bufferArray, line, arguments);
144
145  // check if we have something left in the buffers
146  // if so, append the new String to this one.
147  if (unlikely(this->keepBuffer))
148  {
149    strcat(this->keepBufferArray, this->bufferArray);
150    inputEnd = this->keepBufferArray + strlen(this->bufferArray);
151    newLineBegin = this->keepBufferArray;
152    this->keepBuffer = false;
153  }
154  else
155  {
156    inputEnd = this->bufferArray + strlen(this->bufferArray);
157    newLineBegin = this->bufferArray;
158  }
159
160  // adding all the new Lines
161  while (newLineBegin < inputEnd)
162  {
163    newLineEnd = strchr(newLineBegin, '\n');
164    if (likely(newLineEnd != NULL && *newLineEnd == '\n'))
165      *newLineEnd = '\0';
166    else
167    {
168      unsigned int len = strlen(newLineBegin);
169      char* copyBuffer = new char[len+1];
170      strcpy(copyBuffer, newLineBegin);
171      strncpy(this->keepBufferArray, copyBuffer, len);
172      delete[] copyBuffer;
173      this->keepBufferArray[len] = '\0';
174      this->keepBuffer = true;
175      break;
176    }
177
178    char* addLine = new char[strlen(newLineBegin)+1];
179    strcpy(addLine, newLineBegin);
180
181    this->lineCount++;
182    this->buffer.push_back(addLine);
183    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
184      this->shell->printToDisplayBuffer(addLine);
185
186    if (this->buffer.size() > this->bufferSize)
187    {
188      delete[] this->buffer.front();
189      this->buffer.pop_front();
190    }
191
192    newLineBegin = newLineEnd+1;
193  }
194}
195
196/**
197 * displays some nice output from the Shell
198 */
199void ShellBuffer::debug() const
200{
201  PRINT(3)("Debugging output to console (not this shell)\n");
202
203  list<char*>::const_iterator bufferLine;
204  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
205    printf(*bufferLine);
206}
Note: See TracBrowser for help on using the repository browser.