Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7374 was 7374, checked in by bensch, 18 years ago

orxonox/trunk: added new Files shell_completion_plugin for the new Plugin Structure.
Also created the first namespace: OrxShell

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