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
00362 class SoInput
00363 {
00364
00365 public:
00366
00370 SoInput();
00371
00375 virtual ~SoInput();
00376
00384 SoNONUNICODE static void addDirectoryFirst(const char *dirName);
00385
00386
00392 static void addDirectoryFirst( const SbString& dirName );
00393
00401 SoNONUNICODE static void addDirectoryLast(const char *dirName);
00402
00408 static void addDirectoryLast( const SbString& dirName );
00409
00410 #define DIRECTORIES_SEPARATOR ":;"
00411
00432 SoNONUNICODE static void addEnvDirectoriesFirst(const char *envVarName,
00433 const char *dirSep = DIRECTORIES_SEPARATOR);
00434
00448 static void addEnvDirectoriesFirst(const SbString& envVarName,
00449 const SbString& dirSep = DIRECTORIES_SEPARATOR);
00450
00466 SoNONUNICODE static void addEnvDirectoriesLast(const char *envVarName,
00467 const char *dirSep = DIRECTORIES_SEPARATOR);
00468
00482 static void addEnvDirectoriesLast(const SbString& envVarName,
00483 const SbString& dirSep = DIRECTORIES_SEPARATOR);
00484
00490 SoNONUNICODE static void removeDirectory(const char *dirName);
00491
00495 static void removeDirectory( const SbString& dirName );
00496
00500 static void clearDirectories();
00501
00506 static const SbStringList &getDirectories();
00507
00508
00513 virtual void setFilePointer(FILE *newFP);
00514
00519 virtual FILE* getFilePointer();
00520
00539 SoNONUNICODE virtual SbBool openFile(const char *fileName, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE);
00540
00556 virtual SbBool openFile( const SbString& fileName, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE);
00557
00571 SoNONUNICODE virtual SbBool pushFile(const char *fileName);
00572
00584 virtual SbBool pushFile( const SbString& fileName );
00585
00589 virtual void closeFile();
00590
00598 virtual SbBool isValidFile();
00599
00607 virtual SbBool isValidBuffer();
00608
00614 virtual FILE *getCurFile() const;
00615
00622 SoNONUNICODE virtual const char *getCurFileName() const;
00623
00629 virtual SbString getCurStringFileName() const;
00630
00635 virtual void setBuffer(void *buffer, size_t bufSize);
00636
00640 virtual int getNumBytesRead() const;
00641
00647 virtual SbString getHeader();
00648
00655 virtual float getIVVersion() { return curFile->ivVersion; }
00656
00661 virtual void updateReadPercent( double readPercentage );
00662
00670 static SbBool findAbsolutePath( const SbString& fileName, SbString &fullName );
00671
00672
00678 void setInputParameters( SoInputParameters* parameters);
00679
00683 SoInputParameters* getInputParameters() const;
00684
00685 private:
00686
00687
00688 virtual SbBool isBinary();
00689
00690
00691 virtual SbBool isUtf8();
00692
00693
00694 virtual SbBool isZCompressed();
00695
00696
00697
00698 virtual SbBool get(char &c);
00699
00700
00701
00702 virtual SbBool getASCIIBuffer(char &c);
00703
00704
00705
00706 virtual SbBool getASCIIFile(char &c);
00707
00708
00709
00710
00711 virtual SbBool read(char &c);
00712 virtual SbBool readByte(char &c);
00713 virtual SbBool readByte(unsigned char &c);
00714 virtual SbBool read(SbString &s);
00715 virtual SbBool read(SbName &n, SbBool validIdent = FALSE);
00716 virtual SbBool readBitMask(SbName &n, SbBool validIdent = FALSE);
00717 virtual SbBool read(bool &i);
00718 virtual SbBool read(int32_t &i);
00719 virtual SbBool read(int64_t &i);
00720 virtual SbBool read(uint32_t &i);
00721 virtual SbBool read(uint64_t &i);
00722 virtual SbBool read(short &s);
00723 virtual SbBool read(unsigned short &s);
00724 virtual SbBool read(float &f);
00725 virtual SbBool read(double &d);
00726 virtual SbBool readBinaryArray(unsigned char *c, int length);
00727 virtual SbBool readBinaryArray(int32_t *l, int length);
00728 virtual SbBool readBinaryArray(int64_t *l, int length);
00729 virtual SbBool readBinaryArray(float *f, int length);
00730 virtual SbBool readBinaryArray(short *s, int length);
00731 virtual SbBool readBinaryArray(double *d, int length);
00732
00733
00734 virtual SbBool eof() const;
00735
00736
00737
00738
00739 virtual SbBool read(char &c, SbBool skip);
00740
00741
00742 SbBool isFileVRML2();
00743
00744
00745
00746
00747
00748
00749 void setFileVRML2(SbBool flag);
00750
00751
00752 int getVRMLVersion() { return vrmlFile; }
00753
00754
00755
00756
00757
00758
00759 void setVRMLVersion(int flag) { vrmlFile = flag; }
00760
00761 #ifdef _WIN32
00762
00763 SbString getFileNameFromHandle( HANDLE hFile ) const;
00764
00765 virtual SbBool openFromHandle( UINT hFile, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE );
00766 #endif
00767
00768 private:
00769
00770
00771 static void init();
00772 static void finish();
00773
00774
00775 SoInput(SoInput *dictIn);
00776
00777
00778
00779 virtual void getLocationString(SbString &string) const;
00780
00781
00782 virtual void putBack(char c);
00783 virtual void putBack(const char *string);
00784
00785
00786
00787
00788 virtual void addReference(const SbName &name, SoBase *base,
00789 SbBool addToGlobalDict = TRUE);
00790
00791
00792
00793 virtual void removeReference(const SbName &name);
00794
00795
00796 virtual SoBase * findReference(const SbName &name) const;
00797
00798 virtual void resetFilePointer(FILE *fptr) { curFile->fp = fptr;}
00799
00800 virtual SbBool readLargeBinaryArray(unsigned char *c, size_t length);
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, short *to, size_t len);
00987 void convertInt32Array(char *from, int32_t *to, size_t len);
00988 void convertInt64Array(char *from, int64_t *to, size_t len);
00989 void convertFloatArray(char *from, float *to, size_t len);
00990 void convertDoubleArray(char *from, double *to, size_t len);
00991
00992 SbString bufferToString( const char* buffer );
00993
00999 size_t 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, short *to, int len);
01011 void convertInt32Array(char *from, int32_t *to, int len);
01012 void convertFloatArray(char *from, float *to, int len);
01013 void convertDoubleArray(char *from, double *to, 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