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
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
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[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
[1856]24using namespace std;
[1853]25
[1856]26
[3245]27/**
[4838]28 * standard constructor
[5254]29 */
[5173]30ShellBuffer::ShellBuffer ()
[3365]31{
[5175]32  ShellBuffer::singletonRef = this;
[5206]33  this->shell = NULL;
[5175]34
35  this->lineCount = 0;
[5174]36  this->keepBufferArray[0] = '\0';
[5215]37  this->bufferArray[0] = '\0';
[5174]38  this->keepBuffer = false;
[4320]39
[5177]40  this->setBufferSize(100);
[3365]41}
[1853]42
[5174]43ShellBuffer* ShellBuffer::singletonRef = NULL;
[7314]44SDL_mutex* ShellBuffer::bufferMutex = NULL;
[1853]45
[3245]46/**
[4838]47 * standard deconstructor
[5254]48 */
[5173]49ShellBuffer::~ShellBuffer ()
[3543]50{
[5206]51  if (this->shell != NULL)
52    delete this->shell;
[5174]53
[5246]54  this->flush();
[5174]55
[7314]56  if (ShellBuffer::bufferMutex != NULL)
57    SDL_DestroyMutex(ShellBuffer::bufferMutex);
58  ShellBuffer::bufferMutex = NULL;
59
[5174]60  ShellBuffer::singletonRef = NULL;
[3543]61}
[5174]62
63/**
[5206]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/**
[5174]88 * deletes all the Buffers
89 */
[5246]90void ShellBuffer::flush()
[5174]91{
92  // delete all the Chars in the Buffers
[5781]93  list<char*>::iterator bufferLine;
94  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
[5174]95  {
[5781]96    delete[] (*bufferLine);
[5174]97  }
[5783]98  this->buffer.clear();
[5174]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
[7314]110  if (ShellBuffer::bufferMutex == NULL)
111    ShellBuffer::bufferMutex = SDL_CreateMutex();
112
113  SDL_mutexP(ShellBuffer::bufferMutex);
[5174]114#if DEBUG < 3
115  if (ShellBuffer::singletonRef == NULL)
116#endif
[5183]117    vprintf(line, arguments);
[5174]118#if DEBUG < 3
119  else
120#else
121  if (ShellBuffer::singletonRef != NULL)
122#endif
123    ShellBuffer::singletonRef->addBufferLine(line, arguments);
[7314]124  SDL_mutexV(ShellBuffer::bufferMutex);
[5174]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
[5290]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.
[5174]147  if (unlikely(this->keepBuffer))
148  {
149    strcat(this->keepBufferArray, this->bufferArray);
[5290]150    inputEnd = this->keepBufferArray + strlen(this->bufferArray);
[5174]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
[5290]160  // adding all the new Lines
161  while (newLineBegin < inputEnd)
[5174]162  {
163    newLineEnd = strchr(newLineBegin, '\n');
[5290]164    if (likely(newLineEnd != NULL && *newLineEnd == '\n'))
[5174]165      *newLineEnd = '\0';
166    else
167    {
[5215]168      unsigned int len = strlen(newLineBegin);
[5290]169      char* copyBuffer = new char[len+1];
170      strcpy(copyBuffer, newLineBegin);
171      strncpy(this->keepBufferArray, copyBuffer, len);
172      delete[] copyBuffer;
[5215]173      this->keepBufferArray[len] = '\0';
[5174]174      this->keepBuffer = true;
175      break;
176    }
177
178    char* addLine = new char[strlen(newLineBegin)+1];
179    strcpy(addLine, newLineBegin);
180
[5176]181    this->lineCount++;
[5781]182    this->buffer.push_back(addLine);
[5206]183    if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
184      this->shell->printToDisplayBuffer(addLine);
[5174]185
[5781]186    if (this->buffer.size() > this->bufferSize)
[5174]187    {
[5781]188      delete[] this->buffer.front();
189      this->buffer.pop_front();
[5174]190    }
191
192    newLineBegin = newLineEnd+1;
193  }
194}
195
196/**
[5177]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
[5781]203  list<char*>::const_iterator bufferLine;
204  for (bufferLine = this->buffer.begin(); bufferLine != this->buffer.end(); bufferLine++)
205    printf(*bufferLine);
[5177]206}
Note: See TracBrowser for help on using the repository browser.