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
00060 #include <Inventor/STL/fstream>
00061 #include <Inventor/STL/iostream>
00062 #include <Inventor/STL/list>
00063 #include <Inventor/STL/vector>
00064 #include <Inventor/STL/map>
00065
00066 #if defined(_WIN32)
00067 #pragma warning( push )
00068 #pragma warning( disable: 4251 ) // 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
00069 #endif
00070
00071
00072 class SoNode;
00073 class SoPath;
00074 class SoBase;
00075 class SoDB;
00076 class SbStringList;
00077 class SoInputImpl;
00078 class SoStreamBuffer;
00079 class SbThreadSignal;
00080 class SbThreadMutex;
00081 class SoInputParameters;
00082
00083 #ifndef HIDDEN_FROM_DOC
00085 //
00086
00087
00088
00089
00090
00091
00092
00093
00095
00096 SoINTERNAL struct SoInputFile
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 SoINTERNAL struct SoStreaming
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
00406 class SoInput
00407 {
00408
00409 public:
00410
00414 SoInput();
00415
00419 virtual ~SoInput();
00420
00428 SoNONUNICODE static void addDirectoryFirst(const char *dirName);
00429
00430
00436 static void addDirectoryFirst( const SbString& dirName );
00437
00445 SoNONUNICODE static void addDirectoryLast(const char *dirName);
00446
00452 static void addDirectoryLast( const SbString& dirName );
00453
00454 #define DIRECTORIES_SEPARATOR ":;"
00455
00476 SoNONUNICODE static void addEnvDirectoriesFirst(const char *envVarName,
00477 const char *dirSep = DIRECTORIES_SEPARATOR);
00478
00492 static void addEnvDirectoriesFirst(const SbString& envVarName,
00493 const SbString& dirSep = DIRECTORIES_SEPARATOR);
00494
00510 SoNONUNICODE static void addEnvDirectoriesLast(const char *envVarName,
00511 const char *dirSep = DIRECTORIES_SEPARATOR);
00512
00526 static void addEnvDirectoriesLast(const SbString& envVarName,
00527 const SbString& dirSep = DIRECTORIES_SEPARATOR);
00528
00534 SoNONUNICODE static void removeDirectory(const char *dirName);
00535
00539 static void removeDirectory( const SbString& dirName );
00540
00544 static void clearDirectories();
00545
00550 static const SbStringList &getDirectories();
00551
00552
00557 virtual void setFilePointer(FILE *newFP);
00558
00563 virtual FILE* getFilePointer();
00564
00583 SoNONUNICODE virtual SbBool openFile(const char *fileName, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE);
00584
00600 virtual SbBool openFile( const SbString& fileName, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE);
00601
00615 SoNONUNICODE virtual SbBool pushFile(const char *fileName);
00616
00628 virtual SbBool pushFile( const SbString& fileName );
00629
00633 virtual void closeFile();
00634
00642 virtual SbBool isValidFile();
00643
00651 virtual SbBool isValidBuffer();
00652
00658 virtual FILE *getCurFile() const;
00659
00666 SoNONUNICODE virtual const char *getCurFileName() const;
00667
00673 virtual SbString getCurStringFileName() const;
00674
00679 virtual void setBuffer(void *buffer, size_t bufSize);
00680
00684 virtual int getNumBytesRead() const;
00685
00691 virtual SbString getHeader();
00692
00699 virtual float getIVVersion() { return curFile->ivVersion; }
00700
00705 virtual void updateReadPercent( double readPercentage );
00706
00714 static SbBool findAbsolutePath( const SbString& fileName, SbString &fullName );
00715
00716
00722 void setInputParameters( SoInputParameters* parameters);
00723
00727 SoInputParameters* getInputParameters() const;
00728 SoDEPRECATED enum
00733 FileType
00734 {
00738 OpenInventor,
00742 OpenFlight,
00746 DXF,
00750 IGES,
00754 STEP,
00758 CATIA5,
00762 ALL
00763 } ;
00764
00765 #if 1 SoDEPRECATED
00772 virtual FileType getCurFileType() const;
00773 SoDEPRECATED
00778 virtual void setCurFileType(FileType fileType);
00779
00780 #endif
00782 private:
00783
00784
00785 virtual SbBool isBinary();
00786
00787
00788 virtual SbBool isUtf8();
00789
00790
00791 virtual SbBool isZCompressed();
00792
00793
00794
00795 virtual SbBool get(char &c);
00796
00797
00798
00799 virtual SbBool getASCIIBuffer(char &c);
00800
00801
00802
00803 virtual SbBool getASCIIFile(char &c);
00804
00805
00806
00807
00808 virtual SbBool read(char &c);
00809 virtual SbBool readByte(char &c);
00810 virtual SbBool readByte(unsigned char &c);
00811 virtual SbBool read(SbString &s);
00812 virtual SbBool read(SbName &n, SbBool validIdent = FALSE);
00813 virtual SbBool readBitMask(SbName &n, SbBool validIdent = FALSE);
00814 virtual SbBool read(bool &i);
00815 virtual SbBool read(int32_t &i);
00816 virtual SbBool read(int64_t &i);
00817 virtual SbBool read(uint32_t &i);
00818 virtual SbBool read(uint64_t &i);
00819 virtual SbBool read(short &s);
00820 virtual SbBool read(unsigned short &s);
00821 virtual SbBool read(float &f);
00822 virtual SbBool read(double &d);
00823 virtual SbBool readBinaryArray(unsigned char *c, int length);
00824 virtual SbBool readBinaryArray(int32_t *l, int length);
00825 virtual SbBool readBinaryArray(int64_t *l, int length);
00826 virtual SbBool readBinaryArray(float *f, int length);
00827 virtual SbBool readBinaryArray(short *s, int length);
00828 virtual SbBool readBinaryArray(double *d, int length);
00829
00830
00831 virtual SbBool eof() const;
00832
00833
00834
00835
00836 virtual SbBool read(char &c, SbBool skip);
00837
00838
00839 SbBool isFileVRML2();
00840
00841
00842
00843
00844
00845
00846 void setFileVRML2(SbBool flag);
00847
00848
00849 int getVRMLVersion() { return vrmlFile; }
00850
00851
00852
00853
00854
00855
00856 void setVRMLVersion(int flag) { vrmlFile = flag; }
00857
00858 #ifdef _WIN32
00859
00860 SbString getFileNameFromHandle( HANDLE hFile ) const;
00861
00862 virtual SbBool openFromHandle( UINT hFile, SbBool okIfNotFound = FALSE, SbBool aSync = FALSE );
00863 #endif
00864
00865 private:
00866
00867
00868 static void init();
00869 static void finish();
00870
00871
00872 SoInput(SoInput *dictIn);
00873
00874
00875
00876 virtual void getLocationString(SbString &string) const;
00877
00878
00879 virtual void putBack(char c);
00880 virtual void putBack(const char *string);
00881
00882
00883
00884
00885 virtual void addReference(const SbName &name, SoBase *base,
00886 SbBool addToGlobalDict = TRUE);
00887
00888
00889
00890 virtual void removeReference(const SbName &name);
00891
00892
00893 virtual SoBase * findReference(const SbName &name) const;
00894
00895 virtual void resetFilePointer(FILE *fptr) { curFile->fp = fptr;}
00896
00897 SoInputImpl* m_soInputImpl;
00898
00899
00900
00901
00902
00903
00904 SoNONUNICODE static SbBool findAbsolutePath( const char* fileName, const SbStringList *some_dir, SbString &fullName );
00905
00906
00907
00908
00909
00910 static SbBool findAbsolutePath( const SbString& fileName, const SbStringList *some_dir, SbString &fullName );
00911
00912 int32_t isReadingBinArrayAsMemObj() const;
00913
00914
00915
00916 void updateNumReadBytes( int value);
00917
00918
00919 void setIVVersion(float version) { curFile->ivVersion = version; }
00920 bool isHeaderOk() { return curFile->headerOk; }
00921
00922 void seekCurBuf( size_t size ) { curFile->curBuf += size; updateNumReadBytes(static_cast<int>(size) ); }
00923
00936 static bool readTextFile(const SbString& filename, SbString& dest);
00937
00946 static bool addInMemoryTextFile(const SbString& filename, const SbString& fileContent);
00947
00954 static bool removeInMemoryTextFile(const SbString& filename);
00955
00960 static bool isInMemoryTextFile(const SbString& filename);
00961
00965 long fileTell();
00966
00971 int fileSeek( long offset, int whence );
00972
00974 bool isReadByPlugin();
00975
00976 SbBool isReadingHeader();
00977
00978 private:
00979
00980
00981 static void setDirectories(SbStringList *dir);
00982
00983 SbPList files;
00984
00985 struct SoInputFile *curFile;
00986
00987 SbString backBuf;
00988
00989 int backBufIndex;
00990
00991
00992 void *tmpBuffer;
00993
00994 char *curTmpBuf;
00995
00996 size_t tmpBufSize;
00997
00998
00999
01000 char backupBuf[8];
01001
01002 SbBool backupBufUsed;
01003
01004 std::vector<char> putBackCharBuffer;
01005
01006
01007 FILE *findFile(const SbString& fileName, SbString &fullName);
01008
01009
01010 void initFile(FILE *newFP, const SbString& fileName,
01011 SbString *fullName, SbBool openedHere,
01012 SbDict *refDict = NULL);
01013
01014
01015 void initStreamedFile( FILE* newFP, const SbString& fileName, SbString* fullName, SbBool openedHere,
01016 SbDict* refDict = NULL );
01017
01018 #ifdef _WIN32
01019 FILE *findFromHandle(int nHandle) const;
01020
01021 void initFromHandle(FILE *newFP,
01022 int nHandle, SbBool openedHere,
01023 SbDict *refDict = NULL);
01024 #endif
01025
01026
01027
01028 SbBool checkInventorHeader(SbBool bValidateBufferHeader=FALSE);
01029
01030
01031
01032 SbBool checkHeader(SbBool bValidateBufferHeader=FALSE);
01033
01034
01035 SbBool fromBuffer() const { return (curFile->buffer != NULL); }
01036
01037
01038
01039 SbBool skipWhiteSpace();
01040
01041
01042 SbBool popFile();
01043
01044
01045 size_t freeBytesInBuf() const
01046 { return (curFile->bufSize - (curFile->curBuf - static_cast<char *>(curFile->buffer) )); }
01047
01048
01049
01050 SbBool readInteger(int32_t &l);
01051 SbBool readUnsignedInteger(uint32_t &l);
01052 SbBool readInteger(int64_t &l);
01053 SbBool readUnsignedInteger(uint64_t &l);
01054 SbBool readReal(double &d);
01055
01056
01057
01058 virtual SbBool readHex(uint32_t &l);
01059
01060
01061
01062 SbBool readUnsignedIntegerString(char *str);
01063
01064
01065
01066 int readDigits(char *string);
01067 int readHexDigits(char *string);
01068
01069
01070 int readChar(char *string, char charToRead);
01071
01072
01073 SbBool makeRoomInBuf(size_t nBytes);
01074
01075
01076 void convertShort(char *from, short *s);
01077 void convertInt32(char *from, int32_t *l);
01078 void convertInt64(char *from, int64_t *l);
01079 void convertFloat(char *from, float *f);
01080 void convertDouble(char *from, double *d);
01081 void convertShortArray(char *from, short *to, size_t len);
01082 void convertInt32Array(char *from, int32_t *to, size_t len);
01083 void convertInt64Array(char *from, int64_t *to, size_t len);
01084 void convertFloatArray(char *from, float *to, size_t len);
01085 void convertDoubleArray(char *from, double *to, size_t len);
01086
01087 SbString bufferToString( const char* buffer );
01088
01094 int fileRead( void* ptr, size_t size, size_t count );
01095
01099 int fileReadByte();
01100
01101 static SbBool checkFileCompression( FILE* file, int64_t& fileSize, SbBool& isCompressed );
01102 static SbBool checkFileCompression( unsigned char byte1, unsigned char byte2 );
01103
01104
01105 void convertShortArray(char *from, short *to, int len);
01106 void convertInt32Array(char *from, int32_t *to, int len);
01107 void convertFloatArray(char *from, float *to, int len);
01108 void convertDoubleArray(char *from, double *to, int len);
01109
01110 int vrmlFile;
01111
01112 FileType fileType;
01113
01114
01115
01116
01117 private:
01118
01119 SoInputParameters* m_inputParameters;
01120
01121
01122 static SbStringList *directories;
01123 static SbString *defaultString;
01124
01125
01126 static void* streamingStackCB( void* data );
01127
01128 void commonInit();
01129
01130
01131 void swapStreamBuffer();
01132
01133 char* getType( char* from, size_t sizeOfType, size_t freeBytes );
01134 char* getLongType( char* from, size_t sizeOfType, size_t freeBytes );
01135
01136 static std::list<SoStreamBuffer*>* m_streamBufStack;
01137
01138 static SbThreadSignal* m_availableSig;
01139 static SbThreadMutex* m_availableSigMutex;
01140 static SbThreadSignal* m_stackFillSig;
01141 static SbThreadMutex* m_stackFillSigMutex;
01142 static SbThreadMutex* m_stackMutex;
01143 static SbThreadMutex m_directoriesMutex;
01144 SbThread* m_stackThread;
01145
01146 int64_t m_numReadBytes;
01147
01148
01149 int m_percent, m_progress, m_prevProgressVal, m_prevPercentVal;
01150 double m_result;
01151 std::string m_progressBar;
01152
01153
01154 typedef std::map<SbString, SbString> InMemoryTextFileMap;
01155 static InMemoryTextFileMap s_InMemoryTextFileMap;
01156 static SbThreadSpinlock s_InMemoryTextFileMapMutex;
01157 static SbBool s_InMemoryTextFileEnabled;
01158
01159
01160 SbBool uncompressZData(const void *src, size_t srcLen, void*& dst, size_t& dstLen);
01161 int inflateZData(const void *src, size_t srcLen, void *dst, size_t dstLen);
01162
01163 };
01164
01165 #if defined(_WIN32)
01166 #pragma warning( pop )
01167 #endif
01168
01169 #endif
01170
01171