00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #ifndef _SO_INPUT_
00051 #define _SO_INPUT_
00052
00053 #include <Inventor/misc/SoBasic.h>
00054 #include <Inventor/SbDict.h>
00055 #include <Inventor/SbPList.h>
00056 #include <Inventor/SbString.h>
00057 #include <Inventor/SoDB.h>
00058 #include <Inventor/threads/SbThreadMutex.h>
00059 #include <Inventor/threads/SbThreadSpinlock.h>
00060
00061 #include <Inventor/STL/fstream>
00062 #include <Inventor/STL/iostream>
00063 #include <Inventor/STL/list>
00064 #include <Inventor/STL/vector>
00065 #include <Inventor/STL/map>
00066
00067 #if defined(_WIN32)
00068 #pragma warning( push )
00069 #pragma warning( disable: 4251 ) // 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
00070 #endif
00071
00072
00073 class SoNode;
00074 class SoPath;
00075 class SoBase;
00076 class SoDB;
00077 class SbStringList;
00078 class SoInputImpl;
00079 class SoStreamBuffer;
00080 class SbThreadSignal;
00081 class SoInputParameters;
00082
00083 #ifndef HIDDEN_FROM_DOC
00085 //
00086
00087
00088
00089
00090
00091
00092
00093
00095
00096
00097 {
00098
00099 SbString name;
00100
00101 SbString fullName;
00102
00103 FILE *fp;
00104
00105 void* zFp;
00106
00107 void *buffer;
00108
00109 SbBool bufferOwnership;
00110
00111 char *curBuf;
00112
00113 size_t bufSize;
00114
00115 int lineNum;
00116
00117 SbBool openedHere;
00118 #ifdef _WIN32
00119
00120 SbBool openedFromHandle;
00121 #endif
00122
00123 SbBool binary;
00124
00125 SbBool utf8;
00126
00127 SbBool zCompressed;
00128
00129 SbBool isBigEndian;
00130
00131 SbBool isStreamed;
00132
00133 int streamedBuffersNumber;
00134
00135 int streamedBuffersSize;
00136
00137 long long fileSize;
00138
00139 SbBool readHeader;
00140
00141 bool headerOk;
00142
00143 SbDict *refDict;
00144
00145 SbBool borrowedDict;
00146
00147 float ivVersion;
00148
00149 SbString headerString;
00150
00151 SoDBHeaderCB *postReadCB;
00152
00153 void *CBData;
00154
00155 SoInputFile();
00156 };
00157
00158
00159 {
00160 };
00161
00162 #endif // HIDDEN_FROM_DOC
00163
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00192
00364 class SoInput
00365 {
00366
00367 public:
00368
00372 SoInput();
00373
00377 virtual ~SoInput();
00378
00386 SoNONUNICODE static void addDirectoryFirst(const char *dirName);
00387
00388
00394 static void addDirectoryFirst( const SbString& dirName );
00395
00403 SoNONUNICODE static void addDirectoryLast(const char *dirName);
00404
00410 static void addDirectoryLast( const SbString& dirName );
00411
00412 #define DIRECTORIES_SEPARATOR ":;"
00413
00434 SoNONUNICODE static void addEnvDirectoriesFirst(const char *envVarName,
00435 const char *dirSep = DIRECTORIES_SEPARATOR);
00436
00450 static void addEnvDirectoriesFirst(const SbString& envVarName,
00451 const SbString& dirSep = DIRECTORIES_SEPARATOR);
00452
00468 SoNONUNICODE static void addEnvDirectoriesLast(const char *envVarName,
00469 const char *dirSep = DIRECTORIES_SEPARATOR);
00470
00484 static void addEnvDirectoriesLast(const SbString& envVarName,
00485 const SbString& dirSep = DIRECTORIES_SEPARATOR);
00486
00492 SoNONUNICODE static void removeDirectory(const char *dirName);
00493
00497 static void removeDirectory( const SbString& dirName );
00498
00502 static void clearDirectories();
00503
00508 static const SbStringList &getDirectories();
00509
00510
00515 virtual void setFilePointer(FILE *newFP);
00516
00521 virtual FILE* getFilePointer();
00522
00541 SoNONUNICODE virtual SbBool openFile(const char *fileName, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE);
00542
00558 virtual SbBool openFile( const SbString& fileName, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE);
00559
00573 SoNONUNICODE virtual SbBool pushFile(const char *fileName);
00574
00586 virtual SbBool pushFile( const SbString& fileName );
00587
00591 virtual void closeFile();
00592
00600 virtual SbBool isValidFile();
00601
00609 virtual SbBool isValidBuffer();
00610
00616 virtual FILE *getCurFile() const;
00617
00624 SoNONUNICODE virtual const char *getCurFileName() const;
00625
00631 virtual SbString getCurStringFileName() const;
00632
00637 virtual void setBuffer(void *buffer, size_t bufSize);
00638
00642 virtual int getNumBytesRead() const;
00643
00649 virtual SbString getHeader();
00650
00657 virtual float getIVVersion() { return curFile->ivVersion; }
00658
00663 virtual void updateReadPercent( double readPercentage );
00664
00672 static SbBool findAbsolutePath( const SbString& fileName, SbString &fullName );
00673
00674
00680 void setInputParameters( SoInputParameters* parameters);
00681
00685 SoInputParameters* getInputParameters() const;
00686
00687 private:
00688
00689
00690 virtual SbBool isBinary();
00691
00692
00693 virtual SbBool isUtf8();
00694
00695
00696 virtual SbBool isZCompressed();
00697
00698
00699
00700 virtual SbBool get(char &c);
00701
00702
00703
00704 virtual SbBool getASCIIBuffer(char &c);
00705
00706
00707
00708 virtual SbBool getASCIIFile(char &c);
00709
00710
00711
00712
00713 virtual SbBool read(char &c);
00714 virtual SbBool readByte(char &c);
00715 virtual SbBool readByte(unsigned char &c);
00716 virtual SbBool read(SbString &s);
00717 virtual SbBool read(SbName &n, SbBool validIdent = FALSE);
00718 virtual SbBool readBitMask(SbName &n, SbBool validIdent = FALSE);
00719 virtual SbBool read(bool &i);
00720 virtual SbBool read(int32_t &i);
00721 virtual SbBool read(int64_t &i);
00722 virtual SbBool read(uint32_t &i);
00723 virtual SbBool read(uint64_t &i);
00724 virtual SbBool read(short &s);
00725 virtual SbBool read(unsigned short &s);
00726 virtual SbBool read(float &f);
00727 virtual SbBool read(double &d);
00728 virtual SbBool readBinaryArray(unsigned char *c, int length);
00729 virtual SbBool readBinaryArray(int32_t *l, int length);
00730 virtual SbBool readBinaryArray(int64_t *l, int length);
00731 virtual SbBool readBinaryArray(float *f, int length);
00732 virtual SbBool readBinaryArray(short *s, int length);
00733 virtual SbBool readBinaryArray(double *d, int length);
00734
00735
00736 virtual SbBool eof() const;
00737
00738
00739
00740
00741 virtual SbBool read(char &c, SbBool skip);
00742
00743
00744 SbBool isFileVRML2();
00745
00746
00747
00748
00749
00750
00751 void setFileVRML2(SbBool flag);
00752
00753
00754 int getVRMLVersion() { return vrmlFile; }
00755
00756
00757
00758
00759
00760
00761 void setVRMLVersion(int flag) { vrmlFile = flag; }
00762
00763 #ifdef _WIN32
00764
00765 SbString getFileNameFromHandle( HANDLE hFile ) const;
00766
00767 virtual SbBool openFromHandle( UINT hFile, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE );
00768 #endif
00769
00770 private:
00771
00772
00773 static void init();
00774 static void finish();
00775
00776
00777 SoInput(SoInput *dictIn);
00778
00779
00780
00781 virtual void getLocationString(SbString &string) const;
00782
00783
00784 virtual void putBack(char c);
00785 virtual void putBack(const char *string);
00786
00787
00788
00789
00790 virtual void addReference(const SbName &name, SoBase *base,
00791 SbBool addToGlobalDict = TRUE);
00792
00793
00794
00795 virtual void removeReference(const SbName &name);
00796
00797
00798 virtual SoBase * findReference(const SbName &name) const;
00799
00800 virtual void resetFilePointer(FILE *fptr) { curFile->fp = fptr;}
00801
00802 SoInputImpl* m_soInputImpl;
00803
00804
00805
00806
00807
00808
00809 SoNONUNICODE static SbBool findAbsolutePath( const char* fileName, const SbStringList *some_dir, SbString &fullName );
00810
00811
00812
00813
00814
00815 static SbBool findAbsolutePath( const SbString& fileName, const SbStringList *some_dir, SbString &fullName );
00816
00817 int32_t isReadingBinArrayAsMemObj() const;
00818
00819
00820
00821 void updateNumReadBytes( int value);
00822
00823
00824 void setIVVersion(float version) { curFile->ivVersion = version; }
00825 bool isHeaderOk() { return curFile->headerOk; }
00826
00827 void seekCurBuf( size_t size ) { curFile->curBuf += size; updateNumReadBytes(static_cast<int>(size) ); }
00828
00841 static bool readTextFile(const SbString& filename, SbString& dest);
00842
00851 static bool addInMemoryTextFile(const SbString& filename, const SbString& fileContent);
00852
00859 static bool removeInMemoryTextFile(const SbString& filename);
00860
00865 static bool isInMemoryTextFile(const SbString& filename);
00866
00870 long fileTell();
00871
00876 int fileSeek( long offset, int whence );
00877
00879 bool isReadByPlugin();
00880
00881 SbBool isReadingHeader();
00882
00883 private:
00884
00885
00886 static void setDirectories(SbStringList *dir);
00887
00888 SbPList files;
00889
00890 struct SoInputFile *curFile;
00891
00892 SbString backBuf;
00893
00894 int backBufIndex;
00895
00896
00897 void *tmpBuffer;
00898
00899 char *curTmpBuf;
00900
00901 size_t tmpBufSize;
00902
00903
00904
00905 char backupBuf[8];
00906
00907 SbBool backupBufUsed;
00908
00909 std::vector<char> putBackCharBuffer;
00910
00911
00912 FILE *findFile(const SbString& fileName, SbString &fullName);
00913
00914
00915 void initFile(FILE *newFP, const SbString& fileName,
00916 SbString *fullName, SbBool openedHere,
00917 SbDict *refDict = NULL);
00918
00919
00920 void initStreamedFile( FILE* newFP, const SbString& fileName, SbString* fullName, SbBool openedHere,
00921 SbDict* refDict = NULL );
00922
00923 #ifdef _WIN32
00924 FILE *findFromHandle(int nHandle) const;
00925
00926 void initFromHandle(FILE *newFP,
00927 int nHandle, SbBool openedHere,
00928 SbDict *refDict = NULL);
00929 #endif
00930
00931
00932
00933 SbBool checkInventorHeader(SbBool bValidateBufferHeader=FALSE);
00934
00935
00936
00937 SbBool checkHeader(SbBool bValidateBufferHeader=FALSE);
00938
00939
00940 SbBool fromBuffer() const { return (curFile->buffer != NULL); }
00941
00942
00943
00944 SbBool skipWhiteSpace();
00945
00946
00947 SbBool popFile();
00948
00949
00950 size_t freeBytesInBuf() const
00951 { return (curFile->bufSize - (curFile->curBuf - static_cast<char *>(curFile->buffer) )); }
00952
00953
00954
00955 SbBool readInteger(int32_t &l);
00956 SbBool readUnsignedInteger(uint32_t &l);
00957 SbBool readInteger(int64_t &l);
00958 SbBool readUnsignedInteger(uint64_t &l);
00959 SbBool readReal(double &d);
00960
00961
00962
00963 virtual SbBool readHex(uint32_t &l);
00964
00965
00966
00967 SbBool readUnsignedIntegerString(char *str);
00968
00969
00970
00971 int readDigits(char *string);
00972 int readHexDigits(char *string);
00973
00974
00975 int readChar(char *string, char charToRead);
00976
00977
00978 SbBool makeRoomInBuf(size_t nBytes);
00979
00980
00981 void convertShort(char *from, short *s);
00982 void convertInt32(char *from, int32_t *l);
00983 void convertInt64(char *from, int64_t *l);
00984 void convertFloat(char *from, float *f);
00985 void convertDouble(char *from, double *d);
00986 void convertShortArray(char *from, register short *to, register size_t len);
00987 void convertInt32Array(char *from, register int32_t *to, register size_t len);
00988 void convertInt64Array(char *from, register int64_t *to, register size_t len);
00989 void convertFloatArray(char *from, register float *to, register size_t len);
00990 void convertDoubleArray(char *from, register double *to, register size_t len);
00991
00992 SbString bufferToString( const char* buffer );
00993
00999 int fileRead( void* ptr, size_t size, size_t count );
01000
01004 int fileReadByte();
01005
01006 static SbBool checkFileCompression( FILE* file, int64_t& fileSize, SbBool& isCompressed );
01007 static SbBool checkFileCompression( unsigned char byte1, unsigned char byte2 );
01008
01009
01010 void convertShortArray(char *from, register short *to, register int len);
01011 void convertInt32Array(char *from, register int32_t *to, register int len);
01012 void convertFloatArray(char *from, register float *to, register int len);
01013 void convertDoubleArray(char *from, register double *to, register int len);
01014
01015 int vrmlFile;
01016
01017
01018
01019
01020 private:
01021
01022 SoInputParameters* m_inputParameters;
01023
01024
01025 static SbStringList *directories;
01026 static SbString *defaultString;
01027
01028
01029 static void* streamingStackCB( void* data );
01030
01031 void commonInit();
01032
01033
01034 void swapStreamBuffer();
01035
01036 char* getType( char* from, size_t sizeOfType, size_t freeBytes );
01037 char* getLongType( char* from, size_t sizeOfType, size_t freeBytes );
01038
01039 static std::list<SoStreamBuffer*>* m_streamBufStack;
01040
01041 static SbThreadSignal* m_availableSig;
01042 static SbThreadMutex* m_availableSigMutex;
01043 static SbThreadSignal* m_stackFillSig;
01044 static SbThreadMutex* m_stackFillSigMutex;
01045 static SbThreadMutex* m_stackMutex;
01046 static SbThreadMutex m_directoriesMutex;
01047 SbThread* m_stackThread;
01048
01049 int64_t m_numReadBytes;
01050
01051
01052 int m_percent, m_progress, m_prevProgressVal, m_prevPercentVal;
01053 double m_result;
01054 std::string m_progressBar;
01055
01056
01057 typedef std::map<SbString, SbString> InMemoryTextFileMap;
01058 static InMemoryTextFileMap s_InMemoryTextFileMap;
01059 static SbThreadSpinlock s_InMemoryTextFileMapMutex;
01060 static SbBool s_InMemoryTextFileEnabled;
01061
01062
01063 SbBool uncompressZData(const void *src, size_t srcLen, void*& dst, size_t& dstLen);
01064 int inflateZData(const void *src, size_t srcLen, void *dst, size_t dstLen);
01065
01066 };
01067
01068 #if defined(_WIN32)
01069 #pragma warning( pop )
01070 #endif
01071
01072 #endif
01073
01074