Debian Codegraph @

src/bin/geomutil/vrml2oogl/lib/QvNode.c++

raw
1 #include <ctype.h>
2 #include <QvDB.h>
3 #include <QvInput.h>
4 #include <QvDebugError.h>
5 #include <QvReadError.h>
6 #include <QvField.h>
7 #include <QvFieldData.h>
8 #include <QvNodes.h>
9 #include <QvUnknownNode.h>
10
11 // The global name dictionary
12 QvDict *QvNode::nameDict = NULL;
13
14 // Syntax for writing instances to files
15 #define OPEN_BRACE '{'
16 #define CLOSE_BRACE '}'
17 #define DEFINITION_KEYWORD "DEF"
18 #define REFERENCE_KEYWORD "USE"
19 #define NULL_KEYWORD "NULL"
20
21 void
22 QvNode::init()
23 {
24 if (nameDict == NULL)
25 nameDict = new QvDict;
26 }
27
28 QvNode::QvNode()
29 {
30 refCount = 0;
31 objName = new QvName("");
32 }
33
34 QvNode::~QvNode()
35 {
36 if (! !(*objName))
37 removeName(this, objName->getString());
38 delete objName;
39 }
40
41 void
42 QvNode::ref() const
43 {
44 ((QvNode *) this)->refCount++;
45 }
46
47 void
48 QvNode::unref() const
49 {
50 if (--((QvNode *) this)->refCount == 0)
51 delete (QvNode *) this;
52 }
53
54 void
55 QvNode::unrefNoDelete() const
56 {
57 ((QvNode *) this)->refCount--;
58 }
59
60 const QvName &
61 QvNode::getName() const
62 {
63 return *objName;
64 }
65
66 void
67 QvNode::setName(const QvName &newName)
68 {
69 if (! !(*objName)) {
70 removeName(this, objName->getString());
71 }
72 delete objName;
73
74 const char *str = newName.getString();
75 QvBool isBad = 0;
76
77 if (newName.getLength() > 0 &&
78 !QvName::isNodeNameStartChar(str[0])) isBad = TRUE;
79
80 int i;
81 for (i = 1; i < newName.getLength() && !isBad; i++) {
82 isBad = !QvName::isNodeNameChar(str[i]);
83 }
84
85 if (isBad) {
86 QvString goodString;
87
88 if (!QvName::isNodeNameStartChar(str[0])) {
89 goodString += "_";
90 }
91 for (i = 0; i < newName.getLength(); i++) {
92 char temp[2];
93 temp[0] = str[i]; temp[1] = '\0';
94 if (!QvName::isNodeNameChar(str[i]))
95 goodString += "_";
96 else
97 goodString += temp;
98 }
99 #ifdef DEBUG
100 QvDebugError::post("QvNode::setName", "Bad characters in"
101 " name '%s'. Replacing with name '%s'",
102 str, goodString.getString());
103 #endif
104 objName = new QvName(goodString);
105 }
106 else {
107 objName = new QvName(newName);
108 }
109 if (! !(*objName)) {
110 addName(this, objName->getString());
111 }
112 }
113
114 void
115 QvNode::addName(QvNode *b, const char *name)
116 {
117 QvPList *list;
118 void *t;
119 if (!nameDict->find((u_long)name, t)) {
120 list = new QvPList;
121 nameDict->enter((u_long)name, list);
122 }
123 else {
124 list = (QvPList *)t;
125 }
126
127 list->append(b);
128 }
129
130 void
131 QvNode::removeName(QvNode *b, const char *name)
132 {
133 QvPList *list;
134 QvBool found;
135 void *t;
136 int i;
137
138 found = nameDict->find((u_long) name, t);
139
140 if (found) {
141 list = (QvPList *) t;
142 i = list->find(b);
143
144 if (i < 0)
145 found = FALSE;
146
147 else
148 list->remove(i);
149 }
150
151 if (! found)
152 QvDebugError::post("QvNode::removeName",
153 "Name \"%s\" (node %x) is not in dictionary",
154 name, b);
155 }
156
157 QvBool
158 QvNode::read(QvInput *in, QvNode *&node)
159 {
160 QvBool ret;
161 QvName name;
162
163 if (! in->read(name, TRUE)) {
164 node = NULL;
165 ret = in->headerOk;
166 }
167
168 else if (! name || name == NULL_KEYWORD) {
169 node = NULL;
170 ret = TRUE;
171 }
172
173 else if (name == REFERENCE_KEYWORD) {
174 node = readReference(in);
175 ret = (node != NULL);
176 }
177
178 else
179 ret = readNode(in, name, node);
180
181 return ret;
182 }
183
184 QvBool
185 QvNode::readInstance(QvInput *in)
186 {
187 QvName typeString;
188 QvFieldData *fieldData = getFieldData();
189
190 if (in->read(typeString, TRUE)) {
191 if (typeString == "fields") {
192 if (! fieldData->readFieldTypes(in, this)) {
193 QvReadError::post(in, "Bad field specifications for node");
194 return FALSE;
195 }
196 }
197 else
198 in->putBack(typeString.getString());
199 }
200
201 if (! fieldData->read(in, this))
202 return FALSE;
203
204 return TRUE;
205 }
206
207 QvNode *
208 QvNode::readReference(QvInput *in)
209 {
210 QvName refName;
211 QvNode *node;
212
213 if (! in->read(refName, FALSE)) {
214 QvReadError::post(in, "Premature end of file after "
215 REFERENCE_KEYWORD);
216 node = NULL;
217 }
218
219 else if ((node = in->findReference(refName)) == NULL)
220 QvReadError::post(in, "Unknown reference \"%s\"",
221 refName.getString());
222
223 return node;
224 }
225
226 QvBool
227 QvNode::readNode(QvInput *in, QvName &className, QvNode *&node)
228 {
229 QvBool gotChar;
230 QvName refName;
231 char c;
232 QvBool ret = TRUE, flush = FALSE;
233
234 node = NULL;
235
236 if (className == DEFINITION_KEYWORD) {
237 if (! in->read(refName, FALSE) || ! in->read(className, TRUE)) {
238 QvReadError::post(in, "Premature end of file after "
239 DEFINITION_KEYWORD);
240 ret = FALSE;
241 }
242
243 if (! refName) {
244 QvReadError::post(in, "No name given after ", DEFINITION_KEYWORD);
245 ret = FALSE;
246 }
247
248 if (! className) {
249 QvReadError::post(in, "Invalid definition of %s",
250 refName.getString());
251 ret = FALSE;
252 }
253 }
254
255 if (ret) {
256
257 if (! (gotChar = in->read(c)) || c != OPEN_BRACE) {
258 if (gotChar)
259 QvReadError::post(in, "Expected '%c'; got '%c'",
260 OPEN_BRACE, c);
261 else
262 QvReadError::post(in, "Expected '%c'; got EOF", OPEN_BRACE);
263 ret = FALSE;
264 }
265
266 else {
267 ret = readNodeInstance(in, className, refName, node);
268
269 if (! ret)
270 flush = TRUE;
271
272 else if (! (gotChar = in->read(c)) || c != CLOSE_BRACE) {
273 if (gotChar)
274 QvReadError::post(in, "Expected '%c'; got '%c'",
275 CLOSE_BRACE, c);
276 else
277 QvReadError::post(in, "Expected '%c'; got EOF",
278 CLOSE_BRACE);
279 ret = FALSE;
280 }
281 }
282 }
283
284 if (! ret && flush)
285 flushInput(in);
286
287 return ret;
288 }
289
290 QvBool
291 QvNode::readNodeInstance(QvInput *in, const QvName &className,
292 const QvName &refName, QvNode *&node)
293 {
294 node = createInstance(in, className);
295 if (node == NULL)
296 return FALSE;
297
298 if (! (! refName))
299 in->addReference(refName, node);
300
301 return node->readInstance(in);
302 }
303
304 QvNode *
305 QvNode::createInstance(QvInput *in, const QvName &className)
306 {
307 QvNode *instance;
308 QvString unknownString;
309
310 instance = createInstanceFromName(className);
311
312 if (instance == NULL) {
313
314 if (! in->read(unknownString) || unknownString != "fields") {
315 QvReadError::post(in, "Unknown class \"%s\"",
316 className.getString());
317 return NULL;
318 }
319
320 else if (unknownString == "fields") {
321 QvUnknownNode *tmpNode = new QvUnknownNode;
322 tmpNode->setClassName(className.getString());
323 instance = tmpNode;
324 in->putBack(unknownString.getString());
325 }
326 }
327
328 return instance;
329 }
330
331 QvNode *
332 QvNode::createInstanceFromName(const QvName &className)
333 {
334 #define TRY_CLASS(name, class) \
335 else if (className == name) \
336 inst = new class
337
338 QvNode *inst = NULL;
339
340 if (0) ; // So "else" works in first TRY_CLASS
341 TRY_CLASS("AsciiText", QvAsciiText);
342 TRY_CLASS("Cone", QvCone);
343 TRY_CLASS("Coordinate3", QvCoordinate3);
344 TRY_CLASS("Cube", QvCube);
345 TRY_CLASS("Cylinder", QvCylinder);
346 TRY_CLASS("DirectionalLight", QvDirectionalLight);
347 TRY_CLASS("FontStyle", QvFontStyle);
348 TRY_CLASS("Group", QvGroup);
349 TRY_CLASS("IndexedFaceSet", QvIndexedFaceSet);
350 TRY_CLASS("IndexedLineSet", QvIndexedLineSet);
351 TRY_CLASS("Info", QvInfo);
352 TRY_CLASS("LOD", QvLOD);
353 TRY_CLASS("Material", QvMaterial);
354 TRY_CLASS("MaterialBinding", QvMaterialBinding);
355 TRY_CLASS("MatrixTransform", QvMatrixTransform);
356 TRY_CLASS("Normal", QvNormal);
357 TRY_CLASS("NormalBinding", QvNormalBinding);
358 TRY_CLASS("OrthographicCamera", QvOrthographicCamera);
359 TRY_CLASS("PerspectiveCamera", QvPerspectiveCamera);
360 TRY_CLASS("PointLight", QvPointLight);
361 TRY_CLASS("PointSet", QvPointSet);
362 TRY_CLASS("Rotation", QvRotation);
363 TRY_CLASS("Scale", QvScale);
364 TRY_CLASS("Separator", QvSeparator);
365 TRY_CLASS("ShapeHints", QvShapeHints);
366 TRY_CLASS("Sphere", QvSphere);
367 TRY_CLASS("SpotLight", QvSpotLight);
368 TRY_CLASS("Switch", QvSwitch);
369 TRY_CLASS("Texture2", QvTexture2);
370 TRY_CLASS("Texture2Transform", QvTexture2Transform);
371 TRY_CLASS("TextureCoordinate2", QvTextureCoordinate2);
372 TRY_CLASS("Transform", QvTransform);
373 TRY_CLASS("TransformSeparator", QvTransformSeparator);
374 TRY_CLASS("Translation", QvTranslation);
375 TRY_CLASS("WWWAnchor", QvWWWAnchor);
376 TRY_CLASS("WWWInline", QvWWWInline);
377
378 return inst;
379
380 #undef TRY_CLASS
381 }
382
383 void
384 QvNode::flushInput(QvInput *in)
385 {
386 int nestLevel = 1;
387 char c;
388
389 while (nestLevel > 0 && in->get(c)) {
390
391 if (c == CLOSE_BRACE)
392 nestLevel--;
393
394 else if (c == OPEN_BRACE)
395 nestLevel++;
396 }
397 }
398
399 #undef OPEN_BRACE
400 #undef CLOSE_BRACE
401 #undef DEFINITION_KEYWORD
402 #undef REFERENCE_KEYWORD
403 #undef NULL_KEYWORD