00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00077
00078
00079
00080
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
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