datainputstream.cpp

来自「最新osg包」· C++ 代码 · 共 1,864 行 · 第 1/4 页

CPP
1,864
字号
/********************************************************************** * *    FILE:            DataInputStream.cpp * *    DESCRIPTION:    Implements methods to read simple datatypes from an *                    input stream. * *    CREATED BY:        Rune Schmidt Jensen * *    HISTORY:        Created 11.03.2003 *                    Updated for texture1D by Don Burns, 27.1.2004 *                      Updated for light model - Stan Blinov at 25 august 7512 from World Creation (7.09.2004) * * *    Copyright 2003 VR-C **********************************************************************/#include "DataInputStream.h"#include "StateSet.h"#include "AlphaFunc.h"#include "BlendColor.h"#include "Stencil.h"#include "BlendFunc.h"#include "BlendEquation.h"#include "Depth.h"#include "Material.h"#include "CullFace.h"#include "ColorMask.h"#include "ClipPlane.h"#include "PolygonOffset.h"#include "PolygonMode.h"#include "ShadeModel.h"#include "Point.h"#include "LineWidth.h"#include "LineStipple.h"#include "Texture1D.h"#include "Texture2D.h"#include "Texture3D.h"#include "TextureCubeMap.h"#include "TextureRectangle.h"#include "TexEnv.h"#include "TexEnvCombine.h"#include "TexGen.h"#include "TexMat.h"#include "FragmentProgram.h"#include "VertexProgram.h"#include "LightModel.h"#include "ProxyNode.h"#include "FrontFace.h"#include "Program.h"#include "Viewport.h"#include "Scissor.h"#include "Image.h"#include "ImageSequence.h"#include "PointSprite.h"#include "Multisample.h"#include "Fog.h"#include "Light.h"#include "Group.h"#include "MatrixTransform.h"#include "Camera.h"#include "CameraView.h"#include "Geode.h"#include "LightSource.h"#include "TexGenNode.h"#include "ClipNode.h"#include "Billboard.h"#include "Sequence.h"#include "LOD.h"#include "PagedLOD.h"#include "PositionAttitudeTransform.h"#include "AutoTransform.h"#include "DOFTransform.h"#include "Transform.h"#include "Switch.h"#include "OccluderNode.h"#include "OcclusionQueryNode.h"#include "Impostor.h"#include "CoordinateSystemNode.h"#include "Uniform.h"#include "Shader.h"#include "LightPointNode.h"#include "MultiSwitch.h"#include "VisibilityGroup.h"#include "MultiTextureControl.h"#include "ShapeAttributeList.h"#include "Effect.h"#include "AnisotropicLighting.h"#include "BumpMapping.h"#include "Cartoon.h"#include "Scribe.h"#include "SpecularHighlights.h"#include "Geometry.h"#include "ShapeDrawable.h"#include "Shape.h"#include "Text.h"#include "TerrainTile.h"#include "Locator.h"#include "ImageLayer.h"#include "HeightFieldLayer.h"#include "CompositeLayer.h"#include "SwitchLayer.h"#include "FadeText.h"#include "Text3D.h"#include <osg/Endian>#include <osg/Notify>#include <osg/io_utils>#include <osgDB/ReadFile>#include <osgDB/FileNameUtils>#include <stdio.h>#include <sstream>using namespace ive;using namespace std;DataInputStream::DataInputStream(std::istream* istream, const osgDB::ReaderWriter::Options* options){    unsigned int endianType ;    _loadExternalReferenceFiles = false;    _verboseOutput = false;    _istream = istream;    _owns_istream = false;    _peeking = false;    _peekValue = 0;    _byteswap = 0;    _options = options;    if (_options.get())    {        setLoadExternalReferenceFiles(_options->getOptionString().find("noLoadExternalReferenceFiles")==std::string::npos);        osg::notify(osg::DEBUG_INFO) << "ive::DataInputStream.setLoadExternalReferenceFiles()=" << getLoadExternalReferenceFiles() << std::endl;    }    if(!istream){        throw Exception("DataInputStream::DataInputStream(): null pointer exception in argument.");    }    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.") ;      }      osg::notify(osg::INFO)<<"DataInputStream::DataInputStream: Reading a byteswapped file" << std::endl ;      _byteswap = 1 ;   }    _version = readUInt();    // 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.");    }        if (_version>=VERSION_0033)    {        int compressionLevel = readInt();                if (compressionLevel>0)        {            osg::notify(osg::INFO)<<"compressed ive stream"<<std::endl;                        unsigned int maxSize = readUInt();                        std::string data;            data.reserve(maxSize);                        if (!uncompress(*istream, data))            {                throw Exception("Error in uncompressing .ive");            }                        _istream = new std::stringstream(data);            _owns_istream = true;        }        else        {            osg::notify(osg::INFO)<<"uncompressed ive stream"<<std::endl;        }    }}DataInputStream::~DataInputStream(){    if (_owns_istream) delete _istream;}#ifdef USE_ZLIB#include <zlib.h>bool DataInputStream::uncompress(std::istream& fin, std::string& destination) const{    //#define CHUNK 16384    #define CHUNK 32768    int ret;    unsigned have;    z_stream strm;    unsigned char in[CHUNK];    unsigned char out[CHUNK];        /* allocate inflate state */    strm.zalloc = Z_NULL;    strm.zfree = Z_NULL;    strm.opaque = Z_NULL;    strm.avail_in = 0;    strm.next_in = Z_NULL;    ret = inflateInit2(&strm,                       15 + 32 // autodected zlib or gzip header                       );    if (ret != Z_OK)    {        osg::notify(osg::INFO)<<"failed to init"<<std::endl;        return ret;    }        /* decompress until deflate stream ends or end of file */    do {        //strm.avail_in = fin.readsome((char*)in, CHUNK);        fin.read((char *)in, CHUNK);        strm.avail_in = fin.gcount();                if (strm.avail_in == 0)        {            break;        }        strm.next_in = in;        /* run inflate() on input until output buffer not full */        do {            strm.avail_out = CHUNK;            strm.next_out = out;            ret = inflate(&strm, Z_NO_FLUSH);            switch (ret) {            case Z_NEED_DICT:            case Z_DATA_ERROR:            case Z_MEM_ERROR:                (void)inflateEnd(&strm);                return false;            }            have = CHUNK - strm.avail_out;            destination.append((char*)out, have);                    } while (strm.avail_out == 0);        /* done when inflate() says it's done */    } while (ret != Z_STREAM_END);    /* clean up and return */    (void)inflateEnd(&strm);        return ret == Z_STREAM_END ? true : false;}#elsebool DataInputStream::uncompress(std::istream& fin, std::string& destination) const{    return false;}#endifbool DataInputStream::readBool(){    char c;    _istream->read(&c, CHARSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readBool(): Failed to read boolean value.");    if (_verboseOutput) std::cout<<"read/writeBool() ["<<(int)c<<"]"<<std::endl;    return c!=0;}char DataInputStream::readChar(){    char c;    _istream->read(&c, CHARSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readChar(): Failed to read char value.");    if (_verboseOutput) std::cout<<"read/writeChar() ["<<(int)c<<"]"<<std::endl;    return c;}unsigned char DataInputStream::readUChar(){    unsigned char c;    _istream->read((char*)&c, CHARSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readUChar(): Failed to read unsigned char value.");    if (_verboseOutput) std::cout<<"read/writeUChar() ["<<(int)c<<"]"<<std::endl;    return c;}unsigned short DataInputStream::readUShort(){    unsigned short s;    _istream->read((char*)&s, SHORTSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readUShort(): Failed to read unsigned short value.");    if (_verboseOutput) std::cout<<"read/writeUShort() ["<<s<<"]"<<std::endl;    if (_byteswap) osg::swapBytes((char *)&s,SHORTSIZE);    return s;}unsigned int DataInputStream::readUInt(){    unsigned int s;    _istream->read((char*)&s, INTSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readUInt(): Failed to read unsigned int value.");    if (_byteswap) osg::swapBytes((char *)&s,INTSIZE) ;    if (_verboseOutput) std::cout<<"read/writeUInt() ["<<s<<"]"<<std::endl;    return s;}int DataInputStream::readInt(){    if(_peeking){        _peeking = false;        return _peekValue;    }    int i;    _istream->read((char*)&i, INTSIZE);    // comment out for time being as this check seems to eroneously cause a    // premature exit when reading .ive files under OSX!#?:!    // Robet Osfield, September 12th 2003.    // if (_istream->rdstate() & _istream->failbit)    //    throw Exception("DataInputStream::readInt(): Failed to read int value.");    if (_byteswap) osg::swapBytes((char *)&i,INTSIZE) ;    if (_verboseOutput) std::cout<<"read/writeInt() ["<<i<<"]"<<std::endl;    return i;}/** * Read an integer from the stream, but * save it such that the next readInt call will * return the same integer. */int DataInputStream::peekInt(){    if(_peeking){        return _peekValue;    }    _peekValue  = readInt();    _peeking = true;    return _peekValue;}float DataInputStream::readFloat(){    float f;    _istream->read((char*)&f, FLOATSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readFloat(): Failed to read float value.");    if (_byteswap) osg::swapBytes((char *)&f,FLOATSIZE) ;    if (_verboseOutput) std::cout<<"read/writeFloat() ["<<f<<"]"<<std::endl;    return f;}long DataInputStream::readLong(){    long l;    _istream->read((char*)&l, LONGSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readLong(): Failed to read long value.");    if (_byteswap) osg::swapBytes((char *)&l,LONGSIZE) ;    if (_verboseOutput) std::cout<<"read/writeLong() ["<<l<<"]"<<std::endl;    return l;}unsigned long DataInputStream::readULong(){    unsigned long l;    _istream->read((char*)&l, LONGSIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readULong(): Failed to read unsigned long value.");    if (_byteswap) osg::swapBytes((char *)&l,LONGSIZE) ;    if (_verboseOutput) std::cout<<"read/writeULong() ["<<l<<"]"<<std::endl;    return l;}double DataInputStream::readDouble(){    double d;    _istream->read((char*)&d, DOUBLESIZE);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readDouble(): Failed to read double value.");    if (_byteswap) osg::swapBytes((char *)&d,DOUBLESIZE) ;    if (_verboseOutput) std::cout<<"read/writeDouble() ["<<d<<"]"<<std::endl;    return d;}std::string DataInputStream::readString(){    std::string s;    int size = readInt();    if (size != 0)    {        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 (_verboseOutput) std::cout<<"read/writeString() ["<<s<<"]"<<std::endl;    }    return s;}void DataInputStream::readCharArray(char* data, int size){    _istream->read(data, size);    if (_istream->rdstate() & _istream->failbit)        throw Exception("DataInputStream::readCharArray(): Failed to read char value.");    if (_verboseOutput) std::cout<<"read/writeCharArray() ["<<data<<"]"<<std::endl;}osg::Vec2 DataInputStream::readVec2(){    osg::Vec2 v;    v.x()=readFloat();    v.y()=readFloat();    if (_verboseOutput) std::cout<<"read/writeVec2() ["<<v<<"]"<<std::endl;    return v;}osg::Vec3 DataInputStream::readVec3(){    osg::Vec3 v;    v.x()=readFloat();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?