• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/io/MemoryStream.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/types.h>
00018 #include <spl/io/MemoryStream.h>
00019 #include <spl/text/StringBuffer.h>
00020 
00021 #ifdef _WINDOWS
00022 #include <spl/configwin32.h>
00023 #else
00024 #include <spl/autoconf/config.h>
00025 #endif
00026 
00027 MemoryStream::MemoryStream()
00028 : m_buf(), m_ptr(0)
00029 {
00030 }
00031 
00032 MemoryStream::~MemoryStream()
00033 {
00034 }
00035 
00036 void MemoryStream::Close()
00037 {
00038         m_buf.Clear();
00039         m_ptr = 0;
00040 }
00041 
00042 void MemoryStream::Flush()
00043 {
00044         m_buf.Clear();
00045         m_ptr = 0;
00046 }
00047 
00048 int MemoryStream::Read(Array<byte>& buffer, const int offset, int count)
00049 {
00050         if ( m_buf.Count() == 0 )
00051         {
00052                 return 0;
00053         }
00054         if ( m_ptr+count > m_buf.Count() )
00055         {
00056                 count = m_buf.Count() - m_ptr;
00057         }
00058         int bufPos = offset;
00059         int pos = count;
00060         while ( pos-- > 0 )
00061         {
00062                 buffer[bufPos++] = m_buf.ElementAt(m_ptr);
00063                 m_buf.RemoveAt(m_ptr);
00064         }
00065         return count;
00066 }
00067 
00068 int MemoryStream::ReadByte()
00069 {
00070         if ( m_ptr < m_buf.Count() )
00071         {
00072                 m_buf.ValidateMem();
00073                 return m_buf.RemoveAt(m_ptr);
00074         }
00075         return -1;
00076 }
00077 
00078 long MemoryStream::Seek(const long offset, const SeekOrigin origin)
00079 {
00080         if ( IStream::SEEK_Begin == origin )
00081         {
00082                 m_ptr = (offset > m_buf.Count()) ? m_buf.Count() : (int)offset;
00083         }
00084         else if ( IStream::SEEK_End == origin )
00085         {
00086                 m_ptr = (offset > m_buf.Count()) ? 0 : m_buf.Count() - (int)offset;
00087         }
00088         else
00089         {
00090                 m_ptr += offset;
00091                 if ( m_ptr < 0 )
00092                 {
00093                         m_ptr = 0;
00094                 }
00095                 else if ( m_ptr > m_buf.Count() )
00096                 {
00097                         m_ptr = m_buf.Count();
00098                 }
00099         }
00100         return m_ptr;
00101 }
00102 
00103 void MemoryStream::Write(const Array<byte>& buffer, const int offset, const int count)
00104 {
00105         for ( int x = 0; x < count; x++)
00106         {
00107                 m_buf.Add( buffer[x + offset] );
00108         }
00109 }
00110 
00111 void MemoryStream::WriteByte(byte value)
00112 {
00113         m_buf.Add( value );
00114 }
00115 
00116 bool MemoryStream::CanRead() const
00117 {
00118         return true;
00119 }
00120 
00121 bool MemoryStream::CanSeek() const
00122 {
00123         return true;
00124 }
00125 
00126 bool MemoryStream::CanWrite() const
00127 {
00128         return true;
00129 }
00130 
00131 long MemoryStream::Length() const
00132 {
00133         return m_buf.Count();
00134 }
00135 
00136 long MemoryStream::Position() const
00137 {
00138         return m_ptr;
00139 }
00140 
00141 StringPtr MemoryStream::ToString()
00142 {
00143         StringBuffer buf;
00144         while ( Length() > 0 )
00145         {
00146                 buf.Append((char)ReadByte());
00147         }
00148         return buf.ToString();
00149 }
00150 
00151 #ifdef DEBUG
00152 void MemoryStream::ValidateMem() const
00153 {
00154         m_buf.ValidateMem();
00155 }
00156 
00157 void MemoryStream::CheckMem() const
00158 {
00159         m_buf.CheckMem();
00160 }
00161 #endif