amath  1.8.5
Simple command line calculator
VariableList Class Reference

A list of user defined variables. More...

#include <values.h>

Collaboration diagram for VariableList:

Public Member Functions

 VariableList ()
 
 ~VariableList ()
 
void Clear ()
 
bool Delete (const char *name)
 
VariableGetFirstVariable () const
 
VariableGetVariable (const char *name) const
 
VariableCreateVariable (const char *name)
 
VariableInsertTemporaryVariable (Variable *variable)
 
void RemoveTemporaryVariable ()
 
char * List () const
 
char * ListDefinitions () const
 

Private Member Functions

char * ListContent (bool cmdFormat) const
 

Private Attributes

CharBufferbuf
 
Variablefirst
 

Detailed Description

A list of user defined variables.

Definition at line 49 of file values.h.

Constructor & Destructor Documentation

◆ VariableList()

VariableList::VariableList ( )

Definition at line 93 of file values.cpp.

References buf, CharBuffer::CharBuffer(), and first.

Referenced by Program::Program().

94 {
95  first = nullptr;
96  buf = new CharBuffer();
97 }
CharBuffer * buf
Definition: values.h:68
Variable * first
Definition: values.h:69
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~VariableList()

VariableList::~VariableList ( )

Definition at line 99 of file values.cpp.

References buf, and first.

100 {
101  if (first != nullptr)
102  {
103  delete first;
104  }
105 
106  delete buf;
107 }
CharBuffer * buf
Definition: values.h:68
Variable * first
Definition: values.h:69

Member Function Documentation

◆ Clear()

void VariableList::Clear ( )

Definition at line 109 of file values.cpp.

References buf, CharBuffer::ClearBuffer(), and first.

Referenced by DeleteStatement::Execute().

110 {
111  if (first != nullptr)
112  {
113  delete first;
114  first = nullptr;
115  }
116 
117  buf->ClearBuffer();
118 }
CharBuffer * buf
Definition: values.h:68
void ClearBuffer()
Release memory in buffer.
Definition: charbuf.cpp:65
Variable * first
Definition: values.h:69
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateVariable()

Variable * VariableList::CreateVariable ( const char *  name)

Definition at line 172 of file values.cpp.

References first, Variable::GetName(), Variable::Next, StrIsEqual(), and Variable::Variable().

Referenced by Parser::ParseIdent().

173 {
174  if (first == nullptr)
175  {
176  first = new Variable(name);
177  return first;
178  }
179 
180  Variable* current = first;
181  Variable* last = nullptr;
182 
183  while (current != nullptr)
184  {
185  if (StrIsEqual(name, current->GetName()))
186  {
187  return current;
188  }
189 
190  last = current;
191  current = current->Next;
192  }
193 
194  current = new Variable(name);
195  last->Next = current;
196 
197  return current;
198 }
A user defined variable.
Definition: values.h:76
char * GetName() const
Definition: values.cpp:69
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
Variable * first
Definition: values.h:69
Variable * Next
Definition: values.h:86
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Delete()

bool VariableList::Delete ( const char *  name)

Definition at line 120 of file values.cpp.

References Variable::chainDelete, first, GetVariable(), and Variable::Next.

121 {
122  Variable* var = GetVariable(name);
123 
124  if (var == nullptr)
125  {
126  return false;
127  }
128 
129  if (var == first)
130  {
131  first = var->Next;
132  var->chainDelete = false;
133  delete var;
134  return true;
135  }
136 
137  Variable* current = first->Next;
138  Variable* last = first;
139 
140  while (current != var)
141  {
142  current = current->Next;
143  last = last->Next;
144  }
145 
146  last->Next = var->Next;
147 
148  // Only delete this variable. Not the whole chain.
149  var->chainDelete = false;
150  delete var;
151 
152  return true;
153 }
bool chainDelete
Definition: values.h:91
A user defined variable.
Definition: values.h:76
Variable * GetVariable(const char *name) const
Definition: values.cpp:160
Variable * first
Definition: values.h:69
Variable * Next
Definition: values.h:86
Here is the call graph for this function:

◆ GetFirstVariable()

Variable * VariableList::GetFirstVariable ( ) const

Definition at line 155 of file values.cpp.

References first.

Referenced by ListContent().

156 {
157  return first;
158 }
Variable * first
Definition: values.h:69
Here is the caller graph for this function:

◆ GetVariable()

Variable * VariableList::GetVariable ( const char *  name) const

Definition at line 160 of file values.cpp.

References first, Variable::GetName(), Variable::Next, and StrIsEqual().

Referenced by Delete(), and Parser::ParseIdent().

161 {
162  Variable* current = first;
163 
164  while (current != nullptr && !StrIsEqual(current->GetName(), name))
165  {
166  current = current->Next;
167  }
168 
169  return current;
170 }
A user defined variable.
Definition: values.h:76
char * GetName() const
Definition: values.cpp:69
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
Variable * first
Definition: values.h:69
Variable * Next
Definition: values.h:86
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InsertTemporaryVariable()

Variable * VariableList::InsertTemporaryVariable ( Variable variable)

Definition at line 200 of file values.cpp.

References first, and Variable::Next.

Referenced by Parser::ParseFunctionDef().

201 {
202  // Temporary variables are always inserted at the beginning of the list.
203  Variable* oldFirst = first;
204  first = variable;
205  variable->Next = oldFirst;
206 
207  return variable;
208 }
A user defined variable.
Definition: values.h:76
Variable * first
Definition: values.h:69
Variable * Next
Definition: values.h:86
Here is the caller graph for this function:

◆ List()

char * VariableList::List ( ) const

Definition at line 218 of file values.cpp.

References ListContent().

Referenced by ListVariablesStatement::Execute().

219 {
220  return ListContent(false);
221 }
char * ListContent(bool cmdFormat) const
Definition: values.cpp:228
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ListContent()

char * VariableList::ListContent ( bool  cmdFormat) const
private

Definition at line 228 of file values.cpp.

References CharBuffer::Append(), buf, CharBuffer::Empty(), CharBuffer::EnsureSize(), GetFirstVariable(), Variable::GetName(), CharBuffer::GetString(), NumeralSystem::GetText(), Variable::GetValue(), Variable::Next, Program::Output, and StrLen().

Referenced by List(), and ListDefinitions().

229 {
230  buf->Empty();
231 
232  if (GetFirstVariable() == nullptr)
233  {
234  return static_cast<char*>(cmdFormat ? nullptr : HELPVARSNDEF);
235  }
236 
237  int len = 0;
238  Variable* current = GetFirstVariable();
239 
240  while (current != nullptr)
241  {
242  len += StrLen(current->GetName());
243  len += 3;
244  len += 32; // Max length of value
245  len += cmdFormat ? 2 : 1;
246  current = current->Next;
247  }
248 
249  current = GetFirstVariable();
250 
251  buf->EnsureSize(len);
252 
253  while (current != nullptr)
254  {
255  buf->Append(current->GetName());
256  buf->Append(" = ");
257 
258  Number* num = current->GetValue();
259  const char* val = Program->Output->GetText(num);
260  buf->Append(val);
261  delete num;
262 
263  if (cmdFormat)
264  {
265  buf->Append(';');
266  }
267 
268  buf->Append(NEWLINE);
269  current = current->Next;
270  }
271 
272  return buf->GetString();
273 }
#define NEWLINE
Definition: amath.h:222
Master control class.
Definition: program.h:55
void Empty()
Definition: charbuf.cpp:218
CharBuffer * buf
Definition: values.h:68
Number * GetValue() const
Definition: values.cpp:74
char * GetString() const
Definition: charbuf.cpp:306
A user defined variable.
Definition: values.h:76
void Append(const char *source)
Definition: charbuf.cpp:262
Definition: numb.h:66
virtual const char * GetText(Number *number)=0
#define nullptr
Definition: amath.h:116
char * GetName() const
Definition: values.cpp:69
Variable * GetFirstVariable() const
Definition: values.cpp:155
#define HELPVARSNDEF
Definition: text.h:91
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
Variable * Next
Definition: values.h:86
class NumeralSystem * Output
Definition: program.h:76
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ListDefinitions()

char * VariableList::ListDefinitions ( ) const

Definition at line 223 of file values.cpp.

References ListContent().

Referenced by SaveStatement::Execute().

224 {
225  return ListContent(true);
226 }
char * ListContent(bool cmdFormat) const
Definition: values.cpp:228
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveTemporaryVariable()

void VariableList::RemoveTemporaryVariable ( )

Definition at line 210 of file values.cpp.

References first, and Variable::Next.

Referenced by Parser::ParseFunctionDef().

211 {
212  // Temporary variables are not owned by the variable list. Do not free memory.
213  Variable* newFirst = first->Next;
214  first->Next = nullptr;
215  first = newFirst;
216 }
A user defined variable.
Definition: values.h:76
Variable * first
Definition: values.h:69
Variable * Next
Definition: values.h:86
Here is the caller graph for this function:

Member Data Documentation

◆ buf

CharBuffer* VariableList::buf
private

Definition at line 68 of file values.h.

Referenced by Clear(), ListContent(), VariableList(), and ~VariableList().

◆ first


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