cstring.h

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 /*
00032  *  String ADT.
00033  *  Implements copy semantics. 
00034  *  This class has been purified.
00035  *  This class can be used with klist.
00036  *  See the tests subdirectory for examples of its use.
00037  */
00038 
00039 /*
00040  * $Log: cstring.h,v $
00041  * Revision 1.1  1994/08/17 18:23:46  markc
00042  * Added new classes: Cstring KeyList, KList
00043  * Added new function: RPCgetArg
00044  * Changed typedefs in machineType.h to #defines
00045  *
00046  */
00047 
00048 #ifndef _string_hpp
00049 #define _string_hpp
00050 
00051 #include <string.h>
00052 #include <assert.h>
00053 #include <iostream.h>
00054 
00055 #define VALID(x) assert(x)
00056 #define MAX_STRING_LEN 2000
00057 #define CS_TRUE 1
00058 #define CS_FALSE 0
00059 
00060 #pragma interface
00061 
00062 typedef int CS_BOOL;
00063 
00064 class Cstring
00065 {
00066 public:
00067 friend ostream &operator << (ostream& os, const Cstring& S) {
00068   if (!S.len)
00069     os << "<empty>";
00070   else 
00071     os << S.str;
00072   return os;
00073 }
00074 
00075   // the integer serves to choose the correct constructor
00076   // otherwise you would have to typecast to const char
00077   // to get other constructor
00078   Cstring(int i, char *use_me) :len(0), str((char*) 0) {use(use_me);}
00079   Cstring() : len(0), str((char*) 0) { ; }
00080   Cstring(const char *copy_me) : len(0), str((char*) 0) {set(copy_me);}
00081   Cstring(const Cstring& S) : len(0), str((char*) 0) {set(S);}
00082   ~Cstring() {destroy();}
00083   void destroy() {if (str) delete [] str; len=0; str= (char*) 0;}
00084 
00085   CS_BOOL extend (const char *);
00086   CS_BOOL extend (Cstring &S) {return (extend(S.str));}
00087   Cstring operator+ ( const Cstring& S) const;
00088   Cstring operator+ ( const char *s) const;
00089   Cstring& operator= ( const Cstring& S);
00090   Cstring& operator= ( const char *c);
00091   CS_BOOL operator== (const Cstring& S) const { return (*this == S.str);}
00092   CS_BOOL operator== (const char * s) const;
00093   CS_BOOL operator< (const char * s) const;
00094   CS_BOOL operator< (const Cstring& S) const {return ((*this) < S.str);}
00095   CS_BOOL operator> (const char *s) const;
00096   CS_BOOL operator> (const Cstring& S) const {return ((*this) > S.str);}
00097 
00098   const char *get() const {return (const char*) str;}
00099   char *get_copy() const;
00100   int getLen() const {return len;}
00101   // copy the string from the input
00102   void set(const Cstring& S);
00103   void set(const char *s);
00104 
00105   // use the string in the input
00106   void use(char *s);
00107 
00108   int invariant()
00109     {return (((len>=0) && str) || (!len && !str));}
00110 
00111   int empty() {return (len == 0);}
00112 
00113 protected:
00114   int len;
00115   char *str;
00116   int prefix_compare (const char *s, int &slen) const;
00117   int prefix_compare (const Cstring& S, int &slen) const
00118     {return prefix_compare(S.str, slen);}
00119 };
00120 
00121 void 
00122 Cstring::use (char *use_me)
00123 {
00124   if (str)
00125     delete [] str;
00126   str = (char *)  0;
00127 
00128   if (!use_me) {
00129     len = 0;
00130     return;
00131   } else {
00132     len = strlen(use_me);
00133     if (len >= 0) {
00134       str = use_me;
00135       return;
00136     } else {
00137       len = 0;
00138       return;
00139     }
00140   }
00141 }
00142 
00143 void Cstring::set(const Cstring& S)
00144 {
00145   set(S.str);
00146 }
00147 
00148 void
00149 Cstring::set (const char *s)
00150 {
00151   if (str) 
00152     delete [] str;
00153   str = (char*) 0;
00154 
00155   if (!s) {
00156     len = 0;
00157     return;
00158   }
00159 
00160   len = strlen(s);
00161   if (len >= 0) {
00162     str = new char [len + 1];
00163     if (str)
00164       strcpy (str, s);
00165     else
00166       len = 0;
00167   } else
00168     len = 0;
00169 
00170   VALID(invariant());
00171 }
00172 
00173 int Cstring::operator==(const char *s) const
00174 {
00175   if ((len == strlen(s)) &&
00176       ((!len) || (!strcmp(str, s))))
00177     return CS_TRUE;
00178   else
00179     return 0;
00180 }
00181 
00182 //
00183 // compare up to the length of the smaller string
00184 // used for less than
00185 //
00186 int Cstring::prefix_compare(const char *s, int &slen) const
00187 {
00188   if (!len || !s)
00189     return -2;
00190 
00191   slen = strlen(s);
00192   return (strncmp(str, s, slen > len ? len : slen));
00193 }
00194 
00195 Cstring Cstring::operator+ ( const Cstring& S) const return Ret;
00196 {
00197   Ret = (*this + S.str);
00198   return Ret;
00199 }
00200 
00201 Cstring Cstring::operator + (const char *s) const return Ret;
00202 {
00203   Ret = (*this);
00204   Ret.extend(s);
00205   return Ret;
00206 }
00207 
00208 CS_BOOL Cstring::extend (const char *s)
00209 {
00210   char *new_str;
00211   int new_len;
00212 
00213   if (s) {
00214     if (new_len = strlen(s)) {
00215       if (new_str = new char[len + new_len + 1]) {
00216     if (strcpy(new_str, str) && strcat(new_str, s)) {
00217       if (str)
00218         delete (str);
00219       len += new_len;
00220       str = new_str;
00221       return (CS_TRUE);
00222     }
00223       }
00224     }
00225   } else
00226     return (CS_TRUE);
00227 
00228   return (CS_FALSE);
00229 }
00230  
00231 Cstring& Cstring::operator= ( const Cstring& S) 
00232 {
00233   if (this == &S)
00234     return (*this);
00235   else {
00236     set(S);
00237     return (*this);
00238   }
00239 }
00240 
00241 Cstring& Cstring::operator= ( const char *c) 
00242 {
00243   if (str == c)
00244     return (*this);
00245   else {
00246     set(c);
00247     return (*this);
00248   }
00249 }
00250 
00251 CS_BOOL Cstring::operator< (const char * s) const { 
00252   int slen, res;
00253 
00254   if ((res = prefix_compare(s, slen)) == -2)
00255     return CS_FALSE;
00256   else if (res < 0)
00257     return CS_TRUE;
00258   else if (res > 0)
00259     return CS_FALSE;
00260   else
00261     return (len < slen);
00262 }
00263 
00264 CS_BOOL Cstring::operator> (const char *s) const {
00265   int slen, res;
00266 
00267   if ((res = prefix_compare(s, slen)) == -2)
00268     return CS_TRUE;
00269   else if (res < 0)
00270     return (len > slen);
00271   else if (res > 0)
00272     return CS_TRUE;
00273   else
00274     return (len > slen);
00275 }
00276 
00277 char *Cstring::get_copy() const
00278 {
00279   char *ret= (char*) 0;
00280   if (str) 
00281     ret = strdup(str);
00282   return ret;
00283 }
00284 
00285 
00286 #endif    
00287 
00288 
00289   
00290       
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1