Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TextNoGC.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Sun Apr 17 22:51:25 EDT 2005 by ken@xorian.net
00020 //      modified on Thu Mar 27 17:13:30 PST 1997 by heydon 
00021 //      modified on Tue Aug  8 14:39:21 PDT 1995 by levin 
00022 
00023 /* This is the Text implementation that is *not* linked with a garbage
00024    collector. Hence, its destructor actually deallocates memory. */
00025 
00026 #include "Basics.H"
00027 #include "Text.H"
00028 
00029 #include "TextCommon.C"
00030 
00031 bool Text::GCImpl() throw () { return false; }
00032 
00033 static const char *StrConcat(const char *str1, const char *str2) throw ()
00034 /* Return a newly allocated null-terminated string containing the
00035    concatenation of the null-terminated strings "str1" and "str2". */
00036 {
00037     // Note: null terminator is only included in second string
00038     int len1 = strlen(str1), len2 = strlen(str2) + 1;
00039     char *res = NEW_ARRAY(char, len1 + len2);
00040     if (len1 == 0) {
00041         memcpy(res, str2, len2);
00042     } else {
00043         memcpy(res, str1, len1);
00044         memcpy(res + len1, str2, len2);
00045     }
00046     return res;
00047 }
00048 
00049 // constructor from Text
00050 Text::Text(const Text& t) throw ()
00051 {
00052     this->s = StrCopy(t.s);
00053 }
00054 
00055 Text::Text(const char c) throw ()
00056 {
00057     char *temp = NEW_ARRAY(char, 2);
00058     temp[0] = c; temp[1] = '\0';
00059     this->s = temp;
00060 }
00061 
00062 // destructor
00063 Text::~Text() throw ()
00064 {
00065     if (this->s != EmptyStr) {
00066         delete[] (char *)(this->s);
00067         this->s = (char *)NULL;
00068     }
00069 }
00070 
00071 // assignment
00072 Text& Text::operator = (const char* str) throw ()
00073 {
00074     if (this->s != EmptyStr) delete[] (char *)(this->s);
00075     this->s = StrCopy(str);
00076     return *this;
00077 }
00078 
00079 Text& Text::operator = (const Text& t) throw ()
00080 {
00081     if (this->s != EmptyStr) delete[] (char *)(this->s);
00082     this->s = StrCopy(t.s);
00083     return *this;
00084 }
00085 
00086 // comparison
00087 bool operator == (const Text& t1, const Text& t2) throw ()
00088 {
00089     return ((t1.s == t2.s) || (strcmp(t1.s, t2.s) == 0));
00090 }
00091 
00092 bool operator != (const Text& t1, const Text& t2) throw ()
00093 {
00094     return ((t1.s != t2.s) && (strcmp(t1.s, t2.s) != 0));
00095 }
00096 
00097 // concatenation
00098 Text operator + (const Text& t1, const Text& t2) throw ()
00099 {
00100     return Text(StrConcat(t1.s, t2.s), /*copy=*/ (void*)1);
00101 }
00102 
00103 Text operator + (const char* str, const Text& t) throw ()
00104 {
00105     return Text(StrConcat(str, t.s), /*copy=*/ (void*)1);
00106 }
00107 
00108 Text operator + (const Text& t, const char* str) throw ()
00109 {
00110     return Text(StrConcat(t.s, str), /*copy=*/ (void*)1);
00111 }
00112 
00113 // append
00114 Text& Text::operator += (const char* str) throw ()
00115 {
00116     const char *oldS = this->s;
00117     this->s = StrConcat(oldS, str);
00118     if (oldS != EmptyStr) delete[] (char *)oldS;
00119     return *this;
00120 }
00121 
00122 Text& Text::operator += (const Text& t) throw ()
00123 {
00124     const char *oldS = this->s;
00125     this->s = StrConcat(oldS, t.s);
00126     if (oldS != EmptyStr) delete[] (char *)oldS;
00127     return *this;
00128 }

Generated on Mon May 8 00:48:29 2006 for Vesta by  doxygen 1.4.2