]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/stream.cpp
file I forgot to add...
[wxWidgets.git] / src / common / stream.cpp
index 4f24cfc97aeab89cf40b0362a65e701d6e20f1a8..9750fef812d9aebf254c8a85196076dcc8dcd90d 100644 (file)
 
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
+
+#ifdef __BORLANDC__
+  #pragma hdrstop
+#endif
+
+#ifndef WX_PRECOMP
+  #include "wx/defs.h"
+#endif
+
+#if wxUSE_STREAMS
+
 #include <ctype.h>
 #include <wx/stream.h>
 #include <wx/datstrm.h>
 #include <wx/objstrm.h>
 
-#ifdef __BORLANDC__
-#pragma hdrstop
-#endif
-
 #define BUF_TEMP_SIZE 10000
 
 // ----------------------------------------------------------------------------
@@ -82,6 +89,9 @@ size_t wxStreamBuffer::WriteBack(const char *buf, size_t bufsize)
 {
   char *ptrback;
 
+  if (m_mode != read)
+    return 0;
+
   ptrback = AllocSpaceWBack(bufsize);
   if (!ptrback)
     return 0;
@@ -136,6 +146,8 @@ void wxStreamBuffer::SetBufferIO(size_t bufsize)
 
 void wxStreamBuffer::ResetBuffer()
 {
+  m_stream->m_lasterror = wxStream_NOERROR;
+  m_stream->m_lastcount = 0;
   if (m_mode == read)
     m_buffer_pos = m_buffer_end;
   else
@@ -156,7 +168,7 @@ char *wxStreamBuffer::AllocSpaceWBack(size_t needed_size)
   if (!temp_b)
     return NULL;
   m_wback = temp_b;
-  printf("Buffer(0x%x)->Write: 0x%x, %d\n", this, m_wback, m_wbacksize); 
+  
   return (char *)(m_wback+(m_wbacksize-needed_size));
 }
 
@@ -164,7 +176,6 @@ size_t wxStreamBuffer::GetWBack(char *buf, size_t bsize)
 {
   size_t s_toget = m_wbacksize-m_wbackcur;
 
-  printf("Buffer(0x%x): 0x%x, %d\n", this, m_wback, m_wbacksize);
   if (bsize < s_toget)
     s_toget = bsize;
 
@@ -247,8 +258,8 @@ void wxStreamBuffer::PutChar(char c)
     return;
   }
 
-  if (!GetDataLeft() && !FlushBuffer()) {
-    CHECK_ERROR(wxStream_READ_ERR);
+  if (GetDataLeft() == 0 && !FlushBuffer()) {
+    CHECK_ERROR(wxStream_WRITE_ERR);
     return;
   }
 
@@ -273,6 +284,7 @@ char wxStreamBuffer::GetChar()
   }
 
   GetFromBuffer(&c, 1);
+  
   m_stream->m_lastcount = 1;
   return c;
 }
@@ -281,10 +293,14 @@ size_t wxStreamBuffer::Read(void *buffer, size_t size)
 {
   wxASSERT(m_stream != NULL);
 
+  if (m_mode == write)
+    return 0;
+
   // ------------------
   // Buffering disabled
   // ------------------
 
+  m_stream->m_lasterror = wxStream_NOERROR;
   m_stream->m_lastcount = GetWBack((char *)buffer, size);
   size -= m_stream->m_lastcount;
   if (size == 0)
@@ -329,7 +345,10 @@ size_t wxStreamBuffer::Read(wxStreamBuffer *s_buf)
   char buf[BUF_TEMP_SIZE];
   size_t s = 0, bytes_read = BUF_TEMP_SIZE;
 
-  while (bytes_read == BUF_TEMP_SIZE) {
+  if (m_mode == write)
+    return 0;
+
+  while (bytes_read != 0) {
     bytes_read = Read(buf, bytes_read);
     bytes_read = s_buf->Write(buf, bytes_read);
     s += bytes_read;
@@ -341,10 +360,14 @@ size_t wxStreamBuffer::Write(const void *buffer, size_t size)
 {
   wxASSERT(m_stream != NULL);
 
+  if (m_mode == read)
+    return 0;
+
   // ------------------
   // Buffering disabled
   // ------------------
 
+  m_stream->m_lasterror = wxStream_NOERROR;
   if (!m_buffer_size)
     return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size));
 
@@ -386,6 +409,9 @@ size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf)
   char buf[BUF_TEMP_SIZE];
   size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2;
 
+  if (m_mode == read)
+    return 0;
+
   while (bytes_count == BUF_TEMP_SIZE) {
     b_count2 = sbuf->Read(buf, bytes_count);
     bytes_count = Write(buf, b_count2);
@@ -578,28 +604,29 @@ wxInputStream& wxInputStream::operator>>(char& c)
   return *this;
 }
 
-wxInputStream& wxInputStream::operator>>(short& i)
+wxInputStream& wxInputStream::operator>>(signed short& i)
 {
-  long l;
+  signed long l;
 
   *this >> l;
-  i = (short)l;
+  i = (signed short)l;
   return *this;
 }
 
-wxInputStream& wxInputStream::operator>>(int& i)
+wxInputStream& wxInputStream::operator>>(signed int& i)
 {
-  long l;
+  signed long l;
 
   *this >> l;
-  i = (short)l;
+  i = (signed int)l;
   return *this;
 }
 
-wxInputStream& wxInputStream::operator>>(long& i)
+wxInputStream& wxInputStream::operator>>(signed long& i)
 {
   /* I only implemented a simple integer parser */
-  int c, sign;
+  int c; 
+  int sign;
 
   while (isspace( c = GetC() ) )
      /* Do nothing */ ;
@@ -613,11 +640,15 @@ wxInputStream& wxInputStream::operator>>(long& i)
   if (c == '-') {
     sign = -1;
     c = GetC();
-  } else
+  } else if (c == '+') {
     sign = 1;
+    c = GetC();
+  } else {
+    sign = 1;
+  }
 
   while (isdigit(c)) {
-    i = i*10 + c;
+    i = i*10 + (c - (int)'0');
     c = GetC();
   }
 
@@ -626,6 +657,46 @@ wxInputStream& wxInputStream::operator>>(long& i)
   return *this;
 }
 
+wxInputStream& wxInputStream::operator>>(unsigned short& i)
+{
+  unsigned long l;
+
+  *this >> l;
+  i = (unsigned short)l;
+  return *this;
+}
+
+wxInputStream& wxInputStream::operator>>(unsigned int& i)
+{
+  unsigned long l;
+
+  *this >> l;
+  i = (unsigned int)l;
+  return *this;
+}
+
+wxInputStream& wxInputStream::operator>>(unsigned long& i)
+{
+  /* I only implemented a simple integer parser */
+  int c;
+
+  while (isspace( c = GetC() ) )
+     /* Do nothing */ ;
+
+  i = 0;
+  if (!isdigit(c)) {
+    InputStreamBuffer()->WriteBack(c);
+    return *this;
+  }
+
+  while (isdigit(c)) {
+    i = i*10 + c - '0';
+    c = GetC();
+  }
+
+  return *this;
+}
+
 wxInputStream& wxInputStream::operator>>(double& f)
 {
   /* I only implemented a simple float parser */
@@ -643,8 +714,12 @@ wxInputStream& wxInputStream::operator>>(double& f)
   if (c == '-') {
     sign = -1;
     c = GetC();
-  } else
+  } else if (c == '+') {
+    sign = 1;
+    c = GetC();
+  } else {
     sign = 1;
+  }
 
   while (isdigit(c)) {
     f = f*10 + (c - '0');
@@ -734,7 +809,12 @@ wxOutputStream& wxOutputStream::operator<<(const char *string)
 
 wxOutputStream& wxOutputStream::operator<<(wxString& string)
 {
+#if wxUSE_UNICODE
+  const wxWX2MBbuf buf = string.mb_str();
+  return *this << buf;
+#else
   return Write(string, string.Len());
+#endif
 }
 
 wxOutputStream& wxOutputStream::operator<<(char c)
@@ -742,36 +822,50 @@ wxOutputStream& wxOutputStream::operator<<(char c)
   return Write(&c, 1);
 }
 
-wxOutputStream& wxOutputStream::operator<<(short i)
+wxOutputStream& wxOutputStream::operator<<(signed short i)
 {
-  wxString strint;
+  signed long l = (signed long)i;
+  return *this << l;
+}
 
-  strint.Printf("%i", i);
-  return Write(strint, strint.Len());
+wxOutputStream& wxOutputStream::operator<<(signed int i)
+{
+  signed long l = (signed long)i;
+  return *this << l;
 }
 
-wxOutputStream& wxOutputStream::operator<<(int i)
+wxOutputStream& wxOutputStream::operator<<(signed long i)
 {
-  wxString strint;
+  wxString strlong;
+  strlong.Printf(_T("%ld"), i);
+  return *this << strlong;
+}
 
-  strint.Printf("%i", i);
-  return Write(strint, strint.Len());
+wxOutputStream& wxOutputStream::operator<<(unsigned short i)
+{
+  unsigned long l = (unsigned long)i;
+  return *this << l;
 }
 
-wxOutputStream& wxOutputStream::operator<<(long i)
+wxOutputStream& wxOutputStream::operator<<(unsigned int i)
 {
-  wxString strlong;
+  unsigned long l = (unsigned long)i;
+  return *this << l;
+}
 
-  strlong.Printf("%i", i);
-  return Write((const char *)strlong, strlong.Len());
+wxOutputStream& wxOutputStream::operator<<(unsigned long i)
+{
+  wxString strlong;
+  strlong.Printf(_T("%lu"), i);
+  return *this << strlong;
 }
 
 wxOutputStream& wxOutputStream::operator<<(double f)
 {
   wxString strfloat;
 
-  strfloat.Printf("%f", f);
-  return Write(strfloat, strfloat.Len());
+  strfloat.Printf(_T("%f"), f);
+  return *this << strfloat;
 }
 
 #if wxUSE_SERIAL
@@ -832,3 +926,6 @@ wxOutputStream& wxEndL(wxOutputStream& stream)
   return stream.Write("\n", 1);
 #endif
 }
+
+#endif
+  // wxUSE_STREAMS