Dynamically allocate more memory if the string buffer runs out of space when
authorarjen <arjen>
Fri, 4 May 2007 13:55:18 +0000 (13:55 +0000)
committerarjen <arjen>
Fri, 4 May 2007 13:55:18 +0000 (13:55 +0000)
reading a String object from an input stream.

src/string.cpp

index c1cb1b8..352e638 100644 (file)
@@ -5,7 +5,7 @@
 ***********************
 **      FILE NAME      : string.cpp
 **      SYSTEM NAME    : Andromeda X-Windows Encapsulation
-**      VERSION NUMBER : $Revision: 1.4 $
+**      VERSION NUMBER : $Revision: 1.5 $
 **
 **  DESCRIPTION      :  String class implementation.
 **
 ********************************
 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
 **      CREATION DATE   : Nov 17, 1997
-**      LAST UPDATE     : Nov 03, 2002
+**      LAST UPDATE     : Nov 30, 2003
 **      MODIFICATIONS   : 
 **************************************************************************/
 
 /*****************************
    $Log: string.cpp,v $
-   Revision 1.4  2003-03-29 07:18:54  arjen
+   Revision 1.5  2007-05-04 13:55:18  arjen
+   Dynamically allocate more memory if the string buffer runs out of space when
+   reading a String object from an input stream.
+
+   Revision 1.4  2003/03/29 07:18:54  arjen
    String constructor and assignment from char * are more robust fro NULL pointers.
 
    Revision 1.3  2002/11/03 13:18:57  arjen
@@ -37,7 +41,7 @@
 
 *****************************/
 
-static const char RCSID[] = "$Id: string.cpp,v 1.4 2003-03-29 07:18:54 arjen Exp $";
+static const char RCSID[] = "$Id: string.cpp,v 1.5 2007-05-04 13:55:18 arjen Exp $";
 
 #include <stdio.h>
 #include <ctype.h>
@@ -379,12 +383,27 @@ std::ostream& operator<<(std::ostream& s, const String& x)
 
 std::istream& operator>>(std::istream& s, String& x)
 {
-   char  buf[1024];
+   char  *buf;
    int   i;
 
+   int   bufsize = 1024;
+
+   buf = new char[bufsize];
+
    i = -1;
    do
    {
+      if (i >= bufsize)
+      {
+         //  Buffer is too small. Allocate some new space.
+
+         char *newbuf = new char[bufsize * 2];
+         memcpy(newbuf, buf, bufsize);
+         delete [] buf;
+         buf = newbuf;
+         bufsize *= 2;
+      }
+
       i++;
       s.get(buf[i]);
    }
@@ -394,6 +413,9 @@ std::istream& operator>>(std::istream& s, String& x)
       buf[i] = '\0';
    }
    x = buf;
+
+   delete [] buf;
+
    return s;
 }