From 79c3e0e1aeebb64da2ac893e6ed7b27edca01a64 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Tue, 14 Jul 1998 12:06:50 +0000 Subject: [PATCH] * wxStream: I've rewritten the inheritance * added wxZlib*Stream * updated makefiles and data.cpp * modified a bit wxFile so I can use it in wxFile*Stream git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@263 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/file.h | 12 +-- include/wx/fstream.h | 63 +++++++------ include/wx/mstream.h | 85 +++++++++--------- include/wx/stream.h | 34 +++---- include/wx/zstream.h | 63 +++++++++++++ src/Makefile.in | 1 + src/common/file.cpp | 16 ++-- src/common/fstream.cpp | 111 ++++++++++++----------- src/common/mstream.cpp | 122 ++++++++++++++++++------- src/common/stream.cpp | 9 +- src/common/zstream.cpp | 200 +++++++++++++++++++++++++++++++++++++++++ src/gtk/data.cpp | 18 ++-- src/gtk1/data.cpp | 18 ++-- src/msw/data.cpp | 17 ++-- src/msw/makefile.b32 | 3 + src/msw/makefile.dos | 1 + src/msw/makefile.g95 | 1 + src/msw/makefile.nt | 30 +++++-- 18 files changed, 584 insertions(+), 220 deletions(-) create mode 100644 include/wx/zstream.h create mode 100644 src/common/zstream.cpp diff --git a/include/wx/file.h b/include/wx/file.h index bf58ce69c0..e3f8ab1fc3 100644 --- a/include/wx/file.h +++ b/include/wx/file.h @@ -23,6 +23,7 @@ #include "wx/string.h" #include "wx/filefn.h" +#include "wx/stream.h" // for wxSeekMode // define off_t #include @@ -54,8 +55,6 @@ public: enum OpenMode { read, write, read_write, write_append }; // standard values for file descriptor enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr }; - // seek type - enum SeekMode { FromStart, FromEnd, FromCurrent }; // static functions // ---------------- @@ -84,7 +83,7 @@ public: // returns number of bytes read or ofsInvalid on error off_t Read(void *pBuf, off_t nCount); // returns true on success - bool Write(const void *pBuf, uint nCount); + uint Write(const void *pBuf, uint nCount); // returns true on success bool Write(const wxString& str) { return Write(str.c_str(), str.Len()); } // flush data not yet written @@ -92,9 +91,9 @@ public: // file pointer operations (return ofsInvalid on failure) // move ptr ofs bytes related to start/current off_t/end of file - off_t Seek(off_t ofs, SeekMode mode = FromStart); + off_t Seek(off_t ofs, wxSeekMode mode = wxFromStart); // move ptr to ofs bytes before the end - off_t SeekEnd(off_t ofs = 0) { return Seek(ofs, FromEnd); } + off_t SeekEnd(off_t ofs = 0) { return Seek(ofs, wxFromEnd); } // get current off_t off_t Tell() const; // get current file length @@ -105,6 +104,8 @@ public: bool IsOpened() const { return m_fd != fd_invalid; } // is end of file reached? bool Eof() const; + // is an error occured? + bool Error() const { return m_error; } // dtor closes the file if opened ~wxFile(); @@ -117,6 +118,7 @@ private: wxFile& operator=(const wxFile&); int m_fd; // file descriptor or INVALID_FD if not opened + bool m_error; // error memory }; // ---------------------------------------------------------------------------- diff --git a/include/wx/fstream.h b/include/wx/fstream.h index d41b9bc735..384a614bc4 100644 --- a/include/wx/fstream.h +++ b/include/wx/fstream.h @@ -11,58 +11,65 @@ #ifndef __WXFSTREAM_H__ #define __WXFSTREAM_H__ -#include #include #include #include +#include -class wxFileStreamBase: public wxStream { - DECLARE_CLASS(wxFileStreamBase) +class wxFileInputStream: virtual public wxFile, public wxInputStream { + DECLARE_CLASS(wxFileInputStream) public: - wxFileStreamBase(const wxString& fileName, int iolimit); - virtual ~wxFileStreamBase(); + wxFileInputStream(const wxString& fileName); + virtual ~wxFileInputStream(); wxInputStream& Read(void *buffer, size_t size); - size_t SeekI(int pos, wxWhenceType whence = wxBeginPosition); - size_t TellI() const; + off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); + off_t TellI() const; bool Eof() const { return m_eof; } size_t LastRead() const { return m_lastread; } + bool Ok() const { return wxFile::IsOpened(); } + + protected: + wxFileInputStream() {} + + protected: + bool m_eof; + bool m_ok_i; + size_t m_lastread; +}; + +class wxFileOutputStream: virtual wxFile, public wxOutputStream { + DECLARE_CLASS(wxFileOutputStream) + public: + wxFileOutputStream(const wxString& fileName); + virtual ~wxFileOutputStream(); + wxOutputStream& Write(const void *buffer, size_t size); - size_t SeekO(int pos, wxWhenceType whence = wxBeginPosition); - size_t TellO() const; + off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); + off_t TellO() const; bool Bad() const { return m_bad; } size_t LastWrite() const { return m_lastwrite; } void Sync(); - protected: - size_t m_lastread, m_lastwrite; - bool m_eof, m_bad; - FILE *m_file; -}; + bool IsOpened() const { return wxFile::IsOpened(); } -class wxFileInputStream: public wxFileStreamBase { - DECLARE_CLASS(wxFileInputStream) - public: - wxFileInputStream(const wxString& fileName) : wxFileStreamBase(fileName, 1) {} - virtual ~wxFileInputStream() {} -}; + protected: + wxFileOutputStream() {} -class wxFileOutputStream: public wxFileStreamBase { - DECLARE_CLASS(wxFileOutputStream) - public: - wxFileOutputStream(const wxString& fileName) : wxFileStreamBase(fileName, 2) {} - virtual ~wxFileOutputStream() {} + protected: + bool m_bad; + size_t m_lastwrite; }; -class wxFileStream: public wxFileStreamBase { +class wxFileStream: public wxFileInputStream, public wxFileOutputStream { DECLARE_CLASS(wxFileStream) public: - wxFileStream(const wxString& fileName) : wxFileStreamBase(fileName, 0) {} - virtual ~wxFileStream() {} + wxFileStream(const wxString& fileName); + virtual ~wxFileStream(); }; #endif diff --git a/include/wx/mstream.h b/include/wx/mstream.h index 716c809967..641c0d4244 100644 --- a/include/wx/mstream.h +++ b/include/wx/mstream.h @@ -11,67 +11,68 @@ #ifndef __WXMMSTREAM_H__ #define __WXMMSTREAM_H__ -#include "wx/object.h" -#include "wx/stream.h" +#include -class wxMemoryStreamBase: public wxStream { - DECLARE_CLASS(wxMemoryStreamBase) - public: - wxMemoryStreamBase(char *data, size_t length, int iolimit); +class wxMemoryStreamBase { + protected: + wxMemoryStreamBase(); virtual ~wxMemoryStreamBase(); - // Input part - wxInputStream& Read(void *buffer, size_t size); - size_t SeekI(int pos, wxWhenceType whence = wxBeginPosition); - size_t TellI() const { return m_position_i; } - - bool Eof() const { return m_eof; } - size_t LastRead() const { return m_lastread; } - - // Output part - wxOutputStream& Write(const void *buffer, size_t size); - size_t SeekO(int pos, wxWhenceType whence = wxBeginPosition); - size_t TellO() const { return m_position_o; } - - bool Bad() const { return m_bad; } - size_t LastWrite() const { return m_lastwrite; } - void Sync() {} - - protected: bool ChangeBufferSize(size_t new_length); protected: - bool m_bad, m_eof, m_persistent; - size_t m_lastread, m_lastwrite; + bool m_persistent; size_t m_length; - size_t m_position_i, m_position_o; char *m_buffer; int m_iolimit; }; - -class wxMemoryInputStream: public wxMemoryStreamBase { +class wxMemoryInputStream: virtual public wxMemoryStreamBase, public wxInputStream { DECLARE_CLASS(wxMemoryInputStream) public: - wxMemoryInputStream(char *data, size_t length) - : wxMemoryStreamBase(data, length, 1) - {} + wxMemoryInputStream(const char *data, size_t length); + virtual ~wxMemoryInputStream(); + + wxInputStream& Read(void *buffer, size_t size); + off_t SeekI(off_t pos, wxSeekMode mode); + off_t TellI() const { return m_position_i; } + + bool Eof() const { return m_eof; } + size_t LastRead() const { return m_lastread; } + + protected: + bool m_eof; + off_t m_position_i; + size_t m_lastread; }; -class wxMemoryOutputStream: public wxMemoryStreamBase { - DECLARE_DYNAMIC_CLASS(wxMemoryOutputStream) +class wxMemoryOutputStream: virtual public wxMemoryStreamBase, public wxOutputStream { + DECLARE_CLASS(wxMemoryOutputStream) public: - wxMemoryOutputStream(char *data = NULL, size_t length = 0) - : wxMemoryStreamBase(data, length, 2) - {} + wxMemoryOutputStream(char *data = NULL, size_t length = 0); + virtual ~wxMemoryOutputStream(); + + wxOutputStream& Write(const void *buffer, size_t size); + off_t SeekO(off_t pos, wxSeekMode mode); + off_t TellO() const { return m_position_o; } + + bool Bad() const { return m_bad; } + size_t LastWrite() const { return m_lastwrite; } + + char *GetData() { return m_buffer; } + size_t GetLength() { return m_length; } + + protected: + bool m_bad; + off_t m_position_o; + size_t m_lastwrite; }; -class wxMemoryStream: public wxMemoryStreamBase { - DECLARE_DYNAMIC_CLASS(wxMemoryStream) +class wxMemoryStream: public wxMemoryInputStream, public wxMemoryOutputStream { + DECLARE_CLASS(wxMemoryStream) public: - wxMemoryStream(char *data = NULL, size_t length = 0) - : wxMemoryStreamBase(data, length, 0) - {} + wxMemoryStream(char *data = NULL, size_t length = 0); + virtual ~wxMemoryStream(); }; #endif diff --git a/include/wx/stream.h b/include/wx/stream.h index 9acfab2133..3fef67ff56 100644 --- a/include/wx/stream.h +++ b/include/wx/stream.h @@ -13,7 +13,7 @@ #define __WXSTREAM_H__ #ifdef __GNUG__ -#pragma interface "stream.h" +#pragma interface #endif #include @@ -25,11 +25,11 @@ */ typedef enum { - wxBeginPosition = 0, wxCurrentPosition = 1, wxEndPosition = 2 -} wxWhenceType; + wxFromStart, wxFromCurrent, wxFromEnd +} wxSeekMode; class wxOutputStream; -class wxInputStream: public wxObject { +class wxInputStream: virtual public wxObject { DECLARE_ABSTRACT_CLASS(wxInputStream) public: wxInputStream(); @@ -38,14 +38,14 @@ class wxInputStream: public wxObject { virtual wxInputStream& Read(void *buffer, size_t size) = 0; wxInputStream& Read(wxOutputStream& stream_out); - virtual size_t SeekI(int pos, wxWhenceType whence = wxBeginPosition) = 0; - virtual size_t TellI() const = 0; + virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart) = 0; + virtual off_t TellI() const = 0; virtual bool Eof() const = 0; virtual size_t LastRead() const = 0; }; -class wxOutputStream: public wxObject { +class wxOutputStream: virtual public wxObject { DECLARE_ABSTRACT_CLASS(wxOutputStream) public: wxOutputStream(); @@ -54,8 +54,8 @@ class wxOutputStream: public wxObject { virtual wxOutputStream& Write(const void *buffer, size_t size) = 0; wxOutputStream& Write(wxInputStream& stream_in); - virtual size_t SeekO(int pos, wxWhenceType whence = wxBeginPosition) = 0; - virtual size_t TellO() const = 0; + virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart) = 0; + virtual off_t TellO() const = 0; virtual bool Bad() const = 0; virtual size_t LastWrite() const = 0; @@ -63,11 +63,10 @@ class wxOutputStream: public wxObject { virtual void Sync() {} }; -class wxStream: public wxInputStream, public wxOutputStream { - DECLARE_ABSTRACT_CLASS(wxStream) +class wxStream: virtual public wxInputStream, virtual public wxOutputStream { public: - wxStream() : wxInputStream(), wxOutputStream() {} - virtual ~wxStream() {} + wxStream() {} + virtual ~wxStream() { } }; /* @@ -82,11 +81,12 @@ class wxFilterInputStream: public wxInputStream { virtual wxInputStream& Read(void *buffer, size_t size) { return m_parent_i_stream->Read(buffer, size); } - virtual size_t SeekI(int pos, wxWhenceType whence = wxBeginPosition) - { return m_parent_i_stream->SeekI(pos, whence); } + virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart) + { return m_parent_i_stream->SeekI(pos, mode); } virtual bool Eof() const { return m_parent_i_stream->Eof(); } virtual size_t LastRead() const { return m_parent_i_stream->LastRead(); } + protected: wxInputStream *m_parent_i_stream; }; @@ -99,8 +99,8 @@ class wxFilterOutputStream: public wxOutputStream { virtual wxOutputStream& Write(const void *buffer, size_t size) { return m_parent_o_stream->Write(buffer, size); } - virtual size_t SeekO(int pos, wxWhenceType whence = wxBeginPosition) - { return m_parent_o_stream->SeekO(pos, whence); } + virtual off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart) + { return m_parent_o_stream->SeekO(pos, mode); } virtual bool Bad() const { return m_parent_o_stream->Bad(); } virtual size_t LastWrite() const { return m_parent_o_stream->LastWrite(); } diff --git a/include/wx/zstream.h b/include/wx/zstream.h new file mode 100644 index 0000000000..34bb20f672 --- /dev/null +++ b/include/wx/zstream.h @@ -0,0 +1,63 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: zstream.h +// Purpose: Memory stream classes +// Author: Guilhem Lavaux +// Modified by: +// Created: 11/07/98 +// RCS-ID: $Id$ +// Copyright: (c) Guilhem Lavaux +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// +#ifndef __WXZSTREAM_H__ +#define __WXZSTREAM_H__ + +#ifdef __GNUG__ +#pragma interface +#endif + +#include +#include "zlib.h" + +class wxZlibInputStream: public wxFilterInputStream { + DECLARE_CLASS(wxZlibInputStream) + public: + wxZlibInputStream(wxInputStream& stream); + virtual ~wxZlibInputStream(); + + wxInputStream& Read(void *buffer, size_t size); + off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart); + off_t TellI() const; + + size_t LastRead() const { return m_lastread; } + bool Eof() const; + + protected: + size_t m_lastread; + size_t m_z_size; + unsigned char *m_z_buffer; + bool m_eof; + struct z_stream_s m_inflate; +}; + +class wxZlibOutputStream: public wxFilterOutputStream { + DECLARE_CLASS(wxZlibOutputStream) + public: + wxZlibOutputStream(wxOutputStream& stream); + virtual ~wxZlibOutputStream(); + + wxOutputStream& Write(const void *buffer, size_t size); + off_t SeekO(off_t pos, wxSeekMode mode = wxFromStart); + off_t TellO() const; + + size_t LastWrite() const { return m_lastwrite; } + bool Bad() const; + + protected: + size_t m_lastwrite; + size_t m_z_size; + unsigned char *m_z_buffer; + bool m_bad; + struct z_stream_s m_deflate; +}; + +#endif diff --git a/src/Makefile.in b/src/Makefile.in index 0731893a14..b695ecfff2 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -58,6 +58,7 @@ LIB_CPP_SRC=\ common/stream.cpp \ common/fstream.cpp \ common/mstream.cpp \ + common/zstream.cpp \ common/datstrm.cpp \ \ gtk/app.cpp \ diff --git a/src/common/file.cpp b/src/common/file.cpp index 52a39d4d34..05e18e8866 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -105,6 +105,7 @@ bool wxFile::Exists(const char *sz) wxFile::wxFile(const char *szFileName, OpenMode mode) { m_fd = fd_invalid; + m_error = FALSE; Open(szFileName, mode); } @@ -202,17 +203,18 @@ off_t wxFile::Read(void *pBuf, off_t nCount) } // write -bool wxFile::Write(const void *pBuf, uint nCount) +uint wxFile::Write(const void *pBuf, uint nCount) { wxCHECK( (pBuf != NULL) && IsOpened(), 0 ); int iRc = ::write(m_fd, pBuf, nCount); if ( iRc == -1 ) { wxLogSysError("can't write to file descriptor %d", m_fd); - return FALSE; + m_error = TRUE; + return 0; } else - return TRUE; + return iRc; } // flush @@ -235,21 +237,21 @@ bool wxFile::Flush() // ---------------------------------------------------------------------------- // seek -off_t wxFile::Seek(off_t ofs, SeekMode mode) +off_t wxFile::Seek(off_t ofs, wxSeekMode mode) { wxASSERT( IsOpened() ); int flag = -1; switch ( mode ) { - case FromStart: + case wxFromStart: flag = SEEK_SET; break; - case FromCurrent: + case wxFromCurrent: flag = SEEK_CUR; break; - case FromEnd: + case wxFromEnd: flag = SEEK_END; break; diff --git a/src/common/fstream.cpp b/src/common/fstream.cpp index 6ebb2b1395..ea50fee90a 100644 --- a/src/common/fstream.cpp +++ b/src/common/fstream.cpp @@ -13,93 +13,100 @@ #pragma implementation "fstream.h" #endif -#include +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" #include #include #include +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + + #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) +IMPLEMENT_CLASS(wxFileInputStream, wxInputStream) +IMPLEMENT_CLASS(wxFileOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxFileStream, wxInputStream, wxOutputStream) #endif -wxFileStreamBase::wxFileStreamBase(const wxString& fileName, int iolimit) - : wxStream() +// ---------------------------------------------------------------------------- +// wxFileInputStream +// ---------------------------------------------------------------------------- + +wxFileInputStream::wxFileInputStream(const wxString& fileName) + : wxFile(fileName, read) { - 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; + m_lastread = 0; } -wxFileStreamBase::~wxFileStreamBase() +wxFileInputStream::~wxFileInputStream() { - fclose(m_file); } -wxInputStream& wxFileStreamBase::Read(void *buffer, size_t size) +wxInputStream& wxFileInputStream::Read(void *buffer, size_t size) { - m_lastread = fread(buffer, 1, size, m_file); - m_eof = feof(m_file); + m_lastread = wxFile::Read(buffer, size); return *this; } -wxOutputStream& wxFileStreamBase::Write(const void *buffer, size_t size) +off_t wxFileInputStream::SeekI(off_t pos, wxSeekMode mode) { - m_lastwrite = fwrite(buffer, 1, size, m_file); - m_bad = ferror(m_file) != 0; - return *this; + return wxFile::Seek(pos, mode); +} + +off_t wxFileInputStream::TellI() const +{ + return wxFile::Tell(); +} + +// ---------------------------------------------------------------------------- +// wxFileOutputStream +// ---------------------------------------------------------------------------- + +wxFileOutputStream::wxFileOutputStream(const wxString& fileName) + : wxFile(fileName, write) +{ + m_lastwrite = 0; } -size_t wxFileStreamBase::SeekI(int pos, wxWhenceType whence) +wxFileOutputStream::~wxFileOutputStream() { - 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; +wxOutputStream& wxFileOutputStream::Write(const void *buffer, size_t size) +{ + m_lastwrite = wxFile::Write(buffer, size); + m_bad = wxFile::Error(); + return *this; +} - fseek(m_file, pos, real_whence); - return ftell(m_file); +off_t wxFileOutputStream::TellO() const +{ + return wxFile::Tell(); } -size_t wxFileStreamBase::TellI() const +off_t wxFileOutputStream::SeekO(off_t pos, wxSeekMode mode) { - return ftell(m_file); + return wxFile::Seek(pos, mode); } -size_t wxFileStreamBase::TellO() const +void wxFileOutputStream::Sync() { - return ftell(m_file); + wxFile::Flush(); } -size_t wxFileStreamBase::SeekO(int pos, wxWhenceType whence) +// ---------------------------------------------------------------------------- +// wxFileStream +// ---------------------------------------------------------------------------- + +wxFileStream::wxFileStream(const wxString& fileName) + : wxFile(fileName, read_write) { - return SeekI(pos, whence); } -void wxFileStreamBase::Sync() +wxFileStream::~wxFileStream() { - fflush(m_file); } diff --git a/src/common/mstream.cpp b/src/common/mstream.cpp index 2bfa364e1e..d37a9ddaa3 100644 --- a/src/common/mstream.cpp +++ b/src/common/mstream.cpp @@ -13,32 +13,69 @@ #pragma implementation "mstream.h" #endif +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" #include -#include "wx/stream.h" -#include "wx/mstream.h" +#include +#include + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif #if !USE_SHARED_LIBRARY -IMPLEMENT_CLASS(wxMemoryStreamBase, wxStream) -IMPLEMENT_CLASS(wxMemoryInputStream, wxMemoryStreamBase) -//IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxMemoryStreamBase) -//IMPLEMENT_DYNAMIC_CLASS(wxMemoryStream, wxMemoryStreamBase) +IMPLEMENT_CLASS(wxMemoryInputStream, wxInputStream) +IMPLEMENT_CLASS(wxMemoryOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxMemoryStream, wxInputStream, wxOutputStream) #endif -wxMemoryStreamBase::wxMemoryStreamBase(char *data, size_t length, int iolimit) +// ---------------------------------------------------------------------------- +// wxMemoryStreamBase +// ---------------------------------------------------------------------------- +wxMemoryStreamBase::wxMemoryStreamBase() { - m_buffer = data; - m_iolimit = iolimit; + m_buffer = NULL; + m_iolimit = 0; m_persistent = FALSE; - m_length = length; - m_position_i = m_position_o = 0; + m_length = 0; } wxMemoryStreamBase::~wxMemoryStreamBase() { - free(m_buffer); + if (!m_persistent && m_buffer) + free(m_buffer); +} + +bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size) +{ + if (m_iolimit == 1) + return FALSE; + + 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); +} + +// ---------------------------------------------------------------------------- +// wxMemoryInputStream +// ---------------------------------------------------------------------------- + +wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len) +{ + m_persistent = TRUE; + m_length = len; + m_buffer = (char *)data; // It's bad. + m_position_i = 0; + m_lastread = 0; + m_eof = FALSE; + m_iolimit = 1; } -wxInputStream& wxMemoryStreamBase::Read(void *buffer, size_t size) +wxInputStream& wxMemoryInputStream::Read(void *buffer, size_t size) { if (m_iolimit == 2) { m_eof = TRUE; @@ -54,24 +91,24 @@ wxInputStream& wxMemoryStreamBase::Read(void *buffer, size_t size) return *this; } -size_t wxMemoryStreamBase::SeekI(int pos, wxWhenceType whence) +off_t wxMemoryInputStream::SeekI(off_t pos, wxSeekMode mode) { if (m_iolimit == 2) return 0; - switch (whence) { - case wxBeginPosition: + switch (mode) { + case wxFromStart: if ((size_t)pos > m_length) return m_position_i; return (m_position_i = pos); - case wxCurrentPosition: + case wxFromCurrent: if ((size_t)(m_position_i+pos) > m_length) return m_position_i; return (m_position_i += pos); - case wxEndPosition: + case wxFromEnd: if ((size_t)(m_length-pos) > m_length) return m_position_i; @@ -81,7 +118,22 @@ size_t wxMemoryStreamBase::SeekI(int pos, wxWhenceType whence) return m_position_i; } -wxOutputStream& wxMemoryStreamBase::Write(const void *buffer, size_t size) +// ---------------------------------------------------------------------------- +// wxMemoryOutputStream +// ---------------------------------------------------------------------------- + +wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len) +{ + m_persistent = FALSE; + m_buffer = data; + m_length = len; + m_position_o = 0; + m_lastwrite = 0; + m_bad = FALSE; + m_iolimit = 2; +} + +wxOutputStream& wxMemoryOutputStream::Write(const void *buffer, size_t size) { if (m_iolimit == 1) { m_bad = TRUE; @@ -101,24 +153,24 @@ wxOutputStream& wxMemoryStreamBase::Write(const void *buffer, size_t size) return *this; } -size_t wxMemoryStreamBase::SeekO(int pos, wxWhenceType whence) +off_t wxMemoryOutputStream::SeekO(off_t pos, wxSeekMode mode) { - if (m_iolimit == 2) + if (m_iolimit == 1) return 0; - switch (whence) { - case wxBeginPosition: + switch (mode) { + case wxFromStart: if ((size_t)pos > m_length) return m_position_o; return (m_position_o = pos); - case wxCurrentPosition: + case wxFromCurrent: if ((size_t)(m_position_o+pos) > m_length) return m_position_o; return (m_position_o += pos); - case wxEndPosition: + case wxFromEnd: if ((size_t)(m_length-pos) > m_length) return m_position_o; @@ -128,13 +180,19 @@ size_t wxMemoryStreamBase::SeekO(int pos, wxWhenceType whence) return m_position_o; } -bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size) +// ---------------------------------------------------------------------------- +// wxMemoryStream +// ---------------------------------------------------------------------------- + +wxMemoryStream::wxMemoryStream(char *data, size_t len) + : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0) { - m_length = new_size; - if (!m_buffer) - m_buffer = (char *)malloc(m_length); - else - m_buffer = (char *)realloc(m_buffer, m_length); + m_persistent = FALSE; + m_buffer = data; + m_length = len; + m_iolimit = 0; +} - return (m_buffer != NULL); +wxMemoryStream::~wxMemoryStream() +{ } diff --git a/src/common/stream.cpp b/src/common/stream.cpp index 2d9b313bb9..a9c76aff5b 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -15,22 +15,15 @@ // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" +#include #ifdef __BORLANDC__ #pragma hdrstop #endif -#ifndef WX_PRECOMP -#include "wx/setup.h" -#endif - -#include "wx/object.h" -#include "wx/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) IMPLEMENT_CLASS(wxFilterOutputStream, wxOutputStream) #endif diff --git a/src/common/zstream.cpp b/src/common/zstream.cpp new file mode 100644 index 0000000000..de38cad39c --- /dev/null +++ b/src/common/zstream.cpp @@ -0,0 +1,200 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: zstream.cpp +// Purpose: Compressed stream classes +// Author: Guilhem Lavaux +// Modified by: +// Created: 11/07/98 +// RCS-ID: $Id$ +// Copyright: (c) Guilhem Lavaux +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// +#ifdef __GNUG__ +#pragma implementation "zstream.h" +#endif + +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" +#include +#include +#include +#include "zlib.h" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#if !USE_SHARED_LIBRARY +IMPLEMENT_CLASS(wxZlibInputStream, wxFilterInputStream) +IMPLEMENT_CLASS(wxZlibOutputStream, wxFilterOutputStream) +#endif + +////////////////////// +// wxZlibInputStream +////////////////////// + +wxZlibInputStream::wxZlibInputStream(wxInputStream& stream) + : wxFilterInputStream(stream) +{ + int err; + + m_inflate.zalloc = (alloc_func)0; + m_inflate.zfree = (free_func)0; + m_inflate.opaque = (voidpf)0; + + err = inflateInit(&m_inflate); + if (err != Z_OK) { + inflateEnd(&m_inflate); + return; + } + + m_inflate.avail_in = 0; +} + +wxZlibInputStream::~wxZlibInputStream() +{ + inflateEnd(&m_inflate); +} + +wxInputStream& wxZlibInputStream::Read(void *buffer, size_t size) +{ + int err; + + m_inflate.next_out = (unsigned char *)buffer; + m_inflate.avail_out = size; + m_eof = FALSE; + + while (m_inflate.avail_out > 0) { + if (m_inflate.avail_in == 0) { + wxFilterInputStream::Read(m_z_buffer, m_z_size); + m_inflate.next_in = m_z_buffer; + m_inflate.avail_in = wxFilterInputStream::LastRead(); + if (wxFilterInputStream::Eof()) { + m_lastread = size - m_inflate.avail_out; + return *this; + } + } + err = inflate(&m_inflate, Z_FINISH); + if (err == Z_STREAM_END) { + m_lastread = size - m_inflate.avail_out; + m_eof = TRUE; + return *this; + } + } + + m_lastread = size; + return *this; +} + +off_t wxZlibInputStream::SeekI(off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode)) +{ + return 0; +} + +off_t wxZlibInputStream::TellI() const +{ + return 0; +} + +bool wxZlibInputStream::Eof() const +{ + if (!m_eof) + return wxFilterInputStream::Eof(); + return m_eof; +} + +////////////////////// +// wxZlibOutputStream +////////////////////// + +wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream) + : wxFilterOutputStream(stream) +{ + int err; + + m_deflate.zalloc = (alloc_func)0; + m_deflate.zfree = (free_func)0; + m_deflate.opaque = (voidpf)0; + + err = deflateInit(&m_deflate, Z_DEFAULT_COMPRESSION); + if (err != Z_OK) { + deflateEnd(&m_deflate); + return; + } + m_deflate.avail_in = 0; + m_deflate.next_out = m_z_buffer; + m_deflate.avail_out = m_z_size; +} + +wxZlibOutputStream::~wxZlibOutputStream() +{ + int err; + + while (1) { + err = deflate(&m_deflate, Z_FINISH); + if (err == Z_STREAM_END) + break; + if (err < 0) { + wxDebugMsg("wxZlibOutputStream: error during final deflate"); + break; + } + if (m_deflate.avail_out == 0) { + wxFilterOutputStream::Write(m_z_buffer, m_z_size); + if (wxFilterOutputStream::Bad()) { + wxDebugMsg("wxZlibOutputStream: error during final write"); + break; + } + m_deflate.next_out = m_z_buffer; + m_deflate.avail_out = m_z_size; + } + } + wxFilterOutputStream::Write(m_z_buffer, m_z_size-m_deflate.avail_out); + + deflateEnd(&m_deflate); +} + +wxOutputStream& wxZlibOutputStream::Write(const void *buffer, size_t size) +{ + int err; + + m_deflate.next_in = (unsigned char *)buffer; + m_deflate.avail_in = size; + + m_bad = FALSE; + while (m_deflate.avail_in > 0) { + if (m_deflate.avail_out == 0) { + wxFilterOutputStream::Write(m_z_buffer, m_z_size); + if (wxFilterOutputStream::Bad()) { + m_lastwrite = size - m_deflate.avail_in; + return *this; + } + + m_deflate.next_out = m_z_buffer; + m_deflate.avail_out = m_z_size; + } + err = deflate(&m_deflate, Z_NO_FLUSH); + if (err < 0) { + m_bad = TRUE; + m_lastwrite = size - m_deflate.avail_in; + return *this; + } + } + m_lastwrite = size; + return *this; +} + +off_t wxZlibOutputStream::SeekO(off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode)) +{ + return 0; +} + +off_t wxZlibOutputStream::TellO() const +{ + return 0; +} + +bool wxZlibOutputStream::Bad() const +{ + if (!m_bad) + return wxFilterOutputStream::Bad(); + return m_bad; +} diff --git a/src/gtk/data.cpp b/src/gtk/data.cpp index 416bdd9474..1431dd4dc0 100644 --- a/src/gtk/data.cpp +++ b/src/gtk/data.cpp @@ -348,21 +348,23 @@ IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler) #include "wx/stream.h" #include "wx/fstream.h" #include "wx/mstream.h" +#include "wx/zstream.h" #include "wx/datstrm.h" IMPLEMENT_ABSTRACT_CLASS(wxInputStream, wxObject) IMPLEMENT_ABSTRACT_CLASS(wxOutputStream, wxObject) -IMPLEMENT_ABSTRACT_CLASS2(wxStream, wxInputStream, wxOutputStream) IMPLEMENT_CLASS(wxFilterInputStream, wxInputStream) IMPLEMENT_CLASS(wxFilterOutputStream, wxOutputStream) -IMPLEMENT_CLASS(wxFileStreamBase, wxStream) -IMPLEMENT_CLASS(wxFileInputStream, wxFileStreamBase) -IMPLEMENT_CLASS(wxFileOutputStream, wxFileStreamBase) +IMPLEMENT_CLASS(wxFileInputStream, wxInputStream) +IMPLEMENT_CLASS(wxFileOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxFileStream, wxFileInputStream, wxFileOutputStream) -IMPLEMENT_CLASS(wxMemoryStreamBase, wxStream) -IMPLEMENT_CLASS(wxMemoryInputStream, wxMemoryStreamBase) -IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxMemoryStreamBase) -IMPLEMENT_DYNAMIC_CLASS(wxMemoryStream, wxMemoryStreamBase) +IMPLEMENT_CLASS(wxMemoryInputStream, wxInputStream) +IMPLEMENT_CLASS(wxMemoryOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxMemoryStream, wxMemoryInputStream, wxMemoryOutputStream) + +IMPLEMENT_CLASS(wxZlibInputStream, wxFilterInputStream) +IMPLEMENT_CLASS(wxZlibOutputStream, wxFilterOutputStream) IMPLEMENT_CLASS(wxDataInputStream, wxFilterInputStream) IMPLEMENT_CLASS(wxDataOutputStream, wxFilterInputStream) diff --git a/src/gtk1/data.cpp b/src/gtk1/data.cpp index 416bdd9474..1431dd4dc0 100644 --- a/src/gtk1/data.cpp +++ b/src/gtk1/data.cpp @@ -348,21 +348,23 @@ IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler) #include "wx/stream.h" #include "wx/fstream.h" #include "wx/mstream.h" +#include "wx/zstream.h" #include "wx/datstrm.h" IMPLEMENT_ABSTRACT_CLASS(wxInputStream, wxObject) IMPLEMENT_ABSTRACT_CLASS(wxOutputStream, wxObject) -IMPLEMENT_ABSTRACT_CLASS2(wxStream, wxInputStream, wxOutputStream) IMPLEMENT_CLASS(wxFilterInputStream, wxInputStream) IMPLEMENT_CLASS(wxFilterOutputStream, wxOutputStream) -IMPLEMENT_CLASS(wxFileStreamBase, wxStream) -IMPLEMENT_CLASS(wxFileInputStream, wxFileStreamBase) -IMPLEMENT_CLASS(wxFileOutputStream, wxFileStreamBase) +IMPLEMENT_CLASS(wxFileInputStream, wxInputStream) +IMPLEMENT_CLASS(wxFileOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxFileStream, wxFileInputStream, wxFileOutputStream) -IMPLEMENT_CLASS(wxMemoryStreamBase, wxStream) -IMPLEMENT_CLASS(wxMemoryInputStream, wxMemoryStreamBase) -IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxMemoryStreamBase) -IMPLEMENT_DYNAMIC_CLASS(wxMemoryStream, wxMemoryStreamBase) +IMPLEMENT_CLASS(wxMemoryInputStream, wxInputStream) +IMPLEMENT_CLASS(wxMemoryOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxMemoryStream, wxMemoryInputStream, wxMemoryOutputStream) + +IMPLEMENT_CLASS(wxZlibInputStream, wxFilterInputStream) +IMPLEMENT_CLASS(wxZlibOutputStream, wxFilterOutputStream) IMPLEMENT_CLASS(wxDataInputStream, wxFilterInputStream) IMPLEMENT_CLASS(wxDataOutputStream, wxFilterInputStream) diff --git a/src/msw/data.cpp b/src/msw/data.cpp index 7354a35f1a..55e92191a3 100644 --- a/src/msw/data.cpp +++ b/src/msw/data.cpp @@ -365,18 +365,19 @@ IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler) #include "wx/datstrm.h" IMPLEMENT_ABSTRACT_CLASS(wxInputStream, wxObject) IMPLEMENT_ABSTRACT_CLASS(wxOutputStream, wxObject) -IMPLEMENT_ABSTRACT_CLASS2(wxStream, wxInputStream, wxOutputStream) IMPLEMENT_CLASS(wxFilterInputStream, wxInputStream) IMPLEMENT_CLASS(wxFilterOutputStream, wxOutputStream) -IMPLEMENT_CLASS(wxFileStreamBase, wxStream) -IMPLEMENT_CLASS(wxFileInputStream, wxFileStreamBase) -IMPLEMENT_CLASS(wxFileOutputStream, wxFileStreamBase) +IMPLEMENT_CLASS(wxFileInputStream, wxInputStream) +IMPLEMENT_CLASS(wxFileOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxFileStream, wxFileInputStream, wxFileOutputStream) -IMPLEMENT_CLASS(wxMemoryStreamBase, wxStream) -IMPLEMENT_CLASS(wxMemoryInputStream, wxMemoryStreamBase) -IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxMemoryStreamBase) -IMPLEMENT_DYNAMIC_CLASS(wxMemoryStream, wxMemoryStreamBase) +IMPLEMENT_CLASS(wxMemoryInputStream, wxInputStream) +IMPLEMENT_CLASS(wxMemoryOutputStream, wxOutputStream) +IMPLEMENT_CLASS2(wxMemoryStream, wxMemoryInputStream, wxMemoryOutputStream) + +IMPLEMENT_CLASS(wxZlibInputStream, wxFilterInputStream) +IMPLEMENT_CLASS(wxZlibOutputStream, wxFilterOutputStream) IMPLEMENT_CLASS(wxDataInputStream, wxFilterInputStream) IMPLEMENT_CLASS(wxDataOutputStream, wxFilterInputStream) diff --git a/src/msw/makefile.b32 b/src/msw/makefile.b32 index d3fff4e0cc..c9f885f26a 100644 --- a/src/msw/makefile.b32 +++ b/src/msw/makefile.b32 @@ -108,6 +108,7 @@ COMMONOBJS = \ $(MSWDIR)\stream.obj \ $(MSWDIR)\fstream.obj \ $(MSWDIR)\mstream.obj \ + $(MSWDIR)\zstream.obj \ $(MSWDIR)\datstrm.obj \ $(MSWDIR)\extended.obj @@ -470,6 +471,8 @@ $(MSWDIR)\datstrm.obj: $(COMMDIR)\datstrm.$(SRCSUFF) $(MSWDIR)\mstream.obj: $(COMMDIR)\mstream.$(SRCSUFF) +$(MSWDIR)\zstream.obj: $(COMMDIR)\zstream.$(SRCSUFF) + $(MSWDIR)\fstream.obj: $(COMMDIR)\fstream.$(SRCSUFF) $(MSWDIR)\stream.obj: $(COMMDIR)\stream.$(SRCSUFF) diff --git a/src/msw/makefile.dos b/src/msw/makefile.dos index a5b5c3b10e..4ce5637491 100644 --- a/src/msw/makefile.dos +++ b/src/msw/makefile.dos @@ -109,6 +109,7 @@ COMMONOBJS = \ $(COMMDIR)\stream.obj \ $(COMMDIR)\fstream.obj \ $(COMMDIR)\mstream.obj \ + $(COMMDIR)\zstream.obj \ $(COMMDIR)\datstrm.obj \ $(COMMDIR)\extended.obj diff --git a/src/msw/makefile.g95 b/src/msw/makefile.g95 index 5f7ff56435..f3a34442d6 100644 --- a/src/msw/makefile.g95 +++ b/src/msw/makefile.g95 @@ -114,6 +114,7 @@ COMMONOBJS = \ $(COMMDIR)/stream.$(OBJSUFF) \ $(COMMDIR)/fstream.$(OBJSUFF) \ $(COMMDIR)/mstream.$(OBJSUFF) \ + $(COMMDIR)/zstream.$(OBJSUFF) \ $(COMMDIR)/datstrm.$(OBJSUFF) \ $(COMMDIR)/extended.$(OBJSUFF) diff --git a/src/msw/makefile.nt b/src/msw/makefile.nt index 1e2e750054..b4c7e6f8d6 100644 --- a/src/msw/makefile.nt +++ b/src/msw/makefile.nt @@ -109,11 +109,11 @@ COMMONOBJS = \ $(COMMDIR)\y_tab.obj \ $(COMMDIR)\extended.obj \ $(COMMDIR)\process.obj - -# $(COMMDIR)\fstream.obj \ -# $(COMMDIR)\mstream.obj \ -# $(COMMDIR)\datstrm.obj \ -# $(COMMDIR)\stream.obj \ + $(COMMDIR)\fstream.obj \ + $(COMMDIR)\mstream.obj \ + $(COMMDIR)\zstream.obj \ + $(COMMDIR)\stream.obj \ + $(COMMDIR)\datstrm.obj MSWOBJS = \ $(MSWDIR)\app.obj \ @@ -889,6 +889,26 @@ $(COMMDIR)/time.obj: $*.$(SRCSUFF) $(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@ << +$(COMMDIR)\stream.obj: $*.$(SRCSUFF) + cl @<< +$(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@ +<< + +$(COMMDIR)\fstream.obj: $*.$(SRCSUFF) + cl @<< +$(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@ +<< + +$(COMMDIR)\mstream.obj: $*.$(SRCSUFF) + cl @<< +$(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@ +<< + +$(COMMDIR)\zstream.obj: $*.$(SRCSUFF) + cl @<< +$(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@ +<< + $(COMMDIR)\datstrm.obj: $*.$(SRCSUFF) cl @<< $(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@ -- 2.47.2