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

strings.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 1997, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 #ifndef STRINGS_H
00024 #define STRINGS_H
00025 
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include "std/config.H"
00029 #include "std/hash.H"
00030 #include "std/ref.H"
00031 #include "std/list.H"
00032 
00033 /* ------------------ class definitions --------------------- */
00034 
00035 #define cSTR const STR
00036 class STR : public REFcounter {
00037   protected:
00038    char *_s;
00039    static DllImpExp HASH<char*> *strpool;
00040   public:
00041    static DllImpExp STR *null;
00042 
00043    STR()                    { _s = 0; }
00044    STR(const char *s)       { if (!strpool) strpool = new HASH<char*>(128); 
00045                               if (!s) s = "";
00046                               strpool->add(s, 0, _s); }
00047    STR(char        s)       { char str[2]; str[0]=s; str[1] = '\0'; 
00048                               if (!strpool) strpool = new HASH<char*>(128); 
00049                               strpool->add(str, 0, _s); }
00050    STR(const STR  &s)       { _s = s._s; }
00051    STR(int         x)       { char buff[1024];sprintf(buff,"%d", x);
00052                               if (!strpool) strpool = new HASH<char*>(128);
00053                   strpool->add(buff, 0, _s); }
00054    STR(double      d)       { char buff[1024];sprintf(buff,"%g", d);
00055                               if (!strpool) strpool = new HASH<char*>(128);
00056                   strpool->add(buff, 0, _s); }
00057   ~STR()                    { }
00058    size_t len()               const { return strlen(_s); }
00059    STR  &operator = (cSTR &s)       { _s = s._s; return *this;}
00060    char *operator * ()        const { return _s; }
00061    char  operator [](int   i) const { return _s[i]; }
00062    int   operator ==(cSTR &s) const { return _s == s._s; }
00063    STR*  operator + (cSTR &s) const { char buff[1024];
00064                                       sprintf(buff,"%s%s",_s,s._s);
00065                                       return new STR(buff); }
00066    int   contains   (cSTR &s) const { return strstr(_s, s._s) != 0; }
00067    friend inline ostream &operator<<(ostream &os, cSTR *s) { return os<< s->_s;}
00068 };
00069 
00070 #define Cstr_ptr const str_ptr
00071 #define Cstr_list const str_list
00072 class str_ptr;
00073 
00074 typedef LIST<str_ptr> str_list;
00075 //
00076 //  str_ptrs -- reference counted strings
00077 //    support standard string operations including
00078 //   the '+' operator for concatenating strings.
00079 //   NOTE: the '+' operator doesn't seem to work on the right
00080 //         hand side of the ? operator on the SGIs.
00081 //
00082 extern DllImpExp Cstr_ptr null_str_ptr;
00083 class DllImpExp str_ptr : public REFptr<STR> {
00084   public :
00085    static Cstr_ptr &null_str() { if (null_str_ptr.p_ == 0) 
00086                                     ((str_ptr&) null_str_ptr) = (STR::null=new STR(""));
00087                                  return null_str_ptr; }
00088 
00089    str_ptr()              : REFptr<STR>(new STR("")){ }
00090    str_ptr(Cstr_ptr   &p) : REFptr<STR>(p)          { }
00091    str_ptr(STR        *s) : REFptr<STR>(s?s:null_str_ptr.p_){ }
00092    str_ptr(const char *s) : REFptr<STR>(new STR(s)) { }
00093    str_ptr(char        c) : REFptr<STR>(new STR(c)) { }
00094    str_ptr(int         x) : REFptr<STR>(new STR(x)) { }
00095    str_ptr(double      d) : REFptr<STR>(new STR(d)) { }
00096    size_t  len        ()            const { return   p_->len(); }
00097    char    operator [](int i)       const { return   (*p_)[i];  }
00098    int     operator ==(Cstr_ptr &s) const { return   *s.p_ == *p_;  }
00099    int     operator !=(Cstr_ptr &s) const { return !(*s.p_ == *p_); }
00100    int     operator !  ()           const { return (!p_ || !**p_ || !***p_) ; }
00101            operator int()           const { return !(!p_ || !**p_ || !***p_) ; }
00102    str_ptr operator + (int       i) const { return *this + str_ptr(i);}
00103    str_ptr operator + (const char *s) const { return str_ptr(*p_ + STR(s)); }
00104    str_ptr operator + (Cstr_ptr &s) const { return str_ptr(*p_ + *s.p_); }
00105    str_ptr to_upper   () const;
00106    str_ptr to_lower    () const;
00107    int     contains   (Cstr_ptr &s) const {return p_->contains(*s.p_);}
00108    // Does this string contain any of the strings in str_list s?
00109    int     contains   (Cstr_list &s) const;
00110    friend inline ostream &operator << (ostream &s, Cstr_ptr &p) { return s << **p; }
00111    friend inline istream &operator >> (istream &s,  str_ptr &p) {
00112       char buf[1000] = "\0"; s >> buf; p = str_ptr(buf); return s; }
00113 };
00114 
00115 #ifdef GLUE_AVOID_STATIC_LOCAL_INLINE_VAR
00116 #   define RET_STAT_STR(s)  return str_ptr(s)
00117 #else
00118 #   define RET_STAT_STR(s)  static str_ptr st(s); return st
00119 #endif
00120 
00121 DllImpExp str_list tokenize(Cstr_ptr &str, Cstr_ptr &delim=" ");
00122 extern str_list dir_list(Cstr_ptr &directory);
00123 
00124 #endif // STRINGS_H
00125 

Generated on Mon Sep 15 16:25:57 2003 for gluebase by doxygen1.2.18