Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellBuffer is extern to Shell now… (But NOT used in Shell yet)

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