Open Inventor Release 2024.1.1
 
Loading...
Searching...
No Matches
SoOutput.h
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-2024 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
54#include <Inventor/misc/SoBasic.h>
55#include <Inventor/SbDict.h>
56#include <Inventor/SbString.h>
57#include <Inventor/SoType.h>
58
59class SoBase;
60class SoOutputImpl;
61
63typedef void *SoOutputReallocCB(void *ptr, size_t newSize);
64
66//
67// Class: SoOutput
68//
69// This file contains the definition of the SoOutput (output stream)
70// class. This class is used for writing Inventor data files; it
71// supports both ASCII (default) and binary formats and provides some
72// convenience functions for handling files. It also supports easy
73// indentation.
74//
75// Output can go to: (1) a file pointer created by the caller, (2) a
76// file opened by SoOutput, or (3) a buffer in memory created by the
77// caller. In the third case, the caller supplies a pointer to the
78// buffer, the initial size of the buffer, and a reallocation
79// function to call when that size is exceeded. At any time during
80// the writing, the caller can inquire the address of the buffer and
81// how many bytes have been inserted into it.
82//
83// SoOutput support data compression on the fly. When the file is opened
84// by SoOutput the file can be compressed.
85//
86// SoOutput also contains a dictionary that correlates node and path
87// pointers to temporary names written to files. This is so
88// references to previously defined nodes and paths are written
89// correctly.
90//
92
186class SoOutput {
187 public:
188
193
197 virtual ~SoOutput();
198
212
222 virtual void setFileProperty( const FileProperty properties );
223
230
234 virtual void setFilePointer(FILE *newFP);
235
242 virtual FILE * getFilePointer() const;
243
252 size_t fileWrite( void* buffer, size_t elemSize, size_t count );
253
262 int filePuts( const char* );
263
272 int filePutc( int );
273
279 SoNONUNICODE virtual SbBool openFile(const char *fileName);
280
286 virtual SbBool openFile( const SbString& fileName );
287
291 virtual void closeFile();
292
299 virtual void setBuffer(void *bufPointer, size_t initSize,
300 SoOutputReallocCB *reallocFunc,
301 int32_t offset = 0);
302
307 virtual SbBool getBuffer(void *&bufPointer, size_t &nBytes) const;
308
313 virtual size_t getBufferSize() const { return bufSize; }
314
318 virtual void resetBuffer();
319
323 virtual void setBinary(SbBool flag);
324
328 virtual SbBool isBinary() const { return binary; }
329
334
345 virtual void setHeaderString(const SbString &str);
349 virtual void resetHeaderString();
350
359
365 virtual void setFloatPrecision(int precision);
366
372 virtual void setDoublePrecision(int precision);
373
374
375 private:
376
377#ifdef _WIN32
378 virtual SbBool openFromHandle(UINT hFile);
379#endif
380 virtual void flushFile();
381
382 // Enum that determines stage of operation of writing
383 enum Stage {
384 COUNT_REFS, // Count write references
385 WRITE // Actually write to file
386 };
387
388 // Sets/returns current stage of operation of action
389 void setStage(Stage stage) { curStage = stage; }
390 Stage getStage() const { return curStage; }
391
392 // Increments/decrements indentation level by amount (default 1).
393 // Each indentation level is 4 spaces.
394 void incrementIndent(int amount = 1)
395 { indentLevel += amount; }
396 void decrementIndent(int amount = 1)
397 { indentLevel -= amount; }
398
399 int getIndentLevel(){ return indentLevel; }
400
401 // Writes item of particular type to current file pointer/buffer
402 virtual void write(bool b);
403 virtual void write(char c);
404 virtual void write(unsigned char c);
405 virtual void write(const char *s);
406 virtual void write(const SbString &s);
407 virtual void write(const SbName &n);
408 virtual void write(int32_t i);
412 virtual void write(int64_t i);
416 virtual void write(uint32_t i);
420 virtual void write(uint64_t i);
421 virtual void write(short s);
425 virtual void write(unsigned short s);
426 virtual void write(float f);
427 virtual void write(double d);
428 virtual void writeBinaryArray(unsigned char *c, int length);
429 virtual void writeBinaryArray(int32_t *l, int length);
430 virtual void writeBinaryArray(int64_t *l, int length);
431 virtual void writeBinaryArray(float *f, int length);
432 virtual void writeBinaryArray(double *d, int length);
433 virtual void writeBinaryArray(short *s, int length);
434
435 // Writes indentation to file/buffer based on current indentation level
436 virtual void indent();
437
438 private:
439
440 SbBool isOutFileVRML2() { return vrmlFileOut;}
441 SbBool isOutFileX3D() { return x3dFileOut;}
442 SbBool isHeaderWritten() { return wroteHeader;}
443 void incrementRefOut(int amount = 1)
444 { refOut += amount; }
445 int getRefOut() { return refOut;}
446 // Constructor that gets reference dictionary from another SoOutput
447 SoOutput(SoOutput *dictOut);
448
449 virtual void writeLargeBinaryArray(unsigned char *c, size_t length);
450
451 // Resets things for writing to a new file or changing files
452 virtual void reset();
453
454 // Prevents header and extra white space from being written.
455 // Useful for producing compact strings of data.
456 void setCompact(SbBool flag) { compact = flag; }
457 SbBool isCompact() const { return compact; }
458
459 // Bit mask for various output annotations. Note that annotation is
460 // automatically disabled for non-compact or binary format.
461 enum Annotations {
462 ADDRESSES = (1<<0), // pointer values
463 REF_COUNTS = (1<<1) // node reference counts
464 };
465 void setAnnotation(uint32_t bits)
466 { annotation = bits; }
467 uint32_t getAnnotation()
468 { return (isCompact() || isBinary()) ? 0 : annotation; }
469 SbPList routeList; // List of connections so route's
470 // can be written to a file
471 // Looks up a reference, returning the reference id or -1 if it
472 // hasn't been added yet.
473 int findReferenceIfVRML2(const SoBase *base) const;
474 // Adds a reference to dictionary, returning the reference id
475 // created for it.
476 int addReferenceIfVRML2(const SoBase *base);
477
478 // Adds a reference to dictionary in current file.
479 virtual void addToVRML2Dict(const SbString name, const SoBase *base);
480
481 // Looks up a reference, returning the name for this base pointer or NULL
482 virtual SoBase * findInVRML2Dict( const SbString name) const;
483
484 // Adds a reference to dictionary in current file.
485 virtual void addToVRMLName(const SoBase *base, const char *name);
486
487 // Writes correct file header string to current file/buffer
488 void writeHeader();
489 // Looks up a reference, returning the name for this base pointer or NULL
490 virtual char * findInVRMLName( const SoBase *base) const;
491 SbDict *refVRMLName; // SoBase pointer -> write name
492 SbDict *refVRML2; // SoBase pointer -> write name
493 SbDict *PROTOToWrite; // SoBase pointer -> write name
494 int refOut;
495
496 int32_t isWritingBinArrayAsMemObj() const;
497
498 SoOutputImpl* m_soOutputImpl;
499
504 struct SoOutputEntry {
505 SbName name; // Name of output
506 int64_t offset; // Offset of output within object
507 SoType type; // Type of output
508 };
509
510 // Extracts the file version if the header string isn't empty.
511 // Otherwise it returns the current file version.
512 float getIVVersion();
513
514 private:
515 FILE *fp; // File writing to
516 void *zFp; // gzip file pointer
517 SbBool toBuffer; // TRUE if writing to buffer
518 char *tmpBuffer; // For binary write to file
519 void *buffer; // Buffer writing to
520 char *curBuf; // Current pointer in buffer
521 size_t bufSize; // Maximum buffer size
522 size_t tmpBufSize; // Maximum temporary buffer size
523 SoOutputReallocCB *reallocFunc; // Reallocation function for buffer
524 SbBool openedHere; // TRUE if SoOutput opened file
525 SbBool openedFromHandle; // TRUE if opened from Win32 file handle
526 SbBool binary; // TRUE if writing binary data
527 SbBool compact; // TRUE if writing in compact form
528 SbBool wroteHeader; // TRUE if header was written
529 int indentLevel; // Current indentation level
530 SbDict *refDict; // SoBase pointer -> reference ID
531 SbBool borrowedDict; // TRUE if dict from another SoOutput
532 int refIdCount; // Counter for generating ref IDs
533 SbBool anyRef; // TRUE if any reference in dictionary
534 uint32_t annotation; // annotation bit mask
535 Stage curStage; // Stage of operation
536 SbString headerString; // The header
537 FileProperty m_fileProperties; // File properties (like compression...)
538
539 // Pad a header so that it is correctly aligned for reading from
540 // binary files into memory
541 static SbString padHeader(const SbString &inString);
542
543 // Returns TRUE if writing into memory buffer rather than file
544 SbBool isToBuffer() const
545 { return toBuffer; }
546
552 virtual SbBool setupFileProperties();
553
554 // Returns number of bytes in current buffer
555 size_t bytesInBuf() const
556 { return (curBuf - static_cast<char *>(buffer) ); }
557
558 // Makes sure current buffer can contain nBytes more bytes
559 SbBool makeRoomInBuf(size_t nBytes);
560
561 // Makes sure temporary buffer can contain nBytes more bytes
562 SbBool makeRoomInTmpBuf(size_t nBytes);
563
564 // Adds a reference to dictionary, returning the reference id
565 // created for it.
566 int addReference(const SoBase *base);
567
568 // Looks up a reference, returning the reference id or -1 if it
569 // hasn't been added yet.
570 int findReference(const SoBase *base) const;
571
572 // Convert datatypes to network format during writing
573 void convertShort(short s, char *to);
574 void convertInt32(int32_t l, char *to);
575 void convertInt64(int64_t l, char *to);
576 void convertFloat(float f, char *to);
577 void convertDouble(double d, char *to);
578 void convertShortArray( short *from, char *to, int len);
579 void convertInt32Array( int32_t *from, char *to, int len);
580 void convertInt64Array( int64_t *from, char *to, int len);
581 void convertFloatArray( float *from, char *to, int len);
582 void convertDoubleArray( double *from, char *to, int len);
583
584 const char* getDoublesFormatString() const;
585
586 const char* getFloatsFormatString() const;
587
588
589 SbBool vrmlFileOut;
590 SbBool x3dFileOut;
591 float m_ivVersion;
592
593 private:
594 char m_floatsFmtString[16]; // Output format for floats
595 char m_doublesFmtString[16]; // Output format for doubles
596
597
598 friend class SoBase;
599 friend class SoDB;
600};
601
602#endif /* _SO_OUTPUT_ */
603
604
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:222
Used to write Open Inventor data files.
Definition SoOutput.h:186
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.
void * SoOutputReallocCB(void *ptr, size_t newSize)
Definition SoOutput.h:63
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:313
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:328
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:206
@ DefaultProperty
Regular ascii or binary output.
Definition SoOutput.h:208
@ CompressedZlib
Compressed ascii or binary output using the zlib module.
Definition SoOutput.h:210
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