| 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 |
/* |
| 39 |
* Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 40 |
* |
| 41 |
_______________________________________________________________________ |
| 42 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 43 |
| |
| 44 |
| $Revision: 1.2 $ |
| 45 |
| |
| 46 |
| Classes: |
| 47 |
| SoV1NodekitParts |
| 48 |
| |
| 49 |
| Author(s) : Paul Isaacs and Thad Beier |
| 50 |
| |
| 51 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 52 |
_______________________________________________________________________ |
| 53 |
*/ |
| 54 |
|
| 55 |
|
| 56 |
#include <Inventor/SoDB.h> |
| 57 |
#include <Inventor/misc/upgraders/SoV1NodekitParts.h> |
| 58 |
#include <Inventor/misc/upgraders/SoV1BaseKit.h> |
| 59 |
#include <Inventor/SoPath.h> |
| 60 |
#include <Inventor/errors/SoDebugError.h> |
| 61 |
#include <cstdlib> |
| 62 |
|
| 63 |
//////////////////////////////////////////////////////////////////////// |
| 64 |
// |
| 65 |
// Class: |
| 66 |
// SoV1NodekitParts |
| 67 |
// |
| 68 |
//////////////////////////////////////////////////////////////////////// |
| 69 |
|
| 70 |
//////////////////////////////////////////////////////////////////////// |
| 71 |
// |
| 72 |
// Description: |
| 73 |
// Constructor |
| 74 |
// |
| 75 |
// Use: internal |
| 76 |
|
| 77 |
SoV1NodekitParts::SoV1NodekitParts( SoV1BaseKit *rootOfKit, |
| 78 |
SoV1NodekitParts *partsSoFar ) |
| 79 |
// |
| 80 |
//////////////////////////////////////////////////////////////////////// |
| 81 |
{ |
| 82 |
catalog = rootOfKit->getNodekitCatalog(); // assign the catalog |
| 83 |
|
| 84 |
numEntries = catalog->getNumEntries(); // make an empty node list |
| 85 |
nodeList = new SoNode* [numEntries]; |
| 86 |
int i; |
| 87 |
for ( i = 0; i < numEntries; i++ ) |
| 88 |
nodeList[i] = NULL; |
| 89 |
|
| 90 |
// make rootOfKit be the entry in the nodeList for 'this' |
| 91 |
const int partNum = catalog->getPartNumber( "this" ); |
| 92 |
if ( partNum == SO_V1_CATALOG_NAME_NOT_FOUND ) { |
| 93 |
#ifdef DEBUG |
| 94 |
SoDebugError::post("SoV1NodekitParts::SoV1NodekitParts", |
| 95 |
"can't find entry for top node in catalog"); |
| 96 |
#endif |
| 97 |
} |
| 98 |
else if ( rootOfKit == NULL ) { |
| 99 |
#ifdef DEBUG |
| 100 |
SoDebugError::post("SoV1NodekitParts::SoV1NodekitParts", |
| 101 |
"given root node is NULL"); |
| 102 |
#endif |
| 103 |
} |
| 104 |
else if (catalog->getType(partNum) != rootOfKit->getTypeId()){ |
| 105 |
#ifdef DEBUG |
| 106 |
SoDebugError::post("SoV1NodekitParts::SoV1NodekitParts", |
| 107 |
"given root node is of wrong type"); |
| 108 |
#endif |
| 109 |
} |
| 110 |
else { |
| 111 |
nodeList[partNum] = rootOfKit; |
| 112 |
} |
| 113 |
|
| 114 |
// copy parts that already exist in 'partsSoFar' into this |
| 115 |
// partsList |
| 116 |
if ( partsSoFar != NULL ) { |
| 117 |
for (i = 0; i < partsSoFar->numEntries; i++ ) { |
| 118 |
if ( partsSoFar->nodeList[i] != NULL |
| 119 |
&& partsSoFar->nodeList[i] != rootOfKit ) { |
| 120 |
|
| 121 |
// the part exists already in 'partsSoFar' |
| 122 |
// find the entry in the newly constructed list that |
| 123 |
// corresponds to it. |
| 124 |
|
| 125 |
// first, get name from 'partsSoFar' |
| 126 |
const SbName partName = partsSoFar->catalog->getName( i ); |
| 127 |
|
| 128 |
// now, find corresponding entry in THIS list |
| 129 |
const int oldPartNum = catalog->getPartNumber( partName ); |
| 130 |
|
| 131 |
if ( oldPartNum != SO_V1_CATALOG_NAME_NOT_FOUND ) |
| 132 |
nodeList[oldPartNum] = partsSoFar->nodeList[i]; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
//////////////////////////////////////////////////////////////////////// |
| 139 |
// |
| 140 |
// Description: |
| 141 |
// Destructor |
| 142 |
// |
| 143 |
// Use: internal |
| 144 |
|
| 145 |
SoV1NodekitParts::~SoV1NodekitParts() |
| 146 |
// |
| 147 |
//////////////////////////////////////////////////////////////////////// |
| 148 |
{ |
| 149 |
// delete the nodelist |
| 150 |
delete [ /*numEntries*/ ] nodeList; |
| 151 |
} |
| 152 |
|
| 153 |
//////////////////////////////////////////////////////////////////////// |
| 154 |
// |
| 155 |
// Description: |
| 156 |
// Creates the node for the part asked for. |
| 157 |
// If the part already exists, then just return TRUE without doing anything |
| 158 |
// If not, it creates the part and all necessary parts up to the top of |
| 159 |
// of the nodekit, and places them properly as children in the graph. |
| 160 |
// |
| 161 |
// Note: |
| 162 |
// this routine will NOT search for the requested part name within the |
| 163 |
// catalogs of its child parts. |
| 164 |
// |
| 165 |
// Use: internal |
| 166 |
|
| 167 |
SbBool |
| 168 |
SoV1NodekitParts::makePart( const int partNum ) |
| 169 |
// |
| 170 |
//////////////////////////////////////////////////////////////////////// |
| 171 |
{ |
| 172 |
if ( !partFoundCheck( partNum ) ) |
| 173 |
return FALSE; |
| 174 |
|
| 175 |
// if the part is already in the Node list, just return TRUE |
| 176 |
if ( verifyPartExistence( partNum ) ) |
| 177 |
return TRUE; |
| 178 |
|
| 179 |
// create the node |
| 180 |
const SoNode *inst = (const SoNode *) |
| 181 |
catalog->getDefaultType( partNum ).createInstance(); |
| 182 |
#ifdef DEBUG |
| 183 |
if ( inst == NULL ) { |
| 184 |
SoDebugError::post("SoV1NodekitParts::makePart", |
| 185 |
"Can't make part %s. It belongs to an abstract class. Bad parts catalog", catalog->getName(partNum).getString()); |
| 186 |
abort(); |
| 187 |
} |
| 188 |
#endif |
| 189 |
nodeList[partNum] = (SoNode *) inst; |
| 190 |
|
| 191 |
nodeList[partNum]->ref(); // temporarily ref it, until it has a parent |
| 192 |
|
| 193 |
// if it is the topmost part in the nodekit, just return TRUE |
| 194 |
if ( catalog->getParentName( partNum ) == "" ) { |
| 195 |
nodeList[partNum]->unref(); // undo the temporary ref |
| 196 |
return TRUE; |
| 197 |
} |
| 198 |
|
| 199 |
// create its parent part |
| 200 |
if ( !makePart( catalog->getParentPartNumber( partNum ) ) ) { |
| 201 |
#ifdef DEBUG |
| 202 |
SoDebugError::post("SoV1NodekitParts::makePart", |
| 203 |
"can't make parent for part named %s", |
| 204 |
catalog->getName(partNum).getString() ); |
| 205 |
#endif |
| 206 |
nodeList[partNum]->unref(); // undo the temporary ref |
| 207 |
return FALSE; |
| 208 |
} |
| 209 |
int parentPartNum = catalog->getParentPartNumber( partNum ); |
| 210 |
|
| 211 |
// find the next closest right sibling that already exists |
| 212 |
int sibPartNum, searchPartNum; |
| 213 |
|
| 214 |
for( sibPartNum = -1, |
| 215 |
searchPartNum = catalog->getRightSiblingPartNumber( partNum ); |
| 216 |
sibPartNum == -1 && searchPartNum != SO_V1_CATALOG_NAME_NOT_FOUND; |
| 217 |
searchPartNum = catalog->getRightSiblingPartNumber(searchPartNum )) { |
| 218 |
if ( verifyPartExistence( searchPartNum ) ) |
| 219 |
sibPartNum = searchPartNum; |
| 220 |
} |
| 221 |
|
| 222 |
SoGroup *parentNode = (SoGroup *) nodeList[parentPartNum]; |
| 223 |
if (sibPartNum == -1 ) { |
| 224 |
// no right sibling is made yet, so just add this as a child to parent |
| 225 |
parentNode->addChild( nodeList[partNum] ); |
| 226 |
} |
| 227 |
else { |
| 228 |
// find that sibling's index in the parent |
| 229 |
int sibIndex = parentNode->findChild(nodeList[sibPartNum]); |
| 230 |
|
| 231 |
// insert this node as the new child at that index |
| 232 |
parentNode->insertChild( nodeList[partNum], sibIndex ); |
| 233 |
} |
| 234 |
nodeList[partNum]->unref(); // undo the temporary ref |
| 235 |
return TRUE; |
| 236 |
} |
| 237 |
|
| 238 |
//////////////////////////////////////////////////////////////////////// |
| 239 |
// |
| 240 |
// Description: |
| 241 |
// Replaces the node described with the one passed in. |
| 242 |
// |
| 243 |
// If the part already exists, then the current part is removed from |
| 244 |
// its parent and the node list. |
| 245 |
// |
| 246 |
// Next, the new part is put in the list and made the child of its parent. |
| 247 |
// |
| 248 |
// Note: |
| 249 |
// this routine will NOT search for the requested part name within the |
| 250 |
// catalogs of its child parts. |
| 251 |
// |
| 252 |
// Use: internal |
| 253 |
|
| 254 |
SbBool |
| 255 |
SoV1NodekitParts::replacePart( const int partNum, SoNode *newPartNode ) |
| 256 |
// |
| 257 |
//////////////////////////////////////////////////////////////////////// |
| 258 |
{ |
| 259 |
if ( !partFoundCheck( partNum ) ) |
| 260 |
return FALSE; |
| 261 |
|
| 262 |
int parentPartNum = catalog->getParentPartNumber( partNum ); |
| 263 |
|
| 264 |
// make sure the node given is of the proper type |
| 265 |
if ( newPartNode != NULL |
| 266 |
&& !newPartNode->isOfType( catalog->getType( partNum ) ) ) { |
| 267 |
#ifdef DEBUG |
| 268 |
SoDebugError::post("SoV1NodekitParts::replacePart", |
| 269 |
"the given part is not of the correct type"); |
| 270 |
#endif |
| 271 |
return FALSE; |
| 272 |
} |
| 273 |
|
| 274 |
// if an old part is already in the Node list... |
| 275 |
if ( verifyPartExistence( partNum ) ) { |
| 276 |
// disconnect the child from its parent |
| 277 |
((SoGroup *)nodeList[parentPartNum])->removeChild( nodeList[partNum] ); |
| 278 |
} |
| 279 |
|
| 280 |
// set the part! |
| 281 |
nodeList[partNum] = newPartNode; |
| 282 |
|
| 283 |
// if we only set the node to NULL, then just return |
| 284 |
if ( newPartNode == NULL) |
| 285 |
return TRUE; |
| 286 |
|
| 287 |
// if it is the topmost part in the nodekit, just return TRUE |
| 288 |
if ( catalog->getParentName( partNum ) == "" ) |
| 289 |
return TRUE; |
| 290 |
|
| 291 |
// create its parent part |
| 292 |
if ( !makePart( catalog->getParentPartNumber( partNum ) ) ) { |
| 293 |
#ifdef DEBUG |
| 294 |
SoDebugError::post("SoV1NodekitParts::replacePart", |
| 295 |
"can't make parent for part named %s", |
| 296 |
catalog->getName(partNum).getString() ); |
| 297 |
#endif |
| 298 |
return FALSE; |
| 299 |
} |
| 300 |
|
| 301 |
// find the next closest right sibling that already exists |
| 302 |
int sibPartNum, searchPartNum; |
| 303 |
|
| 304 |
for( sibPartNum = -1, |
| 305 |
searchPartNum = catalog->getRightSiblingPartNumber( partNum ); |
| 306 |
sibPartNum == -1 && searchPartNum != SO_V1_CATALOG_NAME_NOT_FOUND; |
| 307 |
searchPartNum = catalog->getRightSiblingPartNumber( searchPartNum)) { |
| 308 |
if ( verifyPartExistence( searchPartNum ) ) |
| 309 |
sibPartNum = searchPartNum; |
| 310 |
} |
| 311 |
|
| 312 |
SoGroup *parentNode = (SoGroup *) nodeList[parentPartNum]; |
| 313 |
if (sibPartNum == -1 ) { |
| 314 |
// no right sibling is made yet, so just add this as a child to parent |
| 315 |
parentNode->addChild( nodeList[partNum] ); |
| 316 |
} |
| 317 |
else { |
| 318 |
// find that sibling's index in the parent |
| 319 |
int sibIndex = parentNode->findChild(nodeList[sibPartNum]); |
| 320 |
|
| 321 |
// insert this node as the new child at that index |
| 322 |
parentNode->insertChild( nodeList[partNum], sibIndex ); |
| 323 |
} |
| 324 |
return TRUE; |
| 325 |
} |
| 326 |
|
| 327 |
//////////////////////////////////////////////////////////////////////// |
| 328 |
// |
| 329 |
// Description: |
| 330 |
// Gets the part asked for |
| 331 |
// Note: |
| 332 |
// this routine will NOT search for the requested part name within the |
| 333 |
// catalogs of its child parts. |
| 334 |
// |
| 335 |
// Use: private |
| 336 |
|
| 337 |
SoNode * |
| 338 |
SoV1NodekitParts::getPartFromThisCatalog( const int partNum, |
| 339 |
SbBool makeIfNeeded, SbBool leafCheck, |
| 340 |
SbBool publicCheck ) |
| 341 |
// |
| 342 |
//////////////////////////////////////////////////////////////////////// |
| 343 |
{ |
| 344 |
if ( !partFoundCheck( partNum ) ) |
| 345 |
return FALSE; |
| 346 |
|
| 347 |
if ( leafCheck ) { |
| 348 |
if ( !partIsLeafCheck( partNum ) ) |
| 349 |
return FALSE; |
| 350 |
} |
| 351 |
if ( publicCheck ) { |
| 352 |
if ( !partIsPublicCheck( partNum ) ) |
| 353 |
return FALSE; |
| 354 |
} |
| 355 |
|
| 356 |
// Don't do this check any more. For this reduced version of SoBaseKit, |
| 357 |
// allow parts that are lists to be returned with getAnyPart() |
| 358 |
//XXX if ( !partIsNotListCheck( partNum ) ) |
| 359 |
//XXX return FALSE; |
| 360 |
|
| 361 |
if ( makeIfNeeded == FALSE ) { |
| 362 |
// just return whatever you can find |
| 363 |
if ( partNum != SO_V1_CATALOG_NAME_NOT_FOUND |
| 364 |
&& verifyPartExistence(partNum) ) |
| 365 |
return ( nodeList[ partNum ] ); |
| 366 |
else |
| 367 |
return NULL; |
| 368 |
} |
| 369 |
|
| 370 |
// otherwise, we need to make the part |
| 371 |
if ( makePart( partNum ) == TRUE ) |
| 372 |
return ( nodeList[ partNum ] ); // it was made O.K. |
| 373 |
else |
| 374 |
return NULL; // it didn't get made properly |
| 375 |
} |
| 376 |
|
| 377 |
//////////////////////////////////////////////////////////////////////// |
| 378 |
// |
| 379 |
// Description: |
| 380 |
// Sets the part given by nameOfPart |
| 381 |
// Note: |
| 382 |
// this routine will NOT search for the requested part name within the |
| 383 |
// catalogs of its child parts. |
| 384 |
// |
| 385 |
// Use: private |
| 386 |
|
| 387 |
SbBool |
| 388 |
SoV1NodekitParts::setPartFromThisCatalog( const int partNum, |
| 389 |
SoNode *newPartNode, SbBool anyPart ) |
| 390 |
// |
| 391 |
//////////////////////////////////////////////////////////////////////// |
| 392 |
{ |
| 393 |
if ( !partFoundCheck( partNum ) ) |
| 394 |
return FALSE; |
| 395 |
if ( !anyPart ) { |
| 396 |
if ( !partIsLeafCheck( partNum ) ) |
| 397 |
return FALSE; |
| 398 |
if ( !partIsPublicCheck( partNum ) ) |
| 399 |
return FALSE; |
| 400 |
if ( !partIsNotListCheck( partNum ) ) |
| 401 |
return FALSE; |
| 402 |
} |
| 403 |
|
| 404 |
// otherwise, we need to replace the part that is currently being used |
| 405 |
return( replacePart( partNum, newPartNode ) ); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
//////////////////////////////////////////////////////////////////////// |
| 411 |
// |
| 412 |
// Description: |
| 413 |
// Gets the part asked for |
| 414 |
// |
| 415 |
// Use: protected |
| 416 |
|
| 417 |
SoNode * |
| 418 |
SoV1NodekitParts::getAnyPart( const SbName &nameOfPart, SbBool makeIfNeeded, |
| 419 |
SbBool leafCheck, SbBool publicCheck ) |
| 420 |
// |
| 421 |
//////////////////////////////////////////////////////////////////////// |
| 422 |
{ |
| 423 |
// ONLY ALLOW SINGLE NAME PARTS... |
| 424 |
if ( strrchr( nameOfPart.getString(), '.' ) != NULL ) { |
| 425 |
#ifdef DEBUG |
| 426 |
SoDebugError::post("SoV1NodekitParts::getAnyPart", |
| 427 |
"part name %s is not a single-word part", |
| 428 |
nameOfPart.getString()); |
| 429 |
#endif |
| 430 |
return NULL; |
| 431 |
} |
| 432 |
|
| 433 |
// DON'T ALLOW BRACKETS, WHICH SIGNIFY INDEXING INTO A LIST |
| 434 |
// IN THE NEW NODEKITS. |
| 435 |
if ( strrchr( nameOfPart.getString(), '[') != NULL ) { |
| 436 |
#ifdef DEBUG |
| 437 |
SoDebugError::post("SoV1NodekitParts::getAnyPart", |
| 438 |
"part name %s contains an index, which is illegal", |
| 439 |
nameOfPart.getString()); |
| 440 |
#endif |
| 441 |
return FALSE; |
| 442 |
} |
| 443 |
|
| 444 |
// IS THE REQUESTED PART IN THIS CATALOG? |
| 445 |
int partNum = catalog->getPartNumber( nameOfPart ); |
| 446 |
if ( partNum != SO_V1_CATALOG_NAME_NOT_FOUND ) |
| 447 |
|
| 448 |
// IF SO, THEN GET IT FROM THIS CATALOG |
| 449 |
return ( getPartFromThisCatalog( partNum, makeIfNeeded,leafCheck, |
| 450 |
publicCheck)); |
| 451 |
|
| 452 |
else { |
| 453 |
|
| 454 |
// ELSE, SEARCH THE CATALOG RECURSIVELY FOR THE DESIRED PART |
| 455 |
// we need to pass a list to the recursive search saying which |
| 456 |
// types of nodes we have already checked. This avoids infinite |
| 457 |
// loop (does chicken contain egg? does egg contain chicken? etc...) |
| 458 |
SbPList *typesChecked = new SbPList(); |
| 459 |
int thisPartNum = catalog->getPartNumber( "this" ); |
| 460 |
typesChecked->append( |
| 461 |
(void *) catalog->getType(thisPartNum).getName().getString() ); |
| 462 |
|
| 463 |
for (int i = 0; i < numEntries; i++ ) { |
| 464 |
|
| 465 |
// does it lie within this 'intermediary' part? |
| 466 |
if ( catalog->recursiveSearch(i,nameOfPart,typesChecked) == TRUE){ |
| 467 |
|
| 468 |
delete typesChecked; // don't need this anymore |
| 469 |
|
| 470 |
// if not making parts and 'intermediary' is NULL... |
| 471 |
if ( makeIfNeeded == FALSE && !verifyPartExistence( i ) ) |
| 472 |
return NULL; |
| 473 |
|
| 474 |
// create the intermediary part... |
| 475 |
if ( !makePart( i ) ) |
| 476 |
return NULL; // error making the part |
| 477 |
// NOTE: it's okay to cast the node into a base kit here, |
| 478 |
// since the recursive search would only have worked |
| 479 |
// if it was a nodekit |
| 480 |
// UGLY, yes, but it works. |
| 481 |
#ifdef DEBUG |
| 482 |
// supposedly unnecessary type checking: |
| 483 |
if ( !nodeList[i]->isOfType( SoV1BaseKit::getClassTypeId() ) ) |
| 484 |
return NULL; |
| 485 |
#endif |
| 486 |
SoV1BaseKit *intermediary = (SoV1BaseKit *) nodeList[i]; |
| 487 |
|
| 488 |
// now that intermediary is built, get the part from within it |
| 489 |
return(intermediary->getAnyPart(nameOfPart, |
| 490 |
SoNode::getClassTypeId(), makeIfNeeded, |
| 491 |
leafCheck, publicCheck )); |
| 492 |
} |
| 493 |
} |
| 494 |
delete typesChecked; // don't need this anymore |
| 495 |
|
| 496 |
} |
| 497 |
|
| 498 |
// IF YOU GOT HERE, THE PART COULD NOT BE FOUND |
| 499 |
#ifdef DEBUG |
| 500 |
SoDebugError::post("SoV1NodekitParts::getAnyPart", |
| 501 |
"entry named %s not found. returning NULL", |
| 502 |
nameOfPart.getString() ); |
| 503 |
#endif |
| 504 |
return NULL; |
| 505 |
} |
| 506 |
|
| 507 |
//////////////////////////////////////////////////////////////////////// |
| 508 |
// |
| 509 |
// Description: |
| 510 |
// Sets the given part with the node passed in as an argument. |
| 511 |
// Returns TRUE if successful, FALSE if not. |
| 512 |
// |
| 513 |
// Use: private |
| 514 |
|
| 515 |
SbBool |
| 516 |
SoV1NodekitParts::setAnyPart( const SbName &nameOfPart, SoNode *newPartNode, |
| 517 |
SbBool anyPart ) |
| 518 |
// |
| 519 |
//////////////////////////////////////////////////////////////////////// |
| 520 |
{ |
| 521 |
// JUST SINGLE NAME PARTS... |
| 522 |
if ( strrchr( nameOfPart.getString(), '.' ) != NULL ) { |
| 523 |
#ifdef DEBUG |
| 524 |
SoDebugError::post("SoV1NodekitParts::setAnyPart", |
| 525 |
"part name %s is not a single-word part", |
| 526 |
nameOfPart.getString()); |
| 527 |
#endif |
| 528 |
return FALSE; |
| 529 |
} |
| 530 |
|
| 531 |
// DON'T ALLOW BRACKETS, WHICH SIGNIFY INDEXING INTO A LIST |
| 532 |
// IN THE NEW NODEKITS? |
| 533 |
if ( strrchr( nameOfPart.getString(), '[') != NULL ) { |
| 534 |
#ifdef DEBUG |
| 535 |
SoDebugError::post("SoV1NodekitParts::setAnyPart", |
| 536 |
"part name %s contains an index, which is illegal", |
| 537 |
nameOfPart.getString()); |
| 538 |
#endif |
| 539 |
return FALSE; |
| 540 |
} |
| 541 |
|
| 542 |
// NOT A LIST ITEM, IF WE GOT HERE. |
| 543 |
|
| 544 |
// IS THE REQUESTED PART IN THIS CATALOG? |
| 545 |
int partNum = catalog->getPartNumber( nameOfPart ); |
| 546 |
if ( partNum != SO_V1_CATALOG_NAME_NOT_FOUND ) |
| 547 |
|
| 548 |
// IF SO, THEN SET IT USING THIS CATALOG |
| 549 |
return ( setPartFromThisCatalog( partNum, newPartNode, anyPart ) ); |
| 550 |
|
| 551 |
else { |
| 552 |
|
| 553 |
// ELSE, SEARCH THE CATALOG RECURSIVELY FOR THE DESIRED PART |
| 554 |
// we need to pass a list to the recursive search saying which |
| 555 |
// types of nodes we have already checked. This avoids infinite |
| 556 |
// loop (does chicken contain egg? does egg contain chicken? etc...) |
| 557 |
SbPList *typesChecked = new SbPList(); |
| 558 |
int thisPartNum = catalog->getPartNumber( "this" ); |
| 559 |
typesChecked->append( |
| 560 |
(void *) catalog->getType(thisPartNum).getName().getString() ); |
| 561 |
|
| 562 |
for (int i = 0; i < numEntries; i++ ) { |
| 563 |
|
| 564 |
// does it lie within this 'intermediary' part? |
| 565 |
if ( catalog->recursiveSearch(i,nameOfPart,typesChecked) == TRUE){ |
| 566 |
|
| 567 |
delete typesChecked; // don't need this anymore |
| 568 |
|
| 569 |
// create the intermediary part... |
| 570 |
if ( !makePart( i ) ) |
| 571 |
return FALSE; // error making the part |
| 572 |
// NOTE: it's okay to cast the node into a base kit here, |
| 573 |
// since the recursive search would only have worked |
| 574 |
// if it was a nodekit |
| 575 |
// UGLY, yes, but it works. |
| 576 |
#ifdef DEBUG |
| 577 |
// supposedly unnecessary type checking: |
| 578 |
if ( !nodeList[i]->isOfType( SoV1BaseKit::getClassTypeId() ) ) |
| 579 |
return FALSE; |
| 580 |
#endif |
| 581 |
SoV1BaseKit *intermediary = (SoV1BaseKit *) nodeList[i]; |
| 582 |
|
| 583 |
// now that intermediary is built, set the part within it |
| 584 |
return(intermediary->setAnyPart(nameOfPart, newPartNode, |
| 585 |
anyPart )); |
| 586 |
} |
| 587 |
} |
| 588 |
delete typesChecked; // don't need this anymore |
| 589 |
|
| 590 |
} |
| 591 |
|
| 592 |
// IF YOU GOT HERE, THE PART COULD NOT BE FOUND |
| 593 |
#ifdef DEBUG |
| 594 |
SoDebugError::post("SoV1NodekitParts::setAnyPart", |
| 595 |
"entry named %s not found. returning NULL", |
| 596 |
nameOfPart.getString() ); |
| 597 |
#endif |
| 598 |
return FALSE; |
| 599 |
} |
| 600 |
|
| 601 |
//////////////////////////////////////////////////////////////////////// |
| 602 |
// |
| 603 |
// Description: |
| 604 |
// error if partNum is not legal |
| 605 |
// |
| 606 |
SbBool |
| 607 |
SoV1NodekitParts::partFoundCheck( int partNum ) |
| 608 |
{ |
| 609 |
if ( partNum == SO_V1_CATALOG_NAME_NOT_FOUND ) { |
| 610 |
#ifdef DEBUG |
| 611 |
SoDebugError::post("SoV1NodekitParts::partFoundCheck", |
| 612 |
"can't find part"); |
| 613 |
#endif |
| 614 |
return FALSE; |
| 615 |
} |
| 616 |
return TRUE; |
| 617 |
} |
| 618 |
|
| 619 |
//////////////////////////////////////////////////////////////////////// |
| 620 |
// |
| 621 |
// Description: |
| 622 |
// error if partNum is not a leaf |
| 623 |
// |
| 624 |
SbBool |
| 625 |
SoV1NodekitParts::partIsLeafCheck( int partNum ) |
| 626 |
{ |
| 627 |
if ( catalog->isLeaf( partNum ) == FALSE ) { |
| 628 |
#ifdef DEBUG |
| 629 |
SoDebugError::post("SoV1NodekitParts::partIsLeafCheck", |
| 630 |
"can't return the part %s because it is not a leaf node in the nodekit's structure. returning NULL", |
| 631 |
catalog->getName( partNum ).getString() ); |
| 632 |
#endif |
| 633 |
return FALSE; |
| 634 |
} |
| 635 |
return TRUE; |
| 636 |
} |
| 637 |
|
| 638 |
//////////////////////////////////////////////////////////////////////// |
| 639 |
// |
| 640 |
// Description: |
| 641 |
// error if partNum is not public |
| 642 |
// |
| 643 |
SbBool |
| 644 |
SoV1NodekitParts::partIsPublicCheck( int partNum ) |
| 645 |
{ |
| 646 |
if ( catalog->isPublic( partNum ) == FALSE ) { |
| 647 |
#ifdef DEBUG |
| 648 |
SoDebugError::post("SoV1NodekitParts::partIsPublicCheck", |
| 649 |
"can't return the part %s because it is not a public node in the nodekit's structure returning NULL instead", |
| 650 |
catalog->getName( partNum ).getString() ); |
| 651 |
#endif |
| 652 |
return FALSE; |
| 653 |
} |
| 654 |
return TRUE; |
| 655 |
} |
| 656 |
|
| 657 |
//////////////////////////////////////////////////////////////////////// |
| 658 |
// |
| 659 |
// Description: |
| 660 |
// error if partNum is not a list |
| 661 |
// |
| 662 |
SbBool |
| 663 |
SoV1NodekitParts::partIsNotListCheck( int partNum ) |
| 664 |
{ |
| 665 |
if ( catalog->isList( partNum ) == TRUE ) { |
| 666 |
#ifdef DEBUG |
| 667 |
SoDebugError::post("SoV1NodekitParts::partIsNotListCheck", |
| 668 |
"can't return the part %s because it is a list in the nodekit's structure Lists must be accessed throught the methods in the nodekit. returning NULL instead", |
| 669 |
catalog->getName(partNum).getString() ); |
| 670 |
#endif |
| 671 |
return FALSE; |
| 672 |
} |
| 673 |
return TRUE; |
| 674 |
} |
| 675 |
|
| 676 |
//////////////////////////////////////////////////////////////////////// |
| 677 |
// |
| 678 |
// Description: |
| 679 |
// This checks that a part still exists as the child of its parent, |
| 680 |
// and so on up to 'this' |
| 681 |
// It needs to be called as protection against outside parties deciding to |
| 682 |
// delete nodes in the kit. |
| 683 |
// If the integrity of the kit is broken, routines which call |
| 684 |
// 'checkPartIntegrity' will know to create new parts instead, or |
| 685 |
// perhaps return NULL. |
| 686 |
// |
| 687 |
SbBool |
| 688 |
SoV1NodekitParts::verifyPartExistence( int partNum ) |
| 689 |
{ |
| 690 |
SoNode *part = nodeList[partNum]; |
| 691 |
|
| 692 |
// is the part there? |
| 693 |
if ( part == NULL ) |
| 694 |
return FALSE; |
| 695 |
|
| 696 |
// is the part the top of the catalog? Then we're OK |
| 697 |
if ( partNum == catalog->getPartNumber( "this" ) ) |
| 698 |
return TRUE; |
| 699 |
|
| 700 |
int parentPartNum = catalog->getParentPartNumber( partNum ); |
| 701 |
SoGroup *parent = (SoGroup *) nodeList[parentPartNum]; |
| 702 |
|
| 703 |
// parent should exist. |
| 704 |
if ( parent == NULL ) |
| 705 |
return FALSE; |
| 706 |
|
| 707 |
// part should be a valid child of parent |
| 708 |
if ( parent->findChild( part ) < 0 ) |
| 709 |
return FALSE; |
| 710 |
|
| 711 |
// verify the parent |
| 712 |
return( verifyPartExistence( parentPartNum ) ); |
| 713 |
} |