amath  1.8.5
Simple command line calculator
program_stdc.cpp
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #include "amath.h"
31 #include "amathc.h"
32 #include "program_stdc.h"
33 #include "console_stdc.h"
34 #include "console_termios.h"
35 #include "console_windows.h"
36 #include "preferences_stdc.h"
37 #include "lib/charbuf.h"
38 #include "main/evaluator.h"
39 
40 #if !defined(AMIGA) && !defined(HAIKU)
41 
43  : Program()
44 {
45  Console = nullptr;
46  line = nullptr;
47  shellMode = false;
48 }
49 
51 {
52  if (Console != nullptr)
53  {
55  delete Console;
56  }
57 
58  if (line != nullptr)
59  {
60  delete line;
61  }
62 }
63 
64 void StandardProgram::Initialize(int argc, char** argv)
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();
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;
103  line->Append(argv[i]);
104  line->Append(' ');
105  }
106  }
107 
108  if (len > 1)
109  {
111  }
112 }
113 
115 {
116  if(Console == nullptr || !Console->Open())
117  {
118  return;
119  }
120 
122 
123  if (!line->IsEmpty())
124  {
125  Evaluator* evaluator = new Evaluator(line->GetString());
126  evaluator->Evaluate();
127  const char* res = evaluator->GetResult();
130  delete evaluator;
131  return;
132  }
133 
134 #if defined(WINDOWS)
135  Console->Start();
136 #else
137  if (shellMode)
138  {
140  return;
141  }
142 
143  SetAnsiMode(false);
145 #endif
146 }
147 
148 #endif
#define TERMIOS
Definition: amath.h:89
void Append(const char c)
Definition: charbuf.cpp:245
virtual void Start()
Master control class.
Definition: program.h:55
void Empty()
Definition: charbuf.cpp:218
char * GetResult() const
Definition: evaluator.cpp:91
char * GetString() const
Definition: charbuf.cpp:306
virtual bool Load()=0
virtual void Start()=0
virtual ~StandardProgram()
CharBuffer * line
Definition: program_stdc.h:45
void SetAnsiMode(bool value)
Definition: program.cpp:148
void Append(const char *source)
Definition: charbuf.cpp:262
CharBuffer()
Initialize without allocating memory.
Definition: charbuf.cpp:38
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
virtual void Close()
Definition: console.cpp:105
virtual void ResetConsole()
Definition: console.cpp:115
virtual void WriteString(const char *string)=0
virtual void Initialize(int argc, char **argv)
Evaluator(const char *input)
Definition: evaluator.cpp:36
virtual void ShowHelp()
Definition: console.cpp:158
Program()
Definition: program.cpp:48
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
void Evaluate() const
Definition: evaluator.cpp:50
char * GetPrompt() const
virtual bool Open()
Definition: console.cpp:100
bool IsEmpty() const
Definition: charbuf.cpp:174
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