amath  1.8.5
Simple command line calculator
AnsiConoleEngine Class Reference

ANSI console controller. More...

#include <aengine.h>

Collaboration diagram for AnsiConoleEngine:

Public Member Functions

 AnsiConoleEngine (const char *prompt, CharValidator *validator)
 
 ~AnsiConoleEngine ()
 
void StartInput ()
 
bool InputDone () const
 
const char * GetLine () const
 
void SetPrompt (const char *string)
 
const char * ProcessChar (const unsigned char character)
 
void Enable ()
 
void Disable ()
 

Private Member Functions

void CopyLine ()
 
void ShowLast ()
 
void ShowNext ()
 

Private Attributes

char * prompt
 
bool enabled
 
char ** lines
 
CharBufferlinebuf
 
CharValidatorvalidator
 
unsigned int len
 
char * cursor
 
char * endpos
 
int curline
 
int showline
 
bool lineswap
 
char * editline
 
bool escmode
 
bool csimode
 
bool delmode
 
bool linedone
 
CharBufferout
 

Static Private Attributes

static const int maxLines = 100
 
static const int lineSize = 1024
 

Detailed Description

ANSI console controller.

More info on the ANSI console is available at Wikipedia: https://wikipedia.org/wiki/ANSI_escape_code

Definition at line 47 of file aengine.h.

Constructor & Destructor Documentation

◆ AnsiConoleEngine()

AnsiConoleEngine::AnsiConoleEngine ( const char *  prompt,
CharValidator validator 
)

Definition at line 44 of file aengine.cpp.

References AllocAndCopy(), CharBuffer::CharBuffer(), curline, editline, enabled, linebuf, lines, maxLines, out, prompt, and validator.

45 {
46  this->validator = validator;
47  AllocAndCopy(&this->prompt, prompt);
48  linebuf = new CharBuffer();
49  out = new CharBuffer();
50 
51  lines = new char*[maxLines];
52 
53  for (int i = 0; i < maxLines; i++)
54  {
55  lines[i] = nullptr;
56  }
57 
58  editline = nullptr;
59  curline = -1;
60  enabled = true;
61 }
char ** lines
Definition: aengine.h:71
CharBuffer * linebuf
Definition: aengine.h:72
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
CharBuffer * out
Definition: aengine.h:87
static const int maxLines
Definition: aengine.h:69
CharValidator * validator
Definition: aengine.h:73
char * prompt
Definition: aengine.h:66
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
char * editline
Definition: aengine.h:81
Here is the call graph for this function:

◆ ~AnsiConoleEngine()

AnsiConoleEngine::~AnsiConoleEngine ( )

Definition at line 63 of file aengine.cpp.

References linebuf, lines, maxLines, out, and prompt.

64 {
65  for (int i = 0; i < maxLines; i++)
66  {
67  if (lines[i] != nullptr)
68  {
69  delete [] lines[i];
70  }
71  }
72 
73  delete [] lines;
74  delete linebuf;
75  delete out;
76  delete prompt;
77 }
char ** lines
Definition: aengine.h:71
CharBuffer * linebuf
Definition: aengine.h:72
CharBuffer * out
Definition: aengine.h:87
static const int maxLines
Definition: aengine.h:69
char * prompt
Definition: aengine.h:66

Member Function Documentation

◆ CopyLine()

void AnsiConoleEngine::CopyLine ( )
private

Definition at line 268 of file aengine.cpp.

References AllocAndCopy(), curline, editline, CharBuffer::GetString(), linebuf, lines, and maxLines.

Referenced by ProcessChar().

269 {
270  curline++;
271 
272  if (curline == maxLines)
273  {
274  curline--;
275 
276  delete [] lines[0];
277  for (int i = 0; i < maxLines - 1; i++)
278  {
279  lines[i] = lines[i + 1];
280  }
281  }
282 
284 
285  if (editline != nullptr)
286  {
287  delete [] editline;
288  editline = nullptr;
289  }
290 }
char ** lines
Definition: aengine.h:71
char * GetString() const
Definition: charbuf.cpp:306
CharBuffer * linebuf
Definition: aengine.h:72
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
static const int maxLines
Definition: aengine.h:69
char * editline
Definition: aengine.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Disable()

void AnsiConoleEngine::Disable ( )

Definition at line 84 of file aengine.cpp.

References enabled.

85 {
86  enabled = false;
87 }

◆ Enable()

void AnsiConoleEngine::Enable ( )

Definition at line 79 of file aengine.cpp.

References enabled.

80 {
81  enabled = true;
82 }

◆ GetLine()

const char * AnsiConoleEngine::GetLine ( ) const

Definition at line 385 of file aengine.cpp.

References CharBuffer::GetString(), and linebuf.

386 {
387  return linebuf->GetString();
388 }
char * GetString() const
Definition: charbuf.cpp:306
CharBuffer * linebuf
Definition: aengine.h:72
Here is the call graph for this function:

◆ InputDone()

bool AnsiConoleEngine::InputDone ( ) const

Definition at line 380 of file aengine.cpp.

References linedone.

381 {
382  return linedone;
383 }

◆ ProcessChar()

const char * AnsiConoleEngine::ProcessChar ( const unsigned char  character)

Definition at line 104 of file aengine.cpp.

References CharBuffer::Append(), CharBuffer::buf, CopyLine(), csimode, cursor, delmode, CharBuffer::Empty(), enabled, endpos, CharBuffer::EnsureGrowth(), escmode, CharBuffer::GetString(), len, linebuf, linedone, lineSize, out, CharBuffer::ptr, ShowLast(), ShowNext(), CharValidator::Validate(), and validator.

105 {
106  unsigned char ch = character;
107  out->Empty();
108 
109  if (len == 0)
110  {
112  len = lineSize;
113  }
114 
115  bool processed = false;
116 
117  if (ch == 0)
118  {
119  processed = true;
120  }
121  else if (ch == 27)
122  {
123  escmode = true;
124  processed = true;
125  }
126  else if (ch == 155 || (escmode && ch == 79) || (escmode && ch == 91))
127  {
128  csimode = true;
129  processed = true;
130  }
131  else if (csimode)
132  {
133  switch (ch)
134  {
135  case 65: // Arrow up (27 91 65)
136  ShowLast();
137  break;
138  case 66: // Arrow down (27 91 66)
139  ShowNext();
140  break;
141  case 67: // Arrow right (27 91 67)
142  if (cursor != endpos)
143  {
144  cursor++;
146  }
147  break;
148  case 68: // Arrow left (27 91 68)
149  if (cursor != linebuf->buf)
150  {
151  cursor--;
153  }
154  break;
155  case 51: // DEL 27 91 51 126
156  delmode = true;
157  default:
158  // F1 27 79 80
159  // F2 27 79 81
160  break;
161  }
162 
163  escmode = false;
164  csimode = false;
165  processed = true;
166  }
167  else
168  {
169  escmode = false;
170  csimode = false;
171  }
172 
173  // Delete one character to the right
174  if (delmode && ch == 126)
175  {
176  if (cursor != endpos)
177  {
178  char* i = cursor;
179  do
180  {
181  *i = *(i + 1);
182  i++;
183  }
184  while (i != endpos);
185 
186  len++;
187  if (enabled)
188  {
190  }
191  else
192  {
194  }
195  endpos--;
196  linebuf->ptr = endpos;
197  }
198 
199  processed = true;
200  delmode = false;
201  }
202 
203  if (processed)
204  {
205  return out->GetString();
206  }
207 
208  if (ch == 13 || ch == 10)
209  {
210  out->Append(NEWLINE);
211  linebuf->ptr = endpos;
212  CopyLine();
213  linedone = true;
214  }
215  else if (cursor != linebuf->buf && (ch == 8 || ch == 127))
216  {
217  // Deleting in middle of line
218  if (cursor != endpos)
219  {
220  char* i = cursor - 1;
221  do
222  {
223  *i = *(i + 1);
224  i++;
225  }
226  while (i != endpos);
227  }
228 
229  len++;
230  if (enabled)
231  {
234  }
235  else
236  {
238  }
239  cursor--;
240  endpos--;
241  linebuf->ptr = endpos;
242  }
243  else if (validator->Validate(ch))
244  {
245  // Insert in middle of line
246  if (cursor != endpos)
247  {
248  char* i = endpos;
249  do
250  {
251  *i = *(i - 1);
252  i--;
253  }
254  while (i != cursor);
256  }
257 
258  len--;
259  out->Append(ch);
260  *cursor++ = ch;
261  endpos++;
262  linebuf->ptr = endpos;
263  }
264 
265  return out->GetString();
266 }
#define NEWLINE
Definition: amath.h:222
unsigned int len
Definition: aengine.h:74
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
#define DELETE1CHAR
Definition: aengine.cpp:40
char * endpos
Definition: aengine.h:76
void Append(const char *source)
Definition: charbuf.cpp:262
CharBuffer * linebuf
Definition: aengine.h:72
CharBuffer * out
Definition: aengine.h:87
#define CURSORFORWARD
Definition: aengine.cpp:36
#define INSERT1CHAR
Definition: aengine.cpp:39
CharValidator * validator
Definition: aengine.h:73
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:169
#define CURSORBACKWARD
Definition: aengine.cpp:37
static const int lineSize
Definition: aengine.h:70
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
char * cursor
Definition: aengine.h:75
virtual bool Validate(char c)=0
#define DELETE1CHARASC
Definition: aengine.cpp:42
Here is the call graph for this function:

◆ SetPrompt()

void AnsiConoleEngine::SetPrompt ( const char *  string)

Definition at line 390 of file aengine.cpp.

References AllocAndCopy(), and prompt.

391 {
392  delete prompt;
393  AllocAndCopy(&prompt, string);
394 }
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
char * prompt
Definition: aengine.h:66
Here is the call graph for this function:

◆ ShowLast()

void AnsiConoleEngine::ShowLast ( )
private

Definition at line 292 of file aengine.cpp.

References AllocAndCopy(), CharBuffer::Append(), CharBuffer::buf, curline, cursor, editline, CharBuffer::Empty(), endpos, CharBuffer::EnsureSize(), CharBuffer::GetString(), len, linebuf, lines, lineSize, lineswap, out, prompt, showline, and StrLen().

Referenced by ProcessChar().

293 {
294  if (curline == -1)
295  {
296  return;
297  }
298 
299  if (!lineswap)
300  {
302  lineswap = true;
303  showline = curline + 1;
304  }
305  else if (showline == curline + 1)
306  {
307  delete editline;
309  }
310 
311  showline--;
312  if (showline < 0)
313  {
314  showline = 0;
315  }
316 
317  out->Empty();
318  out->EnsureSize(
319  StrLen(DELETELINE) +
320  StrLen(prompt) +
321  StrLen(lines[showline]) + 1);
322 
324  out->Append(prompt);
325  out->Append(lines[showline]);
326 
327  linebuf->Empty();
328  linebuf->EnsureSize(StrLen(lines[showline]));
329  linebuf->Append(lines[showline]);
330 
331  unsigned int linelen = StrLen(linebuf->GetString());
332  cursor = linebuf->buf + linelen;
333  endpos = cursor;
334  len = lineSize - linelen;
335 }
char ** lines
Definition: aengine.h:71
unsigned int len
Definition: aengine.h:74
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
char * endpos
Definition: aengine.h:76
void Append(const char *source)
Definition: charbuf.cpp:262
CharBuffer * linebuf
Definition: aengine.h:72
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
CharBuffer * out
Definition: aengine.h:87
#define DELETELINE
Definition: aengine.cpp:41
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
static const int lineSize
Definition: aengine.h:70
char * prompt
Definition: aengine.h:66
char * buf
Definition: charbuf.h:81
char * cursor
Definition: aengine.h:75
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
char * editline
Definition: aengine.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ShowNext()

void AnsiConoleEngine::ShowNext ( )
private

Definition at line 337 of file aengine.cpp.

References CharBuffer::Append(), CharBuffer::buf, curline, cursor, editline, CharBuffer::Empty(), endpos, CharBuffer::EnsureGrowth(), CharBuffer::EnsureSize(), CharBuffer::GetString(), len, linebuf, lines, lineSize, lineswap, out, prompt, showline, and StrLen().

Referenced by ProcessChar().

338 {
339  if (!lineswap)
340  {
341  return;
342  }
343 
344  showline++;
345  if (showline > curline + 1)
346  {
347  showline = curline + 1;
348  return;
349  }
350 
351  out->Empty();
353  out->Append(prompt);
354 
355  if (showline > curline)
356  {
358  out->Append(editline);
359 
360  linebuf->Empty();
363  }
364  else
365  {
367  out->Append(lines[showline]);
368 
369  linebuf->Empty();
370  linebuf->EnsureSize(StrLen(lines[showline]));
371  linebuf->Append(lines[showline]);
372  }
373 
374  unsigned int linelen = StrLen(linebuf->GetString());
375  cursor = linebuf->buf + linelen;
376  endpos = cursor;
377  len = lineSize - linelen;
378 }
char ** lines
Definition: aengine.h:71
unsigned int len
Definition: aengine.h:74
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
char * endpos
Definition: aengine.h:76
void Append(const char *source)
Definition: charbuf.cpp:262
CharBuffer * linebuf
Definition: aengine.h:72
CharBuffer * out
Definition: aengine.h:87
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:169
#define DELETELINE
Definition: aengine.cpp:41
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
static const int lineSize
Definition: aengine.h:70
char * prompt
Definition: aengine.h:66
char * buf
Definition: charbuf.h:81
char * cursor
Definition: aengine.h:75
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
char * editline
Definition: aengine.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ StartInput()

void AnsiConoleEngine::StartInput ( )

Definition at line 89 of file aengine.cpp.

References CharBuffer::buf, CharBuffer::ClearAndAlloc(), csimode, cursor, delmode, endpos, escmode, len, linebuf, linedone, lineSize, and lineswap.

90 {
92  len = lineSize;
93  cursor = linebuf->buf;
94  endpos = cursor;
95  *endpos = '\0';
96 
97  lineswap = false;
98  escmode = false;
99  csimode = false;
100  delmode = false;
101  linedone = false;
102 }
unsigned int len
Definition: aengine.h:74
char * endpos
Definition: aengine.h:76
CharBuffer * linebuf
Definition: aengine.h:72
static const int lineSize
Definition: aengine.h:70
char * buf
Definition: charbuf.h:81
char * cursor
Definition: aengine.h:75
void ClearAndAlloc(unsigned int size)
Release memory and allocate new size.
Definition: charbuf.cpp:92
Here is the call graph for this function:

Member Data Documentation

◆ csimode

bool AnsiConoleEngine::csimode
private

Definition at line 84 of file aengine.h.

Referenced by ProcessChar(), and StartInput().

◆ curline

int AnsiConoleEngine::curline
private

Definition at line 78 of file aengine.h.

Referenced by AnsiConoleEngine(), CopyLine(), ShowLast(), and ShowNext().

◆ cursor

char* AnsiConoleEngine::cursor
private

Definition at line 75 of file aengine.h.

Referenced by ProcessChar(), ShowLast(), ShowNext(), and StartInput().

◆ delmode

bool AnsiConoleEngine::delmode
private

Definition at line 85 of file aengine.h.

Referenced by ProcessChar(), and StartInput().

◆ editline

char* AnsiConoleEngine::editline
private

Definition at line 81 of file aengine.h.

Referenced by AnsiConoleEngine(), CopyLine(), ShowLast(), and ShowNext().

◆ enabled

bool AnsiConoleEngine::enabled
private

Definition at line 67 of file aengine.h.

Referenced by AnsiConoleEngine(), Disable(), Enable(), and ProcessChar().

◆ endpos

char* AnsiConoleEngine::endpos
private

Definition at line 76 of file aengine.h.

Referenced by ProcessChar(), ShowLast(), ShowNext(), and StartInput().

◆ escmode

bool AnsiConoleEngine::escmode
private

Definition at line 83 of file aengine.h.

Referenced by ProcessChar(), and StartInput().

◆ len

unsigned int AnsiConoleEngine::len
private

Definition at line 74 of file aengine.h.

Referenced by ProcessChar(), ShowLast(), ShowNext(), and StartInput().

◆ linebuf

CharBuffer* AnsiConoleEngine::linebuf
private

◆ linedone

bool AnsiConoleEngine::linedone
private

Definition at line 86 of file aengine.h.

Referenced by InputDone(), ProcessChar(), and StartInput().

◆ lines

char** AnsiConoleEngine::lines
private

Definition at line 71 of file aengine.h.

Referenced by AnsiConoleEngine(), CopyLine(), ShowLast(), ShowNext(), and ~AnsiConoleEngine().

◆ lineSize

const int AnsiConoleEngine::lineSize = 1024
staticprivate

Definition at line 70 of file aengine.h.

Referenced by ProcessChar(), ShowLast(), ShowNext(), and StartInput().

◆ lineswap

bool AnsiConoleEngine::lineswap
private

Definition at line 80 of file aengine.h.

Referenced by ShowLast(), ShowNext(), and StartInput().

◆ maxLines

const int AnsiConoleEngine::maxLines = 100
staticprivate

Definition at line 69 of file aengine.h.

Referenced by AnsiConoleEngine(), CopyLine(), and ~AnsiConoleEngine().

◆ out

CharBuffer* AnsiConoleEngine::out
private

Definition at line 87 of file aengine.h.

Referenced by AnsiConoleEngine(), ProcessChar(), ShowLast(), ShowNext(), and ~AnsiConoleEngine().

◆ prompt

char* AnsiConoleEngine::prompt
private

Definition at line 66 of file aengine.h.

Referenced by AnsiConoleEngine(), SetPrompt(), ShowLast(), ShowNext(), and ~AnsiConoleEngine().

◆ showline

int AnsiConoleEngine::showline
private

Definition at line 79 of file aengine.h.

Referenced by ShowLast(), and ShowNext().

◆ validator

CharValidator* AnsiConoleEngine::validator
private

Definition at line 73 of file aengine.h.

Referenced by AnsiConoleEngine(), and ProcessChar().


The documentation for this class was generated from the following files: