| 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.2 $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoFieldData |
| 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 <assert.h> |
| 55 | #include <cstddef> |
| 56 | #include <Inventor/SbString.h> |
| 57 | #include <Inventor/SoInput.h> |
| 58 | #include <Inventor/SoOutput.h> |
| 59 | #include <Inventor/errors/SoDebugError.h> |
| 60 | #include <Inventor/errors/SoReadError.h> |
| 61 | #include <Inventor/fields/SoField.h> |
| 62 | #include <Inventor/fields/SoFieldData.h> |
| 63 | #include <Inventor/nodes/SoNode.h> |
| 64 | |
| 65 | // Syntax for reading/writing type information to files |
| 66 | #define OPEN_BRACE_CHAR '[' |
| 67 | #define CLOSE_BRACE_CHAR ']' |
| 68 | #define VALUE_SEPARATOR_CHAR ',' |
| 69 | |
| 70 | // In the binary file format, the number of fields integer is actually |
| 71 | // a bitfield containing the number of fields written and whether or |
| 72 | // not there is a description of those fields following. This assumes |
| 73 | // there are less than 2^14 (16,384) different fields in any node. |
| 74 | // We also assume that unsigned shorts have at least 14 bits. |
| 75 | #define NOT_BUILTIN_BIT (1<<14) |
| 76 | |
| 77 | ////////////////////////////////////////////////////////////////////////////// |
| 78 | // |
| 79 | // SoFieldEntry holds the name and offset of a field in an SoFieldData. |
| 80 | // SoEnumEntry holds the type name and name/value pairs for an enum type. |
| 81 | // These are internal to Inventor. A list of each of these is stored |
| 82 | // in the SoFieldData. |
| 83 | // |
| 84 | ////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | struct SoFieldEntry { |
| 87 | SbName name; // Name of field |
| 88 | std::ptrdiff_t offset; // Offset of field within object |
| 89 | }; |
| 90 | |
| 91 | struct SoEnumEntry { |
| 92 | SbName typeName; // Name of enum type |
| 93 | int num; // number of values |
| 94 | int arraySize; // size of arrays |
| 95 | int *vals; // array of values |
| 96 | SbName *names; // array of names |
| 97 | |
| 98 | SoEnumEntry(const SbName &name); |
| 99 | SoEnumEntry(const SoEnumEntry &o); |
| 100 | ~SoEnumEntry(); |
| 101 | |
| 102 | static int growSize; // amount to grow arrays |
| 103 | }; |
| 104 | |
| 105 | int SoEnumEntry::growSize = 6; |
| 106 | |
| 107 | SoEnumEntry::SoEnumEntry(const SbName &name) |
| 108 | { |
| 109 | typeName = name; |
| 110 | num = 0; |
| 111 | arraySize = growSize; |
| 112 | vals = new int[arraySize]; |
| 113 | names = new SbName[arraySize]; |
| 114 | } |
| 115 | |
| 116 | SoEnumEntry::SoEnumEntry(const SoEnumEntry &o) |
| 117 | { |
| 118 | typeName = o.typeName, |
| 119 | num = o.num; |
| 120 | arraySize = num; |
| 121 | vals = new int[arraySize]; |
| 122 | names = new SbName[arraySize]; |
| 123 | for (int i=0; i<num; i++) { |
| 124 | vals[i] = o.vals[i]; |
| 125 | names[i] = o.names[i]; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | SoEnumEntry::~SoEnumEntry() |
| 130 | { |
| 131 | delete [] vals; |
| 132 | delete [] names; |
| 133 | } |
| 134 | |
| 135 | //////////////////////////////////////////////////////////////////////// |
| 136 | // |
| 137 | // Description: |
| 138 | // Copy constructor |
| 139 | // |
| 140 | // Use: public |
| 141 | |
| 142 | SoFieldData::SoFieldData(const SoFieldData &src) : |
| 143 | fields(src.fields.getLength()) |
| 144 | // |
| 145 | //////////////////////////////////////////////////////////////////////// |
| 146 | { |
| 147 | copy(&src); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | //////////////////////////////////////////////////////////////////////// |
| 152 | // |
| 153 | // Description: |
| 154 | // Like a copy constructor, but takes a pointer rather than reference. |
| 155 | // The pointer may be NULL. |
| 156 | // |
| 157 | // Use: public |
| 158 | |
| 159 | SoFieldData::SoFieldData(const SoFieldData *src) : |
| 160 | fields(src ? src->fields.getLength() : 0) |
| 161 | // |
| 162 | //////////////////////////////////////////////////////////////////////// |
| 163 | { |
| 164 | if (src) copy(src); |
| 165 | } |
| 166 | |
| 167 | //////////////////////////////////////////////////////////////////////// |
| 168 | // |
| 169 | // Description: |
| 170 | // Destructor |
| 171 | // |
| 172 | // Use: public |
| 173 | |
| 174 | SoFieldData::~SoFieldData() |
| 175 | |
| 176 | // |
| 177 | //////////////////////////////////////////////////////////////////////// |
| 178 | { |
| 179 | struct SoFieldEntry *tmpField; |
| 180 | struct SoEnumEntry *tmpEnum; |
| 181 | |
| 182 | // Delete the list of fields and enums; |
| 183 | for (int i=0; i<fields.getLength(); i++) { |
| 184 | tmpField = (struct SoFieldEntry *)fields[i]; |
| 185 | delete tmpField; |
| 186 | } |
| 187 | |
| 188 | for (int j=0; j<enums.getLength(); j++) { |
| 189 | tmpEnum = (struct SoEnumEntry *)enums[j]; |
| 190 | delete tmpEnum; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | //////////////////////////////////////////////////////////////////////// |
| 195 | // |
| 196 | // Description: |
| 197 | // Adds a field to current fieldData. |
| 198 | // |
| 199 | // Use: public |
| 200 | |
| 201 | void |
| 202 | SoFieldData::addField(SoFieldContainer *defobj, // Object with default values |
| 203 | const char *fieldName, // Name of field |
| 204 | const SoField *field) // Pointer 2 field (in defNode) |
| 205 | // |
| 206 | //////////////////////////////////////////////////////////////////////// |
| 207 | { |
| 208 | struct SoFieldEntry *newField = new struct SoFieldEntry; |
| 209 | newField->name = fieldName; |
| 210 | newField->offset = (const char *) field - (const char *) defobj; |
| 211 | |
| 212 | fields.append((void *) newField); |
| 213 | } |
| 214 | |
| 215 | //////////////////////////////////////////////////////////////////////// |
| 216 | // |
| 217 | // Description: |
| 218 | // Copies values and flags from field data in object to another |
| 219 | // object (of the same type, presumably). If copyConnections is |
| 220 | // TRUE, any connections to the fields are copied as well. |
| 221 | // |
| 222 | // Use: public |
| 223 | |
| 224 | void |
| 225 | SoFieldData::overlay(SoFieldContainer *to, const SoFieldContainer *from, |
| 226 | SbBool copyConnections) const |
| 227 | // |
| 228 | //////////////////////////////////////////////////////////////////////// |
| 229 | { |
| 230 | // Access both field datas instead of just one, in case they |
| 231 | // differ per instance. |
| 232 | const SoFieldData *fromFD = from->getFieldData(); |
| 233 | const SoFieldData *toFD = to->getFieldData(); |
| 234 | |
| 235 | SoField *fromField, *toField; |
| 236 | int i; |
| 237 | |
| 238 | for (i = 0; i < fromFD->fields.getLength(); i++) { |
| 239 | |
| 240 | // Access the fields using the appropriate field data instances |
| 241 | toField = toFD->getField(to, i); |
| 242 | fromField = fromFD->getField(from, i); |
| 243 | |
| 244 | // If both fields have default values, we don't bother copying |
| 245 | // the value: |
| 246 | if (! fromField->isDefault() || ! toField->isDefault()) |
| 247 | // This method copies just the value... |
| 248 | toField->copyFrom(*fromField); |
| 249 | |
| 250 | // ... so we still have to copy the rest |
| 251 | toField->setIgnored(fromField->isIgnored()); |
| 252 | toField->setDefault(fromField->isDefault()); |
| 253 | toField->enableNotify(fromField->isNotifyEnabled()); |
| 254 | |
| 255 | // Allow the field to fix itself up after the copy. Most |
| 256 | // fields do nothing here, but node, path, and engine fields |
| 257 | // need to make sure instances are handled properly. |
| 258 | toField->fixCopy(copyConnections); |
| 259 | |
| 260 | // Copy the connection if necessary |
| 261 | if (fromField->isConnected() && copyConnections) |
| 262 | toField->copyConnection(fromField); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | //////////////////////////////////////////////////////////////////////// |
| 267 | // |
| 268 | // Description: |
| 269 | // Returns name of field with given index in given object. |
| 270 | // |
| 271 | // Use: public |
| 272 | |
| 273 | const SbName & |
| 274 | SoFieldData::getFieldName(int index) const |
| 275 | // |
| 276 | //////////////////////////////////////////////////////////////////////// |
| 277 | { |
| 278 | return ((SoFieldEntry *) fields[index])->name; |
| 279 | } |
| 280 | |
| 281 | //////////////////////////////////////////////////////////////////////// |
| 282 | // |
| 283 | // Description: |
| 284 | // Returns pointer to field with given index within given node instance. |
| 285 | // |
| 286 | // Use: public |
| 287 | |
| 288 | SoField * |
| 289 | SoFieldData::getField(const SoFieldContainer *object, int index) const |
| 290 | // |
| 291 | //////////////////////////////////////////////////////////////////////// |
| 292 | { |
| 293 | // This generates a CC warning; there's not much we can do about it... |
| 294 | return (SoField *) ((char *) object + |
| 295 | ((SoFieldEntry *) fields[index])->offset); |
| 296 | } |
| 297 | |
| 298 | //////////////////////////////////////////////////////////////////////// |
| 299 | // |
| 300 | // Description: |
| 301 | // Returns index of field, given the field and the instance it is in. |
| 302 | |
| 303 | int |
| 304 | SoFieldData::getIndex(const SoFieldContainer *fc, const SoField *field) const |
| 305 | // |
| 306 | //////////////////////////////////////////////////////////////////////// |
| 307 | { |
| 308 | for (int i = 0; i < getNumFields(); i++) |
| 309 | if (getField(fc, i) == field) |
| 310 | return i; |
| 311 | |
| 312 | #ifdef DEBUG |
| 313 | SoDebugError::post("SoFieldData::getIndex", "Can't find index for field"); |
| 314 | #endif /* DEBUG */ |
| 315 | |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | ///////////////////////////////////////////////////////////////// |
| 320 | // |
| 321 | // Description: |
| 322 | // |
| 323 | // Strips leading and trailing white space off a string, returning |
| 324 | // an SbName. |
| 325 | // |
| 326 | // Use: private in this file. Should really be a method of SbName |
| 327 | // |
| 328 | #include <ctype.h> |
| 329 | static SbName |
| 330 | stripWhite(const char *name) |
| 331 | // |
| 332 | //////////////////////////////////////////////////////////////////////// |
| 333 | { |
| 334 | int firstchar = -1; |
| 335 | int lastchar = -1; |
| 336 | int lastwhite = -1; |
| 337 | |
| 338 | // scan string, marking first & last non-white char, and last white |
| 339 | int i; |
| 340 | for (i=0; name[i]; i++) { |
| 341 | if (isspace(name[i])) |
| 342 | lastwhite = i; |
| 343 | else { |
| 344 | if (firstchar == -1) firstchar = i; |
| 345 | lastchar = i; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // No trailing whitespace? -- just start at first non-white char |
| 350 | if (lastchar > lastwhite) |
| 351 | return SbName(&name[firstchar]); |
| 352 | |
| 353 | // trailing whitespace -- need to copy into buffer (to be able |
| 354 | // to place trailing NULL-termination, since the the string is const). |
| 355 | char buf[500]; |
| 356 | int b; |
| 357 | for (b=0, i=firstchar; i<=lastchar; i++, b++) |
| 358 | buf[b] = name[i]; |
| 359 | buf[b] = 0; |
| 360 | return SbName(buf); |
| 361 | } |
| 362 | |
| 363 | //////////////////////////////////////////////////////////////////////// |
| 364 | // |
| 365 | // Description: |
| 366 | // Adds an enum value to current fieldData. If the enum type |
| 367 | // hasn't been added yet, a new entry is created for it. |
| 368 | // |
| 369 | // Use: public |
| 370 | |
| 371 | void |
| 372 | SoFieldData::addEnumValue(const char *typeNameArg, const char *valNameArg, |
| 373 | int val) |
| 374 | // |
| 375 | //////////////////////////////////////////////////////////////////////// |
| 376 | { |
| 377 | struct SoEnumEntry *e = NULL; |
| 378 | SbName typeName = stripWhite(typeNameArg); |
| 379 | SbName valName = stripWhite(valNameArg); |
| 380 | |
| 381 | // look for an entry for this type name |
| 382 | for (int i=0; i<enums.getLength(); i++) { |
| 383 | e = (struct SoEnumEntry *) enums[i]; |
| 384 | if (e->typeName == typeName) |
| 385 | break; |
| 386 | else |
| 387 | e = NULL; |
| 388 | } |
| 389 | // make an entry if there wasn't one already |
| 390 | if (e == NULL) { |
| 391 | e = new SoEnumEntry(typeName); |
| 392 | enums.append((void*) e); |
| 393 | } |
| 394 | // grow arrays if needed |
| 395 | if (e->num == e->arraySize) { |
| 396 | e->arraySize += SoEnumEntry::growSize; |
| 397 | int *ovals = e->vals; |
| 398 | SbName *onames = e->names; |
| 399 | e->vals = new int[e->arraySize]; |
| 400 | e->names = new SbName[e->arraySize]; |
| 401 | for (int i=0; i<e->num; i++) { |
| 402 | e->vals[i] = ovals[i]; |
| 403 | e->names[i] = onames[i]; |
| 404 | } |
| 405 | delete [] ovals; |
| 406 | delete [] onames; |
| 407 | } |
| 408 | e->vals[e->num] = val; |
| 409 | e->names[e->num] = valName; |
| 410 | e->num++; |
| 411 | } |
| 412 | |
| 413 | //////////////////////////////////////////////////////////////////////// |
| 414 | // |
| 415 | // Description: |
| 416 | // Returns name/value arrays for a given enum type. |
| 417 | // |
| 418 | // Use: public |
| 419 | |
| 420 | void |
| 421 | SoFieldData::getEnumData(const char *typeNameArg, int &num, |
| 422 | int *&vals, SbName *&names) |
| 423 | // |
| 424 | //////////////////////////////////////////////////////////////////////// |
| 425 | { |
| 426 | SbName typeName = stripWhite(typeNameArg); |
| 427 | |
| 428 | // look for an entry for this type name |
| 429 | for (int i=0; i<enums.getLength(); i++) { |
| 430 | struct SoEnumEntry *e = (struct SoEnumEntry *) enums[i]; |
| 431 | if (e->typeName == typeName) { |
| 432 | num = e->num; |
| 433 | vals = e->vals; |
| 434 | names = e->names; |
| 435 | return; |
| 436 | } |
| 437 | } |
| 438 | // no entry found. |
| 439 | num = 0; |
| 440 | vals = NULL; |
| 441 | names = NULL; |
| 442 | } |
| 443 | |
| 444 | //////////////////////////////////////////////////////////////////////// |
| 445 | // |
| 446 | // Description: |
| 447 | // Reads field type information for an unrecognized node. The |
| 448 | // ASCII syntax is a set of field type - field name pairs, |
| 449 | // separated by commas, enclosed withing square brackets. Returns |
| 450 | // TRUE on success, FALSE if there is a syntax error or |
| 451 | // unrecognized field type. |
| 452 | // |
| 453 | // numDescriptions is a very large number when reading ASCII (where |
| 454 | // we just read until the closing square brace). |
| 455 | // |
| 456 | // Use: private |
| 457 | |
| 458 | SbBool |
| 459 | SoFieldData::readFieldDescriptions( |
| 460 | SoInput *in, SoFieldContainer *object, int numDescriptions) const |
| 461 | |
| 462 | // |
| 463 | //////////////////////////////////////////////////////////////////////// |
| 464 | { |
| 465 | SbBool gotChar; |
| 466 | SbName fieldType, fieldName; |
| 467 | char c; |
| 468 | |
| 469 | SbBool isBinary = in->isBinary(); |
| 470 | |
| 471 | SbBool hadFieldsDefined = fields.getLength() > 0; |
| 472 | |
| 473 | if (!isBinary) |
| 474 | if (! ((gotChar = in->read(c)) || c != OPEN_BRACE_CHAR)) |
| 475 | return FALSE; |
| 476 | |
| 477 | for (int i = 0; i < numDescriptions; i++) { |
| 478 | |
| 479 | // Check for closing brace: |
| 480 | if (!isBinary) { |
| 481 | // Check for closing brace: |
| 482 | if (in->read(c) && c == CLOSE_BRACE_CHAR) |
| 483 | return TRUE; |
| 484 | else in->putBack(c); |
| 485 | } |
| 486 | |
| 487 | SbName type, fieldName; |
| 488 | if (!in->read(type, TRUE)) return FALSE; |
| 489 | if (!in->read(fieldName, TRUE)) return FALSE; |
| 490 | |
| 491 | SoType fldType = SoType::fromName(type); |
| 492 | |
| 493 | if (!hadFieldsDefined) { |
| 494 | // Only create fields if fields haven't already been |
| 495 | // defined. This isn't 100% correct-- we'll create |
| 496 | // field for nodes/engines that have not fields if the |
| 497 | // user specifies fields/inputs for them. But that's |
| 498 | // a case I'm not going to worry about (there are VERY |
| 499 | // few nodes that have no fields). |
| 500 | |
| 501 | if (fldType.isBad()) |
| 502 | return FALSE; |
| 503 | |
| 504 | // Create and initialize an instance of the field. |
| 505 | // Add it to the field data. |
| 506 | SoField *fld = (SoField *)(fldType.createInstance()); |
| 507 | fld->setContainer(object); |
| 508 | |
| 509 | // Cast const away: |
| 510 | SoFieldData *This = (SoFieldData *)this; |
| 511 | This->addField(object, fieldName.getString(), fld); |
| 512 | } |
| 513 | #ifdef DEBUG |
| 514 | else { |
| 515 | // Check to make sure specification matches reality: |
| 516 | SoField *f = object->getField(fieldName); |
| 517 | if (f == NULL) { |
| 518 | SoDebugError::post("SoFieldData::readFieldDescriptions", |
| 519 | "%s does not have a field named %s", |
| 520 | object->getTypeId().getName().getString(), |
| 521 | fieldName.getString()); |
| 522 | } |
| 523 | else if (!f->isOfType(fldType)) { |
| 524 | SoDebugError::postWarning("SoFieldData::readFieldDescriptions", |
| 525 | "%s.%s is type %s, NOT type %s", |
| 526 | object->getTypeId().getName().getString(), |
| 527 | fieldName.getString(), |
| 528 | f->getTypeId().getName().getString(), |
| 529 | type.getString()); |
| 530 | } |
| 531 | } |
| 532 | #endif |
| 533 | if (!isBinary) { |
| 534 | // Better get a ',' or a ']' at this point: |
| 535 | if (! in->read(c)) |
| 536 | return FALSE; |
| 537 | if (c != VALUE_SEPARATOR_CHAR) { |
| 538 | if (c == CLOSE_BRACE_CHAR) |
| 539 | return TRUE; |
| 540 | else return FALSE; |
| 541 | } |
| 542 | // Got a ',', continue reading |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return TRUE; |
| 547 | } |
| 548 | |
| 549 | //////////////////////////////////////////////////////////////////////// |
| 550 | // |
| 551 | // Description: |
| 552 | // Reads into fields of object according to SoInput. The third |
| 553 | // parameter indicates whether an unknown field should be reported |
| 554 | // as an error (default is TRUE); this can be FALSE for nodes that |
| 555 | // have children. |
| 556 | // |
| 557 | // Use: internal |
| 558 | |
| 559 | SbBool |
| 560 | SoFieldData::read(SoInput *in, SoFieldContainer *object, |
| 561 | SbBool errorOnUnknownField, SbBool ¬BuiltIn) const |
| 562 | // |
| 563 | //////////////////////////////////////////////////////////////////////// |
| 564 | { |
| 565 | // TRUE if reading an Inventor file pre-Inventor 2.1: |
| 566 | SbBool oldFileFormat = (in->getIVVersion() < 2.1f); |
| 567 | |
| 568 | // TRUE if reading binary file format: |
| 569 | SbBool isBinary = in->isBinary(); |
| 570 | |
| 571 | // Assume it is a built-in node, until we figure out otherwise: |
| 572 | notBuiltIn = FALSE; |
| 573 | |
| 574 | // COMPATIBILITY case: |
| 575 | |
| 576 | if (oldFileFormat && isBinary) { |
| 577 | |
| 578 | // If 2.0 file format, and the thing we're reading |
| 579 | // isn't (and, we assume, wasn't) built-in: |
| 580 | if (in->getIVVersion() > 1.0f && !object->getIsBuiltIn()) { |
| 581 | // The "fields" or "inputs" string is read by SoBase, |
| 582 | // because it may need them to decide whether or not to |
| 583 | // create an UnknownNode or an UnknownEngine. |
| 584 | int numDescriptions; |
| 585 | if (!in->read(numDescriptions)) |
| 586 | return FALSE; |
| 587 | |
| 588 | notBuiltIn = TRUE; |
| 589 | // The rest of it is just like 2.1 format, IF the object |
| 590 | // has any fields: |
| 591 | if (!readFieldDescriptions(in, object, numDescriptions)) |
| 592 | return FALSE; |
| 593 | } |
| 594 | // In the old file format, objects with no fields |
| 595 | // didn't write out anything for the field values: |
| 596 | if (fields.getLength() == 0) return TRUE; |
| 597 | |
| 598 | // This is mostly like Inventor 2.1 file format, except that |
| 599 | // there is no NOT_BUILTIN_BIT: |
| 600 | int numFieldsWritten; |
| 601 | if (!in->read(numFieldsWritten)) |
| 602 | return FALSE; |
| 603 | return readFields(in, object, numFieldsWritten); |
| 604 | } |
| 605 | |
| 606 | // BINARY case: |
| 607 | |
| 608 | else if (isBinary) { |
| 609 | unsigned short numWritten; |
| 610 | |
| 611 | // First read number of fields written |
| 612 | if (! in->read(numWritten)) |
| 613 | return FALSE; |
| 614 | |
| 615 | // Figure out if field descriptions were written: |
| 616 | if (numWritten & NOT_BUILTIN_BIT) { |
| 617 | notBuiltIn = TRUE; |
| 618 | numWritten &= (~NOT_BUILTIN_BIT); // Clear bit |
| 619 | } |
| 620 | |
| 621 | if (notBuiltIn) { |
| 622 | if (!readFieldDescriptions(in, object, numWritten)) |
| 623 | return FALSE; |
| 624 | } |
| 625 | |
| 626 | return readFields(in, object, numWritten); |
| 627 | } |
| 628 | |
| 629 | // ASCII case: |
| 630 | else { |
| 631 | |
| 632 | // Only check for "fields" or "inputs" the first time: |
| 633 | SbBool firstTime = TRUE; |
| 634 | |
| 635 | SbName fieldName; |
| 636 | |
| 637 | // Keep reading fields until done |
| 638 | while (TRUE) { |
| 639 | |
| 640 | // If no field name, just return. |
| 641 | if (!in->read(fieldName, TRUE) || ! fieldName) |
| 642 | return TRUE; |
| 643 | |
| 644 | // Read field descriptions. Field descriptions may be |
| 645 | // given for built-in nodes, and do NOT have to be given |
| 646 | // for non-built-in nodes as long as their code can be |
| 647 | // DSO-loaded. |
| 648 | if (firstTime) { |
| 649 | firstTime = FALSE; |
| 650 | if (fieldName == "fields" || fieldName == "inputs") { |
| 651 | notBuiltIn = TRUE; |
| 652 | if (!readFieldDescriptions(in, object, NOT_BUILTIN_BIT)) |
| 653 | return TRUE; |
| 654 | continue; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | SbBool foundName; |
| 659 | if (! read(in, object, fieldName, foundName)) |
| 660 | return FALSE; |
| 661 | |
| 662 | // No match with any valid field name |
| 663 | if (!foundName) { |
| 664 | if (errorOnUnknownField) { |
| 665 | SoReadError::post(in, "Unknown field \"%s\"", |
| 666 | fieldName.getString()); |
| 667 | return FALSE; |
| 668 | } |
| 669 | else { |
| 670 | // Put the field name back in the stream |
| 671 | in->putBack(fieldName.getString()); |
| 672 | return TRUE; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | //////////////////////////////////////////////////////////////////////// |
| 680 | // |
| 681 | // Used when reading binary file format: |
| 682 | // |
| 683 | // Use: internal |
| 684 | |
| 685 | SbBool |
| 686 | SoFieldData::readFields(SoInput *in, SoFieldContainer *object, |
| 687 | int numWritten) const |
| 688 | // |
| 689 | //////////////////////////////////////////////////////////////////////// |
| 690 | { |
| 691 | SbName fieldName; |
| 692 | |
| 693 | // Read the fields - no tolerance for bad data |
| 694 | for (int f = 0; f < numWritten; f++) { |
| 695 | if (! in->read(fieldName, TRUE) || ! fieldName) |
| 696 | return FALSE; |
| 697 | |
| 698 | SbBool foundName; |
| 699 | if (! read(in, object, fieldName, foundName)) |
| 700 | return FALSE; |
| 701 | |
| 702 | // No match with any valid field name |
| 703 | if (! foundName) { |
| 704 | SoReadError::post(in, "Unknown field \"%s\"", |
| 705 | fieldName.getString()); |
| 706 | return FALSE; |
| 707 | } |
| 708 | } |
| 709 | return TRUE; |
| 710 | } |
| 711 | |
| 712 | //////////////////////////////////////////////////////////////////////// |
| 713 | // |
| 714 | // This function is used when the field name has already been |
| 715 | // read, and just the value needs to be read in. It is used by |
| 716 | // the above read() method and to read in GlobalFields. |
| 717 | // |
| 718 | // Use: internal |
| 719 | |
| 720 | SbBool |
| 721 | SoFieldData::read(SoInput *in, SoFieldContainer *object, |
| 722 | const SbName &fieldName, SbBool &foundName) const |
| 723 | // |
| 724 | //////////////////////////////////////////////////////////////////////// |
| 725 | { |
| 726 | int i; |
| 727 | for (i = 0; i < fields.getLength(); i++) { |
| 728 | if (fieldName == getFieldName(i)) { |
| 729 | if (! getField(object, i)->read(in, fieldName)) |
| 730 | return FALSE; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | if (i == fields.getLength()) |
| 735 | foundName = FALSE; |
| 736 | else foundName = TRUE; |
| 737 | |
| 738 | return TRUE; |
| 739 | } |
| 740 | |
| 741 | //////////////////////////////////////////////////////////////////////// |
| 742 | // |
| 743 | // Description: |
| 744 | // Writes all fields. The info in the SoOutput provides more |
| 745 | // specifics (file pointer, etc.). This may be called for either of |
| 746 | // the 2 stages of writing. |
| 747 | // |
| 748 | // Use: internal |
| 749 | |
| 750 | void |
| 751 | SoFieldData::write(SoOutput *out, const SoFieldContainer *container) const |
| 752 | // |
| 753 | //////////////////////////////////////////////////////////////////////// |
| 754 | { |
| 755 | // If in writing stage and writing binary, count number of fields |
| 756 | // to write and write it |
| 757 | if (out->getStage() == SoOutput::WRITE) { |
| 758 | |
| 759 | SbBool isBuiltIn = container->getIsBuiltIn(); |
| 760 | |
| 761 | // In binary, we combine the isBuiltIn flag with the number of |
| 762 | // fields we're going to write (which is also the number of |
| 763 | // fields we'll describe): |
| 764 | if (out->isBinary()) { |
| 765 | unsigned short numToWrite = 0; |
| 766 | for (int i = 0; i < fields.getLength(); i++) { |
| 767 | SoField *field = getField(container, i); |
| 768 | if (field->shouldWrite()) |
| 769 | numToWrite++; |
| 770 | } |
| 771 | if (!isBuiltIn) |
| 772 | out->write(numToWrite | NOT_BUILTIN_BIT); |
| 773 | else |
| 774 | out->write(numToWrite); |
| 775 | } |
| 776 | if (!isBuiltIn) |
| 777 | writeFieldDescriptions(out, container); |
| 778 | } |
| 779 | |
| 780 | // Let each field do its thing |
| 781 | for (int i = 0; i < fields.getLength(); i++) { |
| 782 | |
| 783 | SoField *field = getField(container, i); |
| 784 | |
| 785 | if (field->shouldWrite()) |
| 786 | field->write(out, getFieldName(i)); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | //////////////////////////////////////////////////////////////////////// |
| 791 | // |
| 792 | // Write out field descriptions |
| 793 | // |
| 794 | // Use: internal |
| 795 | |
| 796 | void |
| 797 | SoFieldData::writeFieldDescriptions(SoOutput *out, |
| 798 | const SoFieldContainer *container) const |
| 799 | // |
| 800 | //////////////////////////////////////////////////////////////////////// |
| 801 | { |
| 802 | SbBool isBinary = out->isBinary(); |
| 803 | |
| 804 | if (!isBinary) { |
| 805 | out->indent(); |
| 806 | if (container->isOfType(SoNode::getClassTypeId())) |
| 807 | out->write("fields"); |
| 808 | else |
| 809 | out->write("inputs"); |
| 810 | out->write(' '); |
| 811 | out->write(OPEN_BRACE_CHAR); |
| 812 | out->write(' '); |
| 813 | out->incrementIndent(2); |
| 814 | } |
| 815 | |
| 816 | // For each field, write out the field type name and field name. |
| 817 | int i = 0; |
| 818 | int numWritten = 0; |
| 819 | for (i = 0; i < getNumFields(); i++) { |
| 820 | |
| 821 | SoField *field = getField(container, i); |
| 822 | |
| 823 | // Don't write descriptions of fields that won't be written: |
| 824 | if (!field->shouldWrite()) |
| 825 | continue; |
| 826 | |
| 827 | SoType type = field->getTypeId(); |
| 828 | |
| 829 | out->write(type.getName().getString()); |
| 830 | if (! isBinary) |
| 831 | out->write(' '); |
| 832 | out->write(getFieldName(i).getString()); |
| 833 | |
| 834 | // Format nice and pretty for ascii |
| 835 | if (! isBinary) { |
| 836 | if (i != getNumFields() - 1) { |
| 837 | out->write(VALUE_SEPARATOR_CHAR); |
| 838 | ++numWritten; |
| 839 | if ((numWritten%4) == 0) { // 4 pairs per line |
| 840 | out->write('\n'); |
| 841 | out->indent(); |
| 842 | } else out->write(' '); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // Write out the closing brace of the field type information. |
| 848 | if (! out->isBinary()) { |
| 849 | out->write(' '); |
| 850 | out->write(CLOSE_BRACE_CHAR); |
| 851 | out->write('\n'); |
| 852 | out->decrementIndent(2); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | //////////////////////////////////////////////////////////////////////// |
| 857 | // |
| 858 | // Description: |
| 859 | // Copies all fields from given field data into this one, creating |
| 860 | // the new fields. This should be used only for copying fields from |
| 861 | // a base class into a derived class, since that's the only |
| 862 | // situation in which we are guaranteed that the field offsets are |
| 863 | // the same. |
| 864 | // |
| 865 | // Use: internal |
| 866 | |
| 867 | void |
| 868 | SoFieldData::copy(const SoFieldData *from) |
| 869 | // |
| 870 | //////////////////////////////////////////////////////////////////////// |
| 871 | { |
| 872 | int i; |
| 873 | if (from == NULL) |
| 874 | return; |
| 875 | |
| 876 | // Copy field entries |
| 877 | for (i = 0; i < from->fields.getLength(); i++) { |
| 878 | |
| 879 | struct SoFieldEntry *fromField = |
| 880 | (struct SoFieldEntry *) from->fields[i]; |
| 881 | |
| 882 | fields.append((void *) new struct SoFieldEntry(*fromField)); |
| 883 | } |
| 884 | |
| 885 | // Copy enum entries |
| 886 | for (i = 0; i < from->enums.getLength(); i++) { |
| 887 | |
| 888 | struct SoEnumEntry *fromEnum = (struct SoEnumEntry *) from->enums[i]; |
| 889 | |
| 890 | enums.append((void *) new struct SoEnumEntry(*fromEnum)); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | //////////////////////////////////////////////////////////////////////// |
| 895 | // |
| 896 | // Description: |
| 897 | // Returns TRUE if the values in the fields of one container are |
| 898 | // the same as those in another (of the same type, presumably). |
| 899 | // |
| 900 | // Use: internal |
| 901 | |
| 902 | SbBool |
| 903 | SoFieldData::isSame(const SoFieldContainer *c1, |
| 904 | const SoFieldContainer *c2) const |
| 905 | // |
| 906 | //////////////////////////////////////////////////////////////////////// |
| 907 | { |
| 908 | int i; |
| 909 | |
| 910 | for (i = 0; i < fields.getLength(); i++) |
| 911 | if (! getField(c1, i)->isSame(*getField(c2, i))) |
| 912 | return FALSE; |
| 913 | |
| 914 | return TRUE; |
| 915 | } |