amath  1.8.5
Simple command line calculator
delete.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 "delete.h"
31 #include "amath.h"
32 #include "amathc.h"
33 #include "loc/text.h"
34 #include "system/program.h"
35 
36 /**
37  * @brief Constructor used to delete either all variable or functions.
38  *
39  */
42 {
43  type = symbol;
44  name = nullptr;
45  argument = nullptr;
46 }
47 
48 /**
49  * @brief Constructor used to delete a Variable.
50  *
51  */
52 DeleteStatement::DeleteStatement(const char* name) :
54 {
55  type = symvariable;
56  AllocAndCopy(&this->name, name);
57  argument = nullptr;
58 }
59 
60 /**
61  * @brief Constructor used to delete a function.
62  *
63  */
64 DeleteStatement::DeleteStatement(const char* name, const char* argument) :
66 {
67  type = symfunction;
68  AllocAndCopy(&this->name, name);
69  AllocAndCopy(&this->argument, argument);
70 }
71 
72 
74 {
75  if (name != nullptr)
76  {
77  delete [] name;
78  }
79 
80  if (argument != nullptr)
81  {
82  delete [] argument;
83  }
84 }
85 
87 {
88  bool success = true;
90 
91  if (type == symvariable && name == nullptr)
92  {
93  Program->Variables->Clear();
94  }
95  else if (type == symvariable && name != nullptr)
96  {
97  success = Program->Variables->Delete(name);
98  const char* msg = HELPVARNDEF;
99 
101  StrLen(msg) +
102  StrLen(name) +
103  StrLen(NEWLINE) + 1);
104 
105  output->Append(msg);
108  }
109  else if (type == symfunction && name == nullptr)
110  {
111  Program->Functions->Clear();
112  }
113  else if (type == symfunction && name != nullptr)
114  {
115  success = Program->Functions->Delete(name, argument);
116  const char* msg = HELPFUNNDEF;
117 
119  StrLen(msg) +
120  StrLen(name) + 2 +
122  StrLen(NEWLINE) + 1);
123 
124  output->Append(msg);
126  output->Append("(");
128  output->Append(")");
130  }
131 
132  char* temp = success
133  ? statementText
135 
136  return temp;
137 }
#define NEWLINE
Definition: amath.h:222
char * name
Definition: delete.h:50
Delete variable or function.
Definition: delete.h:39
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
char * Execute()
Definition: delete.cpp:86
char * statementText
Definition: node.h:55
Base class for all statements in a syntax tree.
Definition: node.h:40
char * argument
Definition: delete.h:51
DeleteStatement(const char *name, const char *argument)
Constructor used to delete a function.
Definition: delete.cpp:64
void Append(const char *source)
Definition: charbuf.cpp:262
class FunctionList * Functions
Definition: program.h:78
#define HELPVARNDEF
Definition: text.h:80
void Clear()
Definition: values.cpp:109
#define HELPFUNNDEF
Definition: text.h:81
Symbol type
Definition: delete.h:49
StatementNode()
Definition: node.cpp:34
class VariableList * Variables
Definition: program.h:77
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
CharBuffer * output
Definition: nodes.h:85
DeleteStatement(Symbol symbol)
Constructor used to delete either all variable or functions.
Definition: delete.cpp:40
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
bool Delete(const char *name, const char *argument)
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
friend bool VariableList::Delete(const char *name)
DeleteStatement(const char *name)
Constructor used to delete a Variable.
Definition: delete.cpp:52