From 53663be8a5ea8e1c6f178839209b67e8228e4642 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Thu, 10 Jul 2003 11:47:27 +0000
Subject: [PATCH] added functions to read/write several elements at once (patch
 754986)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21856 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt           |   1 +
 docs/latex/wx/datistrm.tex |  26 +++++-
 docs/latex/wx/datostrm.tex |  25 ++++++
 include/wx/datstrm.h       |  14 +++-
 src/common/datstrm.cpp     | 160 ++++++++++++++++++++++++++++++++++++-
 5 files changed, 223 insertions(+), 3 deletions(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index a5124cac04..7a74e203cd 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -58,6 +58,7 @@ All:
 - added event sink argument to wxEvtHandler::Connect()
 - added support for POST method and alt ports to wxHTTP (Roger Chickering)
 - added wxSocket::IPAddress() (Chris Mellon)
+- wxDataStreams can read/write many elements at once (Mickael Gilabert)
 
 wxBase:
 
diff --git a/docs/latex/wx/datistrm.tex b/docs/latex/wx/datistrm.tex
index 158ffffb2e..7f45970c4c 100644
--- a/docs/latex/wx/datistrm.tex
+++ b/docs/latex/wx/datistrm.tex
@@ -78,30 +78,55 @@ big-endian order).
 
 Reads a single byte from the stream.
 
+\func{void}{Read8}{\param{wxUint8 *}{buffer}, \param{size\_t }{size}}
+
+Reads bytes from the stream in a specified buffer. The amount of
+bytes to read is specified by the {\it size} variable.
+
 \membersection{wxDataInputStream::Read16}
 
 \func{wxUint16}{Read16}{\void}
 
 Reads a 16 bit unsigned integer from the stream.
 
+\func{void}{Read16}{\param{wxUint16 *}{buffer}, \param{size\_t }{size}}
+
+Reads 16 bit unsigned integers from the stream in a specified buffer. the
+amount of 16 bit unsigned integer to read is specified by the {\it size} variable.
+
 \membersection{wxDataInputStream::Read32}
 
 \func{wxUint32}{Read32}{\void}
 
 Reads a 32 bit unsigned integer from the stream.
 
+\func{void}{Read32}{\param{wxUint32 *}{buffer}, \param{size\_t }{size}}
+
+Reads 32 bit unsigned integers from the stream in a specified buffer. the amount of
+32 bit unsigned integer to read is specified by the {\it size} variable.
+
 \membersection{wxDataInputStream::Read64}
 
 \func{wxUint64}{Read64}{\void}
 
 Reads a 64 bit unsigned integer from the stream.
 
+\func{void}{Read64}{\param{wxUint64 *}{buffer}, \param{size\_t }{size}}
+
+Reads 64 bit unsigned integers from the stream in a specified buffer. the amount of
+64 bit unsigned integer to read is specified by the {\it size} variable.
+
 \membersection{wxDataInputStream::ReadDouble}
 
 \func{double}{ReadDouble}{\void}
 
 Reads a double (IEEE encoded) from the stream.
 
+\func{void}{ReadDouble}{\param{double *}{buffer}, \param{size\_t }{size}}
+
+Reads double data (IEEE encoded) from the stream in a specified buffer. the amount of
+double to read is specified by the {\it size} variable.
+
 \membersection{wxDataInputStream::ReadString}\label{wxdatainputstreamreadstring}
 
 \func{wxString}{ReadString}{\void}
@@ -116,4 +141,3 @@ object passed to constructor and returns the result as wxString. You are
 responsible for using the same convertor as when writing the stream.
 
 See also \helpref{wxDataOutputStream::WriteString}{wxdataoutputstreamwritestring}.
-
diff --git a/docs/latex/wx/datostrm.tex b/docs/latex/wx/datostrm.tex
index 5cb53a3467..c904e34f10 100644
--- a/docs/latex/wx/datostrm.tex
+++ b/docs/latex/wx/datostrm.tex
@@ -60,30 +60,55 @@ little-endian order.
 
 Writes the single byte {\it i8} to the stream.
 
+\func{void}{Write8}{\param{const wxUint8 *}{buffer}, \param{size\_t }{size}}
+
+Writes an array of bytes to the stream. The amount of bytes to write is
+specified with the {\it size} variable.
+
 \membersection{wxDataOutputStream::Write16}
 
 \func{void}{Write16}{{\param wxUint16 }{i16}}
 
 Writes the 16 bit unsigned integer {\it i16} to the stream.
 
+\func{void}{Write16}{\param{const wxUint16 *}{buffer}, \param{size\_t }{size}}
+
+Writes an array of 16 bit unsigned integer to the stream. The amount of
+16 bit unsigned integer to write is specified with the {\it size} variable.
+
 \membersection{wxDataOutputStream::Write32}
 
 \func{void}{Write32}{{\param wxUint32 }{i32}}
 
 Writes the 32 bit unsigned integer {\it i32} to the stream.
 
+\func{void}{Write32}{\param{const wxUint32 *}{buffer}, \param{size\_t }{size}}
+
+Writes an array of 32 bit unsigned integer to the stream. The amount of
+32 bit unsigned integer to write is specified with the {\it size} variable.
+
 \membersection{wxDataOutputStream::Write64}
 
 \func{void}{Write64}{{\param wxUint64 }{i64}}
 
 Writes the 64 bit unsigned integer {\it i64} to the stream.
 
+\func{void}{Write64}{\param{const wxUint64 *}{buffer}, \param{size\_t }{size}}
+
+Writes an array of 64 bit unsigned integer to the stream. The amount of
+64 bit unsigned integer to write is specified with the {\it size} variable.
+
 \membersection{wxDataOutputStream::WriteDouble}
 
 \func{void}{WriteDouble}{{\param double }{f}}
 
 Writes the double {\it f} to the stream using the IEEE format.
 
+\func{void}{WriteDouble}{\param{const double *}{buffer}, \param{size\_t }{size}}
+
+Writes an array of double to the stream. The amount of double to write is
+specified with the {\it size} variable.
+
 \membersection{wxDataOutputStream::WriteString}\label{wxdataoutputstreamwritestring}
 
 \func{void}{WriteString}{{\param const wxString\&}{string}}
diff --git a/include/wx/datstrm.h b/include/wx/datstrm.h
index b36eb1de06..c90ac629f2 100644
--- a/include/wx/datstrm.h
+++ b/include/wx/datstrm.h
@@ -2,7 +2,7 @@
 // Name:        datstrm.h
 // Purpose:     Data stream classes
 // Author:      Guilhem Lavaux
-// Modified by:
+// Modified by: Mickael Gilabert
 // Created:     28/06/1998
 // RCS-ID:      $Id$
 // Copyright:   (c) Guilhem Lavaux
@@ -41,6 +41,12 @@ public:
     double ReadDouble();
     wxString ReadString();
 
+    void Read64(wxUint64 *buffer, size_t size);
+    void Read32(wxUint32 *buffer, size_t size);
+    void Read16(wxUint16 *buffer, size_t size);
+    void Read8(wxUint8 *buffer, size_t size);
+    void ReadDouble(double *buffer, size_t size);
+
     wxDataInputStream& operator>>(wxString& s);
     wxDataInputStream& operator>>(wxInt8& c);
     wxDataInputStream& operator>>(wxInt16& i);
@@ -83,6 +89,12 @@ public:
     void WriteDouble(double d);
     void WriteString(const wxString& string);
 
+    void Write64(const wxUint64 *buffer, size_t size);
+    void Write32(const wxUint32 *buffer, size_t size);
+    void Write16(const wxUint16 *buffer, size_t size);
+    void Write8(const wxUint8 *buffer, size_t size);
+    void WriteDouble(const double *buffer, size_t size);
+
     wxDataOutputStream& operator<<(const wxChar *string);
     wxDataOutputStream& operator<<(const wxString& string);
     wxDataOutputStream& operator<<(wxInt8 c);
diff --git a/src/common/datstrm.cpp b/src/common/datstrm.cpp
index b0ef8302a2..a2c2aa55da 100644
--- a/src/common/datstrm.cpp
+++ b/src/common/datstrm.cpp
@@ -2,7 +2,7 @@
 // Name:        datstrm.cpp
 // Purpose:     Data stream classes
 // Author:      Guilhem Lavaux
-// Modified by:
+// Modified by: Mickael Gilabert
 // Created:     28/06/98
 // RCS-ID:      $Id$
 // Copyright:   (c) Guilhem Lavaux
@@ -126,6 +126,85 @@ wxString wxDataInputStream::ReadString()
     return wxEmptyString;
 }
 
+void wxDataInputStream::Read64(wxUint64 *buffer, size_t size)
+{
+  m_input->Read(buffer, size * 8);
+
+  if (m_be_order)
+  {
+    for (wxUint32 i=0; i<size; i++)
+    {
+      wxUint64 v = wxUINT64_SWAP_ON_LE(*buffer);
+      *(buffer++) = v;
+    }
+  }
+  else
+  {
+    for (wxUint32 i=0; i<size; i++)
+    {
+      wxUint64 v = wxUINT64_SWAP_ON_BE(*buffer);
+      *(buffer++) = v;
+    }
+  }
+}
+
+void wxDataInputStream::Read32(wxUint32 *buffer, size_t size)
+{
+  m_input->Read(buffer, size * 4);
+
+  if (m_be_order)
+  {
+    for (wxUint32 i=0; i<size; i++)
+    {
+      wxUint32 v = wxUINT32_SWAP_ON_LE(*buffer);
+      *(buffer++) = v;
+    }
+  }
+else
+  {
+    for (wxUint32 i=0; i<size; i++)
+    {
+      wxUint32 v = wxUINT32_SWAP_ON_BE(*buffer);
+      *(buffer++) = v;
+    }
+  }
+}
+
+void wxDataInputStream::Read16(wxUint16 *buffer, size_t size)
+{
+  m_input->Read(buffer, size * 2);
+
+  if (m_be_order)
+  {
+    for (wxUint32 i=0; i<size; i++)
+    {
+      wxUint16 v = wxUINT16_SWAP_ON_LE(*buffer);
+      *(buffer++) = v;
+    }
+  }
+  else
+  {
+    for (wxUint32 i=0; i<size; i++)
+    {
+      wxUint16 v = wxUINT16_SWAP_ON_BE(*buffer);
+      *(buffer++) = v;
+    }
+  }
+}
+
+void wxDataInputStream::Read8(wxUint8 *buffer, size_t size)
+{
+  m_input->Read(buffer, size);
+}
+
+void wxDataInputStream::ReadDouble(double *buffer, size_t size)
+{
+  for (wxUint32 i=0; i<size; i++)
+  {
+    *(buffer++) = ReadDouble();
+  }
+}
+
 wxDataInputStream& wxDataInputStream::operator>>(wxString& s)
 {
   s = ReadString();
@@ -274,6 +353,85 @@ void wxDataOutputStream::WriteDouble(double d)
   m_output->Write(buf, 10);
 }
 
+void wxDataOutputStream::Write64(const wxUint64 *buffer, size_t size)
+{
+  if (m_be_order)
+  {
+    for (wxUint32 i=0; i<size ;i++)
+    {
+      wxUint64 i64 = wxUINT64_SWAP_ON_LE(*buffer);
+      buffer++;
+      m_output->Write(&i64, 8);
+    }
+  }
+  else
+  {
+    for (wxUint32 i=0; i<size ;i++)
+    {
+      wxUint64 i64 = wxUINT64_SWAP_ON_BE(*buffer);
+      buffer++;
+      m_output->Write(&i64, 8);
+    }
+  }
+}
+
+void wxDataOutputStream::Write32(const wxUint32 *buffer, size_t size)
+{
+  if (m_be_order)
+  {
+    for (wxUint32 i=0; i<size ;i++)
+    {
+      wxUint32 i32 = wxUINT32_SWAP_ON_LE(*buffer);
+      buffer++;
+      m_output->Write(&i32, 4);
+    }
+  }
+  else
+  {
+    for (wxUint32 i=0; i<size ;i++)
+    {
+      wxUint32 i32 = wxUINT32_SWAP_ON_BE(*buffer);
+      buffer++;
+      m_output->Write(&i32, 4);
+    }
+  }
+}
+
+void wxDataOutputStream::Write16(const wxUint16 *buffer, size_t size)
+{
+  if (m_be_order)
+  {
+    for (wxUint32 i=0; i<size ;i++)
+    {
+      wxUint16 i16 = wxUINT16_SWAP_ON_LE(*buffer);
+      buffer++;
+      m_output->Write(&i16, 2);
+    }
+  }
+  else
+  {
+    for (wxUint32 i=0; i<size ;i++)
+    {
+      wxUint16 i16 = wxUINT16_SWAP_ON_BE(*buffer);
+      buffer++;
+      m_output->Write(&i16, 2);
+    }
+  }
+}
+
+void wxDataOutputStream::Write8(const wxUint8 *buffer, size_t size)
+{
+  m_output->Write(buffer, size);
+}
+
+void wxDataOutputStream::WriteDouble(const double *buffer, size_t size)
+{
+  for (wxUint32 i=0; i<size; i++)
+  {
+    WriteDouble(*(buffer++));
+  }
+}
+
 wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string)
 {
   Write32(wxStrlen(string));
-- 
2.50.0

