amath  1.8.5
Simple command line calculator
preferences.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 "preferences.h"
33 #include "lib/numb.h"
34 #include "lib/real.h"
35 #include "lib/ntextd.h"
36 #include "lib/charbuf.h"
37 #include "main/lexer.h"
38 #include "main/nodes.h"
39 #include "main/parser.h"
40 
42 {
43  buf = new CharBuffer();
45 }
46 
48 {
49  delete buf;
50  delete prompt;
51 }
52 
54 {
55  AllocAndCopy(&this->prompt, "> ");
56  digits = 9;
57  refactorNames = false;
58 }
59 
61 {
62  static char* promptkw = Lexer::FindKeyword(symprompt);
63  static char* digitkw = Lexer::FindKeyword(symdigits);
64  static char delimiter = *(Lexer::FindKeyword(symdelimiter));
65  static char promptq = '"';
66 
68  NumeralSystem* ns = new DecimalSystem(2);
69  const char* dtext = ns->GetText(d);
70  delete d;
71 
74  StrLen(promptkw) + sizeof(SPACE) + sizeof(promptq) +
75  StrLen(prompt) + sizeof(promptq) + sizeof(delimiter) +
76  StrLen(digitkw) + sizeof(SPACE) + StrLen(dtext) + sizeof(delimiter) + sizeof(char));
77 
78  buf->Append(promptkw);
80  buf->Append(promptq);
82  buf->Append(promptq);
83  buf->Append(delimiter);
84  buf->Append(digitkw);
86  buf->Append(dtext);
87  buf->Append(delimiter);
88 
89  delete ns;
90  return buf->GetString();
91 }
92 
93 void PreferencesBase::SetPrefs(char* prefs)
94 {
95  if (prefs == nullptr)
96  {
97  return;
98  }
99 
100  Parser* parser = new Parser(prefs);
101  SyntaxNode* node = parser->Parse();
102  delete parser;
103  node->Execute();
104  delete node;
105 }
106 
108 {
109  return prompt;
110 }
111 
112 void PreferencesBase::SetPrompt(const char* prompt)
113 {
114  delete this->prompt;
115  AllocAndCopy(&this->prompt, prompt);
116 }
117 
119 {
120  return digits;
121 }
122 
123 void PreferencesBase::SetDigits(int digits)
124 {
125  this->digits = digits;
126 }
127 
129 {
130  return refactorNames;
131 }
132 
134 {
135  refactorNames = yes;
136 }
char * GetDescription() const
Definition: preferences.cpp:60
void Append(const char c)
Definition: charbuf.cpp:245
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
Encapsulates an lexical analyzer. Provides token for the parser.
Definition: lexer.h:48
#define SPACE
Definition: amath.h:214
Base class for all nodes in a syntax tree.
Definition: nodes.h:65
void Append(const char *source)
Definition: charbuf.cpp:262
Definition: numb.h:66
virtual const char * GetText(Number *number)=0
CharBuffer()
Initialize without allocating memory.
Definition: charbuf.cpp:38
static char * FindKeyword(Symbol symbol)
Definition: lexer.cpp:248
virtual char * Execute()=0
void SetDigits(int digits)
Represent a real number with 15 significant digits.
Definition: real.h:45
CharBuffer * buf
Definition: preferences.h:56
Parser(const char *input)
Definition: parser.cpp:43
static void SetPrefs(char *prefs)
Definition: preferences.cpp:93
virtual ~PreferencesBase()
Definition: preferences.cpp:47
void SetPrompt(const char *prompt)
bool GetRefactorNames()
void SetRefactorNames(bool yes)
RealNumber(signed int i)
Definition: real.cpp:71
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
char * GetPrompt() const
Base class for all numeral systems.
Definition: ntext.h:49
DecimalSystem(unsigned int digits)
Definition: ntextd.cpp:66
SyntaxNode * Parse()
Parses the input into a syntax tree.
Definition: parser.cpp:56
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
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
int GetDigits() const
Encapsulates a recursive descent parser.
Definition: parser.h:49