Esempio n. 1
0
 //---------------------------------------------------------------------
 void DeflateStream::seek( size_t pos )
 {
     if (!mIsCompressedValid)
     {
         mCompressedStream->seek(pos);
         return;
     }
     if (getAccessMode() & WRITE)
     {
         mTmpWriteStream->seek(pos);
     }
     else
     {
         if (pos == 0)
         {
             mCurrentPos = 0;
             mZStream->next_in = mTmp;
             mCompressedStream->seek(0);
             mZStream->avail_in = static_cast<uint>(mCompressedStream->read(mTmp, getAvailInForSinglePass()));
             inflateReset(mZStream);
         }
         else 
         {
             skip(pos - tell());
         }
     }       
 }
Esempio n. 2
0
    //---------------------------------------------------------------------
	void DeflateStream::seek( size_t pos )
	{
		if (!mIsCompressedValid)
		{
			mCompressedStream->seek(pos);
			return;
		}
		if (getAccessMode() & WRITE)
		{
			mTmpWriteStream->seek(pos);
		}
		else
		{
			if (pos == 0)
			{
				mCurrentPos = 0;
				mZStream->next_in = mTmp;
				mCompressedStream->seek(0);
				mZStream->avail_in = mCompressedStream->read(mTmp, OGRE_DEFLATE_TMP_SIZE);			
				inflateReset(mZStream);
			}
			else 
			{
				skip(pos - tell());
			}
		}		
	}
    //---------------------------------------------------------------------
	void DeflateStream::init()
	{
		mpZStream = OGRE_ALLOC_T(z_stream, 1, MEMCATEGORY_GENERAL);
		mpZStream->zalloc = OgreZalloc;
		mpZStream->zfree = OgreZfree;
		
		if (getAccessMode() == READ)
		{
			mpTmp = (unsigned char*)OGRE_MALLOC(OGRE_DEFLATE_TMP_SIZE, MEMCATEGORY_GENERAL);
			size_t restorePoint = mCompressedStream->tell();
			// read early chunk
			mpZStream->next_in = mpTmp;
			mpZStream->avail_in = mCompressedStream->read(mpTmp, OGRE_DEFLATE_TMP_SIZE);
			
			if (inflateInit(mpZStream) != Z_OK)
			{
				mIsCompressedValid = false;
			}
			else
				mIsCompressedValid = true;
			
			if (mIsCompressedValid)
			{
				// in fact, inflateInit on some implementations doesn't try to read
				// anything. We need to at least read something to test
				Bytef testOut[4];
				size_t savedIn = mpZStream->avail_in;
				mpZStream->avail_out = 4;
				mpZStream->next_out = testOut;
				if (inflate(mpZStream, Z_SYNC_FLUSH) != Z_OK)
					mIsCompressedValid = false;
				// restore for reading
				mpZStream->avail_in = savedIn;
				mpZStream->next_in = mpTmp;

				inflateReset(mpZStream);
			}

			if (!mIsCompressedValid)
			{
				// Not compressed data!
				// Fail gracefully, fall back on reading the underlying stream direct
				destroy();
				mCompressedStream->seek(restorePoint);
			}				
		}
		else 
		{
			// Write to temp file
			char tmpname[L_tmpnam];
			tmpnam(tmpname);
			mTempFileName = tmpname;
			std::fstream *f = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)();
			f->open(tmpname, std::ios::binary | std::ios::out);
			mTmpWriteStream = DataStreamPtr(OGRE_NEW FileStreamDataStream(f));
			
		}

	}
Esempio n. 4
0
 //---------------------------------------------------------------------
 size_t DeflateStream::write(const void* buf, size_t count)
 {
     if ((getAccessMode() & WRITE) == 0)
         OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
                     "Not a writable stream", "DeflateStream::write");
     
     return mTmpWriteStream->write(buf, count);
 }
Esempio n. 5
0
 //---------------------------------------------------------------------
 void DeflateStream::close(void)
 {
     if (getAccessMode() & WRITE)
     {
         compressFinal();
     }
     
     // don't close underlying compressed stream in case used for something else
 }
Esempio n. 6
0
    //---------------------------------------------------------------------
    void DeflateStream::destroy()
    {
        if (getAccessMode() == READ)
            inflateEnd(mZStream);

        OGRE_FREE(mZStream, MEMCATEGORY_GENERAL);
        mZStream = 0;
        OGRE_FREE(mTmp, MEMCATEGORY_GENERAL);
        mTmp = 0;
    }
Esempio n. 7
0
 //---------------------------------------------------------------------
 bool DeflateStream::eof(void) const
 {
     if (getAccessMode() & WRITE)
         return mTmpWriteStream->eof();
     else 
     {
         if (!mIsCompressedValid)
             return mCompressedStream->eof();
         else
             return mCompressedStream->eof() && mZStream->avail_in == 0;
     }
 }
Esempio n. 8
0
    //---------------------------------------------------------------------
    size_t DeflateStream::tell(void) const
    {
        if (!mIsCompressedValid)
        {
            return mCompressedStream->tell();
        }
        else if(getAccessMode() & WRITE) 
        {
            return mTmpWriteStream->tell();
        }
        else
        {
            return mCurrentPos;
        }

    }
Esempio n. 9
0
 //---------------------------------------------------------------------
 void DeflateStream::skip(long count)
 {
     if (!mIsCompressedValid)
     {
         mCompressedStream->skip(count);
         return;
     }
     
     if (getAccessMode() & WRITE)
     {
         mTmpWriteStream->skip(count);
     }
     else 
     {
         if (count > 0)
         {
             if (!mReadCache.ff(count))
             {
                 OGRE_EXCEPT(Exception::ERR_INVALID_STATE, 
                             "You can only skip within the cache range in a deflate stream.",
                             "DeflateStream::skip");
             }
         }
         else if (count < 0)
         {
             if (!mReadCache.rewind((size_t)(-count)))
             {
                 OGRE_EXCEPT(Exception::ERR_INVALID_STATE, 
                             "You can only skip within the cache range in a deflate stream.",
                             "DeflateStream::skip");
             }
         }
     }       
     mCurrentPos = static_cast<size_t>(static_cast<long>(mCurrentPos) + count);
     
     
 }
Esempio n. 10
0
    //---------------------------------------------------------------------
    void DeflateStream::init()
    {
        mZStream = OGRE_ALLOC_T(z_stream, 1, MEMCATEGORY_GENERAL);
        mZStream->zalloc = OgreZalloc;
        mZStream->zfree = OgreZfree;
        
        if (getAccessMode() == READ)
        {
            mTmp = (unsigned char*)OGRE_MALLOC(OGRE_DEFLATE_TMP_SIZE, MEMCATEGORY_GENERAL);
            size_t restorePoint = mCompressedStream->tell();
            // read early chunk
            mZStream->next_in = mTmp;
            mZStream->avail_in = static_cast<uint>(mCompressedStream->read(mTmp, getAvailInForSinglePass()));
            
            if (inflateInit(mZStream) != Z_OK)
            {
                mIsCompressedValid = false;
            }
            else
                mIsCompressedValid = true;
            
            if (mIsCompressedValid)
            {
                // in fact, inflateInit on some implementations doesn't try to read
                // anything. We need to at least read something to test
                Bytef testOut[4];
                size_t savedIn = mZStream->avail_in;
                mZStream->avail_out = 4;
                mZStream->next_out = testOut;
                if (inflate(mZStream, Z_SYNC_FLUSH) != Z_OK)
                    mIsCompressedValid = false;
                // restore for reading
                mZStream->avail_in = static_cast<uint>(savedIn);
                mZStream->next_in = mTmp;

                inflateReset(mZStream);
            }

            if (!mIsCompressedValid)
            {
                // Not compressed data!
                // Fail gracefully, fall back on reading the underlying stream direct
                destroy();
                mCompressedStream->seek(restorePoint);
            }               
        }
        else 
        {
            if(mTempFileName.empty())
            {
                // Write to temp file
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT
                char* tmpname = _tempnam(".", "ogre");
                if (!tmpname)
                {
                    // Having no file name here will cause various problems later.
                    OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Temporary file name generation failed.", "DeflateStream::init");
                }
                else
                {
                    mTempFileName = tmpname;
                    free(tmpname);
                }
#elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
                mTempFileName = macTempFileName();
#else
                char tmpname[] = "/tmp/ogreXXXXXX";
                if (mkstemp(tmpname) == -1)
                    OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Temporary file name generation failed.", "DeflateStream::init");

                mTempFileName = tmpname;
#endif
            }

            std::fstream *f = OGRE_NEW_T(std::fstream, MEMCATEGORY_GENERAL)();
            f->open(mTempFileName.c_str(), std::ios::binary | std::ios::out);
            mTmpWriteStream = DataStreamPtr(OGRE_NEW FileStreamDataStream(f));
            
        }

    }
Esempio n. 11
0
    //---------------------------------------------------------------------
    size_t DeflateStream::read(void* buf, size_t count)
    {
        if (!mIsCompressedValid)
        {
            return mCompressedStream->read(buf, count);
        }
        
        if (getAccessMode() & WRITE)
        {
            return mTmpWriteStream->read(buf, count);
        }
        else 
        {

            size_t restorePoint = mCompressedStream->tell();
            // read from cache first
            size_t cachereads = mReadCache.read(buf, count);
            
            size_t newReadUncompressed = 0;

            if (cachereads < count)
            {
                mZStream->avail_out = static_cast<uint>(count - cachereads);
                mZStream->next_out = (Bytef*)buf + cachereads;
                
                while (mZStream->avail_out)
                {
                    // Pull next chunk of compressed data from the underlying stream
                    if (!mZStream->avail_in && !mCompressedStream->eof())
                    {
                        mZStream->avail_in = static_cast<uint>(mCompressedStream->read(mTmp, getAvailInForSinglePass()));
                        mZStream->next_in = mTmp;
                    }
                    
                    if (mZStream->avail_in)
                    {
                        int availpre = mZStream->avail_out;
                        int status = inflate(mZStream, Z_SYNC_FLUSH);
                        size_t readUncompressed = availpre - mZStream->avail_out;
                        newReadUncompressed += readUncompressed;
                        if (status != Z_OK)
                        {
                            // End of data, or error
                            if (status != Z_STREAM_END)
                            {
                                mCompressedStream->seek(restorePoint);
                                OGRE_EXCEPT(Exception::ERR_INVALID_STATE, 
                                            "Error in compressed stream",
                                            "DeflateStrea::read");
                            }
                            else 
                            {
                                // back up the stream so that it can be used from the end onwards                                                   
                                long unusedCompressed = mZStream->avail_in;
                                mCompressedStream->skip(-unusedCompressed);
                            }

                            break;
                        }
                    }
                }
            }
            
            // Cache the last bytes read
            mReadCache.cacheData((char*)buf + cachereads, newReadUncompressed);
            
            mCurrentPos += newReadUncompressed + cachereads;
            
            return newReadUncompressed + cachereads;
        }
    }
Esempio n. 12
0
	void Property::operator=(const Quaternion& value)
	{
		mada_assert(getValueType() == QuaternionType);
		mada_assert(getAccessMode() == ReadWrite);
		m_value = value;
	}
Esempio n. 13
0
	void Property::operator=(const Vector3& value)
	{
		mada_assert(getValueType() == Vector3Type);
		mada_assert(getAccessMode() == ReadWrite);
		m_value = value;
	}
Esempio n. 14
0
	void Property::operator=(const String& value)
	{
		mada_assert(getValueType() == StringType);
		mada_assert(getAccessMode() == ReadWrite);
		m_value = value;
	}
Esempio n. 15
0
	void Property::operator=(float value)
	{
		mada_assert(getValueType() == FloatType);
		mada_assert(getAccessMode() == ReadWrite);
		m_value = value;
	}
Esempio n. 16
0
	void Property::operator=(bool value)
	{
		mada_assert(getValueType() == BoolType);
		mada_assert(getAccessMode() == ReadWrite);
		m_value = value;
	}