String constructor and assignment from char * are more robust fro NULL pointers.
authorarjen <arjen>
Sat, 29 Mar 2003 07:18:54 +0000 (07:18 +0000)
committerarjen <arjen>
Sat, 29 Mar 2003 07:18:54 +0000 (07:18 +0000)
src/string.cpp

index 1bcded9..c1cb1b8 100644 (file)
@@ -5,7 +5,7 @@
 ***********************
 **      FILE NAME      : string.cpp
 **      SYSTEM NAME    : Andromeda X-Windows Encapsulation
-**      VERSION NUMBER : $Revision: 1.3 $
+**      VERSION NUMBER : $Revision: 1.4 $
 **
 **  DESCRIPTION      :  String class implementation.
 **
 
 /*****************************
    $Log: string.cpp,v $
-   Revision 1.3  2002-11-03 13:18:57  arjen
+   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
    New functions - String::escape() and String::unescape()
 
    Revision 1.2  2002/09/28 06:42:11  arjen
@@ -34,7 +37,7 @@
 
 *****************************/
 
-static const char RCSID[] = "$Id: string.cpp,v 1.3 2002-11-03 13:18:57 arjen Exp $";
+static const char RCSID[] = "$Id: string.cpp,v 1.4 2003-03-29 07:18:54 arjen Exp $";
 
 #include <stdio.h>
 #include <ctype.h>
@@ -61,9 +64,16 @@ String::String(char c)    //  Create a String from a char
 String::String(const char *s)    //  Create a String from a char *
 {
    p = new srep;
-   p->s = new char[strlen(s)+1];
-   strcpy(p->s, s);
+
+   p->s = 0;
    p->n = 1;
+
+   if (s != 0)
+   {
+      p->s = new char[strlen(s)+1];
+      strcpy(p->s, s);
+      p->n = 1;
+   }
 }
 
 String::String(const String& x)   // Create a String from another String
@@ -122,9 +132,12 @@ String& String::operator=(const char *s)
    else if (p->n == 1)
       delete p->s;
 
-   p->s = new char[strlen(s)+1];
-   strcpy(p->s, s);
-   p->n = 1;
+   if (s != 0)
+   {
+      p->s = new char[strlen(s)+1];
+      strcpy(p->s, s);
+      p->n = 1;
+   }
    return *this;
 }