Debian Codegraph @

lib/database/src/so/SoDB.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, 1991 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.2 $
44 |
45 | Description:
46 | Definition of the SoDB class
47 |
48 | Author(s) : Paul S. Strauss, Nick Thompson, Gavin Bell
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <cstdlib>
55 #include <cassert>
56 #include <Inventor/SbDict.h>
57 #include <Inventor/SbString.h>
58 #include <Inventor/SoDB.h>
59 #include <Inventor/SoInput.h>
60 #include <Inventor/SoOutput.h>
61 #include <Inventor/SoPath.h>
62 #include <Inventor/SoType.h>
63 #include <Inventor/actions/SoAction.h>
64 #include <Inventor/details/SoDetail.h>
65 #include <Inventor/elements/SoElement.h>
66 #include <Inventor/engines/SoEngine.h>
67 #include <Inventor/engines/SoFieldConverter.h>
68 #include <Inventor/errors/SoDebugError.h>
69 #include <Inventor/errors/SoError.h>
70 #include <Inventor/errors/SoReadError.h>
71 #include <Inventor/events/SoEvent.h>
72 #include <Inventor/fields/SoField.h>
73 #include <Inventor/fields/SoSFTime.h>
74 #include <Inventor/nodes/SoNode.h>
75 #include <Inventor/nodes/SoSeparator.h>
76 #include <Inventor/sensors/SoTimerSensor.h>
77
78 #include <SoDebug.h>
79
80 #include "upgraders/SoUpgraders.h" /* V1 upgraders */
81 #include "fields/SoGlobalField.h" /* GlobalField routines */
82
83 // Internal class for storing headers and the corresponding
84 // callback functions.
85 SoINTERNAL struct SoDBHeaderData {
86 SbString headerString;
87 SbBool isBinary;
88 float ivVersion;
89 SoDBHeaderCB *preCB;
90 SoDBHeaderCB *postCB;
91 void *userData;
92 };
93
94 /////////////////////////////////////////////////////////////////////////////
95 //
96 // This class exists only for the "realTime" global field. It is
97 // exactly the same as SoSFTime in all respects except one: it
98 // redefines the connectionStatusChanged() method, allowing the global
99 // database to keep track of connections to realTime. This allows SoDB
100 // to disable the time-update sensor when nothing is connected to the
101 // realTime global field, improving performance.
102 //
103 // Note that this class does not set up a run-time type id; it is
104 // inherited as is from SoSFTime, meaning that realTime is treated
105 // just like any other SoSFTime field.
106 //
107
108 class SoSFRealTime : public SoSFTime {
109 public:
110 SoSFRealTime();
111 virtual ~SoSFRealTime();
112
113 private:
114 virtual void connectionStatusChanged(int numConnections);
115
116 int totalNumConnections;
117 };
118
119 SoSFRealTime::SoSFRealTime()
120 {
121 totalNumConnections = 0;
122 }
123
124 SoSFRealTime::~SoSFRealTime()
125 {
126 }
127
128 void
129 SoSFRealTime::connectionStatusChanged(int numConnections)
130 {
131 // Determine if realTime is connected to anything.
132 totalNumConnections += numConnections;
133
134 // Enable sensor if we have any connections:
135 SoDB::enableRealTimeSensor((totalNumConnections > 0));
136 }
137
138 // This identifies the current version of Inventor
139 const char *SoDB::versionString = "SGI Open Inventor 2.1 Beta";
140
141 // This is the famous global database that you hear so much about
142 SoDB *SoDB::globalDB = NULL;
143
144 // This keeps track of the number of current notifications. When this
145 // is decremented to 0, all immediate idle queue sensors are triggered.
146 int SoDB::notifyCount = 0;
147
148 // This list stores information for the registered headers
149 SbPList *SoDB::headerList;
150
151 // This dictionary stores field conversion engine types
152 SbDict *SoDB::conversionDict;
153
154 // The global realTime field:
155 SoSFRealTime *SoDB::realTime;
156
157 // Sensor which touches realTime every once in a while
158 SoTimerSensor *SoDB::realTimeSensor;
159
160 /////////////////////////////////////////////////////////////////////////////
161
162 ////////////////////////////////////////////////////////////////////////
163 //
164 // Description:
165 // Initializes the global database. After this call, the (private)
166 // global variable "globalDB" refers to the global database. This
167 // sets up all standard nodes, actions, elements, etc..
168 //
169 // Use: public, static
170
171 void
172 SoDB::init()
173 //
174 ////////////////////////////////////////////////////////////////////////
175 {
176 // Initialize global DB only once.
177 if (globalDB == NULL) {
178
179 globalDB = new SoDB;
180
181 // Initialize the runtime type system
182 SoType::init();
183
184 // Setup for file reading (initializes list of directories to
185 // search in).
186 SoInput::init();
187
188 // Set up field conversion dictionary
189 conversionDict = new SbDict;
190
191 //
192 // Initialize all standard classes. The significant ordering
193 // rules are:
194 // actions must be done before nodes
195 // elements must be done before actions
196 //
197 SoBase::initClass();
198 SoFieldContainer::initClass();
199 SoPath::initClass();
200 SoGlobalField::initClass();
201
202 SoError::initClasses();
203 SoElement::initElements();
204 SoAction::initClasses();
205 SoNode::initClasses();
206 SoField::initClasses();
207 SoEngine::initClasses();
208 SoEvent::initClasses();
209 SoDetail::initClasses();
210
211 SoUpgrader::initClasses();
212
213 // Create the header list, and register valid headers we know about
214 headerList = new SbPList;
215 SoDB::registerHeader(SoOutput::getDefaultASCIIHeader(),
216 FALSE, 2.1f,
217 NULL, NULL, NULL);
218 SoDB::registerHeader(SoOutput::getDefaultBinaryHeader(),
219 TRUE, 2.1f,
220 NULL, NULL, NULL);
221 SoDB::registerHeader("#Inventor V2.0 ascii",
222 FALSE, 2.0f,
223 NULL, NULL, NULL);
224 SoDB::registerHeader("#Inventor V2.0 binary",
225 TRUE, 2.0f,
226 NULL, NULL, NULL);
227 SoDB::registerHeader("#Inventor V1.0 ascii",
228 FALSE, 1.0f,
229 NULL, NULL, NULL);
230 SoDB::registerHeader("#Inventor V1.0 binary",
231 TRUE, 1.0f,
232 NULL, NULL, NULL);
233
234 // For now, treat VRML files as Inventor 2.1 files.
235 // In the future, we might want to verify that the VRML file
236 // contains strictly VRML nodes, i.e. any Inventor (non-VRML)
237 // nodes in the file generate read warnings.
238 SoDB::registerHeader("#VRML V1.0 ascii",
239 FALSE, 2.1f,
240 NULL, NULL, NULL);
241
242 // Create realTime global field. We have to bypass the
243 // standard createGlobalField stuff because there is no
244 // specific typeId info for SoSFRealTime.
245 realTime = new SoSFRealTime;
246 // Construct a global field (to add it to the dictionary); we
247 // don't actually need a pointer to it.
248 (void) new SoGlobalField("realTime", realTime);
249 realTime->setValue(SbTime::getTimeOfDay());
250 realTime->getContainer()->ref();
251
252 // And setup the sensor to touch it periodically
253 realTimeSensor = new SoTimerSensor;
254 realTimeSensor->setFunction((SoSensorCB *)
255 &SoDB::realTimeSensorCallback);
256 #ifdef DEBUG
257 if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) {
258 SoDebug::NamePtr("realTimeSensor", realTimeSensor);
259 }
260 #endif
261
262 // This doesn't have to be scheduled very often, because
263 // the SoXt viewers update realTime right after a redraw, so
264 // anything that is continuously animating will constantly
265 // redraw as fast as either swapbuffers() or the redraw (if
266 // single-buffered) can occur.
267 // I chose 1/12 of a second because it is a multiple of the
268 // two most common vide rates-- 60 HZ and 72 HZ
269 realTimeSensor->setInterval(SbTime(1.0/12.0));
270
271 // Don't schedule the sensor until something is connected to
272 // realTime. If nothing is connected, there's no sense wasting
273 // time triggering the sensor.
274
275 // Initialize delay queue timeout to 1/12th of a second to
276 // make sure that redraws occur even when event processing is
277 // time-consuming.
278 setDelaySensorTimeout(SbTime(1.0/12.0));
279 }
280 }
281
282 ////////////////////////////////////////////////////////////////////////
283 //
284 // Description:
285 // Returns a character string identifying the version of the
286 // Inventor library in use.
287 //
288 // Use: public, static
289
290 const char *
291 SoDB::getVersion()
292 //
293 ////////////////////////////////////////////////////////////////////////
294 {
295 return versionString;
296 }
297
298 ////////////////////////////////////////////////////////////////////////
299 //
300 // Description:
301 // Registers callbacks to be invoked when a particular header
302 // string is found. Returns FALSE if the string is not a valid header
303 // string. Returns TRUE if the header is successfully registered.
304 // Note, nothing prevents you from registering the same string multiple
305 // times.
306
307 //
308 // Use: public, static
309
310 SbBool
311 SoDB::registerHeader(const SbString &header, SbBool isBinary, float ivVersion,
312 SoDBHeaderCB *preCB, SoDBHeaderCB *postCB, void *userData)
313 //
314 ////////////////////////////////////////////////////////////////////////
315 {
316 // Header string cannot be greater than 80 characters in length,
317 // and must have at least one character beyond the initial comment char.
318 int headerLength = header.getLength();
319 if (headerLength > 80 || headerLength < 2)
320 return (FALSE);
321
322 // The first character must be the comment character
323 const char *string = header.getString();
324 if (string[0] != '#')
325 return (FALSE);
326
327 // The string must not contain any newline characters.
328 for (int i = 1; i < header.getLength(); i++)
329 if (string[i] == '\n')
330 return (FALSE);
331
332 SoDBHeaderData *data = new SoDBHeaderData;
333
334 // Binary headers *must* be padded for correct alignment, but to make things
335 // simpler when looking up headers, we'll just pad all headers - including
336 // ascii ones.
337 data->headerString = SoOutput::padHeader(header);
338
339 data->isBinary = isBinary;
340 data->ivVersion = ivVersion;
341 data->preCB = preCB;
342 data->postCB = postCB;
343 data->userData = userData;
344 headerList->append(data);
345
346 return (TRUE);
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350 //
351 // Description:
352 // Return the number of registered headers
353 //
354 // Use: public, static
355
356 int
357 SoDB::getNumHeaders()
358 //
359 ////////////////////////////////////////////////////////////////////////
360 {
361 return (headerList->getLength());
362 }
363
364 ////////////////////////////////////////////////////////////////////////
365 //
366 // Description:
367 // Return the i'th header
368 //
369 // Use: public, static
370
371 SbString
372 SoDB::getHeaderString(int whichHeader)
373 //
374 ////////////////////////////////////////////////////////////////////////
375 {
376 if (whichHeader < 0 || whichHeader >= headerList->getLength())
377 return (SbString(""));
378
379 SoDBHeaderData *data = NULL;
380 data = (SoDBHeaderData *) (*headerList)[whichHeader];
381 if (!data)
382 return (SbString(""));
383
384 return (data->headerString);
385 }
386
387 ////////////////////////////////////////////////////////////////////////
388 //
389 // Description:
390 // Look up the given header string in our list of registered
391 // headers, and pass back the data associated with that header i.e.
392 // pointers to the pre- and post- callbacks, the user data, and
393 // a boolean indicating whether it's binary or ascii.
394 // The return value is true if the string is found in the list of
395 // registered headers.
396 // If the substringOK flag is TRUE, then we also look for registered
397 // headers that are substrings of the given header string.
398 //
399 // Use: public, static
400
401 SbBool
402 SoDB::getHeaderData(const SbString &header,
403 SbBool &isBinary, float &ivVersion,
404 SoDBHeaderCB *&preCB, SoDBHeaderCB *&postCB,
405 void *&userData, SbBool substringOK)
406 //
407 ////////////////////////////////////////////////////////////////////////
408 {
409 int whichHeader = -1;
410 SoDBHeaderData *data;
411 SbString paddedHeader = SoOutput::padHeader(header);
412
413 // First look for an exact match
414 for (int i = headerList->getLength()-1; i >= 0 && whichHeader == -1; i--) {
415 data = (SoDBHeaderData *) (*headerList)[i];
416 SbString registeredString = data->headerString;
417
418 if (paddedHeader == registeredString) {
419 whichHeader = i;
420
421 }
422 }
423
424 // If we didn't find an exact match,
425 // look for a substring that is a valid registered string
426 if (whichHeader == -1 && substringOK) {
427
428 for (int i = headerList->getLength()-1; i >= 0 && whichHeader == -1; i--) {
429 data = (SoDBHeaderData *) (*headerList)[i];
430 SbString registeredString = data->headerString;
431
432 if (paddedHeader.getLength() >= registeredString.getLength()) {
433
434 // See how much padding there is in the registered header string
435 const char *registeredStr = data->headerString.getString();
436 int lastNonPadChar = registeredString.getLength() - 1;
437 while (registeredStr[lastNonPadChar] == ' ' && lastNonPadChar > 0)
438 lastNonPadChar--;
439
440 // Is the registered header (minus the padding) a substring
441 // of the the given header string?
442 if (registeredString.getSubString(0, lastNonPadChar) ==
443 paddedHeader.getSubString(0, lastNonPadChar)) {
444 whichHeader = i;
445 }
446 }
447 }
448 }
449
450 if (whichHeader == -1) {
451 isBinary = FALSE;
452 ivVersion = -1;
453 preCB = NULL;
454 postCB = NULL;
455 userData = NULL;
456 return (FALSE);
457 }
458
459 data = (SoDBHeaderData *) (*headerList)[whichHeader];
460 isBinary = data->isBinary;
461 ivVersion = data->ivVersion;
462 preCB = data->preCB;
463 postCB = data->postCB;
464 userData = data->userData;
465 return (TRUE);
466 }
467
468 ////////////////////////////////////////////////////////////////////////
469 //
470 // Description:
471 // Returns TRUE if the given character string is a valid registered
472 // header string. Some trivial tests that can
473 // be made on the string before calling this are: it must begin
474 // with a '#'; it should be no more than 80 characters; newlines are
475 // not allowed
476 //
477 // Use: public, static
478
479 SbBool
480 SoDB::isValidHeader(const char *testString)
481 //
482 ////////////////////////////////////////////////////////////////////////
483 {
484 char buf[81], *c;
485
486 // Use only the first 80 characters
487 strncpy(buf,testString,80);
488
489 // Make sure it is NULL-terminated
490 buf[80] = '\0';
491
492 // Find first newline in string, if any, and end the string there
493 if ((c = strchr(buf,'\n')) != NULL)
494 *c = '\0';
495
496 SbString paddedHeader = SoOutput::padHeader(SbString(buf));
497
498 for (int i = headerList->getLength()-1; i >= 0; i--) {
499 SoDBHeaderData *data = (SoDBHeaderData *) (*headerList)[i];
500 if (data->headerString == paddedHeader)
501 return (TRUE);
502 }
503
504 // Didn't find this string in the list of valid headers.
505 return (FALSE);
506 }
507
508 ////////////////////////////////////////////////////////////////////////
509 //
510 // Description:
511 // Reads a graph from the file specified by the given SoInput,
512 // returning a pointer to the resulting root node in rootNode.
513 // Returns FALSE on error.
514 //
515 // Use: public, static
516
517 SbBool
518 SoDB::read(SoInput *in, SoNode *&rootNode)
519 //
520 ////////////////////////////////////////////////////////////////////////
521 {
522 SoBase *base;
523 SbBool ret;
524
525 ret = read(in, base);
526
527 if (base != NULL) {
528 if (base->isOfType(SoNode::getClassTypeId()))
529 rootNode = (SoNode *) base;
530
531 else {
532 SoReadError::post(in, "looking for a node but got %s",
533 base->getTypeId().getName().getString());
534 ret = FALSE;
535
536 // Free the scene we just read by refing/unrefing
537 base->ref();
538 base->unref();
539 }
540 }
541
542 else
543 rootNode = NULL;
544
545 return ret;
546 }
547
548 ////////////////////////////////////////////////////////////////////////
549 //
550 // Description:
551 // Reads a path from the file specified by the given SoInput,
552 // returning a pointer to the resulting path in path. Returns FALSE
553 // on error.
554 //
555 // Use: public, static
556
557 SbBool
558 SoDB::read(SoInput *in, SoPath *&path)
559 //
560 ////////////////////////////////////////////////////////////////////////
561 {
562 SoBase *base;
563 SbBool ret;
564
565 ret = read(in, base);
566
567 if (base != NULL) {
568 if (base->isOfType(SoPath::getClassTypeId()))
569 path = (SoPath *) base;
570
571 else {
572 SoReadError::post(in, "looking for a path but got %s",
573 base->getTypeId().getName().getString());
574 ret = FALSE;
575
576 // Free the scene we just read by refing/unrefing
577 base->ref();
578 base->unref();
579 }
580 }
581
582 else
583 path = NULL;
584
585 return ret;
586 }
587
588 ////////////////////////////////////////////////////////////////////////
589 //
590 // Description:
591 // Reads all graphs from the file specified by the given SoInput.
592 // If there is only one graph in the file and its root is an
593 // SoSeparator, a pointer to the root is returned. Otherwise, this
594 // creates an SoSeparator, adds the root nodes of all graphs read
595 // as children of it, and returns a pointer to it.
596 //
597 // If a graph is a path, it will be read and its root returned.
598 //
599 // This returns NULL on error.
600 //
601 // Use: public, static
602
603 SoSeparator *
604 SoDB::readAll(SoInput *in)
605 //
606 ////////////////////////////////////////////////////////////////////////
607 {
608 SoBase *base;
609 SoSeparator *root = new SoSeparator;
610
611 root->ref();
612
613 // Keep on reading until there are no more graphs to read
614 do {
615 if (! read(in, base)) {
616 root->unref();
617 return NULL;
618 }
619 else if (base != NULL) {
620 // Did we read a node or a path?
621 if (base->isOfType(SoNode::getClassTypeId()))
622 root->addChild((SoNode *) base);
623 else if (base->isOfType(SoPath::getClassTypeId())) {
624 SoNode *pathHead = ((SoPath *) base)->getHead();
625 if (pathHead != NULL) {
626 pathHead->ref();
627 root->addChild(pathHead);
628 pathHead->unref();
629 }
630 }
631 }
632 } while (base != NULL);
633
634 // If only one graph was read, and it had a separator for a root,
635 // get rid of the one we created
636 if (root->getNumChildren() == 1 &&
637 root->getChild(0)->isOfType(SoSeparator::getClassTypeId())) {
638
639 SoSeparator *graphRoot = (SoSeparator *) root->getChild(0);
640 graphRoot->ref();
641 root->unref();
642 root = graphRoot;
643 }
644
645 root->unrefNoDelete();
646 return root;
647 }
648
649 ////////////////////////////////////////////////////////////////////////
650 //
651 // Description:
652 // Just like UNIX select() call, but does our tasks while waiting.
653 // Can be used in applications with their own event loops.
654 //
655 // Use: public, static
656
657 int
658 SoDB::doSelect(int nfds, fd_set *readfds, fd_set *writefds,
659 fd_set *exceptfds, struct timeval *userTimeOut)
660 //
661 ////////////////////////////////////////////////////////////////////////
662 {
663 #ifdef DEBUG
664 if (globalDB == NULL) {
665 SoDebugError::post("SoDB::doSelect", "SoDB::init() was never called");
666 return 0;
667 }
668 #endif /* DEBUG */
669
670 return globalDB->sensorManager.doSelect(nfds, readfds, writefds,
671 exceptfds, userTimeOut);
672 }
673
674 ////////////////////////////////////////////////////////////////////////
675 //
676 // Description:
677 // Registers a field conversion engine that can be used to
678 // convert from one type of field to another. The type id's of the
679 // two fields are passed in, as is the type id of the field
680 // converter engine (derived from SoFieldConverter).
681 //
682 // Use: extender, static
683
684 void
685 SoDB::addConverter(SoType fromField, SoType toField, SoType converterFunc)
686 {
687
688 #ifdef DEBUG
689 // Make sure the converter is of the correct type
690 if (! converterFunc.isDerivedFrom(SoFieldConverter::getClassTypeId())) {
691 SoDebugError::post("SoDB::addConverter",
692 "class \"%s\" is not derived from SoFieldConverter",
693 converterFunc.getName().getString());
694 return;
695 }
696 #endif
697
698 assert(sizeof(converterFunc) == sizeof(uint32_t));
699 conversionDict->enter(getConversionKey(fromField, toField),
700 (void *)(unsigned long)*(uint32_t *)&converterFunc);
701 }
702
703 ////////////////////////////////////////////////////////////////////////
704 //
705 // Description:
706 // Returns the field conversion engine registered for the two
707 // given field types. If no such engine exists, SoType::badType()
708 // is returned.
709 //
710 // Use: extender, static
711
712 SoType
713 SoDB::getConverter(SoType fromField, SoType toField)
714 {
715 void *typePtr;
716
717 if (conversionDict->find(getConversionKey(fromField, toField), typePtr))
718 {
719 uint32_t val = (unsigned long)typePtr;
720 SoType converterFunc = *(SoType *)&val;
721 assert(sizeof(converterFunc) == sizeof(val));
722 return converterFunc;
723 }
724
725 return SoType::badType();
726 }
727
728 ////////////////////////////////////////////////////////////////////////
729 //
730 // Description:
731 // Accesses sensor manager
732 //
733 // Use: internal, static
734
735 SoSensorManager *
736 SoDB::getSensorManager()
737 //
738 ////////////////////////////////////////////////////////////////////////
739 {
740 return &globalDB->sensorManager;
741 }
742
743 ////////////////////////////////////////////////////////////////////////
744 //
745 // Description:
746 // Enables/disables realTime sensor processing.
747 //
748 // Use: internal, static
749
750 void
751 SoDB::enableRealTimeSensor(SbBool enable)
752 //
753 ////////////////////////////////////////////////////////////////////////
754 {
755 if (enable && realTimeSensor->getInterval() != SbTime::zero()) {
756
757 // If we are enabling the sensor now, call the callback once
758 // to set the current time. Since the sensor may become
759 // enabled because of a new connection, the realTime field may
760 // be queried before the sensor callback is called. By calling
761 // it now, the realTime field will contain the current time in
762 // this case. However, we have to disable notification on the
763 // realTime field since we area already in the process of
764 // notifying.
765 SbBool wasEnabled = realTime->enableNotify(FALSE);
766 realTimeSensorCallback();
767 realTime->enableNotify(wasEnabled);
768 realTimeSensor->schedule();
769 }
770 else
771 realTimeSensor->unschedule();
772 }
773
774 ////////////////////////////////////////////////////////////////////////
775 //
776 // Description:
777 // Reads an SoBase from the file specified by the given SoInput,
778 // returning a pointer to the resulting instance in base. Returns
779 // FALSE on error.
780 //
781 // Use: private, static
782
783 SbBool
784 SoDB::read(SoInput *in, SoBase *&base)
785 //
786 ////////////////////////////////////////////////////////////////////////
787 {
788 SbBool ret;
789 const char *dataFileName;
790 char *searchPath = NULL;
791
792 #ifdef DEBUG
793 if (globalDB == NULL) {
794 SoDebugError::post("SoDB::read", "SoDB::init() was never called");
795 return FALSE;
796 }
797 #endif /* DEBUG */
798
799 // Before reading, see if the SoInput is reading from a named
800 // file. If so, make sure the directory search path in the SoInput
801 // is set up to read from the same directory the file is in.
802 dataFileName = in->getCurFileName();
803
804 if (dataFileName != NULL) {
805 const char *slashPtr;
806
807 // Set up the directory search stack if necessary. Look for
808 // the last '/' in the path. If there is none, there's no
809 // path. Otherwise, remove the slash and everything after it.
810 if ((slashPtr = strrchr(dataFileName, '/')) != NULL) {
811 searchPath = strdup(dataFileName);
812 searchPath[slashPtr - dataFileName] = '\0';
813 SoInput::addDirectoryFirst(searchPath);
814 }
815 }
816
817 ret = SoBase::read(in, base, SoBase::getClassTypeId());
818
819 // If no valid base was read, but we haven't hit EOF, that means
820 // that there's extra crap in the input that's not an Inventor
821 // thing. If so, report an error.
822 if (ret && base == NULL && ! in->eof()) {
823 char c;
824 in->get(c);
825 SoReadError::post(in, "Extra characters ('%c') found in input", c);
826 ret = FALSE;
827 }
828
829 // Clean up directory list if necessary
830 if (searchPath != NULL) {
831 SoInput::removeDirectory(searchPath);
832 free(searchPath);
833 }
834
835 return ret;
836 }
837
838 ////////////////////////////////////////////////////////////////////////
839 //
840 // Description:
841 // Create a global field with the given name and type.
842 //
843 // Use: public, static
844
845 SoField *
846 SoDB::createGlobalField(const SbName &name, SoType type)
847 //
848 ////////////////////////////////////////////////////////////////////////
849 {
850 SbBool alreadyExists;
851 SoGlobalField *result = SoGlobalField::create(name, type, alreadyExists);
852
853 if (result == NULL)
854 return NULL;
855
856 return result->getMyField();
857 }
858
859 ////////////////////////////////////////////////////////////////////////
860 //
861 // Description:
862 // Get the global field with the given name.
863 //
864 // Use: public, static
865
866 SoField *
867 SoDB::getGlobalField(const SbName &name)
868 //
869 ////////////////////////////////////////////////////////////////////////
870 {
871 SoGlobalField *result = SoGlobalField::find(name);
872 if (result == NULL) return NULL;
873
874 return result->getMyField();
875 }
876
877 ////////////////////////////////////////////////////////////////////////
878 //
879 // Description:
880 // Rename the global field with the given name.
881 //
882 // Use: public, static
883
884 void
885 SoDB::renameGlobalField(const SbName &oldName, const SbName &newName)
886 //
887 ////////////////////////////////////////////////////////////////////////
888 {
889 if (oldName == newName) return;
890
891 SoGlobalField *oldGlobalField = SoGlobalField::find(oldName);
892 if (oldGlobalField == NULL) return;
893
894 if (newName == SbName("")) {
895 oldGlobalField->unref();
896 return;
897 }
898
899 SoGlobalField *newGlobalField = SoGlobalField::find(newName);
900 if (newGlobalField != NULL) {
901 newGlobalField->unref();
902 }
903
904 oldGlobalField->changeName(newName);
905 }
906
907 ////////////////////////////////////////////////////////////////////////
908 //
909 // Description:
910 // Set how often we should touch realTime.
911 //
912 // Use: public, static
913
914 void
915 SoDB::setRealTimeInterval(const SbTime &deltaT)
916 //
917 ////////////////////////////////////////////////////////////////////////
918 {
919 if (deltaT == SbTime::zero()) {
920 realTimeSensor->setInterval(deltaT);
921 realTimeSensor->unschedule();
922 }
923 else {
924 realTimeSensor->setInterval(deltaT);
925 realTimeSensor->schedule();
926 }
927 }
928
929 ////////////////////////////////////////////////////////////////////////
930 //
931 // Description:
932 // Get how often realTime is touched.
933 //
934 // Use: public, static
935
936 const SbTime &
937 SoDB::getRealTimeInterval()
938 //
939 ////////////////////////////////////////////////////////////////////////
940 {
941 return realTimeSensor->getInterval();
942 }
943
944 ////////////////////////////////////////////////////////////////////////
945 //
946 // Description:
947 // Set the delay queue sensor timeout.
948 //
949 // Use: public, static
950
951 void
952 SoDB::setDelaySensorTimeout(const SbTime &t)
953 //
954 ////////////////////////////////////////////////////////////////////////
955 {
956 getSensorManager()->setDelaySensorTimeout(t);
957 }
958
959 ////////////////////////////////////////////////////////////////////////
960 //
961 // Description:
962 // Get the delay queue sensor timeout.
963 //
964 // Use: public, static
965
966 const SbTime &
967 SoDB::getDelaySensorTimeout()
968 //
969 ////////////////////////////////////////////////////////////////////////
970 {
971 return getSensorManager()->getDelaySensorTimeout();
972 }
973
974 ////////////////////////////////////////////////////////////////////////
975 //
976 // Description:
977 // Callback to update the realTime global field.
978 //
979 // Use: private
980
981 void
982 SoDB::realTimeSensorCallback()
983 //
984 ////////////////////////////////////////////////////////////////////////
985 {
986 realTime->setValue(SbTime::getTimeOfDay());
987 }
988