From 121fa06ab6af9129e776af13949fc251a3cafc49 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sun, 19 Sep 2004 21:26:45 +0000
Subject: [PATCH] added wxStringStream classes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29223 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 build/bakefiles/files.bkl  |   2 +
 docs/changes.txt           |   1 +
 docs/latex/wx/category.tex |   2 +
 docs/latex/wx/classes.tex  |   2 +
 docs/latex/wx/sistream.tex |  35 +++++++++++
 docs/latex/wx/sostream.tex |  43 ++++++++++++++
 include/wx/sstream.h       |  89 ++++++++++++++++++++++++++++
 src/common/sstream.cpp     | 117 +++++++++++++++++++++++++++++++++++++
 8 files changed, 291 insertions(+)
 create mode 100644 docs/latex/wx/sistream.tex
 create mode 100644 docs/latex/wx/sostream.tex
 create mode 100644 include/wx/sstream.h
 create mode 100644 src/common/sstream.cpp

diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl
index a0063646f0..a54d6e654c 100644
--- a/build/bakefiles/files.bkl
+++ b/build/bakefiles/files.bkl
@@ -176,6 +176,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
     src/common/object.cpp
     src/common/process.cpp
     src/common/regex.cpp
+    src/common/sstream.cpp
     src/common/stopwatch.cpp
     src/common/strconv.cpp
     src/common/stream.cpp
@@ -272,6 +273,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
     wx/regex.h
     wx/scopeguard.h
     wx/snglinst.h
+    wx/sstream.h
     wx/stack.h
     wx/stockitem.h
     wx/stopwatch.h
diff --git a/docs/changes.txt b/docs/changes.txt
index dd17b1bbde..8d1e1b4612 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -210,6 +210,7 @@ All:
 - deprecated wxDateTime::SetToTheWeek() in favour of SetToWeekOfYear()
 - active mode support in wxFTP (Randall Fox)
 - sped up wxHTTP and wxFTP
+- added wxStringInput/OutputStreams
 
 All (GUI):
 
diff --git a/docs/latex/wx/category.tex b/docs/latex/wx/category.tex
index 15c650d73e..f32ccb78da 100644
--- a/docs/latex/wx/category.tex
+++ b/docs/latex/wx/category.tex
@@ -491,6 +491,8 @@ libraries, and to provide enhanced functionality.
 \twocolitem{\helpref{wxFileOutputStream}{wxfileoutputstream}}{File output stream class}
 \twocolitem{\helpref{wxFFileInputStream}{wxffileinputstream}}{Another file input stream class}
 \twocolitem{\helpref{wxFFileOutputStream}{wxffileoutputstream}}{Another file output stream class}
+\twocolitem{\helpref{wxStringInputStream}{wxstringinputstream}}{String input stream class}
+\twocolitem{\helpref{wxStringOutputStream}{wxstringoutputstream}}{String output stream class}
 \twocolitem{\helpref{wxZlibInputStream}{wxzlibinputstream}}{Zlib (compression) input stream class}
 \twocolitem{\helpref{wxZlibOutputStream}{wxzliboutputstream}}{Zlib (compression) output stream class}
 \twocolitem{\helpref{wxZipInputStream}{wxzipinputstream}}{Input stream for reading from ZIP archives}
diff --git a/docs/latex/wx/classes.tex b/docs/latex/wx/classes.tex
index 2f72237211..b9d1e08456 100644
--- a/docs/latex/wx/classes.tex
+++ b/docs/latex/wx/classes.tex
@@ -296,7 +296,9 @@
 \input strtotxt.tex
 \input wxstring.tex
 \input strcldat.tex
+\input sistream.tex
 \input strlist.tex
+\input sostream.tex
 \input tokenizr.tex
 \input sysclevt.tex
 \input sysopt.tex
diff --git a/docs/latex/wx/sistream.tex b/docs/latex/wx/sistream.tex
new file mode 100644
index 0000000000..09f372c447
--- /dev/null
+++ b/docs/latex/wx/sistream.tex
@@ -0,0 +1,35 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% Name:        sistream.tex
+%% Purpose:     wxStringInputStream docs
+%% Author:      Vadim Zeitlin
+%% Modified by:
+%% Created:     2004-09-19
+%% RCS-ID:      $Id$
+%% Copyright:   (c) 2004 Vadim Zeitlin
+%% License:     wxWidgets licence
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\section{\class{wxStringInputStream}}\label{wxstringinputstream}
+
+This class implements an input stream which reads data from a string. It
+supports seeking.
+
+\wxheading{Derived from}
+
+\helpref{wxInputStream}{wxinputstream}
+
+\wxheading{Include files}
+
+<wx/sckstrm.h>
+
+
+\latexignore{\rtfignore{\wxheading{Members}}}
+
+\membersection{wxStringInputStream::wxStringInputStream}
+
+\func{}{wxStringInputStream}{\param{const wxString\&}{ s}}
+
+Creates a new read-only stream using the specified string. Note that the string
+is copied by the stream so if the original string is modified after using this
+constructor, changes to it are not reflected when reading from stream. 
+
diff --git a/docs/latex/wx/sostream.tex b/docs/latex/wx/sostream.tex
new file mode 100644
index 0000000000..1f64d55d16
--- /dev/null
+++ b/docs/latex/wx/sostream.tex
@@ -0,0 +1,43 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% Name:        sostream.tex
+%% Purpose:     wxStringOutputStream docs
+%% Author:      Vadim Zeitlin
+%% Modified by:
+%% Created:     2004-09-19
+%% RCS-ID:      $Id$
+%% Copyright:   (c) 2004 Vadim Zeitlin
+%% License:     wxWidgets licence
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\section{\class{wxStringOutputStream}}\label{wxstringoutputstream}
+
+This class implements an output stream which writes data either to a
+user-provided or internally allocated string. Note that currently this stream
+does not support seeking.
+
+\wxheading{Derived from}
+
+\helpref{wxOutputStream}{wxoutputstream}
+
+\wxheading{Include files}
+
+<wx/sckstrm.h>
+
+
+\latexignore{\rtfignore{\wxheading{Members}}}
+
+\membersection{wxStringOutputStream::wxStringOutputStream}
+
+\func{}{wxStringOutputStream}{\param{wxString}{ *str = \texttt{NULL}}}
+
+If the provided pointer is non-\texttt{NULL}, data will be written to it.
+Otherwise, an internal string is used for the data written to this stream, use 
+\helpref{GetString()}{wxstringoutputstreamgetstring} to get access to it.
+
+
+\membersection{wxStringOutputStream::GetString}\label{wxstringoutputstreamgetstring}
+
+\constfunc{const wxString\&}{GetString}{\void}
+
+Returns the string containing all the data written to the stream so far.
+
diff --git a/include/wx/sstream.h b/include/wx/sstream.h
new file mode 100644
index 0000000000..7ff7cf9d83
--- /dev/null
+++ b/include/wx/sstream.h
@@ -0,0 +1,89 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        wx/sstream.h
+// Purpose:     string-based streams
+// Author:      Vadim Zeitlin
+// Modified by:
+// Created:     2004-09-19
+// RCS-ID:      $Id$
+// Copyright:   (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
+// Licence:     wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef _WX_SSTREAM_H_
+#define _WX_SSTREAM_H_
+
+#include "wx/stream.h"
+
+#if wxUSE_STREAMS
+
+// ----------------------------------------------------------------------------
+// wxStringInputStream is a stream reading from the given (fixed size) string
+// ----------------------------------------------------------------------------
+
+class WXDLLIMPEXP_BASE wxStringInputStream : public wxInputStream
+{
+public:
+    // ctor associates the stream with the given string which makes a copy of
+    // it
+    wxStringInputStream(const wxString& s)
+        : m_str(s)
+    {
+        m_pos = 0;
+    }
+
+protected:
+    virtual size_t GetSize() const { return m_str.length(); }
+    virtual off_t OnSysSeek(off_t ofs, wxSeekMode mode);
+    virtual off_t OnSysTell() const;
+    virtual size_t OnSysRead(void *buffer, size_t size);
+
+private:
+    // the string we're reading from
+    wxString m_str;
+
+    // position in the stream in bytes, *not* in chars
+    size_t m_pos;
+
+
+    DECLARE_NO_COPY_CLASS(wxStringInputStream)
+};
+
+// ----------------------------------------------------------------------------
+// wxStringOutputStream writes data to the given string, expanding it as needed
+// ----------------------------------------------------------------------------
+
+class WXDLLIMPEXP_BASE wxStringOutputStream : public wxOutputStream
+{
+public:
+    // The stream will write data either to the provided string or to an
+    // internal string which can be retrieved using GetString()
+    wxStringOutputStream(wxString *pString = NULL)
+    {
+        m_str = pString ? pString : &m_strInternal;
+        m_pos = m_str->length() / sizeof(wxChar);
+    }
+
+    // get the string containing current output
+    const wxString& GetString() const { return *m_str; }
+
+protected:
+    virtual size_t OnSysWrite(const void *buffer, size_t size);
+
+private:
+    // internal string, not used if caller provided his own string
+    wxString m_strInternal;
+
+    // pointer given by the caller or just pointer to m_strInternal
+    wxString *m_str;
+
+    // position in the stream in bytes, *not* in chars
+    size_t m_pos;
+
+
+    DECLARE_NO_COPY_CLASS(wxStringOutputStream)
+};
+
+#endif // wxUSE_STREAMS
+
+#endif // _WX_SSTREAM_H_
+
diff --git a/src/common/sstream.cpp b/src/common/sstream.cpp
new file mode 100644
index 0000000000..7ceedb65b5
--- /dev/null
+++ b/src/common/sstream.cpp
@@ -0,0 +1,117 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        common/sstream.cpp
+// Purpose:     string-based streams implementation
+// Author:      Vadim Zeitlin
+// Modified by:
+// Created:     2004-09-19
+// RCS-ID:      $Id$
+// Copyright:   (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
+// Licence:     wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
+
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#ifdef __BORLANDC__
+    #pragma hdrstop
+#endif
+
+#if wxUSE_STREAMS
+
+#include "wx/sstream.h"
+
+// ============================================================================
+// wxStringInputStream implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// seek/tell
+// ----------------------------------------------------------------------------
+
+off_t wxStringInputStream::OnSysSeek(off_t ofs, wxSeekMode mode)
+{
+    switch ( mode )
+    {
+        case wxFromStart:
+            // nothing to do, ofs already ok
+            break;
+
+        case wxFromEnd:
+            ofs += m_str.length()*sizeof(wxChar);
+            break;
+
+        case wxFromCurrent:
+            ofs += m_pos;
+            break;
+
+        default:
+            wxFAIL_MSG( _T("invalid seek mode") );
+            return wxInvalidOffset;
+    }
+
+    m_pos = wx_static_cast(size_t, ofs);
+
+    return ofs;
+}
+
+off_t wxStringInputStream::OnSysTell() const
+{
+    return wx_static_cast(off_t, m_pos);
+}
+
+// ----------------------------------------------------------------------------
+// actual IO
+// ----------------------------------------------------------------------------
+
+size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
+{
+    const size_t sizeMax = m_str.length()*sizeof(wxChar) - m_pos;
+
+    if ( size >= sizeMax )
+    {
+        if ( sizeMax == 0 )
+        {
+            m_lasterror = wxSTREAM_EOF;
+            return 0;
+        }
+
+        size = sizeMax;
+    }
+
+    memcpy(buffer, m_str.data() + m_pos, size);
+    m_pos += size;
+
+    return size;
+}
+
+// ============================================================================
+// wxStringOutputStream implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// actual IO
+// ----------------------------------------------------------------------------
+
+size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
+{
+    // in Unicode mode we might not be able to write the last byte
+    size_t len = size / sizeof(wxChar);
+
+    const wxChar *p = wx_static_cast(const wxChar *, buffer);
+
+    m_str->Append(wxString(p, p + len + 1));
+
+    // return number of bytes actually written
+    return len*sizeof(wxChar);
+}
+
+#endif // wxUSE_STREAMS
+
-- 
2.50.0

