From Corbin Holtz, support for endian testing and bytes swapping. With mods

from Robert to use include/osg/Endian
This commit is contained in:
Robert Osfield
2003-12-19 22:21:57 +00:00
parent 3ee8ed15a6
commit ca86461446
6 changed files with 109 additions and 13 deletions

View File

@@ -49,29 +49,47 @@
#include "Geometry.h"
#include <osg/Endian>
#include <osgDB/ReadFile>
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() ["<<s<<"]"<<std::endl;
if (_byteswap) osg::swapBytes((char *)&s,SHORTSIZE);
return s;
}
@@ -132,6 +152,8 @@ unsigned int DataInputStream::readUInt(){
if (_verboseOutput) std::cout<<"read/writeUInt() ["<<s<<"]"<<std::endl;
if (_byteswap) osg::swapBytes((char *)&s,INTSIZE) ;
return s;
}
@@ -151,6 +173,8 @@ int DataInputStream::readInt(){
if (_verboseOutput) std::cout<<"read/writeInt() ["<<i<<"]"<<std::endl;
if (_byteswap) osg::swapBytes((char *)&i,INTSIZE) ;
return i;
}
@@ -176,6 +200,7 @@ float DataInputStream::readFloat(){
if (_verboseOutput) std::cout<<"read/writeFloat() ["<<f<<"]"<<std::endl;
if (_byteswap) osg::swapBytes((char *)&f,FLOATSIZE) ;
return f;
}
@@ -187,6 +212,7 @@ long DataInputStream::readLong(){
if (_verboseOutput) std::cout<<"read/writeLong() ["<<l<<"]"<<std::endl;
if (_byteswap) osg::swapBytes((char *)&l,LONGSIZE) ;
return l;
}
@@ -209,6 +235,7 @@ double DataInputStream::readDouble(){
if (_verboseOutput) std::cout<<"read/writeDouble() ["<<d<<"]"<<std::endl;
if (_byteswap) osg::swapBytes((char *)&d,DOUBLESIZE) ;
return d;
}
@@ -217,8 +244,8 @@ std::string DataInputStream::readString(){
int size = readInt();
s.resize(size);
_istream->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() ["<<s<<"]"<<std::endl;
@@ -337,6 +364,10 @@ osg::IntArray* DataInputStream::readIntArray(){
if (_verboseOutput) std::cout<<"read/writeIntArray() ["<<size<<"]"<<std::endl;
if (_byteswap) {
for (int i = 0 ; i < size ; i++ ) osg::swapBytes((char *)&(a[i]),INTSIZE) ;
}
return a;
}
@@ -365,10 +396,15 @@ osg::UShortArray* DataInputStream::readUShortArray(){
if (_verboseOutput) std::cout<<"read/writeUShortArray() ["<<size<<"]"<<std::endl;
if (_byteswap)
{
for (int i = 0 ; i < size ; i++ ) osg::swapBytes((char *)&(a[i]),SHORTSIZE) ;
}
return a;
}
osg::UIntArray* DataInputStream::readUIntArray(){
osg::UIntArray* DataInputStream::readUIntArray()
{
int size = readInt();
osg::UIntArray* a = new osg::UIntArray(size);
@@ -379,10 +415,15 @@ osg::UIntArray* DataInputStream::readUIntArray(){
if (_verboseOutput) std::cout<<"read/writeUIntArray() ["<<size<<"]"<<std::endl;
if (_byteswap)
{
for (int i = 0 ; i < size ; i++ ) osg::swapBytes((char *)&(a[i]),INTSIZE) ;
}
return a;
}
osg::UByte4Array* DataInputStream::readUByte4Array(){
osg::UByte4Array* DataInputStream::readUByte4Array()
{
int size = readInt();
osg::UByte4Array* a = new osg::UByte4Array(size);
@@ -396,8 +437,10 @@ osg::UByte4Array* DataInputStream::readUByte4Array(){
return a;
}
osg::FloatArray* DataInputStream::readFloatArray(){
osg::FloatArray* DataInputStream::readFloatArray()
{
int size = readInt();
osg::FloatArray* a = new osg::FloatArray(size);
_istream->read((char*)&((*a)[0]), FLOATSIZE*size);
@@ -407,10 +450,15 @@ osg::FloatArray* DataInputStream::readFloatArray(){
if (_verboseOutput) std::cout<<"read/writeFloatArray() ["<<size<<"]"<<std::endl;
if (_byteswap)
{
for (int i = 0 ; i < size ; i++ ) osg::swapBytes((char *)&(a[i]),FLOATSIZE) ;
}
return a;
}
osg::Vec2Array* DataInputStream::readVec2Array(){
osg::Vec2Array* DataInputStream::readVec2Array()
{
int size = readInt();
osg::Vec2Array* a = new osg::Vec2Array(size);
@@ -421,10 +469,19 @@ osg::Vec2Array* DataInputStream::readVec2Array(){
if (_verboseOutput) std::cout<<"read/writeVec2Array() ["<<size<<"]"<<std::endl;
if (_byteswap)
{
float *ptr = (float*)&((*a)[0]) ;
for (int i = 0 ; i < size*2 ; i++ )
{
osg::swapBytes((char *)&(ptr[i]), FLOATSIZE) ;
}
}
return a;
}
osg::Vec3Array* DataInputStream::readVec3Array(){
osg::Vec3Array* DataInputStream::readVec3Array()
{
int size = readInt();
osg::Vec3Array* a = new osg::Vec3Array(size);
@@ -436,6 +493,13 @@ osg::Vec3Array* DataInputStream::readVec3Array(){
if (_verboseOutput) std::cout<<"read/writeVec3Array() ["<<size<<"]"<<std::endl;
if (_byteswap)
{
float *ptr = (float*)&((*a)[0]) ;
for (int i = 0 ; i < size*3 ; i++ ) {
osg::swapBytes((char *)&(ptr[i]),FLOATSIZE) ;
}
}
return a;
}
@@ -450,6 +514,12 @@ osg::Vec4Array* DataInputStream::readVec4Array(){
if (_verboseOutput) std::cout<<"read/writeVec4Array() ["<<size<<"]"<<std::endl;
if (_byteswap) {
float *ptr = (float*)&((*a)[0]) ;
for (int i = 0 ; i < size*4 ; i++ ) {
osg::swapBytes((char *)&(ptr[i]),FLOATSIZE) ;
}
}
return a;
}

View File

@@ -14,6 +14,7 @@
#include <osg/Matrix>
#include <osg/Geometry>
#include "IveVersion.h"
#include "DataTypeSize.h"
#include "Exception.h"
@@ -84,6 +85,8 @@ private:
StateAttributeMap _stateAttributeMap;
DrawableMap _drawableMap;
NodeMap _nodeMap;
int _byteswap ;
};
}

View File

@@ -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(){}

View File

@@ -13,6 +13,7 @@
#include <osg/Matrix>
#include <osg/Geometry>
#include "IveVersion.h"
#include "DataTypeSize.h"
#include "Exception.h"

View File

@@ -10,7 +10,4 @@
#define LONGSIZE 4
#define DOUBLESIZE 8
// NOTE: Update anytime the binary format changes
#define VERSION 0x01
#endif

View File

@@ -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