Debian Codegraph @

lib/database/src/so/SoTranscribe.c++

raw
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 | SoTranSender, SoTranReceiver
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/SoDB.h>
56 #include <Inventor/SoInput.h>
57 #include <Inventor/SoOutput.h>
58 #include <Inventor/misc/SoTranscribe.h>
59 #include <Inventor/actions/SoWriteAction.h>
60 #include <Inventor/errors/SoDebugError.h>
61 #include <Inventor/fields/SoFieldData.h>
62 #include <Inventor/nodes/SoGroup.h>
63
64 // Internal command codes
65 #define TRAN_INSERT 0
66 #define TRAN_INSERTP 1
67 #define TRAN_REMOVE 2
68 #define TRAN_REPLACE 3
69 #define TRAN_MODIFY 4
70 #define TRAN_END 5
71 #define TRAN_NUM 6 /* Number of transcription commands */
72
73 //////////////////////////////////////////////////////////////////////////////
74 //
75 // SoTranSender class
76 //
77 //////////////////////////////////////////////////////////////////////////////
78
79 ////////////////////////////////////////////////////////////////////////
80 //
81 // Description:
82 // Constructor - takes SoOutput to write transcription to.
83 //
84 // Use: public
85
86 SoTranSender::SoTranSender(SoOutput *output)
87 //
88 ////////////////////////////////////////////////////////////////////////
89 {
90 out = output;
91 }
92
93 ////////////////////////////////////////////////////////////////////////
94 //
95 // Description:
96 // Adds INSERT command to output.
97 //
98 // Use: public
99
100 void
101 SoTranSender::insert(SoNode *node)
102 //
103 ////////////////////////////////////////////////////////////////////////
104 {
105 node->ref();
106 addCommand(TRAN_INSERT);
107 addNode(node);
108 node->unref();
109 }
110
111 ////////////////////////////////////////////////////////////////////////
112 //
113 // Description:
114 // Adds INSERTP command to output.
115 //
116 // Use: public
117
118 void
119 SoTranSender::insert(SoNode *node, SoNode *parent, int n)
120 //
121 ////////////////////////////////////////////////////////////////////////
122 {
123 node->ref();
124 if (parent != NULL)
125 parent->ref();
126 addCommand(TRAN_INSERTP);
127 addNode(node);
128 addNodeRef(parent);
129 addInt(n);
130 node->unref();
131 if (parent != NULL)
132 parent->unref();
133 }
134
135 ////////////////////////////////////////////////////////////////////////
136 //
137 // Description:
138 // Adds REMOVE command to output.
139 //
140 // Use: public
141
142 void
143 SoTranSender::remove(SoNode *parent, int n)
144 //
145 ////////////////////////////////////////////////////////////////////////
146 {
147 if (parent != NULL)
148 parent->ref();
149 addCommand(TRAN_REMOVE);
150 addNodeRef(parent);
151 addInt(n);
152 if (parent != NULL)
153 parent->unref();
154 }
155
156 ////////////////////////////////////////////////////////////////////////
157 //
158 // Description:
159 // Adds REPLACE command to output.
160 //
161 // Use: public
162
163 void
164 SoTranSender::replace(SoNode *parent, int n, SoNode *newNode)
165 //
166 ////////////////////////////////////////////////////////////////////////
167 {
168 if (parent != NULL)
169 parent->ref();
170 newNode->ref();
171 addCommand(TRAN_REPLACE);
172 addNodeRef(parent);
173 addInt(n);
174 addNode(newNode);
175 if (parent != NULL)
176 parent->unref();
177 newNode->unref();
178 }
179
180 ////////////////////////////////////////////////////////////////////////
181 //
182 // Description:
183 // Adds MODIFY command to output.
184 //
185 // Use: public
186
187 void
188 SoTranSender::modify(SoNode *node)
189 //
190 ////////////////////////////////////////////////////////////////////////
191 {
192 node->ref();
193 addCommand(TRAN_MODIFY);
194 addNodeRef(node);
195 addNode(node, FALSE);
196 node->unref();
197 }
198
199 ////////////////////////////////////////////////////////////////////////
200 //
201 // Description:
202 // This is called to make sure the data is ready to send. It adds
203 // an END command to the output.
204 //
205 // Use: public
206
207 void
208 SoTranSender::prepareToSend()
209 //
210 ////////////////////////////////////////////////////////////////////////
211 {
212 addCommand(TRAN_END);
213 }
214
215 ////////////////////////////////////////////////////////////////////////
216 //
217 // Description:
218 // Adds string corresponding to command code to buffer.
219 //
220 // Use: private
221
222 void
223 SoTranSender::addCommand(int command)
224 //
225 ////////////////////////////////////////////////////////////////////////
226 {
227 out->write(command);
228
229 if (! out->isBinary())
230 out->write('\n');
231 }
232
233 ////////////////////////////////////////////////////////////////////////
234 //
235 // Description:
236 // Adds data for node to output. Uses Inventor database write to do
237 // the deed. If addNames is TRUE (the default), also adds names.
238 //
239 // Use: private
240
241 void
242 SoTranSender::addNode(SoNode *node, SbBool addNames)
243 //
244 ////////////////////////////////////////////////////////////////////////
245 {
246 SoWriteAction wa(out);
247
248 wa.apply(node);
249
250 // Add a list of node "names" in order
251 if (addNames)
252 addNodeNames(node);
253 }
254
255 ////////////////////////////////////////////////////////////////////////
256 //
257 // Description:
258 // Adds a reference to the given node to the output. The reference
259 // is written as a string containing the address of the node. The
260 // actual address doesn't matter - just that all nodes have unique
261 // references.
262 //
263 // Use: private
264
265 void
266 SoTranSender::addNodeRef(const SoNode *node)
267 //
268 ////////////////////////////////////////////////////////////////////////
269 {
270 // Convert the node pointer to a string
271 char s[16];
272
273 sprintf(s, "%p", node);
274
275 out->write(s);
276
277 if (! out->isBinary())
278 out->write('\n');
279 }
280
281 ////////////////////////////////////////////////////////////////////////
282 //
283 // Description:
284 // Adds an integer to the buffer.
285 //
286 // Use: private
287
288 void
289 SoTranSender::addInt(int n)
290 //
291 ////////////////////////////////////////////////////////////////////////
292 {
293 out->write(n);
294
295 if (! out->isBinary())
296 out->write('\n');
297 }
298
299 ////////////////////////////////////////////////////////////////////////
300 //
301 // Description:
302 // Adds a dictionary of references for all nodes under the given
303 // root to the output. This traverses the graph in the usual style;
304 // the receiver must traverse in the same manner to keep references
305 // correct.
306 //
307 // Use: private
308
309 void
310 SoTranSender::addNodeNames(const SoNode *root)
311 //
312 ////////////////////////////////////////////////////////////////////////
313 {
314 addNodeRef(root);
315
316 if (root->isOfType(SoGroup::getClassTypeId())) {
317
318 const SoGroup *group = (const SoGroup *) root;
319 int i;
320
321 for (i = 0; i < group->getNumChildren(); i++)
322 addNodeNames(group->getChild(i));
323 }
324 }
325
326 //////////////////////////////////////////////////////////////////////////////
327 //
328 // SoTranReceiver class
329 //
330 //////////////////////////////////////////////////////////////////////////////
331
332 ///////////////////////////////////////////////////////
333 //
334 // This structure is added to the nameToEntryDict dictionary that
335 // correlates unique node names to node pointers. The unique names are
336 // created by printing the pointer to the transcribed node into a
337 // string buffer. This structure stores a pointer to the node on the
338 // receiving end. It also stores a reference count for that node so we
339 // can tell how many times it has been added to the dictionary. When
340 // this gets decremented to 0, the dictionary entry is removed, so we
341 // can free up some memory. Without this, transcription just uses up
342 // memory because new nodes keep getting inserted and none get removed
343 // from the dictionary.
344 //
345 ///////////////////////////////////////////////////////
346
347 struct SoTranDictEntry {
348 SoNode *node; // Pointer to node
349 int32_t refCount; // Number of times node is added to dict
350 };
351
352 ////////////////////////////////////////////////////////////////////////
353 //
354 // Description:
355 // Constructor - takes default root node.
356 //
357 // Use: public
358
359 SoTranReceiver::SoTranReceiver(SoGroup *rootNode)
360 //
361 ////////////////////////////////////////////////////////////////////////
362 {
363 char s[16];
364
365 // Add root to dictionaries with name for NULL, since that is how
366 // the root is referred to.
367 sprintf(s, "%p", (void *)NULL);
368 SbName name(s);
369 addEntry(rootNode, name);
370
371 root = rootNode;
372 }
373
374 ////////////////////////////////////////////////////////////////////////
375 //
376 // Description:
377 // Destructor
378 //
379 // Use: public
380
381 SoTranReceiver::~SoTranReceiver()
382 //
383 ////////////////////////////////////////////////////////////////////////
384 {
385 // Free up SoTranDictEntry instances stored in dictionary
386 nameToEntryDict.applyToAll(SoTranReceiver::deleteDictEntry);
387 }
388
389 ////////////////////////////////////////////////////////////////////////
390 //
391 // Description:
392 // Interprets transcription commands from the given SoInput.
393 // Returns FALSE on error.
394 //
395 // Use: public
396
397 SbBool
398 SoTranReceiver::interpret(SoInput *in)
399 //
400 ////////////////////////////////////////////////////////////////////////
401 {
402 int commandCode;
403 SbBool done = FALSE;
404
405 // Run through commands in buffer, changing local DB
406 do {
407
408 if (! in->read(commandCode))
409 return FALSE;
410
411 // Process command
412 if (! interpretCommand(commandCode, in, done)) {
413 SoDebugError::post("SoTranReceiver::interpret",
414 "in command \"%d\"", commandCode);
415 return FALSE;
416 }
417
418 } while (! done);
419
420 return TRUE;
421 }
422
423 ////////////////////////////////////////////////////////////////////////
424 //
425 // Description:
426 // Interprets one database change command (with given code) from
427 // input. Sets done to TRUE if end command was found. Returns T/F
428 // error status.
429 //
430 // Use: private
431
432 SbBool
433 SoTranReceiver::interpretCommand(int commandCode, SoInput *in, SbBool &done)
434 //
435 ////////////////////////////////////////////////////////////////////////
436 {
437 SoNode *node, *parentNode, *nodeRef;
438 SoGroup *parent;
439 const SoFieldData *fd;
440 int i;
441
442 switch (commandCode) {
443
444 case TRAN_INSERT:
445
446 if (! getNodeAndNames(in, node)) // New node and names
447 return FALSE;
448
449 root->addChild(node);
450 node->unref();
451 break;
452
453 case TRAN_INSERTP:
454
455 if (! getNodeAndNames(in, node) || // New node and names
456 ! getNodeReference(in, parentNode) || // Parent node
457 ! in->read(i)) // Child index
458 return FALSE;
459
460 parent = (SoGroup *) parentNode;
461 parent->insertChild(node, i);
462 node->unref();
463 break;
464
465 case TRAN_REMOVE:
466
467 if (! getNodeReference(in, parentNode) || // Parent of node
468 ! in->read(i)) // Child index
469 return FALSE;
470
471 parent = (SoGroup *) parentNode;
472 if (i < parent->getNumChildren()) {
473 // Remove references, recursively
474 removeNodeReferences(parent->getChild(i));
475 parent->removeChild(i);
476 }
477
478 break;
479
480 case TRAN_REPLACE:
481
482 if (! getNodeReference(in, parentNode) || // Parent of node
483 ! in->read(i) || // Child index
484 ! getNodeAndNames(in, node)) // New node and names
485 return FALSE;
486
487 // Out with the old, in with the new
488 parent = (SoGroup *) parentNode;
489
490 if (i < parent->getNumChildren()) {
491 // Remove references, recursively
492 removeNodeReferences(parent->getChild(i));
493
494 parent->removeChild(i);
495 parent->insertChild(node, i);
496 node->unref();
497 }
498
499 break;
500
501 case TRAN_MODIFY:
502
503 if (! getNodeReference(in, nodeRef) || // Reference to node
504 ! getNode(in, node)) // New node definition
505 return FALSE;
506
507 // Change node fields to new contents
508 fd = nodeRef->getFieldData();
509 if (fd != NULL)
510 fd->overlay(nodeRef, node, TRUE);
511
512 // Get rid of new definition
513 node->unref();
514
515 break;
516
517 case TRAN_END:
518 done = TRUE;
519 break;
520
521 default:
522 return FALSE;
523 }
524
525 return TRUE;
526 }
527
528 ////////////////////////////////////////////////////////////////////////
529 //
530 // Description:
531 // Reads a node (and its childrem, if any) from the buffer,
532 // returning the node. This also reads the node names data from the
533 // buffer, updating the node dictionaries. Returns FALSE on error.
534 //
535 // Use: private
536
537 SbBool
538 SoTranReceiver::getNodeAndNames(SoInput *in, SoNode *&node)
539 //
540 ////////////////////////////////////////////////////////////////////////
541 {
542 SoNode *oldNode;
543
544 // Read node
545 if (! getNode(in, node))
546 return FALSE;
547
548 // Read node names, looking for existing node with that name
549 if (! getNodeNames(in, node, TRUE, oldNode))
550 return FALSE;
551
552 // If node already existed, get rid of new stuff and return old node
553 if (oldNode != NULL) {
554 node->unref();
555 node = oldNode;
556 node->ref();
557 }
558 return TRUE;
559 }
560
561 ////////////////////////////////////////////////////////////////////////
562 //
563 // Description:
564 // Reads a node (and its childrem, if any) from the buffer,
565 // returning the node (after ref'ing it). Returns FALSE on error.
566 //
567 // Use: private
568
569 SbBool
570 SoTranReceiver::getNode(SoInput *in, SoNode *&node)
571 //
572 ////////////////////////////////////////////////////////////////////////
573 {
574 if (! SoDB::read(in, node))
575 return FALSE;
576
577 node->ref();
578
579 return TRUE;
580 }
581
582 ////////////////////////////////////////////////////////////////////////
583 //
584 // Description:
585 // Reads reference names for each node under root. Assumes that the
586 // order of traversal is the same here as when sending the name. If
587 // lookForNode is TRUE, it looks for the node in the dictionary. If
588 // it is found, oldRoot is set to point to it. Otherwise, oldRoot
589 // is set to NULL. If lookForNode is FALSE, this just recurses on
590 // the children, if any, to keep the name-to-node correspondences
591 // correct.
592 //
593 // Use: private
594
595 SbBool
596 SoTranReceiver::getNodeNames(SoInput *in, SoNode *root,
597 SbBool lookForNode, SoNode *&oldRoot)
598 //
599 ////////////////////////////////////////////////////////////////////////
600 {
601 SbName name;
602 void *ptr;
603 SoTranDictEntry *e;
604
605 // Read reference as SbName to get unique pointer
606 if (! in->read(name))
607 return FALSE;
608
609 if (lookForNode) {
610
611 // See if the node is already in the dictionary. If so,
612 // increment the reference count and return the old root.
613 if (nameToEntryDict.find((unsigned long) name.getString(), ptr)) {
614 e = (SoTranDictEntry *) ptr;
615 e->refCount++;
616
617 oldRoot = e->node;
618 }
619
620 // Otherwise, add a new entry
621 else {
622 addEntry(root, name);
623 oldRoot = NULL;
624 }
625 }
626
627 else
628 oldRoot = NULL;
629
630 if (root->isOfType(SoGroup::getClassTypeId())) {
631
632 SoNode *child;
633 SoGroup *group = (SoGroup *) root;
634 int i;
635
636 for (i = 0; i < group->getNumChildren(); i++) {
637
638 // Recurse
639 if (! getNodeNames(in, group->getChild(i), oldRoot == NULL, child))
640 return FALSE;
641
642 // If child node was already in dictionary, replace child
643 // of group with node from dictionary.
644 if (child != NULL)
645 group->replaceChild(i, child);
646 }
647 }
648
649 return TRUE;
650 }
651
652 ////////////////////////////////////////////////////////////////////////
653 //
654 // Description:
655 // Reads a node reference from the input and returns (in node) a
656 // pointer to the node referenced by it. If no such node is found,
657 // NULL is returned. Returns FALSE on error.
658 //
659 // Use: private
660
661 SbBool
662 SoTranReceiver::getNodeReference(SoInput *in, SoNode *&node)
663 //
664 ////////////////////////////////////////////////////////////////////////
665 {
666 SbName name;
667 void *ptr;
668
669 // Read reference as SbName to get unique pointer
670 if (! in->read(name))
671 return FALSE;
672
673 // Look up pointer in dictionary
674 if (nameToEntryDict.find((unsigned long) name.getString(), ptr))
675 node = ((SoTranDictEntry *) ptr)->node;
676 else {
677 node = NULL;
678 return FALSE;
679 }
680
681 return TRUE;
682 }
683
684 ////////////////////////////////////////////////////////////////////////
685 //
686 // Description:
687 // Removes reference to node in dictionaries. The passed node is a
688 // node in the graph on the receiving side. If there are no more
689 // references left, removes dictionary entry completely.
690 // (Otherwise, just decrements dictionary entry reference count.)
691 // This is a recursive procedure.
692 //
693 // Use: private
694
695 void
696 SoTranReceiver::removeNodeReferences(SoNode *node)
697 //
698 ////////////////////////////////////////////////////////////////////////
699 {
700 void *ptr;
701 const char *nameString;
702 SoTranDictEntry *e;
703
704 // Find the name of the node, using the name dictionary
705 nodeToNameDict.find((unsigned long) node, ptr);
706 nameString = (const char *) ptr;
707
708 // Find the dictionary entry for the node, using the name
709 SbName name(nameString);
710 nameToEntryDict.find((unsigned long) name.getString(), ptr);
711 e = (SoTranDictEntry *) ptr;
712
713 // Decrement the reference count and check for 0
714 if (--e->refCount == 0) {
715
716 // This node is going away. Remove its dictionary entries and
717 // free up the SoTranDictEntry
718 nameToEntryDict.remove((unsigned long) name.getString());
719 nodeToNameDict.remove((unsigned long) node);
720 e->node->unref();
721 delete e;
722
723 // Recurse on its children, if it has any
724 if (node->isOfType(SoGroup::getClassTypeId())) {
725
726 const SoGroup *group = (const SoGroup *) node;
727 int i;
728
729 for (i = 0; i < group->getNumChildren(); i++)
730 removeNodeReferences(group->getChild(i));
731 }
732 }
733 }
734
735 ////////////////////////////////////////////////////////////////////////
736 //
737 // Description:
738 // Adds an entry to the dictionaries.
739 //
740 // Use: private
741
742 void
743 SoTranReceiver::addEntry(SoNode *node, SbName &name)
744 //
745 ////////////////////////////////////////////////////////////////////////
746 {
747 SoTranDictEntry *e = new SoTranDictEntry;
748
749 e->node = node;
750 e->node->ref();
751 e->refCount = 1;
752
753 nameToEntryDict.enter((unsigned long) name.getString(), (void *) e);
754 nodeToNameDict.enter((unsigned long) node, (void *) name.getString());
755 }
756
757 ////////////////////////////////////////////////////////////////////////
758 //
759 // Description:
760 // Deletes (frees up) an entry from the nameToEntryDict.
761 //
762 // Use: private
763
764 void
765 SoTranReceiver::deleteDictEntry(unsigned long, void *value)
766 //
767 ////////////////////////////////////////////////////////////////////////
768 {
769 SoTranDictEntry *e = (SoTranDictEntry *) value;
770
771 e->node->unref();
772 delete e;
773 }