From ca864614463810b053a60fa34a7f904976f65f0a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 19 Dec 2003 22:21:57 +0000 Subject: [PATCH] From Corbin Holtz, support for endian testing and bytes swapping. With mods from Robert to use include/osg/Endian --- src/osgPlugins/ive/DataInputStream.cpp | 88 ++++++++++++++++++++++--- src/osgPlugins/ive/DataInputStream.h | 3 + src/osgPlugins/ive/DataOutputStream.cpp | 3 +- src/osgPlugins/ive/DataOutputStream.h | 1 + src/osgPlugins/ive/DataTypeSize.h | 3 - src/osgPlugins/ive/IveVersion.h | 24 +++++++ 6 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 src/osgPlugins/ive/IveVersion.h diff --git a/src/osgPlugins/ive/DataInputStream.cpp b/src/osgPlugins/ive/DataInputStream.cpp index 240234694..4329518b0 100644 --- a/src/osgPlugins/ive/DataInputStream.cpp +++ b/src/osgPlugins/ive/DataInputStream.cpp @@ -49,29 +49,47 @@ #include "Geometry.h" +#include #include + using namespace ive; using namespace std; DataInputStream::DataInputStream(std::istream* istream) { + unsigned int endianType ; + _verboseOutput = false; _istream = istream; _peeking = false; _peekValue = 0; + _byteswap = 0; if(!istream){ throw Exception("DataInputStream::DataInputStream(): null pointer exception in argument."); } - _version = readInt(); - + endianType = readUInt() ; + + if ( endianType != ENDIAN_TYPE) { + // Make sure the file is simply swapped + if ( endianType != OPPOSITE_ENDIAN_TYPE ) { + throw Exception("DataInputStream::DataInputStream(): This file has an unreadable endian type.") ; + } + if (_verboseOutput) std::cout<<"DataInputStream::DataInputStream: Reading a byteswapped file" << std::endl ; + _byteswap = 1 ; + } + + _version = readUInt(); + if (_byteswap) osg::swapBytes4((char *)&_version) ; + // Are we trying to open a binary .ive file which version are newer than this library. if(_version>VERSION){ throw Exception("DataInputStream::DataInputStream(): The version found in the file is newer than this library can handle."); } + } DataInputStream::~DataInputStream(){} @@ -120,6 +138,8 @@ unsigned short DataInputStream::readUShort(){ if (_verboseOutput) std::cout<<"read/writeUShort() ["<read((char*)s.c_str(), size); - if (_istream->rdstate() & _istream->failbit) - throw Exception("DataInputStream::readString(): Failed to read string value."); + //if (_istream->rdstate() & _istream->failbit) + // throw Exception("DataInputStream::readString(): Failed to read string value."); if (_verboseOutput) std::cout<<"read/writeString() ["<read((char*)&((*a)[0]), FLOATSIZE*size); @@ -407,10 +450,15 @@ osg::FloatArray* DataInputStream::readFloatArray(){ if (_verboseOutput) std::cout<<"read/writeFloatArray() ["< #include +#include "IveVersion.h" #include "DataTypeSize.h" #include "Exception.h" @@ -84,6 +85,8 @@ private: StateAttributeMap _stateAttributeMap; DrawableMap _drawableMap; NodeMap _nodeMap; + + int _byteswap ; }; } diff --git a/src/osgPlugins/ive/DataOutputStream.cpp b/src/osgPlugins/ive/DataOutputStream.cpp index 3db143306..17128ff85 100644 --- a/src/osgPlugins/ive/DataOutputStream.cpp +++ b/src/osgPlugins/ive/DataOutputStream.cpp @@ -61,7 +61,8 @@ DataOutputStream::DataOutputStream(std::ostream * ostream) _ostream = ostream; if(!_ostream) throw Exception("DataOutputStream::DataOutputStream(): null pointer exception in argument."); - writeInt(VERSION); + writeUInt(ENDIAN_TYPE) ; + writeUInt(VERSION); } DataOutputStream::~DataOutputStream(){} diff --git a/src/osgPlugins/ive/DataOutputStream.h b/src/osgPlugins/ive/DataOutputStream.h index 2b525ea6d..c9b7b0ff1 100644 --- a/src/osgPlugins/ive/DataOutputStream.h +++ b/src/osgPlugins/ive/DataOutputStream.h @@ -13,6 +13,7 @@ #include #include +#include "IveVersion.h" #include "DataTypeSize.h" #include "Exception.h" diff --git a/src/osgPlugins/ive/DataTypeSize.h b/src/osgPlugins/ive/DataTypeSize.h index c4cf9f05e..cd3a8017f 100644 --- a/src/osgPlugins/ive/DataTypeSize.h +++ b/src/osgPlugins/ive/DataTypeSize.h @@ -10,7 +10,4 @@ #define LONGSIZE 4 #define DOUBLESIZE 8 -// NOTE: Update anytime the binary format changes -#define VERSION 0x01 - #endif diff --git a/src/osgPlugins/ive/IveVersion.h b/src/osgPlugins/ive/IveVersion.h new file mode 100644 index 000000000..fea17cad4 --- /dev/null +++ b/src/osgPlugins/ive/IveVersion.h @@ -0,0 +1,24 @@ + +#ifndef IVE_VERSION +#define IVE_VERSION 1 + + +/* The VERSION tag should be updated any time the + IVE format changes in order to support backward + compatibility (if implemented). VERSION is + stored in the 2nd 4 bytes of the file */ + +#define VERSION 0x00000002 + + +/* The BYTE_SEX tag is used to check the endian + of the IVE file being read in. The IVE format + is always written in the native endian of the + machine to provide optimum reading of the file. + BYTE_SEX is stored in the first 4 bytes of the + file */ +#define ENDIAN_TYPE 0x01020304 +#define OPPOSITE_ENDIAN_TYPE 0x04030201 + + +#endif