Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

String.h

Go to the documentation of this file.
00001 /*
00002     File:       String.h
00003 
00004     Function:   Classes for manipulating strings
00005 
00006     Author:     Andrew Willmott
00007 
00008     Copyright:  (c) 1996-2000, Andrew Willmott
00009 */
00010 
00011 #ifndef __String__
00012 #define __String__
00013 
00014 #include "cl/PtrArray.h"
00015 #include <string.h>
00016 #include <iostream.h>
00017 #include <fstream.h>
00018 
00019 
00020 // --- StrConst Class ---------------------------------------------------------
00021 
00022 class String;
00023 
00024 class StrConst
00025 {
00026 public:
00027     StrConst() : theString(0) {};
00028 
00029     StrConst(const Char *s) : theString(s) {};
00030 
00031                 operator const Char *() const
00032                 { return(theString); };
00033                 
00034     Bool        operator == (StrConst s) const
00035                 { return(strcmp(theString, s.theString) == 0); };
00036     Bool        operator != (StrConst s) const
00037                 { return(strcmp(theString, s.theString) != 0); };
00038 
00039     Bool        operator == (const Char *sc) const
00040                 { return(strcmp(theString, sc) == 0); };
00041     Bool        operator != (const Char *sc) const
00042                 { return(strcmp(theString, sc) != 0); };
00043 
00044     Bool        IsNull() const
00045                 { return(theString == 0); }
00046     Int         FindChar(Char c) const;
00048     Int         FindCharLast(Char c) const;
00050     String      SubString(Int start, Int length);
00052     String      Prefix(Int length);
00054     String      Suffix(Int length);
00056 
00057     Int         Length() const
00058                 { return(strlen(theString)); };
00059     const Char  *CString() const
00060                 { return theString; };
00061 
00062 protected:  
00063     const Char  *theString; 
00064 };
00065 
00066 inline Bool operator == (const Char *cs, StrConst s) 
00067 { return(s == cs); };
00068 inline Bool operator != (const Char *cs, StrConst s)
00069 { return(s != cs); };
00070 
00071 inline ostream &operator << (ostream &s, StrConst sb)
00072 {
00073     s << (const Char *) sb;
00074     return(s); 
00075 }
00076 
00077 // --- String class -----------------------------------------------------------
00078 
00079 
00080 class String : public Array<Char>
00082 {
00083 public:
00084                 String() : Array<Char>(1) { item[0] = 0; };
00085                 String(const String &sb) : Array<Char>(sb) {};
00086                 String(StrConst s);
00087                 ~String() {};
00088                 
00089                 operator StrConst() const
00090                 { return(StrConst(item)); };
00091                 operator const Char *() const
00092                 { return(item); };
00093 
00094     Char        *CString() const
00095                 { return(item); };
00096                 
00097     String      &Append(StrConst s);
00099     String      &Insert(StrConst s, Int place);
00101     String      &Delete(Int start, Int length);
00103     
00104     String      &operator = (StrConst s);
00105     String      &operator = (const Char *s);
00106 
00115     Bool        operator == (const Char *s) const
00116                 { return(strcmp(item, s) == 0); };
00117     Bool        operator != (const Char *s) const
00118                 { return(strcmp(item, s) != 0); };
00119         
00120     Bool        operator == (StrConst s) const
00121                 { return(strcmp(item, s) == 0); };
00122     Bool        operator != (StrConst s) const
00123                 { return(strcmp(item, s) != 0); };
00124                 
00125     Bool        operator == (const String &s) const
00126                 { return(strcmp(item, s.CString()) == 0); };
00127     Bool        operator != (const String &s) const
00128                 { return(strcmp(item, s.CString()) != 0); };
00129                 
00130     String      &operator += (StrConst s)
00131                 { return(Append(s)); };
00133 
00134     istream     &ReadString(istream &s);    
00136     istream     &ReadWord(istream &s);      
00138     istream     &ReadLine(istream &s);
00140 
00141     String      &Printf(const Char *format, ...);
00155     Int         Length() const
00156                 { return(NumItems() - 1); };
00157 
00158     // isn't this wonderful? C++ won't downcast to StrConst in all situations
00159     // we'd like, so need to replicate the calls here.
00160     String      SubString(Int start, Int length) const
00161                 { return(StrConst(item).SubString(start, length)); };
00162     String      Prefix(Int length) const
00163                 { return(StrConst(item).Prefix(length)); };
00164     String      Suffix(Int length) const
00165                 { return(StrConst(item).Suffix(length)); };
00166     Int         FindChar(Char c) const
00167                 { return(StrConst(item).FindChar(c)); };
00168     Int         FindCharLast(Char c) const
00169                 { return(StrConst(item).FindCharLast(c)); };
00170 
00171     Void        Free()
00172                 { Clear(); };
00173     
00174     friend      StrConst;
00175 };
00176 
00177 inline Bool operator == (const Char *cs, const String &s) 
00178 { return(s == cs); };
00179 inline Bool operator != (const Char *cs, const String &s)
00180 { return(s != cs); };
00181 
00182 inline Bool operator == (StrConst cs, const String &s)
00183 { return(s == cs); };
00184 inline Bool operator != (StrConst cs, const String &s)
00185 { return(s != cs); };
00186 
00187 inline ostream &operator << (ostream &s, const String &sb)
00188 {
00189     s << sb.CString();
00190     return(s); 
00191 }
00192 istream &operator >> (istream &s, String &str);
00193 
00194 
00195 // --- TempString class --------------------------------------
00196 
00197 //  A TempString is a String that will not be used further by the code that
00198 //  produced it, so is free for further use.
00199 //  Purpose: For speeding up concatenation sequences. These operators ensure 
00200 //  that only one String is used for all the concatenations in a sequence like
00201 //  a + b + c.
00202 
00203 class TempString : public String
00204 {
00205 
00206 public:
00207     TempString() : String() {};
00208 };
00209 
00210 inline ostream &operator << (ostream &s, TempString &sb)
00211 { s << (String &) sb; return(s); }
00212 inline TempString operator + (StrConst a, StrConst b)
00213 { TempString t; t.Append(a).Append(b); return(t); }
00214 inline TempString &operator + (TempString &a, StrConst b)
00215 { return((TempString &) a.Append(b)); }
00216 inline TempString &operator + (StrConst a, TempString &b)
00217 { return((TempString &) b.Insert(a, 0));}
00218 
00219 
00220 // --- Utility routines -----------------------------------------
00221 
00222 typedef PtrArray<const Char> StrConstArray;
00223 // XXX need to change to actual StrConst based to make
00224 // == "asd" comparisons work: workaround == StrConst("asd").
00225 
00226 Bool        IsEndOfLine(istream &s);
00227 Void        ChompWhiteSpace(istream &s);
00228 // reads from s until next non-whitespace character.
00229 TempString  SubstituteEnvVars(StrConst str);
00230 // replace environment variables in s (e.g., $HOME) with their
00231 // environment-defined values.
00232 Void Split(StrConst line, StrConstArray &a, StrConst sep = " \t");
00233 // WARNING: 'a' is only valid until the next time Split is called.
00234 
00235 
00236 #endif

Generated at Sat Aug 5 00:16:33 2000 for Class Library by doxygen 1.1.0 written by Dimitri van Heesch, © 1997-2000