00001
00002
00003
00004 #ifndef DYNSTR_H
00005 #define DYNSTR_H
00006
00007 #include <string.h>
00008
00009
00010
00011 #ifdef DO_IMPLEMENT_DYNSTR
00012 #define DO_IMPLEMENTING // If app has not defined it already
00013 #endif
00014 #include "dynobj/DynObj.h"
00015
00016
00017 class DynStr;
00018
00019
00020 DO_DECL_TYPE_INFO(DynStr,DYNSTR_TYPE_ID);
00021
00022
00023
00024
00025
00026 template<class T>
00027 class DynArrI;
00028
00029
00030 #define DynStrNull ((DynStr&)*(DynStr*)0)
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #define STR_AT_END ((int)0x80000000)
00042
00051
00052 class DynStr : public DynObj {
00053 public:
00057 virtual DynObjType* docall doGetType( const DynI **pself=0 ) const;
00058
00060 virtual DynStr& docall AssignA(const char *s, int l=-1){ return *this; }
00061
00063 virtual int docall Strcmp(const char *s) const { return 0; }
00065 virtual int docall Stricmp(const char *s) const { return false; }
00066
00068 virtual const char* docall Get( ) const { return NULL; }
00070 virtual int docall GetByte(int ix) const { return -1; }
00072 virtual int docall GetUniChar(int ix) const { return -1; }
00074 virtual bool docall SetByte( int ix, char ch ) { return false; }
00076 virtual bool docall SetUniChar( int ix, int ch ) { return false; }
00077
00079 virtual int docall Length() const { return -1; };
00081 virtual int docall ByteSize() const { return -1; };
00082
00084 virtual DynStr& docall Insert(int at, const char *s, int byte_len) { return *this; }
00086 virtual DynStr& docall InsertChar(int at, int ch){ return *this; }
00088 virtual DynStr& docall Remove(int pos, int cnt){ return *this; }
00089
00091 virtual DynStr& docall AppendW(const wchar_t *s, int l=-1){ return *this; }
00092
00094 virtual DynStr& docall Reset() { return *this; }
00095
00097 virtual const char* docall Right(int from_pos) const { return 0; };
00099 virtual DynStr& docall Strip(bool left=true, bool right=true) { return *this; }
00100
00102 virtual bool docall IsEmpty() const { return false; }
00103
00105 virtual int docall SubstChar(int ch_find, int ch_repl) { return -1; }
00107 virtual int docall Subst(const char *s_find, const char *s_repl) { return -1; }
00108
00110 virtual int docall Find(const char *str) const { return -1; }
00113 virtual bool docall FindAt(const char *str, int pos=0) const { return false; }
00114
00116 virtual int docall FindBytePos(const char *str) const { return -1; }
00119 virtual bool docall FindAtBytePos(const char *str, int pos=0) const { return false; }
00120
00122 virtual int docall BytePos2Index( int pb ){ return -1; }
00124 virtual int docall Index2BytePos( int pi ){ return -1; }
00125
00126 #if DO_RETURN_STRUCT_WITH_DTOR_SAFE==1
00127
00133 struct DynStrRetVal : protected DynObjRetVal {
00134 DynStrRetVal( DynStr *pds ) : DynObjRetVal(pds) { }
00135 DynStr *Release(){ return (DynStr*)DynObjRetVal::Release(); }
00136 operator const char*() { return m_pdo ? ((DynStr*)m_pdo)->Get() : NULL; }
00137
00138
00139 DynStrRetVal& operator + (const char* ps) {
00140 if(m_pdo) ((DynStr*)m_pdo)->Append(ps);
00141 return *this;
00142 }
00143 };
00144
00146 virtual DynStrRetVal docall Concat(const char *str){ return DynStrRetVal(NULL); }
00147 #else
00148
00149 virtual void docall Concat_CannotUse( const char* str ){ }
00150 #endif
00151
00153 virtual wchar_t* docall ToWideChar( DynArrI<wchar_t>& wa ){ return NULL; }
00154
00155
00157
00162 DynStr& operator = (const char *s){ return AssignA(s,-1); }
00164 DynStr& operator = (const wchar_t *s){ return AssignW(s,-1); }
00166 bool operator == (const char *s) const { return !Strcmp(s); }
00168 bool operator !=(const char *s) const { return Strcmp(s)!=0; }
00169
00171 operator const char* () const { return Get(); }
00173 const char* c_str() const { return Get(); }
00174
00176
00177
00178 DynStr& AssignW(const wchar_t *pws, int l=-1){ Trunc(0); AppendW(pws,l); return *this; }
00180 DynStr& Prepend(const char *s){ return Insert(0,s,-1); }
00182 DynStr& Append(const char *s){ return Insert(STR_AT_END,s,-1); }
00184 DynStr& Prepend(int ch){ return InsertChar(0,ch); return *this; }
00186 DynStr& Append(int ch){ return InsertChar(STR_AT_END,ch); return *this; }
00188 DynStr& Insert(int at, const char *s){
00189 if(s) Insert(at,s,(int)strlen(s));
00190 return *this;
00191 }
00192
00194 DynStr& operator += (const char *s) { return Append(s); }
00195 DynStr& operator += (const wchar_t *ws) { return AppendW(ws); }
00196
00198 #if DO_RETURN_STRUCT_WITH_DTOR_SAFE==1
00199 DynStrRetVal operator + (const char *s) { return Concat(s); }
00200 #endif
00201
00203 DynStr& operator += (int ch) { Append(ch); return *this; }
00205 bool StartsWith(const char *str) const { return FindAt(str,0); }
00207 bool EndsWith(const char *str) const { return FindAt(str,STR_AT_END); }
00208
00210 DynStr& Suffix(const char *str){ if(!FindAt(str,STR_AT_END)) Append(str); return *this; }
00212 DynStr& Prefix(const char *str){ if(!FindAt(str,0)) Prepend(str); return *this; }
00213
00215 DynStr& Trunc( int at ){ return Remove(at,-1); }
00217 int operator [] (int ix) const { return GetUniChar(ix); }
00218
00219
00220 DynStr& AppendArg( bool b, const char *fmt=NULL ){
00221 if( fmt ){
00222 char buf[64];
00223 int l=sprintf(buf,fmt,b);
00224 if( l>0 ) Append(buf);
00225 }
00226 else Append( b ? "true" : "false" );
00227 return *this;
00228 }
00229 DynStr& AppendArg( int i, const char *fmt="%d" ){
00230 char buf[64];
00231 int l=sprintf(buf,fmt,i);
00232 if( l>0 ) Append(buf);
00233 return *this;
00234 }
00235 DynStr& AppendArg( unsigned int i, const char *fmt="%u" ){
00236 char buf[64];
00237 int l=sprintf(buf,fmt,i);
00238 if( l>0 ) Append(buf);
00239 return *this;
00240 }
00241 DynStr& AppendArg( char ch, const char *fmt="%c" ){
00242 char buf[64];
00243 int l=sprintf(buf,fmt,ch);
00244 if( l>0 ) Append(buf);
00245 return *this;
00246 }
00247 DynStr& AppendArg( unsigned char ch, const char *fmt="%c" ){
00248 char buf[64];
00249 int l=sprintf(buf,fmt,ch);
00250 if( l>0 ) Append(buf);
00251 return *this;
00252 }
00253 DynStr& AppendArg( double d, const char *fmt="%e" ){
00254 char buf[64];
00255 int l=sprintf(buf,fmt,d);
00256 if( l>0 ) Append(buf);
00257 return *this;
00258 }
00259 DynStr& AppendArg( const void* pv, const char *fmt="%p" ){
00260 char buf[64];
00261 int l=sprintf(buf,fmt,pv);
00262 if( l>0 ) Append(buf);
00263 return *this;
00264 }
00265 DynStr& AppendArg( const char *s, const char *fmt="%s" ){
00266
00267 if( s ) Append(s);
00268 return *this;
00269 }
00270 DynStr& AppendArg( const wchar_t *ws, const char *fmt="%s" ){
00271
00272 if( ws ) AppendW(ws);
00273 return *this;
00274 }
00275
00276 #if DO_USE_TEMPLATES==1
00277
00278
00279 #include "DynStrPrintf.hpp"
00280 #endif // DO_USE_TEMPLATES==1
00281 };
00282
00283
00284
00285 #ifdef DO_IMPLEMENT_DYNSTR
00286
00287
00288 DynObjType g_do_vtype_DynStr("DynStr","DynObj",DYNSTR_TYPE_ID,0,sizeof(DynStr),NULL,NULL,__FILE__);
00289 DynObjType* DynStr::doGetType( const DynI **pself ) const {
00290 if(pself) *pself=(const DynI*)(const void*)this;
00291 return &g_do_vtype_DynStr;
00292 }
00293 #endif //DO_IMPLEMENT_...
00294
00295
00296 #endif // DYNSTR_H
00297
00298