amath  1.8.5
Simple command line calculator
StandardProgram Class Reference

#include <program_stdc.h>

Inheritance diagram for StandardProgram:
Collaboration diagram for StandardProgram:

Public Member Functions

 StandardProgram ()
 
virtual ~StandardProgram ()
 
virtual void Initialize (int argc, char **argv)
 
virtual void Start ()
 
- Public Member Functions inherited from Program
 Program ()
 
virtual ~Program ()
 
virtual void Exit ()
 
void NewPositionalInput (short unsigned int base, short unsigned int digits)
 
void NewPositionalOutput (short unsigned int base, short unsigned int digits)
 
void SetLastResult (Number *number)
 
void SetPrompt (const char *text) const
 
int GetExitStatus () const
 
bool GetAnsiMode () const
 
void SetAnsiMode (bool value)
 
struct NumberGetLastResult () const
 

Private Attributes

CharBufferline
 

Additional Inherited Members

- Public Attributes inherited from Program
class LanguageLanguage
 
class ConsoleBaseConsole
 
class PreferencesBasePreferences
 
class FilesystemBaseFilesystem
 
class NumeralSystemInput
 
class NumeralSystemOutput
 
class VariableListVariables
 
class FunctionListFunctions
 
class GraphList * Graphs
 
- Protected Member Functions inherited from Program
void InitAnsiMode ()
 
- Protected Attributes inherited from Program
bool shellMode
 
bool ansiMode
 
int status
 

Detailed Description

Definition at line 36 of file program_stdc.h.

Constructor & Destructor Documentation

◆ StandardProgram()

StandardProgram::StandardProgram ( )

Definition at line 42 of file program_stdc.cpp.

References Program::Console, line, Program::Program(), and Program::shellMode.

Referenced by main().

43  : Program()
44 {
45  Console = nullptr;
46  line = nullptr;
47  shellMode = false;
48 }
CharBuffer * line
Definition: program_stdc.h:45
Program()
Definition: program.cpp:48
bool shellMode
Definition: program.h:83
class ConsoleBase * Console
Definition: program.h:72
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~StandardProgram()

StandardProgram::~StandardProgram ( )
virtual

Definition at line 50 of file program_stdc.cpp.

References ConsoleBase::Close(), Program::Console, and line.

51 {
52  if (Console != nullptr)
53  {
54  Console->Close();
55  delete Console;
56  }
57 
58  if (line != nullptr)
59  {
60  delete line;
61  }
62 }
CharBuffer * line
Definition: program_stdc.h:45
virtual void Close()
Definition: console.cpp:105
class ConsoleBase * Console
Definition: program.h:72
Here is the call graph for this function:

Member Function Documentation

◆ Initialize()

void StandardProgram::Initialize ( int  argc,
char **  argv 
)
virtual

Implements Program.

Definition at line 64 of file program_stdc.cpp.

References CharBuffer::Append(), CharBuffer::CharBuffer(), Program::Console, CharBuffer::DeleteLastChar(), CharBuffer::Empty(), CharBuffer::EnsureSize(), PreferencesBase::GetPrompt(), line, Program::Preferences, Program::SetAnsiMode(), Program::shellMode, StrIsEqual(), and StrLen().

65 {
66 #if defined(WINDOWS)
67  Console = new WindowsConsole(Preferences->GetPrompt(), Language);
68 #elif defined(TERMIOS)
69  Console = new TermiosConsole(Preferences->GetPrompt(), Language);
70 #else
71  Console = new StandardConsole(Preferences->GetPrompt(), Language);
72 #endif
73  SetAnsiMode(true);
74 
75  line = new CharBuffer();
76  line->Empty();
77 
78  bool options = true;
79  unsigned int len = 1;
80  for (int i = 1; i < argc; i++)
81  {
82  if (options)
83  {
84  if (StrIsEqual(argv[i], "noansi") || StrIsEqual(argv[i], "--noansi"))
85  {
86  SetAnsiMode(false);
87  continue;
88  }
89  else if (StrIsEqual(argv[i], "shell") || StrIsEqual(argv[i], "--shell"))
90  {
91  shellMode = true;
92  }
93  else
94  {
95  options = false;
96  }
97  }
98 
99  if (!options)
100  {
101  len += StrLen(argv[i]) + 1;
102  line->EnsureSize(len);
103  line->Append(argv[i]);
104  line->Append(' ');
105  }
106  }
107 
108  if (len > 1)
109  {
110  line->DeleteLastChar();
111  }
112 }
Encapsulates the IO of a Standard C console.
Definition: console_stdc.h:48
void Empty()
Definition: charbuf.cpp:218
CharBuffer * line
Definition: program_stdc.h:45
void SetAnsiMode(bool value)
Definition: program.cpp:148
void Append(const char *source)
Definition: charbuf.cpp:262
class Language * Language
Definition: program.h:71
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
void DeleteLastChar()
Definition: charbuf.cpp:228
bool shellMode
Definition: program.h:83
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
char * GetPrompt() const
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
class ConsoleBase * Console
Definition: program.h:72
class PreferencesBase * Preferences
Definition: program.h:73
Here is the call graph for this function:

◆ Start()

void StandardProgram::Start ( )
virtual

Implements Program.

Definition at line 114 of file program_stdc.cpp.

References Program::Console, Evaluator::Evaluate(), Evaluator::Evaluator(), Evaluator::GetResult(), CharBuffer::GetString(), CharBuffer::IsEmpty(), line, PreferencesBase::Load(), ConsoleBase::Open(), Program::Preferences, ConsoleBase::ResetConsole(), Program::SetAnsiMode(), Program::shellMode, ConsoleBase::ShowHelp(), ConsoleBase::Start(), and ConsoleBase::WriteString().

115 {
116  if(Console == nullptr || !Console->Open())
117  {
118  return;
119  }
120 
121  Preferences->Load();
122 
123  if (!line->IsEmpty())
124  {
125  Evaluator* evaluator = new Evaluator(line->GetString());
126  evaluator->Evaluate();
127  const char* res = evaluator->GetResult();
128  Console->WriteString(res);
130  delete evaluator;
131  return;
132  }
133 
134 #if defined(WINDOWS)
135  Console->Start();
136 #else
137  if (shellMode)
138  {
139  Console->Start();
140  return;
141  }
142 
143  SetAnsiMode(false);
144  Console->ShowHelp();
145 #endif
146 }
char * GetResult() const
Definition: evaluator.cpp:91
char * GetString() const
Definition: charbuf.cpp:306
virtual bool Load()=0
virtual void Start()=0
CharBuffer * line
Definition: program_stdc.h:45
void SetAnsiMode(bool value)
Definition: program.cpp:148
virtual void ResetConsole()
Definition: console.cpp:115
virtual void WriteString(const char *string)=0
virtual void ShowHelp()
Definition: console.cpp:158
bool shellMode
Definition: program.h:83
void Evaluate() const
Definition: evaluator.cpp:50
virtual bool Open()
Definition: console.cpp:100
bool IsEmpty() const
Definition: charbuf.cpp:174
class ConsoleBase * Console
Definition: program.h:72
class PreferencesBase * Preferences
Definition: program.h:73
Here is the call graph for this function:

Member Data Documentation

◆ line

CharBuffer* StandardProgram::line
private

Definition at line 45 of file program_stdc.h.

Referenced by Initialize(), StandardProgram(), Start(), and ~StandardProgram().


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