Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/shell/shell_buffer.cc @ 8141

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

gui: better outputting in the ShellBuffer

File size: 3.3 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
35    this->lineCount = 0;
36    this->bufferArray[0] = '\0';
37
38    this->setMaxBufferSize(100);
39  }
40
41  ShellBuffer* ShellBuffer::singletonRef = NULL;
42  std::list<std::string> ShellBuffer::buffer;
43  char ShellBuffer::bufferArray[SHELL_BUFFER_SIZE];
44
45
46  /**
47   * @brief standard deconstructor
48   */
49  ShellBuffer::~ShellBuffer ()
50  {
51    ShellBuffer::singletonRef = NULL;
52  }
53
54  /**
55   * @brief deletes all the Buffers
56   */
57  void ShellBuffer::flush()
58  {
59    this->buffer.clear();
60  }
61
62  /**
63   * @brief adds a new Line to the List of Buffers
64   * @param line the Line as in the first argument in printf
65   */
66  bool ShellBuffer::addBufferLineStatic(const char* line, ...)
67  {
68    static OrxThread::Mutex ShellBuffer__bufferMutex;
69
70    OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex);
71
72    va_list arguments;
73    va_start(arguments, line);
74    vsnprintf(ShellBuffer::bufferArray, SHELL_BUFFER_SIZE, line, arguments);
75    va_end(arguments);
76
77#if DEBUG_LEVEL < 3
78    if (ShellBuffer::singletonRef == NULL)
79#endif
80      printf(ShellBuffer::bufferArray);
81#if DEBUG_LEVEL < 3
82    else
83#else
84    if (ShellBuffer::singletonRef != NULL)
85#endif
86      ShellBuffer::singletonRef->addBufferLine(ShellBuffer::bufferArray);
87    return true;
88  }
89
90  /**
91   * @brief add a Line to the List of Buffers
92   * @param line
93   * @param arguments
94   *
95   * This function Adds one line to the buffer.
96   * and displays the line as the First Line of the display-buffer
97   */
98  void ShellBuffer::addBufferLine(const char* line)
99  {
100    std::string inputBuffer = this->keepBuffer + line;
101
102    int lineBegin = 0;
103    int lineEnd = 0;
104    // adding all the new Lines
105    while (lineEnd < inputBuffer.size())
106    {
107      lineBegin = lineEnd;
108      lineEnd = inputBuffer.find('\n', (lineBegin == 0) ? 0: ++lineBegin);
109      if (likely(lineEnd != std::string::npos ))
110      {
111        this->lineCount++;
112        this->buffer.push_front(inputBuffer.substr(lineBegin, lineEnd - lineBegin));
113      }
114      else      // No end of Line reached.
115      {
116        this->keepBuffer = inputBuffer.substr(lineBegin, inputBuffer.size() - lineBegin);
117        break;
118      }
119
120      if (inputBuffer[lineBegin] == '\n')
121        lineBegin++, lineEnd++;
122
123      if (this->buffer.size() > this->maxBufferSize)
124        this->buffer.pop_back();
125    }
126  }
127
128  /**
129   * @brief displays some nice output from the Shell
130   */
131  void ShellBuffer::debug() const
132  {
133    PRINT(3)("Debugging output to console (not this shell)\n");
134
135    std::list<std::string>::const_iterator bufferLine;
136    for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine)
137      printf((*bufferLine).c_str());
138  }
139
140}
Note: See TracBrowser for help on using the repository browser.