From 3d4c6a214ac5b00dbf5e314e369471135db81c0d Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Sun, 12 Jul 1998 15:16:09 +0000 Subject: [PATCH] Added wxStream but I haven't tested them. Modified wxDataStream. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@234 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/datstrm.h | 22 +++---- src/Makefile.in | 3 + src/common/datstrm.cpp | 72 ++++++++++------------ src/common/fstream.cpp | 105 ++++++++++++++++++++++++++++++++ src/common/mstream.cpp | 133 +++++++++++++++++++++++++++++++++++++++++ src/common/stream.cpp | 73 ++++++++++++++++++++++ src/msw/makefile.b32 | 9 +++ src/msw/makefile.dos | 3 + src/msw/makefile.g95 | 3 + src/msw/makefile.nt | 3 + 10 files changed, 375 insertions(+), 51 deletions(-) create mode 100644 src/common/fstream.cpp create mode 100644 src/common/mstream.cpp create mode 100644 src/common/stream.cpp diff --git a/include/wx/datstrm.h b/include/wx/datstrm.h index b5bd8a1664..debcaaf0c9 100644 --- a/include/wx/datstrm.h +++ b/include/wx/datstrm.h @@ -16,15 +16,12 @@ #pragma interface "datstrm.h" #endif -#include "wx/wx.h" +#include -class wxDataStream { +class wxDataStream: public wxFilterInputStream { public: - wxDataStream(iostream& s); - wxDataStream(istream& s); - wxDataStream(ostream& s); - - virtual ~wxDataStream(); + wxDataInputStream(wxInputStream& s); + virtual ~wxDataInputStream(); unsigned long Read32(); unsigned short Read16(); @@ -32,6 +29,12 @@ public: double ReadDouble(); wxString ReadLine(); wxString ReadString(); +}; + +class wxDataOutputStream: public wxFilterOutputStream { + public: + wxDataOutputStream(wxOutputStream& s); + virtual ~wxDataOutputStream(); void Write32(unsigned long i); void Write16(unsigned short i); @@ -39,10 +42,7 @@ public: void WriteDouble(double d); void WriteLine(const wxString& line); void WriteString(const wxString& string); -protected: - istream *m_istream; - ostream *m_ostream; }; #endif - // __HELPBASEH__ + // __DATSTREAMH__ diff --git a/src/Makefile.in b/src/Makefile.in index 51ee9ca103..0731893a14 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -55,6 +55,9 @@ LIB_CPP_SRC=\ common/time.cpp \ common/timercmn.cpp \ common/utilscmn.cpp \ + common/stream.cpp \ + common/fstream.cpp \ + common/mstream.cpp \ common/datstrm.cpp \ \ gtk/app.cpp \ diff --git a/src/common/datstrm.cpp b/src/common/datstrm.cpp index df9095cdff..489c0cea97 100644 --- a/src/common/datstrm.cpp +++ b/src/common/datstrm.cpp @@ -26,36 +26,23 @@ #include "wx/datstrm.h" -wxDataStream::wxDataStream(istream& s) +wxDataInputStream::wxDataInputStream(wxInputStream& s) + : wxFilterInputStream(s) { - m_istream = &s; - m_ostream = NULL; } -wxDataStream::wxDataStream(ostream& s) +wxDataInputStream::~wxDataInputStream() { - m_istream = NULL; - m_ostream = &s; } -wxDataStream::wxDataStream(iostream& s) -{ - m_istream = &s; - m_ostream = &s; -} - -wxDataStream::~wxDataStream() -{ -} - -unsigned long wxDataStream::Read32() +unsigned long wxDataInputStream::Read32() { char buf[4]; if (!m_istream) return 0; - m_istream->read(buf, 4); + Read(buf, 4); return (unsigned long)buf[0] | ((unsigned long)buf[1] << 8) | @@ -63,34 +50,34 @@ unsigned long wxDataStream::Read32() ((unsigned long)buf[3] << 24); } -unsigned short wxDataStream::Read16() +unsigned short wxDataInputStream::Read16() { char buf[2]; if (!m_istream) return 0; - m_istream->read(buf, 2); + Read(buf, 2); return (unsigned short)buf[0] | ((unsigned short)buf[1] << 8); } -unsigned char wxDataStream::Read8() +unsigned char wxDataInputStream::Read8() { char buf; if (!m_istream) return 0; - m_istream->read(&buf, 1); + Read(&buf, 1); return (unsigned char)buf; } // Must be at global scope for VC++ 5 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes); -double wxDataStream::ReadDouble() +double wxDataInputStream::ReadDouble() { #if USE_APPLE_IEEE char buf[10]; @@ -98,25 +85,25 @@ double wxDataStream::ReadDouble() if (!m_istream) return 0.0; - m_istream->read(buf, 10); + Read(buf, 10); return ConvertFromIeeeExtended((unsigned char *)buf); #else return 0.0; #endif } -wxString wxDataStream::ReadLine() +wxString wxDataInputStream::ReadLine() { char i_strg[255]; if (!m_istream) return ""; - m_istream->getline(i_strg, 255); + // TODO: Implement ReadLine return i_strg; } -wxString wxDataStream::ReadString() +wxString wxDataInputStream::ReadString() { wxString wx_string; char *string; @@ -128,7 +115,7 @@ wxString wxDataStream::ReadString() len = Read32(); string = new char[len+1]; - m_istream->read(string, len); + Read(string, len); string[len] = 0; wx_string = string; @@ -137,7 +124,12 @@ wxString wxDataStream::ReadString() return wx_string; } -void wxDataStream::Write32(unsigned long i) +wxDataOutputStream::wxDataOutputStream(wxOutputStream& s) + : wxFilterOutputStream(s) +{ +} + +void wxDataOutputStream::Write32(unsigned long i) { char buf[4]; @@ -148,10 +140,10 @@ void wxDataStream::Write32(unsigned long i) buf[1] = (i >> 8) & 0xff; buf[2] = (i >> 16) & 0xff; buf[3] = (i >> 24) & 0xff; - m_ostream->write(buf, 4); + Write(buf, 4); } -void wxDataStream::Write16(unsigned short i) +void wxDataOutputStream::Write16(unsigned short i) { char buf[2]; @@ -160,18 +152,18 @@ void wxDataStream::Write16(unsigned short i) buf[0] = i & 0xff; buf[1] = (i >> 8) & 0xff; - m_ostream->write(buf, 2); + Write(buf, 2); } -void wxDataStream::Write8(unsigned char i) +void wxDataOutputStream::Write8(unsigned char i) { if (!m_ostream) return; - m_ostream->write(&i, 1); + Write(&i, 1); } -void wxDataStream::WriteLine(const wxString& line) +void wxDataOutputStream::WriteLine(const wxString& line) { #ifdef __WXMSW__ wxString tmp_string = line + "\r\n"; @@ -182,22 +174,22 @@ void wxDataStream::WriteLine(const wxString& line) if (!m_ostream) return; - m_ostream->write((const char *) tmp_string, tmp_string.Length()); + Write((const char *) tmp_string, tmp_string.Length()); } -void wxDataStream::WriteString(const wxString& string) +void wxDataOutputStream::WriteString(const wxString& string) { if (!m_ostream) return; Write32(string.Length()); - m_ostream->write((const char *) string, string.Length()); + Write((const char *) string, string.Length()); } // Must be at global scope for VC++ 5 extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes); -void wxDataStream::WriteDouble(double d) +void wxDataOutputStream::WriteDouble(double d) { char buf[10]; @@ -210,5 +202,5 @@ void wxDataStream::WriteDouble(double d) # pragma warning "wxDataStream::WriteDouble() not using IeeeExtended - will not work!" buf[0] = '\0'; #endif - m_ostream->write(buf, 10); + Write(buf, 10); } diff --git a/src/common/fstream.cpp b/src/common/fstream.cpp new file mode 100644 index 0000000000..6ebb2b1395 --- /dev/null +++ b/src/common/fstream.cpp @@ -0,0 +1,105 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: fstream.cpp +// Purpose: "File stream" classes +// Author: Julian Smart +// Modified by: +// Created: 11/07/98 +// RCS-ID: $Id$ +// Copyright: (c) Guilhem Lavaux +// Licence: wxWindows license +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "fstream.h" +#endif + +#include +#include +#include +#include + +#define BUF_TEMP_SIZE 10000 + +#if !USE_SHARED_LIBRARY +IMPLEMENT_CLASS(wxFileStreamBase, wxStream) +IMPLEMENT_CLASS(wxFileInputStream, wxFileStreamBase) +IMPLEMENT_CLASS(wxFileOutputStream, wxFileStreamBase) +IMPLEMENT_CLASS(wxFileStream, wxFileStreamBase) +#endif + +wxFileStreamBase::wxFileStreamBase(const wxString& fileName, int iolimit) + : wxStream() +{ + char *open_mode; + + switch (iolimit) { + case 0: + open_mode = "a+"; + break; + case 1: + open_mode = "rb"; + break; + case 2: + open_mode = "wb"; + break; + } + m_file = fopen(fileName, open_mode); + fseek(m_file, 0, SEEK_SET); + + m_eof = FALSE; + m_bad = FALSE; +} + +wxFileStreamBase::~wxFileStreamBase() +{ + fclose(m_file); +} + +wxInputStream& wxFileStreamBase::Read(void *buffer, size_t size) +{ + m_lastread = fread(buffer, 1, size, m_file); + m_eof = feof(m_file); + return *this; +} + +wxOutputStream& wxFileStreamBase::Write(const void *buffer, size_t size) +{ + m_lastwrite = fwrite(buffer, 1, size, m_file); + m_bad = ferror(m_file) != 0; + return *this; +} + +size_t wxFileStreamBase::SeekI(int pos, wxWhenceType whence) +{ + int real_whence; + + if (whence == wxBeginPosition) + real_whence = SEEK_SET; + else if (whence == wxCurrentPosition) + real_whence = SEEK_CUR; + else if (whence == wxEndPosition) + real_whence = SEEK_END; + + fseek(m_file, pos, real_whence); + return ftell(m_file); +} + +size_t wxFileStreamBase::TellI() const +{ + return ftell(m_file); +} + +size_t wxFileStreamBase::TellO() const +{ + return ftell(m_file); +} + +size_t wxFileStreamBase::SeekO(int pos, wxWhenceType whence) +{ + return SeekI(pos, whence); +} + +void wxFileStreamBase::Sync() +{ + fflush(m_file); +} diff --git a/src/common/mstream.cpp b/src/common/mstream.cpp new file mode 100644 index 0000000000..a12041ee0e --- /dev/null +++ b/src/common/mstream.cpp @@ -0,0 +1,133 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: mmstream.cpp +// Purpose: "Memory stream" classes +// Author: Guilhem Lavaux +// Modified by: +// Created: 04/01/98 +// RCS-ID: $Id$ +// Copyright: (c) Guilhem Lavaux +// Licence: wxWindows license +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "mmstream.h" +#endif + +#include +#include +#include + +wxMemoryStreamBase::wxMemoryStreamBase(char *data, size_t length, int iolimit) +{ + m_buffer = data; + m_iolimit = iolimit; + m_persistent = FALSE; + m_length = length; + m_position_i = m_position_o = 0; +} + +wxMemoryStreamBase::~wxMemoryStreamBase() +{ + free(m_buffer); +} + +wxInputStream& wxMemoryStreamBase::Read(void *buffer, size_t size) +{ + if (m_iolimit == 2) { + m_eof = TRUE; + return *this; + } + if (m_position_i+size > m_length) + size = m_length-m_position_i; + + memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size); + m_position_i += size; + m_lastread = size; + + return *this; +} + +size_t wxMemoryStreamBase::SeekI(int pos, wxWhenceType whence) +{ + if (m_iolimit == 2) + return 0; + + switch (whence) { + case wxBeginPosition: + if ((size_t)pos > m_length) + return m_position_i; + return (m_position_i = pos); + + case wxCurrentPosition: + if ((size_t)(m_position_i+pos) > m_length) + return m_position_i; + + return (m_position_i += pos); + + case wxEndPosition: + if ((size_t)(m_length-pos) > m_length) + return m_position_i; + + return (m_position_i = m_length-pos); + } + + return m_position_i; +} + +wxOutputStream& wxMemoryStreamBase::Write(const void *buffer, size_t size) +{ + if (m_iolimit == 1) { + m_bad = TRUE; + return *this; + } + + if (m_position_o+size > m_length) + if (!ChangeBufferSize(m_position_o+size)) { + m_bad = TRUE; + return *this; + } + + memcpy(m_buffer+m_position_o, buffer, size); + m_position_o += size; + m_lastwrite = size; + + return *this; +} + +size_t wxMemoryStreamBase::SeekO(int pos, wxWhenceType whence) +{ + if (m_iolimit == 2) + return 0; + + switch (whence) { + case wxBeginPosition: + if ((size_t)pos > m_length) + return m_position_o; + return (m_position_o = pos); + + case wxCurrentPosition: + if ((size_t)(m_position_o+pos) > m_length) + return m_position_o; + + return (m_position_o += pos); + + case wxEndPosition: + if ((size_t)(m_length-pos) > m_length) + return m_position_o; + + return (m_position_o = m_length-pos); + } + + return m_position_o; +} + +bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size) +{ + m_length = new_size; + if (!m_buffer) + m_buffer = (char *)malloc(m_length); + else + m_buffer = (char *)realloc(m_buffer, m_length); + + return (m_buffer != NULL); +} diff --git a/src/common/stream.cpp b/src/common/stream.cpp new file mode 100644 index 0000000000..fc7bcc8b9a --- /dev/null +++ b/src/common/stream.cpp @@ -0,0 +1,73 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: stream.cpp +// Purpose: wxStream base classes +// Author: Guilhem Lavaux +// Modified by: +// Created: 11/07/98 +// RCS-ID: $Id$ +// Copyright: (c) Guilhem Lavaux +// Licence: wxWindows license +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "stream.h" +#endif + +#include +#include "stream.h" + +#if !USE_SHARED_LIBRARY +IMPLEMENT_ABSTRACT_CLASS(wxInputStream, wxObject) +IMPLEMENT_ABSTRACT_CLASS(wxOutputStream, wxObject) +IMPLEMENT_ABSTRACT_CLASS2(wxStream, wxInputStream, wxOutputStream) +IMPLEMENT_CLASS(wxFilterInputStream, wxInputStream) +#endif + +wxInputStream::wxInputStream() + : wxObject() +{ +} + +wxInputStream::~wxInputStream() +{ +} + +#define BUF_TEMP_SIZE 10000 + +wxInputStream& wxInputStream::Read(wxOutputStream& stream_out) +{ + char buf[BUF_TEMP_SIZE]; + size_t bytes_read = BUF_TEMP_SIZE; + + while (bytes_read == BUF_TEMP_SIZE && !stream_out.Bad()) { + bytes_read = Read(buf, bytes_read).LastRead(); + + stream_out.Write(buf, bytes_read); + } + return *this; +} + +wxOutputStream::wxOutputStream() + : wxObject() +{ +} + +wxOutputStream::~wxOutputStream() +{ +} + +wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in) +{ + stream_in.Read(*this); + return *this; +} + +wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) + : wxInputStream() +{ + m_parent_stream = &stream; +} + +wxFilterInputStream::~wxFilterInputStream() +{ +} diff --git a/src/msw/makefile.b32 b/src/msw/makefile.b32 index 75d427e83a..d3fff4e0cc 100644 --- a/src/msw/makefile.b32 +++ b/src/msw/makefile.b32 @@ -105,6 +105,9 @@ COMMONOBJS = \ $(MSWDIR)\time.obj \ $(MSWDIR)\wxexpr.obj \ $(MSWDIR)\y_tab.obj \ + $(MSWDIR)\stream.obj \ + $(MSWDIR)\fstream.obj \ + $(MSWDIR)\mstream.obj \ $(MSWDIR)\datstrm.obj \ $(MSWDIR)\extended.obj @@ -465,6 +468,12 @@ $(MSWDIR)\time.obj: $(COMMDIR)\time.$(SRCSUFF) $(MSWDIR)\datstrm.obj: $(COMMDIR)\datstrm.$(SRCSUFF) +$(MSWDIR)\mstream.obj: $(COMMDIR)\mstream.$(SRCSUFF) + +$(MSWDIR)\fstream.obj: $(COMMDIR)\fstream.$(SRCSUFF) + +$(MSWDIR)\stream.obj: $(COMMDIR)\stream.$(SRCSUFF) + $(MSWDIR)\extended.obj: $(COMMDIR)\extended.c ######################################################## diff --git a/src/msw/makefile.dos b/src/msw/makefile.dos index a22b97f133..a5b5c3b10e 100644 --- a/src/msw/makefile.dos +++ b/src/msw/makefile.dos @@ -106,6 +106,9 @@ COMMONOBJS = \ $(COMMDIR)\string.obj \ $(COMMDIR)\time.obj \ $(COMMDIR)\y_tab.obj \ + $(COMMDIR)\stream.obj \ + $(COMMDIR)\fstream.obj \ + $(COMMDIR)\mstream.obj \ $(COMMDIR)\datstrm.obj \ $(COMMDIR)\extended.obj diff --git a/src/msw/makefile.g95 b/src/msw/makefile.g95 index 594bfc4da9..5f7ff56435 100644 --- a/src/msw/makefile.g95 +++ b/src/msw/makefile.g95 @@ -111,6 +111,9 @@ COMMONOBJS = \ $(COMMDIR)/string.$(OBJSUFF) \ $(COMMDIR)/time.$(OBJSUFF) \ $(COMMDIR)/y_tab.$(OBJSUFF) \ + $(COMMDIR)/stream.$(OBJSUFF) \ + $(COMMDIR)/fstream.$(OBJSUFF) \ + $(COMMDIR)/mstream.$(OBJSUFF) \ $(COMMDIR)/datstrm.$(OBJSUFF) \ $(COMMDIR)/extended.$(OBJSUFF) diff --git a/src/msw/makefile.nt b/src/msw/makefile.nt index 455d1af599..e4a1cdba0e 100644 --- a/src/msw/makefile.nt +++ b/src/msw/makefile.nt @@ -107,6 +107,9 @@ COMMONOBJS = \ $(COMMDIR)\time.obj \ $(COMMDIR)\wxexpr.obj \ $(COMMDIR)\y_tab.obj \ + $(COMMDIR)\stream.obj \ + $(COMMDIR)\fstream.obj \ + $(COMMDIR)\mstream.obj \ $(COMMDIR)\datstrm.obj \ $(COMMDIR)\extended.obj \ $(COMMDIR)\process.obj -- 2.47.2