From: arjen Date: Fri, 4 May 2007 13:55:18 +0000 (+0000) Subject: Dynamically allocate more memory if the string buffer runs out of space when X-Git-Tag: AXE_0_4~3 X-Git-Url: http://www.andromeda.nl/gitweb/?p=AXE.git;a=commitdiff_plain;h=589f864578ff59a36815af5d897d2d94b9fe5578 Dynamically allocate more memory if the string buffer runs out of space when reading a String object from an input stream. --- diff --git a/src/string.cpp b/src/string.cpp index c1cb1b8..352e638 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -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. ** @@ -17,13 +17,17 @@ ******************************** ** 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 #include @@ -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; }