Open Inventor Release 2023.2.3
 
Loading...
Searching...
No Matches
SoOutput.h
Go to the documentation of this file.
1/*=======================================================================
2 * Copyright 1991-1996, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 *
5 * UNPUBLISHED -- Rights reserved under the copyright laws of the United
6 * States. Use of a copyright notice is precautionary only and does not
7 * imply publication or disclosure.
8 *
9 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
10 * Use, duplication or disclosure by the Government is subject to restrictions
11 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
12 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
13 * in similar or successor clauses in the FAR, or the DOD or NASA FAR
14 * Supplement. Contractor/manufacturer is Silicon Graphics, Inc.,
15 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
16 *
17 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
18 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
19 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
20 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
21 * GRAPHICS, INC.
22**=======================================================================*/
23/*=======================================================================
24** Author : Paul S. Strauss (MMM yyyy)
25** Modified by : Gavin Bell (MMM yyyy)
26**=======================================================================*/
27/*=======================================================================
28 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
29 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
30 *** ***
31 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
32 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
33 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
34 *** ***
35 *** RESTRICTED RIGHTS LEGEND ***
36 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
37 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
38 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
39 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
40 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
41 *** ***
42 *** COPYRIGHT (C) 1996-2021 BY FEI S.A.S, ***
43 *** BORDEAUX, FRANCE ***
44 *** ALL RIGHTS RESERVED ***
45**=======================================================================*/
46/*=======================================================================
47** Modified by : VSG (MMM YYYY)
48**=======================================================================*/
49
50
51#ifndef _SO_OUTPUT_
52#define _SO_OUTPUT_
53
55#include <Inventor/SbDict.h>
56#include <Inventor/SbString.h>
57#include <Inventor/SoType.h>
58
59class SoBase;
60class SoOutputImpl;
61
62typedef void *SoOutputReallocCB(void *ptr, size_t newSize);
63
65//
66// Class: SoOutput
67//
68// This file contains the definition of the SoOutput (output stream)
69// class. This class is used for writing Inventor data files; it
70// supports both ASCII (default) and binary formats and provides some
71// convenience functions for handling files. It also supports easy
72// indentation.
73//
74// Output can go to: (1) a file pointer created by the caller, (2) a
75// file opened by SoOutput, or (3) a buffer in memory created by the
76// caller. In the third case, the caller supplies a pointer to the
77// buffer, the initial size of the buffer, and a reallocation
78// function to call when that size is exceeded. At any time during
79// the writing, the caller can inquire the address of the buffer and
80// how many bytes have been inserted into it.
81//
82// SoOutput support data compression on the fly. When the file is opened
83// by SoOutput the file can be compressed.
84//
85// SoOutput also contains a dictionary that correlates node and path
86// pointers to temporary names written to files. This is so
87// references to previously defined nodes and paths are written
88// correctly.
89//
91
185class SoOutput {
186 public:
187
192
196 virtual ~SoOutput();
197
211
221 virtual void setFileProperty( const FileProperty properties );
222
229
233 virtual void setFilePointer(FILE *newFP);
234
241 virtual FILE * getFilePointer() const;
242
251 size_t fileWrite( void* buffer, size_t elemSize, size_t count );
252
261 int filePuts( const char* );
262
271 int filePutc( int );
272
278 SoNONUNICODE virtual SbBool openFile(const char *fileName);
279
285 virtual SbBool openFile( const SbString& fileName );
286
290 virtual void closeFile();
291
298 virtual void setBuffer(void *bufPointer, size_t initSize,
299 SoOutputReallocCB *reallocFunc,
300 int32_t offset = 0);
301
306 virtual SbBool getBuffer(void *&bufPointer, size_t &nBytes) const;
307
312 virtual size_t getBufferSize() const { return bufSize; }
313
317 virtual void resetBuffer();
318
322 virtual void setBinary(SbBool flag);
323
327 virtual SbBool isBinary() const { return binary; }
328
333
344 virtual void setHeaderString(const SbString &str);
348 virtual void resetHeaderString();
349
358
364 virtual void setFloatPrecision(int precision);
365
371 virtual void setDoublePrecision(int precision);
372
373
374 private:
375
376#ifdef _WIN32
377 virtual SbBool openFromHandle(UINT hFile);
378#endif
379 virtual void flushFile();
380
381 // Enum that determines stage of operation of writing
382 enum Stage {
383 COUNT_REFS, // Count write references
384 WRITE // Actually write to file
385 };
386
387 // Sets/returns current stage of operation of action
388 void setStage(Stage stage) { curStage = stage; }
389 Stage getStage() const { return curStage; }
390
391 // Increments/decrements indentation level by amount (default 1).
392 // Each indentation level is 4 spaces.
393 void incrementIndent(int amount = 1)
394 { indentLevel += amount; }
395 void decrementIndent(int amount = 1)
396 { indentLevel -= amount; }
397
398 int getIndentLevel(){ return indentLevel; }
399
400 // Writes item of particular type to current file pointer/buffer
401 virtual void write(bool b);
402 virtual void write(char c);
403 virtual void write(unsigned char c);
404 virtual void write(const char *s);
405 virtual void write(const SbString &s);
406 virtual void write(const SbName &n);
407 virtual void write(int32_t i);
411 virtual void write(int64_t i);
415 virtual void write(uint32_t i);
419 virtual void write(uint64_t i);
420 virtual void write(short s);
424 virtual void write(unsigned short s);
425 virtual void write(float f);
426 virtual void write(double d);
427 virtual void writeBinaryArray(unsigned char *c, int length);
428 virtual void writeBinaryArray(int32_t *l, int length);
429 virtual void writeBinaryArray(int64_t *l, int length);
430 virtual void writeBinaryArray(float *f, int length);
431 virtual void writeBinaryArray(double *d, int length);
432 virtual void writeBinaryArray(short *s, int length);
433
434 // Writes indentation to file/buffer based on current indentation level
435 virtual void indent();
436
437 private:
438
439 SbBool isOutFileVRML2() { return vrmlFileOut;}
440 SbBool isOutFileX3D() { return x3dFileOut;}
441 SbBool isHeaderWritten() { return wroteHeader;}
442 void incrementRefOut(int amount = 1)
443 { refOut += amount; }
444 int getRefOut() { return refOut;}
445 // Constructor that gets reference dictionary from another SoOutput
446 SoOutput(SoOutput *dictOut);
447
448 virtual void writeLargeBinaryArray(unsigned char *c, size_t length);
449
450 // Resets things for writing to a new file or changing files
451 virtual void reset();
452
453 // Prevents header and extra white space from being written.
454 // Useful for producing compact strings of data.
455 void setCompact(SbBool flag) { compact = flag; }
456 SbBool isCompact() const { return compact; }
457
458 // Bit mask for various output annotations. Note that annotation is
459 // automatically disabled for non-compact or binary format.
460 enum Annotations {
461 ADDRESSES = (1<<0), // pointer values
462 REF_COUNTS = (1<<1) // node reference counts
463 };
464 void setAnnotation(uint32_t bits)
465 { annotation = bits; }
466 uint32_t getAnnotation()
467 { return (isCompact() || isBinary()) ? 0 : annotation; }
468 SbPList routeList; // List of connections so route's
469 // can be written to a file
470 // Looks up a reference, returning the reference id or -1 if it
471 // hasn't been added yet.
472 int findReferenceIfVRML2(const SoBase *base) const;
473 // Adds a reference to dictionary, returning the reference id
474 // created for it.
475 int addReferenceIfVRML2(const SoBase *base);
476
477 // Adds a reference to dictionary in current file.
478 virtual void addToVRML2Dict(const SbString name, const SoBase *base);
479
480 // Looks up a reference, returning the name for this base pointer or NULL
481 virtual SoBase * findInVRML2Dict( const SbString name) const;
482
483 // Adds a reference to dictionary in current file.
484 virtual void addToVRMLName(const SoBase *base, const char *name);
485
486 // Writes correct file header string to current file/buffer
487 void writeHeader();
488 // Looks up a reference, returning the name for this base pointer or NULL
489 virtual char * findInVRMLName( const SoBase *base) const;
490 SbDict *refVRMLName; // SoBase pointer -> write name
491 SbDict *refVRML2; // SoBase pointer -> write name
492 SbDict *PROTOToWrite; // SoBase pointer -> write name
493 int refOut;
494
495 int32_t isWritingBinArrayAsMemObj() const;
496
497 SoOutputImpl* m_soOutputImpl;
498
503 struct SoOutputEntry {
504 SbName name; // Name of output
505 int64_t offset; // Offset of output within object
506 SoType type; // Type of output
507 };
508
509 // Extracts the file version if the header string isn't empty.
510 // Otherwise it returns the current file version.
511 float getIVVersion();
512
513 private:
514 FILE *fp; // File writing to
515 void *zFp; // gzip file pointer
516 SbBool toBuffer; // TRUE if writing to buffer
517 char *tmpBuffer; // For binary write to file
518 void *buffer; // Buffer writing to
519 char *curBuf; // Current pointer in buffer
520 size_t bufSize; // Maximum buffer size
521 size_t tmpBufSize; // Maximum temporary buffer size
522 SoOutputReallocCB *reallocFunc; // Reallocation function for buffer
523 SbBool openedHere; // TRUE if SoOutput opened file
524 SbBool openedFromHandle; // TRUE if opened from Win32 file handle
525 SbBool binary; // TRUE if writing binary data
526 SbBool compact; // TRUE if writing in compact form
527 SbBool wroteHeader; // TRUE if header was written
528 int indentLevel; // Current indentation level
529 SbDict *refDict; // SoBase pointer -> reference ID
530 SbBool borrowedDict; // TRUE if dict from another SoOutput
531 int refIdCount; // Counter for generating ref IDs
532 SbBool anyRef; // TRUE if any reference in dictionary
533 uint32_t annotation; // annotation bit mask
534 Stage curStage; // Stage of operation
535 SbString headerString; // The header
536 FileProperty m_fileProperties; // File properties (like compression...)
537
538 // Pad a header so that it is correctly aligned for reading from
539 // binary files into memory
540 static SbString padHeader(const SbString &inString);
541
542 // Returns TRUE if writing into memory buffer rather than file
543 SbBool isToBuffer() const
544 { return toBuffer; }
545
551 virtual SbBool setupFileProperties();
552
553 // Returns number of bytes in current buffer
554 size_t bytesInBuf() const
555 { return (curBuf - static_cast<char *>(buffer) ); }
556
557 // Makes sure current buffer can contain nBytes more bytes
558 SbBool makeRoomInBuf(size_t nBytes);
559
560 // Makes sure temporary buffer can contain nBytes more bytes
561 SbBool makeRoomInTmpBuf(size_t nBytes);
562
563 // Adds a reference to dictionary, returning the reference id
564 // created for it.
565 int addReference(const SoBase *base);
566
567 // Looks up a reference, returning the reference id or -1 if it
568 // hasn't been added yet.
569 int findReference(const SoBase *base) const;
570
571 // Convert datatypes to network format during writing
572 void convertShort(short s, char *to);
573 void convertInt32(int32_t l, char *to);
574 void convertInt64(int64_t l, char *to);
575 void convertFloat(float f, char *to);
576 void convertDouble(double d, char *to);
577 void convertShortArray( short *from, char *to, int len);
578 void convertInt32Array( int32_t *from, char *to, int len);
579 void convertInt64Array( int64_t *from, char *to, int len);
580 void convertFloatArray( float *from, char *to, int len);
581 void convertDoubleArray( double *from, char *to, int len);
582
583 const char* getDoublesFormatString() const;
584
585 const char* getFloatsFormatString() const;
586
587
588 SbBool vrmlFileOut;
589 SbBool x3dFileOut;
590 float m_ivVersion;
591
592 private:
593 char m_floatsFmtString[16]; // Output format for floats
594 char m_doublesFmtString[16]; // Output format for doubles
595
596
597 friend class SoBase;
598 friend class SoDB;
599};
600
601#endif /* _SO_OUTPUT_ */
602
603
void * SoOutputReallocCB(void *ptr, size_t newSize)
Definition SoOutput.h:62
Character string stored in a hash table.
Definition SbName.h:162
List of generic (void *) pointers.
Definition SbPList.h:77
Class for smart character strings.
Definition SbString.h:202
Base class for all nodes, paths, and engines.
Definition SoBase.h:111
Scene graph database class.
Definition SoDB.h:219
Used to write Open Inventor data files.
Definition SoOutput.h:185
virtual FILE * getFilePointer() const
Returns the file pointer in use, or NULL if using a buffer.
int filePutc(int)
Write a byte to the currently open file.
static SbString getDefaultASCIIHeader()
Returns the string representing the default ASCII header.
virtual void resetBuffer()
Resets buffer for output again.
size_t fileWrite(void *buffer, size_t elemSize, size_t count)
Write a buffer to the currently open file.
virtual void resetHeaderString()
Resets the header for output files to be the default header.
SoOutput()
Constructor.
int filePuts(const char *)
Write a string to the currently open file.
virtual void setHeaderString(const SbString &str)
Sets the header for output files.
virtual SoNONUNICODE SbBool openFile(const char *fileName)
Opens named file, sets current file to result.
virtual void closeFile()
Closes current file if opened with openFile().
virtual void setFilePointer(FILE *newFP)
Sets file pointer to write to.
virtual void setBuffer(void *bufPointer, size_t initSize, SoOutputReallocCB *reallocFunc, int32_t offset=0)
Sets up memory buffer to write to, initial size, reallocation function (which is called if there is n...
virtual ~SoOutput()
Destructor.
virtual size_t getBufferSize() const
The total number of bytes allocated to a memory buffer may be larger than the number of bytes written...
Definition SoOutput.h:312
static SbString getDefaultBinaryHeader()
Returns the string representing the default binary header.
virtual SbBool openFile(const SbString &fileName)
Opens named file, sets current file to result.
virtual void setFloatPrecision(int precision)
Sets the precision for writing floating point numbers (type float), i.e. the number of significant di...
virtual SbBool isBinary() const
Returns current state of binary flag.
Definition SoOutput.h:327
virtual void setBinary(SbBool flag)
Sets whether output should be ASCII (default) or binary.
virtual void setFileProperty(const FileProperty properties)
Sets a combination of properties for the current file.
FileProperty getFileProperty() const
Returns the current properties used by SoOutput.
virtual SbBool getBuffer(void *&bufPointer, size_t &nBytes) const
Returns memory buffer being written to and the new size of the buffer.
FileProperty
This enum defines the properties used when data are written to a file.
Definition SoOutput.h:205
@ DefaultProperty
Regular ascii or binary output.
Definition SoOutput.h:207
@ CompressedZlib
Compressed ascii or binary output using the zlib module.
Definition SoOutput.h:209
virtual void setDoublePrecision(int precision)
Sets the precision for writing floating point numbers (type double), i.e. the number of significant d...
SbBool isWritingForScaleViz() const
Returns TRUE if ScaleViz is currently writing data for synchronization.
Stores runtime type information.
Definition SoType.h:98
int SbBool
Boolean type.
Definition SbBase.h:87
unsigned int UINT
Definition port.h:357