Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: \n at the beginning won't infinit-loop anymore

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