-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.cpp
More file actions
119 lines (100 loc) · 2.67 KB
/
Copy pathterminal.cpp
File metadata and controls
119 lines (100 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
////////////////////////////////////////////////////////////
// terminal.c - implementation for S-Expression terminals
//
// written by Larry I. Gritz, 1993
// The George Washington University, Dept. of EE&CS
// Computer Graphics and Animation Lab
////////////////////////////////////////////////////////////
#include <iostream>
#include <malloc.h>
#include <string>
#include <stdio.h>
#include "gp.h"
using namespace std;
// Constructor for a TerminalSet
TerminalSet::TerminalSet (void)
{
n = 0; // Currently no terminals in the list
maxn = 16; // Allocate room for 16 terminals to start
terminals = (Terminal *) malloc (maxn*sizeof(Terminal));
}
// Destructor
TerminalSet::~TerminalSet (void)
{
if (terminals)
{
//for (int i = 0; i < n; ++i)
//free (terminals[i].name);
//delete terminals;
//free (terminals);
}
}
// Add a new terminal to the set
void TerminalSet::add (const char *name, float val)
{
// First, check to see if the name is already in the list
for (int i = 0; i < n; ++i)
{
if (! strcmp (terminals[i].name, name))
{
cout << "TerminalSet Error: attempt to add existing terminal: " << terminals[i].name << '\n';
return;
}
}
// If we need more space, allocate it
if (n == maxn - 1)
{
maxn *= 2;
terminals = (Terminal *) realloc (terminals,
maxn * sizeof (Terminal));
}
// Okay, now add it
terminals[n].name = strdup (name);
terminals[n++].val = val;
}
// Modify the value of a terminal in the set
//
void TerminalSet::modify (const char *name, float val)
{
for (int i = 0; i < n; ++i)
{
if (! strcmp (terminals[i].name, name))
{
terminals[i].val = val;
return;
}
}
// Oops! We didn't find the terminal
cout << "TerminalSet Error: attempt to modify non-existing terminal: " << name << '\n';
}
// Retrieve the current value of a terminal
float TerminalSet::get (const char *name)
{
for (int i = 0; i < n; ++i)
if (! strcmp (terminals[i].name, name))
return terminals[i].val;
// Oops! We didn't find the terminal
cout << "TerminalSet Error: could not find terminal: " << name << '\n';
return 0;
}
// Return the index number for a named terminal
int TerminalSet::index (const char *name)
{
for (int i = 0; i < n; ++i)
if (! strcmp (terminals[i].name, name))
return i;
// Oops! We didn't find the terminal
cout << "TerminalSet Error: could not index terminal: " << name << '\n';
return -1;
}
// Print the entire table of terminals
void TerminalSet::print (void)
{
cout << "\nTerminal set listing:";
cout << "\n---------------------\n";
for (int i = 0; i < n; ++i)
cout << '\"' << terminals[i].name << "\" = " << terminals[i].val << '\n';
cout << '\n';
}
// Declaration for the globally visible terminal set
TerminalSet Tset;