Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellBuffer with STD::STRING… hope it works

File size: 4.1 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
20#include <stdarg.h>
21
22#include "debug.h"
23#include "shell.h"
24#include "lib/util/threading.h"
25
26namespace OrxShell
27{
28  /**
29   * @brief standard constructor
30   */
31  ShellBuffer::ShellBuffer ()
32  {
33    ShellBuffer::singletonRef = this;
34    this->shell = NULL;
35
36    this->lineCount = 0;
37    this->bufferArray[0] = '\0';
38
39    this->setBufferSize(100);
40  }
41
42  ShellBuffer* ShellBuffer::singletonRef = NULL;
43  std::list<std::string> ShellBuffer::buffer;
44
45  /**
46   * @brief standard deconstructor
47   */
48  ShellBuffer::~ShellBuffer ()
49  {
50    if (this->shell != NULL)
51      delete this->shell;
52
53    ShellBuffer::singletonRef = NULL;
54  }
55
56  /**
57   * @brief registers the Shell to the Buffer
58   * @param shell the Shell to register.
59   */
60  void ShellBuffer::registerShell(Shell* shell)
61  {
62    if (this->shell == NULL)
63      this->shell = shell;
64    else
65      PRINTF(1)("already registered a Shell to the ShellBuffer\n");
66  }
67
68  /**
69   * @brief unregisters the Shell from the Buffer
70   * @param shell the Shell to unregister.
71   */
72  void ShellBuffer::unregisterShell(Shell* shell)
73  {
74    if (this->shell == shell)
75      this->shell = NULL;
76    else
77      PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n");
78  }
79
80  /**
81   * @brief deletes all the Buffers
82   */
83  void ShellBuffer::flush()
84  {
85    this->buffer.clear();
86  }
87
88  /**
89   * @brief adds a new Line to the List of Buffers
90   * @param line the Line as in the first argument in printf
91   */
92  bool ShellBuffer::addBufferLineStatic(const char* line, ...)
93  {
94    va_list arguments;
95    va_start(arguments, line);
96
97    static OrxThread::Mutex ShellBuffer__bufferMutex;
98
99    OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex);
100#if DEBUG_LEVEL < 3
101    if (ShellBuffer::singletonRef == NULL)
102#endif
103      vprintf(line, arguments);
104#if DEBUG_LEVEL < 3
105    else
106#else
107    if (ShellBuffer::singletonRef != NULL)
108#endif
109      ShellBuffer::singletonRef->addBufferLine(line, arguments);
110    return true;
111  }
112
113  /**
114   * @brief add a Line to the List of Buffers
115   * @param line
116   * @param arguments
117   *
118   * This function Adds one line to the buffer.
119   * and displays the line as the First Line of the display-buffer
120   */
121  void ShellBuffer::addBufferLine(const char* line, va_list arguments)
122  {
123    // copy the output to the bufferArray
124    vsprintf(this->bufferArray, line, arguments);
125
126    std::string inputBuffer = this->keepBuffer + this->bufferArray;
127
128    int lineBegin = 0;
129    int lineEnd = 0;
130    // adding all the new Lines
131    while (lineEnd < inputBuffer.size())
132    {
133      lineBegin = lineEnd;
134      lineEnd = inputBuffer.find('\n', (lineBegin == 0) ? 0: ++lineBegin);
135      if (likely(lineEnd != std::string::npos ))
136      {
137        this->lineCount++;
138
139        printf("Test:: %s \n", inputBuffer.substr(lineBegin, lineEnd - lineBegin).c_str());
140        this->buffer.push_front(inputBuffer.substr(lineBegin, lineEnd - lineBegin));
141        if (likely (this->shell != NULL) && unlikely (this->shell->isActive()))
142          this->shell->printToDisplayBuffer(this->buffer.front());
143      }
144      else      // No end of Line reached.
145      {
146        this->keepBuffer = inputBuffer.substr(lineBegin, inputBuffer.size() - lineBegin);
147        break;
148      }
149
150
151
152      if (this->buffer.size() > this->bufferSize)
153        this->buffer.pop_back();
154    }
155  }
156
157  /**
158   * @brief displays some nice output from the Shell
159   */
160  void ShellBuffer::debug() const
161  {
162    PRINT(3)("Debugging output to console (not this shell)\n");
163
164    std::list<std::string>::const_iterator bufferLine;
165    for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine)
166      printf((*bufferLine).c_str());
167  }
168
169}
Note: See TracBrowser for help on using the repository browser.