#include <ctype.h>
#include <wx/stream.h>
#include <wx/datstrm.h>
+#include <wx/objstrm.h>
#ifdef __BORLANDC__
#pragma hdrstop
wxStreamBuffer::~wxStreamBuffer()
{
+ wxDELETEA(m_buffer_start);
}
void wxStreamBuffer::WriteBack(char c)
// Assume that if we write "back" we have read a few bytes: so we have some
// space.
+ if (m_buffer_pos == m_buffer_start)
+ return;
m_buffer_pos--;
*m_buffer_pos = c;
void wxStreamBuffer::SetBufferIO(char *buffer_start, char *buffer_end)
{
- m_buffer_pos = m_buffer_start = buffer_start;
- m_buffer_end = buffer_end;
+ size_t ret;
+
+ m_buffer_start = buffer_start;
+ m_buffer_end = buffer_end;
m_buffer_size = m_buffer_end-m_buffer_start;
+
+ if (m_istream) {
+ ret = m_istream->DoRead(m_buffer_start, m_buffer_size);
+ m_buffer_end = m_buffer_start + ret;
+ }
+ m_buffer_pos = m_buffer_start;
}
void wxStreamBuffer::SetBufferIO(size_t bufsize)
{
- if (m_buffer_start)
- delete[] m_buffer_start;
+ char *b_start;
+
+ wxDELETE(m_buffer_start);
if (!bufsize) {
m_buffer_start = NULL;
m_buffer_end = NULL;
m_buffer_pos = NULL;
m_buffer_size = 0;
+ return;
}
- m_buffer_start = new char[bufsize];
- m_buffer_end = m_buffer_start + bufsize;
- if (m_istream)
- m_buffer_pos = m_buffer_end;
- else
- m_buffer_pos = m_buffer_start;
- m_buffer_size = bufsize;
+ b_start = new char[bufsize];
+
+ SetBufferIO(b_start, b_start + bufsize);
}
void wxStreamBuffer::ResetBuffer()
read_ret = m_istream->DoRead(m_buffer_start, m_buffer_size);
- // Read failed
+ // Read failed: EOF
if (read_ret == 0) {
m_istream->m_lastread = orig_size-size;
+ m_istream->m_eof = TRUE;
m_buffer_pos = m_buffer_end = m_buffer_start;
return;
} else {
{
m_i_destroybuf = TRUE;
m_i_streambuf = new wxStreamBuffer(*this);
+ m_eof = FALSE;
+ m_lastread = 0;
}
wxInputStream::wxInputStream(wxStreamBuffer *buffer)
{
m_i_destroybuf = FALSE;
m_i_streambuf = buffer;
+ m_eof = FALSE;
+ m_lastread = 0;
}
wxInputStream::~wxInputStream()
return *this;
}
+wxInputStream& wxInputStream::operator>>(int& i)
+{
+ long l;
+
+ *this >> l;
+ i = (short)l;
+ return *this;
+}
+
wxInputStream& wxInputStream::operator>>(long& i)
{
/* I only implemented a simple integer parser */
}
if (c == '.') {
- float f_multiplicator = 0.1;
+ float f_multiplicator = (float) 0.1;
c = GetC();
while (isdigit(c)) {
return *this;
}
+#if USE_SERIAL
+wxInputStream& wxInputStream::operator>>(wxObject *& obj)
+{
+ wxObjectInputStream obj_s(*this);
+ obj = obj_s.LoadObject();
+ return *this;
+}
+#endif
+
off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode)
{
- off_t ret_off;
+ off_t ret_off, diff, last_access;
+
+ last_access = m_i_streambuf->GetLastAccess();
switch (mode) {
case wxFromStart:
- if ( (unsigned)abs (DoTellInput()-pos) > m_i_streambuf->GetLastAccess() ) {
+ diff = DoTellInput() - pos;
+ if ( diff < 0 || diff > last_access ) {
ret_off = DoSeekInput(pos, wxFromStart);
m_i_streambuf->ResetBuffer();
return ret_off;
} else {
- m_i_streambuf->SetIntPosition(DoTellInput() - pos);
+ m_i_streambuf->SetIntPosition(last_access - diff);
return pos;
}
case wxFromCurrent:
- if ( ((unsigned)pos > m_i_streambuf->GetLastAccess()) || (pos < 0) ) {
+ diff = pos + m_i_streambuf->GetIntPosition();
+
+ if ( (diff > last_access) || (diff < 0) ) {
ret_off = DoSeekInput(pos, wxFromCurrent);
m_i_streambuf->ResetBuffer();
return ret_off;
} else {
- m_i_streambuf->SetIntPosition(pos);
+ m_i_streambuf->SetIntPosition(diff);
return pos;
}
case wxFromEnd:
{
m_o_destroybuf = TRUE;
m_o_streambuf = new wxStreamBuffer(*this);
+ m_bad = FALSE;
+ m_lastwrite = 0;
}
wxOutputStream::wxOutputStream(wxStreamBuffer *buffer)
{
m_o_destroybuf = FALSE;
m_o_streambuf = buffer;
+ m_bad = FALSE;
+ m_lastwrite = 0;
}
wxOutputStream::~wxOutputStream()
return Write(strfloat, strfloat.Len());
}
+#if USE_SERIAL
+wxOutputStream& wxOutputStream::operator<<(wxObject& obj)
+{
+ wxObjectOutputStream obj_s(*this);
+ obj_s.SaveObject(obj);
+ return *this;
+}
+#endif
+
+// ----------------------------------------------------------------------------
+// wxStream
+// ----------------------------------------------------------------------------
+
+wxStream::wxStream()
+ : wxInputStream(), wxOutputStream()
+{
+}
+
// ----------------------------------------------------------------------------
// wxFilterInputStream
// ----------------------------------------------------------------------------
+wxFilterInputStream::wxFilterInputStream()
+ : wxInputStream(NULL)
+{
+}
+
wxFilterInputStream::wxFilterInputStream(wxInputStream& stream)
: wxInputStream(NULL)
{
m_parent_i_stream = &stream;
+ wxDELETE(m_i_streambuf); // In case m_i_streambuf has been initialized.
+ m_i_destroybuf = FALSE;
m_i_streambuf = stream.InputStreamBuffer();
}
return m_parent_i_stream->TellI();
}
-
// ----------------------------------------------------------------------------
// wxFilterOutputStream
// ----------------------------------------------------------------------------
+wxFilterOutputStream::wxFilterOutputStream()
+ : wxOutputStream(NULL)
+{
+}
+
wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream)
: wxOutputStream(NULL)
{
m_parent_o_stream = &stream;
+ wxDELETE(m_o_streambuf); // In case m_o_streambuf has been initialized.
+ m_o_destroybuf = FALSE;
m_o_streambuf = stream.OutputStreamBuffer();
}
return m_parent_o_stream->TellO();
}
+// ----------------------------------------------------------------------------
+// wxFilterStream
+// ----------------------------------------------------------------------------
+
+wxFilterStream::wxFilterStream()
+{
+}
+
+wxFilterStream::wxFilterStream(wxStream& stream)
+ : wxFilterInputStream(stream), wxFilterOutputStream(stream)
+{
+}
+
// ----------------------------------------------------------------------------
// Some IOManip function
// ----------------------------------------------------------------------------