Debian Codegraph @

lib/database/src/so/fields/SoSFEnum.c++

raw symbols SCIP
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoSFEnum
47 |
48 | Author(s) : Paul S. Strauss
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54
55 #include <Inventor/errors/SoDebugError.h>
56 #include <Inventor/errors/SoReadError.h>
57 #include <Inventor/fields/SoSFEnum.h>
58
59 //////////////////////////////////////////////////////////////////////////////
60 //
61 // SoSFEnum class
62 //
63 //////////////////////////////////////////////////////////////////////////////
64
65 // Use standard definitions except for constructor/destructor
66 SO_SFIELD_REQUIRED_SOURCE(SoSFEnum);
67 SO_SFIELD_VALUE_SOURCE(SoSFEnum, int, int);
68
69 ////////////////////////////////////////////////////////////////////////
70 //
71 // Description:
72 // Constructor.
73 //
74 // Use: extender
75
76 SoSFEnum::SoSFEnum()
77 //
78 ////////////////////////////////////////////////////////////////////////
79 {
80 enumValues = NULL;
81 enumNames = NULL;
82 numEnums = 0;
83 legalValuesSet = FALSE;
84 }
85
86 ////////////////////////////////////////////////////////////////////////
87 //
88 // Description:
89 // Destructor.
90 //
91 // Use: extender
92
93 SoSFEnum::~SoSFEnum()
94 //
95 ////////////////////////////////////////////////////////////////////////
96 {
97 if (!legalValuesSet && numEnums > 0) {
98 delete[] enumValues;
99 delete[] enumNames;
100 }
101 }
102
103 ////////////////////////////////////////////////////////////////////////
104 //
105 // Description:
106 // Sets value from given SbName, which must contain the name of a
107 // valid enumerator.
108 //
109 // Use: public
110
111 void
112 SoSFEnum::setValue(const SbName &name)
113 //
114 ////////////////////////////////////////////////////////////////////////
115 {
116 int enumVal;
117
118 if (findEnumValue(name, enumVal))
119 setValue(enumVal);
120
121 #ifdef DEBUG
122 else
123 SoDebugError::post("SoSFEnum::setValue",
124 "No enum for name \"%s\"", name.getString());
125 #endif /* DEBUG */
126 }
127
128 ////////////////////////////////////////////////////////////////////////
129 //
130 // Description:
131 // Called by nodes to set legal enum names and values
132 //
133 // Use: extender public
134
135 void
136 SoSFEnum::setEnums(int num, int *vals, SbName *names)
137
138 //
139 ////////////////////////////////////////////////////////////////////////
140 {
141 #ifdef DEBUG
142 if (legalValuesSet == TRUE) {
143 SoDebugError::post("SoSFEnum::setEnums",
144 "setEnums called twice");
145 }
146 #endif
147
148 legalValuesSet = TRUE;
149 numEnums = num;
150 enumValues = vals;
151 enumNames = names;
152 }
153
154 ////////////////////////////////////////////////////////////////////////
155 //
156 // Description:
157 // Looks up enum name, returns value. Returns FALSE if not found.
158 //
159 // Use: protected
160
161 SbBool
162 SoSFEnum::findEnumValue(const SbName &name, int &val)
163 //
164 ////////////////////////////////////////////////////////////////////////
165 {
166 int i;
167
168 // Look through names table for one that matches
169 for (i = 0; i < numEnums; i++) {
170 if (name == enumNames[i]) {
171 val = enumValues[i];
172 return TRUE;
173 }
174 }
175
176 if (!legalValuesSet) {
177 // Must be part of an unknown node, add name, value:
178 int *oldValues = enumValues;
179 SbName *oldNames = enumNames;
180 enumValues = new int[numEnums+1];
181 enumNames = new SbName[numEnums+1];
182 if (numEnums != 0) {
183 for (i = 0; i < numEnums; i++) {
184 enumValues[i] = oldValues[i];
185 enumNames[i] = oldNames[i];
186 }
187 delete[] oldValues;
188 delete[] oldNames;
189 }
190 val = numEnums;
191 enumValues[numEnums] = numEnums;
192 enumNames[numEnums] = name;
193 ++numEnums;
194 return TRUE;
195 }
196
197 return FALSE;
198 }
199
200 ////////////////////////////////////////////////////////////////////////
201 //
202 // Description:
203 // Looks up enum value, returns pointer to name. Returns FALSE if
204 // not found.
205 //
206 // Use: protected
207
208 SbBool
209 SoSFEnum::findEnumName(int val, const SbName *&name) const
210 //
211 ////////////////////////////////////////////////////////////////////////
212 {
213 int i;
214
215 // Find string corresponding to given value
216 for (i = 0; i < numEnums; i++) {
217 if (val == enumValues[i]) {
218 name = &enumNames[i];
219 return TRUE;
220 }
221 }
222
223 return FALSE;
224 }
225
226 ////////////////////////////////////////////////////////////////////////
227 //
228 // Description:
229 // Reads value from file. Returns FALSE on error.
230 //
231 // Use: private
232
233 SbBool
234 SoSFEnum::readValue(SoInput *in)
235 //
236 ////////////////////////////////////////////////////////////////////////
237 {
238 SbName n;
239
240 // Read mnemonic value as a character string identifier
241 if (! in->read(n, TRUE))
242 return FALSE;
243
244 if (findEnumValue(n, value))
245 return TRUE;
246
247 // Not found? Too bad
248 SoReadError::post(in, "Unknown SoSFEnum enumeration value \"%s\"",
249 n.getString());
250 return FALSE;
251 }
252
253 ////////////////////////////////////////////////////////////////////////
254 //
255 // Description:
256 // Writes value of field to file.
257 //
258 // Use: private
259
260 void
261 SoSFEnum::writeValue(SoOutput *out) const
262 //
263 ////////////////////////////////////////////////////////////////////////
264 {
265 const SbName *n;
266
267 #ifdef DEBUG
268 if (enumValues == NULL) {
269 SoDebugError::post("SoSFEnum::writeValue",
270 "Enum values were never initialized");
271 return;
272 }
273 #endif /* DEBUG */
274
275 // Find and write string corresponding to given value
276 if (findEnumName(value, n))
277 out->write(n->getString());
278
279 // Whoops! Something bad was in the field value
280 else {
281 SoDebugError::post("SoSFEnum::writeValue",
282 "Illegal value (%d) in field", value);
283 }
284 }