Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7897 was 7764, checked in by bensch, 19 years ago

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

File size: 3.3 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
[7374]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
[1853]17
[5173]18#include "shell_buffer.h"
[7737]19
20#include <stdarg.h>
21
[5174]22#include "debug.h"
[5175]23#include "shell.h"
[7737]24#include "lib/util/threading.h"
[1853]25
[7374]26namespace OrxShell
27{
[7375]28  /**
29   * @brief standard constructor
30   */
31  ShellBuffer::ShellBuffer ()
32  {
33    ShellBuffer::singletonRef = this;
[1853]34
[7375]35    this->lineCount = 0;
36    this->bufferArray[0] = '\0';
[1856]37
[7762]38    this->setMaxBufferSize(100);
[7375]39  }
[5175]40
[7375]41  ShellBuffer* ShellBuffer::singletonRef = NULL;
[7737]42  std::list<std::string> ShellBuffer::buffer;
[4320]43
[7375]44  /**
45   * @brief standard deconstructor
46   */
47  ShellBuffer::~ShellBuffer ()
48  {
49    ShellBuffer::singletonRef = NULL;
50  }
[5174]51
[7375]52  /**
53   * @brief deletes all the Buffers
54   */
55  void ShellBuffer::flush()
56  {
57    this->buffer.clear();
58  }
[5206]59
[7375]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);
[5206]68
[7737]69    static OrxThread::Mutex ShellBuffer__bufferMutex;
[5174]70
[7737]71    OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex);
[7729]72#if DEBUG_LEVEL < 3
[7375]73    if (ShellBuffer::singletonRef == NULL)
[5174]74#endif
[7375]75      vprintf(line, arguments);
[7729]76#if DEBUG_LEVEL < 3
[7375]77    else
[5174]78#else
[7375]79    if (ShellBuffer::singletonRef != NULL)
[5174]80#endif
[7375]81      ShellBuffer::singletonRef->addBufferLine(line, arguments);
82    return true;
83  }
[5174]84
[7375]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);
[5290]97
[7761]98    std::string inputBuffer = this->keepBuffer + this->bufferArray;
[5174]99
[7761]100    int lineBegin = 0;
101    int lineEnd = 0;
[7375]102    // adding all the new Lines
[7761]103    while (lineEnd < inputBuffer.size())
[7375]104    {
[7761]105      lineBegin = lineEnd;
106      lineEnd = inputBuffer.find('\n', (lineBegin == 0) ? 0: ++lineBegin);
107      if (likely(lineEnd != std::string::npos ))
[7375]108      {
[7761]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);
[7375]115        break;
116      }
[5174]117
[7764]118      if (inputBuffer[lineBegin] == '\n')
119        lineBegin++, lineEnd++;
120
[7762]121      if (this->buffer.size() > this->maxBufferSize)
[7744]122        this->buffer.pop_back();
[7375]123    }
[5174]124  }
125
[7375]126  /**
[7729]127   * @brief displays some nice output from the Shell
[7375]128   */
129  void ShellBuffer::debug() const
130  {
131    PRINT(3)("Debugging output to console (not this shell)\n");
[5177]132
[7375]133    std::list<std::string>::const_iterator bufferLine;
[7744]134    for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine)
[7375]135      printf((*bufferLine).c_str());
136  }
137
[5177]138}
Note: See TracBrowser for help on using the repository browser.