Torque3D Documentation / _generateds / resizeStream.cpp

resizeStream.cpp

Engine/source/core/resizeStream.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "core/resizeStream.h"
 25
 26ResizeFilterStream::ResizeFilterStream()
 27 : m_pStream(NULL),
 28   m_startOffset(0),
 29   m_streamLen(0),
 30   m_currOffset(0),
 31   m_lastBytesRead(0)
 32{
 33   //
 34}
 35
 36ResizeFilterStream::~ResizeFilterStream()
 37{
 38   detachStream();
 39}
 40
 41bool ResizeFilterStream::attachStream(Stream* io_pSlaveStream)
 42{
 43   AssertFatal(io_pSlaveStream != NULL, "NULL Slave stream?");
 44
 45   m_pStream     = io_pSlaveStream;
 46   m_startOffset = 0;
 47   m_streamLen   = m_pStream->getStreamSize();
 48   m_currOffset  = 0;
 49   setStatus(EOS);
 50   return true;
 51}
 52
 53void ResizeFilterStream::detachStream()
 54{
 55   m_pStream     = NULL;
 56   m_startOffset = 0;
 57   m_streamLen   = 0;
 58   m_currOffset  = 0;
 59   setStatus(Closed);
 60}
 61
 62Stream* ResizeFilterStream::getStream()
 63{
 64   return m_pStream;
 65}
 66
 67bool ResizeFilterStream::setStreamOffset(const U32 in_startOffset, const U32 in_streamLen)
 68{
 69   AssertFatal(m_pStream != NULL, "stream not attached!");
 70   if (m_pStream == NULL)
 71      return false;
 72
 73   U32 start  = in_startOffset;
 74   U32 end    = in_startOffset + in_streamLen;
 75   U32 actual = m_pStream->getStreamSize();
 76
 77   if (start >= actual || end > actual)
 78      return false;
 79
 80   m_startOffset = start;
 81   m_streamLen   = in_streamLen;
 82   m_currOffset  = 0;
 83
 84   if (m_streamLen != 0)
 85      setStatus(Ok);
 86   else
 87      setStatus(EOS);
 88
 89   return true;
 90}
 91
 92U32 ResizeFilterStream::getPosition() const
 93{
 94   AssertFatal(m_pStream != NULL, "Error, stream not attached");
 95   if (m_pStream == NULL)
 96      return 0;
 97
 98   return m_currOffset;
 99}
100
101bool ResizeFilterStream::setPosition(const U32 in_newPosition)
102{
103   AssertFatal(m_pStream != NULL, "Error, stream not attached");
104   if (m_pStream == NULL)
105      return false;
106
107   if (in_newPosition < m_streamLen) {
108      m_currOffset = in_newPosition;
109      return true;
110   } else {
111      m_currOffset = m_streamLen;
112      return false;
113   }
114}
115
116U32 ResizeFilterStream::getStreamSize()
117{
118   AssertFatal(m_pStream != NULL, "Error, stream not attached");
119
120   return m_streamLen;
121}
122
123bool ResizeFilterStream::_read(const U32 in_numBytes, void* out_pBuffer)
124{
125   AssertFatal(m_pStream != NULL, "Error, stream not attached");
126   m_lastBytesRead = 0;
127
128   if (in_numBytes == 0)
129      return true;
130
131   AssertFatal(out_pBuffer != NULL, "Invalid output buffer");
132   if (getStatus() == Closed) {
133      AssertFatal(false, "Attempted read from closed stream");
134      return false;
135   }
136
137   U32 savePosition = m_pStream->getPosition();
138   if (m_pStream->setPosition(m_startOffset + m_currOffset) == false)
139      return false;
140
141   U32 actualSize = in_numBytes;
142   U32 position   = m_startOffset + m_currOffset;
143   if (in_numBytes + position > m_startOffset + m_streamLen)
144      actualSize = m_streamLen - (position - m_startOffset);
145
146   if (actualSize == 0) {
147      setStatus(EOS);
148      return false;
149   }
150
151   bool success = m_pStream->read(actualSize, out_pBuffer);
152   m_currOffset += actualSize;
153   m_lastBytesRead = actualSize;
154
155   setStatus(m_pStream->getStatus());
156
157   m_pStream->setPosition(savePosition);
158   return success;
159}
160
161