Open Inventor Reference
MeVis/ThirdParty/Sources/Inventor/inventor/lib/database/include/Inventor/SoOutput.h
Go to the documentation of this file.
00001 /*
00002  *
00003  *  Copyright (C) 2000 Silicon Graphics, Inc.  All Rights Reserved. 
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  Further, this software is distributed without any warranty that it is
00016  *  free of the rightful claim of any third person regarding infringement
00017  *  or the like.  Any license provided herein, whether implied or
00018  *  otherwise, applies only to this software file.  Patent licenses, if
00019  *  any, provided herein do not apply to combinations of this program with
00020  *  other software, or any other product whatsoever.
00021  * 
00022  *  You should have received a copy of the GNU Lesser General Public
00023  *  License along with this library; if not, write to the Free Software
00024  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  *
00026  *  Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
00027  *  Mountain View, CA  94043, or:
00028  * 
00029  *  http://www.sgi.com 
00030  * 
00031  *  For further information regarding this notice, see: 
00032  * 
00033  *  http://oss.sgi.com/projects/GenInfo/NoticeExplan/
00034  *
00035  */
00036 
00037 
00038 /*
00039  * Copyright (C) 1990,91   Silicon Graphics, Inc.
00040  *
00041  _______________________________________________________________________
00042  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
00043  |
00044  |   $Revision: 1.1.1.1 $
00045  |
00046  |   Description:
00047  |      This file contains the definition of the SoOutput class.
00048  |
00049  |   Classes:
00050  |      SoOutput
00051  |
00052  |   Author(s)          : Paul S. Strauss, Gavin Bell
00053  |
00054  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
00055  _______________________________________________________________________
00056  */
00057 
00058 #ifndef  _SO_OUTPUT_
00059 #define  _SO_OUTPUT_
00060 
00061 #include <Inventor/misc/SoBasic.h>
00062 #include <Inventor/SbDict.h>
00063 #include <Inventor/SbString.h>
00064 
00065 class SoBase;
00066 
00067 typedef void *SoOutputReallocCB(void *ptr, size_t newSize);
00068 
00069 
00072 
00088 
00089 
00090 class INVENTOR_API SoOutput {
00091  public:
00092 
00095     SoOutput();
00096 
00099     ~SoOutput();
00100 
00102     void                setFilePointer(FILE *newFP);
00103 
00105     FILE *              getFilePointer() const;
00106 
00108     SbBool              openFile(const char *fileName);
00109 
00112     void                closeFile();
00113 
00118     void                setBuffer(void *bufPointer, size_t initSize,
00119                                   SoOutputReallocCB *reallocFunc, 
00120                                   int32_t offset = 0);
00121 
00124     SbBool              getBuffer(void *&bufPointer, size_t &nBytes) const;
00125 
00128     size_t              getBufferSize() const { return bufSize; }
00129 
00131     void                resetBuffer();
00132 
00134     void                setBinary(SbBool flag);
00135 
00137     SbBool              isBinary() const                { return binary; }
00138 
00149     void                setHeaderString(const SbString &str);
00150 
00152     void                resetHeaderString();
00153     
00155     static SbString     getDefaultASCIIHeader();
00157     static SbString     getDefaultBinaryHeader();
00158 
00163     void                setFloatPrecision(int precision);
00164                                                         
00165   SoEXTENDER public:
00166 
00168     enum Stage {
00169         COUNT_REFS,             
00170         WRITE                   
00171     };
00172 
00174     void                setStage(Stage stage)           { curStage = stage; }
00175     Stage               getStage() const                { return curStage; }
00176 
00179     void                incrementIndent(int amount = 1)
00180         { indentLevel += amount; }
00181     void                decrementIndent(int amount = 1)
00182         { indentLevel -= amount; }
00183 
00185     void                write(char           c);
00186     void                write(const char    *s);
00187     void                write(const SbString &s);
00188     void                write(const SbName   &n);
00189     void                write(int            i);
00190     void                write(unsigned int   i);
00191     void                write(short          s);
00192     void                write(unsigned short s);
00199     void                write(float          f);
00200     void                write(double         d);
00201     void                writeBinaryArray(unsigned char *c, int length);
00202     void                writeBinaryArray(int32_t *l, int length);
00203     void                writeBinaryArray(float *f, int length);
00204     void                writeBinaryArray(double *d, int length);
00205 
00207     void                indent();
00208 
00209   SoINTERNAL public:
00210 
00213     SoOutput(SoOutput *dictOut);
00214 
00216     void                reset();
00217 
00220     void                setCompact(SbBool flag)         { compact = flag; }
00221     SbBool              isCompact() const               { return compact; }
00222 
00225     enum Annotations {
00226         ADDRESSES  = (1<<0),    
00227         REF_COUNTS = (1<<1)     
00228     };
00229     void                setAnnotation(uint32_t bits)
00230         { annotation = bits; }
00231     uint32_t            getAnnotation()
00232         { return (isCompact() || isBinary()) ? 0 : annotation; }
00233 
00234   private:
00235     FILE                *fp;            
00236     SbBool              toBuffer;       
00237     char                *tmpBuffer;     
00238     void                *buffer;        
00239     char                *curBuf;        
00240     size_t              bufSize;        
00241     size_t              tmpBufSize;     
00242     SoOutputReallocCB   *reallocFunc;   
00243     SbBool              openedHere;     
00244     SbBool              binary;         
00245     SbBool              compact;        
00246     SbBool              wroteHeader;    
00247     int                 indentLevel;    
00248     SbDict              *refDict;       
00249     SbBool              borrowedDict;   
00250     int                 refIdCount;     
00251     SbBool              anyRef;         
00252     uint32_t            annotation;     
00253     Stage               curStage;       
00254     SbString            headerString;   
00255     SbString            fmtString;      
00256 
00258     void                writeHeader();
00259     
00262     static SbString     padHeader(const SbString &inString);
00263 
00265     SbBool              isToBuffer() const
00266         { return toBuffer; }
00267 
00269     size_t              bytesInBuf() const
00270         { return (curBuf - (char *) buffer); }
00271 
00273     SbBool              makeRoomInBuf(size_t nBytes);
00274 
00276     SbBool              makeRoomInTmpBuf(size_t nBytes);
00277 
00280     int                 addReference(const SoBase *base);
00281 
00284     int                 findReference(const SoBase *base) const;
00285 
00287     void                convertShort(short s, char *to);
00288     void                convertInt32(int32_t l, char *to);
00289     void                convertFloat(float f, char *to);
00290     void                convertDouble(double d, char *to);
00291     void                convertShortArray( short *from, char *to, int len);
00292     void                convertInt32Array( int32_t *from, char *to, int len);
00293     void                convertFloatArray( float *from, char *to, int len);
00294     void                convertDoubleArray( double *from, char *to, int len);
00295 
00296 friend class SoBase;
00297 friend class SoDB;
00298 };
00299 
00300 #endif /* _SO_OUTPUT_ */
00301