comparison anagram/agcore/configparam.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * configparam.cpp
7 */
8
9 #include "agstring.h"
10 #include "configparam.h"
11 #include "rpk.h"
12 #include "rule.h"
13 #include "stacks.h"
14
15 //#define INCLUDE_LOGGING
16 #include "log.h"
17
18
19 ConfigParam::ConfigParam(const char *n)
20 : name(n)
21 {}
22
23 void ConfigParam::setIntegerValue(const int, ErrorHandler &e) {
24 LOGSECTION("ConfigParam::setValue(int)");
25 char buf[200];
26 sprintf(buf, "Parameter takes string value: %s", name);
27 e.badParam(buf);
28 }
29
30 void ConfigParam::setStringValue(const AgString, ErrorHandler &e) {
31 LOGSECTION("ConfigParam::setValue(AgString)");
32 char buf[200];
33 sprintf(buf, "Parameter takes integer value: %s", name);
34 e.badParam(buf);
35 }
36
37 void ConfigParam::set(int k, ErrorHandler &e) {
38 LOGSECTION("ConfigParam::set(int)");
39 LOGV(string_base);
40 AgString name = buildAgString();
41 LOGV(name);
42 strlwr(name.pointer());
43 LOGV(name);
44 int i;
45 for (i = 0; i < paramCount; i++) {
46 if (name != table[i]->name) {
47 continue;
48 }
49 table[i]->setIntegerValue(k, e);
50 return;
51 }
52 char buf[500];
53 sprintf(buf, "Parameter not defined: %s", name.pointer());
54 e.badParam(buf);
55 }
56
57 void ConfigParam::set(ErrorHandler &e) {
58 LOGSECTION("ConfigParam::set()");
59 AgString param = buildAgString();
60
61 AgString name = buildAgString();
62 strlwr(name.pointer());
63 LOGV(name) LCV(param);
64 int i;
65 for (i = 0; i < paramCount; i++) {
66 if (name != table[i]->name) continue;
67 table[i]->setStringValue(param, e);
68 return;
69 }
70 char buf[500];
71 sprintf(buf, "Parameter not defined: %s", name.pointer());
72 e.badParam(buf);
73 }
74
75 void ConfigParam::resetAll() {
76 LOGSECTION("ConfigParam::resetAll");
77 int i;
78 LOGV(paramCount);
79 for (i = 0; i < paramCount; i++) {
80 //LOGV(table[i]->asString());
81 table[i]->reset();
82 LOGV(table[i]->asString());
83 }
84 }
85
86 void ConfigParam::initAll() {
87 LOGSECTION("ConfigParam::initAll");
88 int i;
89 LOGV(paramCount);
90 for (i = 0; i < paramCount; i++) {
91 LOGV(i) LCV(table[i]->asString());
92 table[i]->init();
93 LOGV(table[i]->asString());
94 }
95 }
96
97 void IntegerParam::setIntegerValue(const int x, ConfigParam::ErrorHandler &) {
98 parameter = x;
99 }
100
101 void ConfigSwitch::setIntegerValue(const int x, ConfigParam::ErrorHandler &) {
102 parameter = x;
103 }
104
105 void ConfigSwitch::setStringValue(const AgString s, ConfigParam::ErrorHandler &e) {
106 char *pointer = s.pointer();
107 strlwr(pointer);
108 if (s == "off") {
109 parameter = 0;
110 }
111 else if (s == "on") {
112 parameter = 1;
113 }
114 else {
115 char buf[100];
116 sprintf(buf, "Switch takes on/off values only: %s", name);
117 e.badParam(buf);
118 }
119 }
120
121 void StringParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &) {
122 parameter = s;
123 }
124
125 void TextParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &) {
126 parameter = s;
127 }
128
129 CastParam::CastParam(const char *name, int &p)
130 : Param<int>(name, p)
131 {}
132
133 void CastParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &e) {
134 LOGSECTION("CastParam::setValue(AgString)");
135 Cast type(s);
136 if (strcmp(name, "default token type") == 0 && type.wrapperRequired()) {
137 e.badParam("Token with wrapper cannot be default token type");
138 return;
139 }
140 parameter = type;
141 }
142
143 AgString CastParam::asString() {
144 LOGSECTION("CastParam::asString()");
145 LOGV(parameter);
146 LOGV((int) Cast(parameter)) LCV((AgString) Cast(parameter));
147
148 if (parameter) {
149 return Cast(parameter).name();
150 }
151 else {
152 return AgString("UNDEFINED");
153 }
154 }
155
156 TokenParam::TokenParam(const char *name, int &p)
157 : Param<int>(name, p)
158 {
159 LOGSECTION("TokenParam::TokenParam");
160 LOGV(name) LCV(p);
161 }
162
163 void TokenParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &) {
164 LOGSECTION("TokenParam::setValue(AgString)");
165 parameter = id_token(Symbol(s.pointer()));
166 }
167
168 AgString TokenParam::asString() {
169 LOGSECTION("TokenParam::asString");
170 LOGV(parameter) LCV(Token::count());
171 Token token(parameter);
172 //AgString name = Token(parameter)->token_name->string;
173 AgString name = token.isNotNull() ? token->token_name->string :
174 AgString("UNDEFINED");
175 LOGV(name);
176 //if (name.size() == 0) name = "UNDEFINED";
177 return name;
178 }
179
180 AgString ConfigSwitch::asString() {
181 return AgString(parameter ? "ON" : "OFF");
182 }
183
184 AgString StringParam::asString() {
185 char buf[200];
186 if (parameter.exists()) {
187 sprintf(buf, "\"%s\"", parameter.pointer());
188 return AgString(buf);
189 }
190 return AgString("UNDEFINED");
191 }
192
193 AgString TextParam::asString() {
194 if (parameter.exists()) return parameter;
195 return AgString("UNDEFINED");
196 }
197
198 AgString IntegerParam::asString() {
199 char buf[20];
200 sprintf(buf, "%d", parameter);
201 return AgString(buf);
202 }