added wxStringStream classes
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 19 Sep 2004 21:26:45 +0000 (21:26 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 19 Sep 2004 21:26:45 +0000 (21:26 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29223 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

build/bakefiles/files.bkl
docs/changes.txt
docs/latex/wx/category.tex
docs/latex/wx/classes.tex
docs/latex/wx/sistream.tex [new file with mode: 0644]
docs/latex/wx/sostream.tex [new file with mode: 0644]
include/wx/sstream.h [new file with mode: 0644]
src/common/sstream.cpp [new file with mode: 0644]

index a0063646f0f4ce5c23567dcca9b1325ecb1513c9..a54d6e654c2991da6ea022955a67a334a7ea033b 100644 (file)
@@ -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
index dd17b1bbde3018fca73c8ff2e544db5247fcda4f..8d1e1b4612dc224cf138a8fa73ddc78f68070764 100644 (file)
@@ -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):
 
index 15c650d73e0214c21d01a260ce4674e32d44888b..f32ccb78dab216f88bdb7fb9ac5af1fdf84fbfd5 100644 (file)
@@ -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}
index 2f722372112bb8e97e15de327b087d789073faae..b9d1e08456958cb1656a06d3bc2b61552d8655e1 100644 (file)
 \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 (file)
index 0000000..09f372c
--- /dev/null
@@ -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 (file)
index 0000000..1f64d55
--- /dev/null
@@ -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 (file)
index 0000000..7ff7cf9
--- /dev/null
@@ -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 (file)
index 0000000..7ceedb6
--- /dev/null
@@ -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
+