amath  1.8.5
Simple command line calculator
CharBuffer Class Reference

Encapsulate an character array which can be used as a string. More...

#include <charbuf.h>

Public Member Functions

 CharBuffer ()
 Initialize without allocating memory. More...
 
 CharBuffer (unsigned int size)
 Initialize while allocating specified amount of memory. More...
 
 ~CharBuffer ()
 
void ClearBuffer ()
 Release memory in buffer. More...
 
void ClearAndCopy (const char *source)
 Release memory, allocate and copy source. More...
 
void ClearAndAlloc (unsigned int size)
 Release memory and allocate new size. More...
 
void EnsureSize (unsigned int size)
 Ensure a memory block of specified size is allocated. More...
 
void EnsureSize (unsigned int blocksize, unsigned int blocks)
 
void EnsureMinimumSize ()
 
void EnsureGrowth (unsigned int size)
 
void Empty ()
 
bool IsEmpty () const
 
bool Is (const char *string) const
 Compare content of CharBuffer with string) More...
 
bool Contains (const char c) const
 
void Copy (CharBuffer *buf)
 
void Append (const char *source)
 
void Append (const char c)
 
void Append (const char c, unsigned int count)
 
void DeleteLastChar ()
 
bool RemoveTrailing (const char c)
 
bool RemoveTrailing (const char *string)
 
char * GetString () const
 

Private Attributes

char * buf
 
char * ptr
 
unsigned int cursize
 

Static Private Attributes

static const unsigned int minimumSize = 64
 

Friends

class AnsiConoleEngine
 

Detailed Description

Encapsulate an character array which can be used as a string.

The CharBuffer class eases the task of allocating a releasing memory.

Definition at line 44 of file charbuf.h.

Constructor & Destructor Documentation

◆ CharBuffer() [1/2]

◆ CharBuffer() [2/2]

CharBuffer::CharBuffer ( unsigned int  size)
explicit

Initialize while allocating specified amount of memory.

Definition at line 49 of file charbuf.cpp.

References buf, cursize, minimumSize, and ptr.

Referenced by SaveStatement::Execute(), and PositionalNumeralSystem::GetText().

50 {
51  cursize = (size < minimumSize ? minimumSize : size);
52  buf = new char[cursize];
53  ptr = buf;
54 }
unsigned int cursize
Definition: charbuf.h:83
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
static const unsigned int minimumSize
Definition: charbuf.h:84
Here is the caller graph for this function:

◆ ~CharBuffer()

CharBuffer::~CharBuffer ( )

Definition at line 56 of file charbuf.cpp.

References ClearBuffer().

57 {
58  ClearBuffer();
59 }
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
Here is the call graph for this function:

Member Function Documentation

◆ Append() [1/3]

◆ Append() [2/3]

◆ Append() [3/3]

void CharBuffer::Append ( const char  c,
unsigned int  count 
)

Definition at line 250 of file charbuf.cpp.

References ptr.

Referenced by ErrorNode::Execute(), and StandardFilesystem::ListDirectory().

251 {
252  if (count == 0)
253  {
254  return;
255  }
256 
257  unsigned int n = count;
258  while (n--)
259  *ptr++ = c;
260 }
char * ptr
Definition: charbuf.h:82
Here is the caller graph for this function:

◆ ClearAndAlloc()

void CharBuffer::ClearAndAlloc ( unsigned int  size)

Release memory and allocate new size.

Definition at line 92 of file charbuf.cpp.

References buf, ClearBuffer(), cursize, minimumSize, and ptr.

Referenced by ErrorNode::Execute(), and AnsiConoleEngine::StartInput().

93 {
94  ClearBuffer();
95  cursize = (size < minimumSize ? minimumSize : size);
96  buf = new char[cursize];
97  ptr = buf;
98 }
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
unsigned int cursize
Definition: charbuf.h:83
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
static const unsigned int minimumSize
Definition: charbuf.h:84
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearAndCopy()

void CharBuffer::ClearAndCopy ( const char *  source)

Release memory, allocate and copy source.

Definition at line 81 of file charbuf.cpp.

References AllocAndCopy(), buf, ClearBuffer(), cursize, and ptr.

Referenced by Evaluator::Evaluate(), ExecuteStatement::Execute(), PromptStatement::Execute(), HelpStatement::Execute(), and NumericValueNode::GetNodeText().

82 {
83  ClearBuffer();
84  cursize = AllocAndCopy(&buf, source);
85  ptr = buf + cursize - sizeof(char);
86 }
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
unsigned int cursize
Definition: charbuf.h:83
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearBuffer()

void CharBuffer::ClearBuffer ( )

Release memory in buffer.

Definition at line 65 of file charbuf.cpp.

References buf, cursize, and ptr.

Referenced by FunctionList::Clear(), VariableList::Clear(), ClearAndAlloc(), ClearAndCopy(), Evaluator::Evaluate(), StatementBlockNode::Execute(), HelpStatement::StatementHelp(), and ~CharBuffer().

66 {
67  if (buf != nullptr)
68  {
69  delete [] buf;
70  }
71 
72  buf = nullptr;
73  ptr = buf;
74  cursize = 0;
75 }
unsigned int cursize
Definition: charbuf.h:83
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the caller graph for this function:

◆ Contains()

bool CharBuffer::Contains ( const char  c) const

Definition at line 199 of file charbuf.cpp.

References buf, and ptr.

Referenced by DecimalSystem::GetText().

200 {
201  char* i = buf;
202 
203  if (i == nullptr || buf == ptr)
204  return false;
205 
206  do
207  {
208  if (*i == c)
209  return true;
210 
211  i++;
212  }
213  while (i != ptr);
214 
215  return false;
216 }
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the caller graph for this function:

◆ Copy()

void CharBuffer::Copy ( CharBuffer buf)

Definition at line 233 of file charbuf.cpp.

References buf, cursize, EnsureSize(), GetString(), and ptr.

Referenced by PositionalNumeralSystem::GetText().

234 {
235  EnsureSize(source->cursize);
236  const char* s = source->GetString();
237 
238  ptr = buf;
239  // ReSharper disable once CppPossiblyErroneousEmptyStatements
240  while ((*ptr++ = *s++));
241 
242  ptr--;
243 }
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeleteLastChar()

void CharBuffer::DeleteLastChar ( )

Definition at line 228 of file charbuf.cpp.

References ptr.

Referenced by StandardProgram::Initialize().

229 {
230  ptr--;
231 }
char * ptr
Definition: charbuf.h:82
Here is the caller graph for this function:

◆ Empty()

◆ EnsureGrowth()

void CharBuffer::EnsureGrowth ( unsigned int  size)

Definition at line 169 of file charbuf.cpp.

References buf, EnsureSize(), and ptr.

Referenced by PlotStatement::Execute(), StatementBlockNode::Execute(), DecimalSystem::GetText(), StandardFilesystem::ListDirectory(), AnsiConoleEngine::ProcessChar(), and AnsiConoleEngine::ShowNext().

170 {
171  EnsureSize((unsigned int)((ptr - buf) + size));
172 }
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EnsureMinimumSize()

void CharBuffer::EnsureMinimumSize ( )

Definition at line 100 of file charbuf.cpp.

References buf, minimumSize, and ptr.

Referenced by Empty(), and FunctionDefinitionNode::FunctionDefinitionNode().

101 {
102  if (buf == nullptr)
103  {
104  unsigned int size = minimumSize;
105  buf = new char[size];
106  ptr = buf;
107  }
108 }
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
static const unsigned int minimumSize
Definition: charbuf.h:84
Here is the caller graph for this function:

◆ EnsureSize() [1/2]

void CharBuffer::EnsureSize ( unsigned int  size)

Ensure a memory block of specified size is allocated.

Definition at line 114 of file charbuf.cpp.

References buf, cursize, MemCopy(), minimumSize, and ptr.

Referenced by Copy(), EnsureGrowth(), ErrorNode::ErrorNode(), MemoryStatement::Execute(), InputStatement::Execute(), OutputStatement::Execute(), DigitsStatement::Execute(), EvalStatement::Execute(), DeleteStatement::Execute(), PreferencesBase::GetDescription(), PositionalNumeralSystem::GetName(), UnaryNode::GetText(), FunctionNode::GetText(), AbsoluteNode::GetText(), FactorialNode::GetText(), NumericOperator::GetText(), StandardProgram::Initialize(), FunctionList::ListContent(), VariableList::ListContent(), StandardFilesystem::ListDirectory(), AnsiConoleEngine::ShowLast(), and AnsiConoleEngine::ShowNext().

115 {
116  if (cursize < size)
117  {
118  unsigned int tempsize = cursize;
119  cursize = (size < minimumSize ? minimumSize : size);
120 
121  if (buf == nullptr)
122  { // Nothing allocated yet. Just allocate requested size.
123  buf = new char[cursize];
124  ptr = buf;
125  }
126  else if (buf == ptr)
127  { // Already allocated but buffer is empty.
128  delete [] buf;
129  buf = new char[cursize];
130  ptr = buf;
131  }
132  else
133  { // Buffer already in use.
134  // Make at least double size
135  cursize = cursize < tempsize * 2 ? tempsize * 2 : cursize;
136  unsigned int offset = (unsigned int)(ptr - buf);
137  char* temp = new char[cursize];
138  MemCopy(temp, buf, tempsize);
139  delete [] buf;
140  buf = temp;
141  ptr = buf + offset;
142  }
143  }
144 }
unsigned int cursize
Definition: charbuf.h:83
void MemCopy(void *destination, const void *source, unsigned int length)
Copy a block of memory, handling overlap.
Definition: memcpy.c:75
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
static const unsigned int minimumSize
Definition: charbuf.h:84
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EnsureSize() [2/2]

void CharBuffer::EnsureSize ( unsigned int  blocksize,
unsigned int  blocks 
)

Definition at line 146 of file charbuf.cpp.

References buf, cursize, MemCopy(), and ptr.

Referenced by StandardFilesystem::LoadTextFile().

147 {
148  if (cursize < blocksize * blocks)
149  {
150  if (buf == nullptr)
151  {
152  cursize = blocksize * blocks;
153  buf = new char[cursize];
154  ptr = buf;
155  }
156  else
157  {
158  unsigned int tptr = (unsigned int)(ptr - buf);
159  char* temp = new char[blocksize * blocks];
160  MemCopy(temp, buf, cursize);
161  delete [] buf;
162  cursize = blocksize * blocks;
163  buf = temp;
164  ptr = buf + tptr;
165  }
166  }
167 }
unsigned int cursize
Definition: charbuf.h:83
void MemCopy(void *destination, const void *source, unsigned int length)
Copy a block of memory, handling overlap.
Definition: memcpy.c:75
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetString()

char * CharBuffer::GetString ( ) const

Definition at line 306 of file charbuf.cpp.

References buf, and ptr.

Referenced by Copy(), AnsiConoleEngine::CopyLine(), ErrorNode::ErrorNode(), LoadStatement::Execute(), SaveStatement::Execute(), ShowStatement::Execute(), ExecuteStatement::Execute(), PlotStatement::Execute(), ListStatement::Execute(), MemoryStatement::Execute(), FunctionDefinitionNode::Execute(), DigitsStatement::Execute(), OutputStatement::Execute(), PromptStatement::Execute(), EvalStatement::Execute(), InputStatement::Execute(), DeleteStatement::Execute(), HelpStatement::Execute(), ErrorNode::Execute(), StatementBlockNode::Execute(), UserFunction::GetDefinitionName(), UserFunction::GetDefinitionText(), PreferencesBase::GetDescription(), AnsiConoleEngine::GetLine(), PositionalNumeralSystem::GetName(), NumericValueNode::GetNodeText(), Evaluator::GetResult(), PositionalNumeralSystem::GetSpecialCase(), DecimalSystem::GetText(), FunctionDefinitionNode::GetText(), UnaryNode::GetText(), PositionalNumeralSystem::GetText(), FunctionNode::GetText(), AbsoluteNode::GetText(), FactorialNode::GetText(), NumericOperator::GetText(), UserFunction::InitializeTexts(), Is(), FunctionList::ListContent(), VariableList::ListContent(), StandardFilesystem::ListDirectory(), StandardLanguage::LoadCatalog(), AnsiConoleEngine::ProcessChar(), AnsiConoleEngine::ShowLast(), AnsiConoleEngine::ShowNext(), StandardProgram::Start(), and HelpStatement::StatementHelp().

307 {
308  *ptr = '\0';
309  return buf;
310 }
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the caller graph for this function:

◆ Is()

bool CharBuffer::Is ( const char *  string) const

Compare content of CharBuffer with string)

Definition at line 194 of file charbuf.cpp.

References GetString(), and StrIsEqual().

Referenced by PositionalNumeralSystem::GetText().

195 {
196  return StrIsEqual(GetString(), string);
197 }
char * GetString() const
Definition: charbuf.cpp:306
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsEmpty()

bool CharBuffer::IsEmpty ( ) const

Definition at line 174 of file charbuf.cpp.

References buf, and ptr.

Referenced by StandardProgram::Start().

175 {
176  char* i = buf;
177 
178  if (i == nullptr || buf == ptr)
179  return true;
180 
181  do
182  {
183  // Blank space characters
184  if (*i != ' ' && *i != '\t' && *i != '\r' && *i != '\n')
185  return false;
186 
187  i++;
188  }
189  while (i != ptr);
190 
191  return true;
192 }
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the caller graph for this function:

◆ RemoveTrailing() [1/2]

bool CharBuffer::RemoveTrailing ( const char  c)

Definition at line 270 of file charbuf.cpp.

References buf, and ptr.

Referenced by PromptStatement::Execute(), DecimalSystem::GetText(), and PositionalNumeralSystem::GetText().

271 {
272  if (ptr == buf)
273  {
274  return false;
275  }
276 
277  if (*(--ptr) == c)
278  {
279  return true;
280  }
281 
282  ptr++;
283  return false;
284 }
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the caller graph for this function:

◆ RemoveTrailing() [2/2]

bool CharBuffer::RemoveTrailing ( const char *  string)

Definition at line 286 of file charbuf.cpp.

References buf, ptr, StrIsEqual(), and StrLen().

287 {
288  int len = StrLen(string) * sizeof(char);
289  char* s = ptr - len;
290  if (s < buf)
291  {
292  return false;
293  }
294 
295  *ptr = '\0';
296 
297  if (StrIsEqual(s, string))
298  {
299  ptr = s;
300  return true;
301  }
302 
303  return false;
304 }
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
char * ptr
Definition: charbuf.h:82
char * buf
Definition: charbuf.h:81
Here is the call graph for this function:

Friends And Related Function Documentation

◆ AnsiConoleEngine

friend class AnsiConoleEngine
friend

Definition at line 79 of file charbuf.h.

Member Data Documentation

◆ buf

◆ cursize

unsigned int CharBuffer::cursize
private

Definition at line 83 of file charbuf.h.

Referenced by CharBuffer(), ClearAndAlloc(), ClearAndCopy(), ClearBuffer(), Copy(), and EnsureSize().

◆ minimumSize

const unsigned int CharBuffer::minimumSize = 64
staticprivate

Definition at line 84 of file charbuf.h.

Referenced by CharBuffer(), ClearAndAlloc(), EnsureMinimumSize(), and EnsureSize().

◆ ptr


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