Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cleaner outtakes of the ShellBuffer

File size: 4.2 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: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell_buffer.h"
19#include "debug.h"
20#include "list.h"
21#include "shell.h"
22
23#include "stdlibincl.h"
24
25using namespace std;
26
27
28/**
29 * standard constructor
30 * @todo this constructor is not jet implemented - do it
31*/
32ShellBuffer::ShellBuffer ()
33{
34  ShellBuffer::singletonRef = this;
35
36  this->lineCount = 0;
37  this->keepBufferArray[0] = '\0';
38  this->keepBuffer = false;
39  this->buffer = new tList<char>;
40  this->bufferIterator = this->buffer->getIterator();
41
42  this->setBufferSize(10);
43}
44
45ShellBuffer* ShellBuffer::singletonRef = NULL;
46
47/**
48 * standard deconstructor
49*/
50ShellBuffer::~ShellBuffer ()
51{
52  if (Shell::isInstanciated())
53    delete Shell::getInstance();
54
55  this->flushBuffers();
56  delete bufferIterator;
57  delete buffer;
58
59  ShellBuffer::singletonRef = NULL;
60}
61
62/**
63 * deletes all the Buffers
64 */
65void ShellBuffer::flushBuffers()
66{
67  // delete all the Chars in the Buffers
68  char* charElem = bufferIterator->firstElement();
69  while (charElem != NULL)
70  {
71    delete charElem;
72
73    charElem = bufferIterator->nextElement();
74  }
75  delete this->bufferIterator;
76  delete this->buffer;
77  this->buffer = new tList<char>;
78  this->bufferIterator = this->buffer->getIterator();
79}
80
81/**
82 * adds a new Line to the List of Buffers
83 * @param line the Line as in the first argument in printf
84 */
85bool ShellBuffer::addBufferLineStatic(const char* line, ...)
86{
87  va_list arguments;
88  va_start(arguments, line);
89
90#if DEBUG < 3
91  if (ShellBuffer::singletonRef == NULL)
92#endif
93
94//  vprintf(line, arguments);
95#if DEBUG < 3
96  else
97#else
98  if (ShellBuffer::singletonRef != NULL)
99#endif
100    ShellBuffer::singletonRef->addBufferLine(line, arguments);
101  return true;
102}
103
104/**
105 * add a Line to the List of Buffers
106 * @param line
107 * @param arguments
108 *
109 * This function Adds one line to the buffer.
110 * and displays the line as the First Line of the display-buffer
111 */
112void ShellBuffer::addBufferLine(const char* line, va_list arguments)
113{
114  vsprintf(this->bufferArray, line, arguments);
115
116  char* inputEnd;
117  char* newLineBegin;
118  char* newLineEnd;
119
120   // check if we have something left in the buffers
121  if (unlikely(this->keepBuffer))
122  {
123    strcat(this->keepBufferArray, this->bufferArray);
124    inputEnd = this->keepBufferArray + strlen(this->keepBufferArray);
125    newLineBegin = this->keepBufferArray;
126    this->keepBuffer = false;
127  }
128  else
129  {
130    inputEnd = this->bufferArray + strlen(this->bufferArray);
131    newLineBegin = this->bufferArray;
132  }
133
134   // adding all the new Lines
135  while (newLineBegin < inputEnd)
136  {
137    newLineEnd = strchr(newLineBegin, '\n');
138    if (newLineEnd != NULL && *newLineEnd == '\n')
139      *newLineEnd = '\0';
140    else
141    {
142//       newLineEnd = newLineBegin + strlen(newLineBegin);
143      strcpy(this->keepBufferArray, newLineBegin);
144      this->keepBuffer = true;
145      break;
146    }
147
148    char* addLine = new char[strlen(newLineBegin)+1];
149    strcpy(addLine, newLineBegin);
150
151    this->buffer->add(addLine);
152    this->lineCount++;
153
154    if (this->buffer->getSize() > this->bufferSize)
155    {
156      delete this->buffer->firstElement();
157      this->buffer->remove(this->buffer->firstElement());
158    }
159
160    newLineBegin = newLineEnd+1;
161  }
162}
163
164/**
165 * moves the buffer around lineCount lines upwards (negative values move down)
166 * @param lineCount the Count of lines to move upwards
167 *
168 * @todo
169 */
170void ShellBuffer::moveBuffer(unsigned int lineCount)
171{
172}
173
174/**
175 * @param lineNumber the n-th line from the bottom
176 * @returns the Buffer at Line lineNumber
177 */
178const char* ShellBuffer::getBufferLine(unsigned int lineNumber)
179{
180  tIterator<char>* charIterator = this->buffer->getIterator();
181  char* charElem = charIterator->firstElement();
182
183  int i = 1;
184  while (charElem != NULL)
185  {
186    if (i++ < lineNumber)
187    {
188      delete charIterator;
189      return charElem;
190    }
191
192    charElem = charIterator->nextElement();
193  }
194  delete charIterator;
195}
Note: See TracBrowser for help on using the repository browser.