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

mstring.h

Go to the documentation of this file.
00001 
00027 #ifndef __MAGIC_MSTRING_H__
00028 #define __MAGIC_MSTRING_H__
00029 
00030 #include <string.h> // fgetS and safedup require
00031 #include <stdlib.h> // atoi() requires
00032 
00033 #include "magic/mtypes.h"
00034 #include "magic/mobject.h"
00035 #include "magic/mmagisupp.h"
00036 
00037 namespace MagiC {
00038     class TextOStream;
00039     class TextIStream;
00040     class DataOStream;
00041     class DataIStream;
00042 
00043     // Externals
00044     class RegExp;
00045 
00046     class String;
00047     class SubString;
00048 
00049     String  strformat   (const char* format, ...);
00050 }
00051 
00052 BEGIN_NAMESPACE (MagiC);
00053 
00055 //                         ----         o                                    //
00056 //                        (      |          _                                //
00057 //                         ---  -+- |/\ | |/ \   ___                         //
00058 //                            )  |  |   | |   | (   \                        //
00059 //                        ___/    \ |   | |   |  ---/                        //
00060 //                                               __/                         //
00062 
00063 // Obey the global no-bounds-checking option
00064 #ifdef NOCHECKBOUNDS
00065 #define STRING_NOCHECKBOUNDS 1
00066 #endif
00067 
00072 #ifdef STRING_NOCHECKBOUNDS // TODO!
00073 #define STRING_GETCHAR(n) return mData[n];
00074 #else
00075 #define STRING_GETCHAR(n) return mData[n];
00076 #endif
00077 
00082 class String : public Comparable {
00083     decl_dynamic (String);
00084   public:
00085     enum strcrflags {STRCRFL_NONE=0, STRCRFL_OWN=2};
00086 
00087                     String              ();
00088                     String              (const String& orig);
00089                     String              (const char*);
00090                     String              (char* str, enum strcrflags flags);
00091                     String              (const char*, int n);
00092                     String              (char*, uint n, bool own);
00093     explicit        String              (int i, int base=10);
00094     explicit        String              (uint i, int base=10);
00095     explicit        String              (long i, int base=10);
00096     explicit        String              (float f, char fmt='g', int prec=6);
00097     explicit        String              (double f, char fmt='g', int prec=6);
00098     virtual         ~String             ();
00099 
00100     // Manipulation
00101     String&         assign              (char c);
00102     String&         append              (char c);
00103     String&         append              (const String& other) {return append (other.mData, other.mLen);}
00104     String&         append              (const char*, uint);
00105     String&         replace             (uint position, const char* buffer, int length=-1);
00106 
00107     // Operators
00108     String&         operator=           (const String&);
00109     String&         operator=           (const char*);
00110     String&         operator=           (char c) {return assign(c);}
00111     int             operator!=          (const char* b) const {return !operator== (b);}
00112     int             operator==			(const char*) const;
00113     int             operator==			(const String& b) const {return (*this) == b.mData;}
00114     int             operator==			(const Comparable& other) const;
00115     String&         operator+=          (const String& str) {return append (str.mData, str.mLen);}
00116     String&         operator+=          (char c) {return append (c);}
00117     String          operator+           (const String& str) const;
00118     String          operator+           (const char* str) const;
00119     const char      operator[]          (int n) const {STRING_GETCHAR(n)} // Varies on bounds checking, see above
00120     char&           operator[]          (int n) {STRING_GETCHAR(n)} // Varies on bounds checking, see above
00121 
00122     // Information
00123     bool            isNull              () const {if (this) return !mData; return 0;}
00124     void            nullify             ();
00125     bool            isEmpty             () const {return (!this || !mData || !mLen);}
00126     void            empty               ();
00127     uint            length              () const {return mLen;}
00128     int             maxLength           () const {return mMaxLen;}
00129     //void          truncate            (uint pos);
00130     //void          fill                (char c, int len=-1)
00131 
00132     // Formatting
00133     String          arg                 (const String& str, int fieldwidth=0) const;
00134     String          arg                 (const char* str, int fieldwidth=0) const {return arg (String (str), fieldwidth);}
00135     String          arg                 (const char c, int fieldwidth=0) const {return arg (String (c), fieldwidth);}
00136     String          arg                 (float x, int fieldwidth=0, char fmt='g', int prec=-1) const {return arg (String (x, fmt, prec), fieldwidth);}
00137     String          arg                 (double x, int fieldwidth=0, char fmt='g', int prec=-1) const {return arg (String (x, fmt, prec), fieldwidth);}
00138     String          arg                 (int x, int fieldwidth=0, int base=10) const {return arg (String (x, base), fieldwidth);}
00139     String          arg                 (long x, int fieldwidth=0, int base=10) const {return arg (String (x, base), fieldwidth);}
00140     //String&       sprintf             (const char* format, ...);
00141 
00142     // Searching
00143     int             find                (const String&, uint start=0) const;
00144     int             find                (const char c, uint start=0) const;
00145     int             findRev             (const String&, int start=-1) const;
00146     int             regmatch            (const char* regexpr) const;
00147     int             regmatch            (const char* regexpr, Array<String>& target) const;
00148     int             regmatch            (RegExp& compiled, Array<String>& target) const;
00149 
00150     // Manipulations
00151     String          mid                 (uint start, int len=-1) const;
00152     String          left                (uint n) const;
00153     String          right               (uint n) const;
00154     void            upper               () const;
00155     void            lower               () const;
00156     void            split               (Array<String>& target, const char delim=' ') const;
00157     void            join                (const Array<String>& source, const char delim=' ');
00158     String&         dellast             (uint n);
00159     void            chop                ();
00160     String          stripWhiteSpace     () const;
00161     String          simplifyWhiteSpace  () const;
00162 
00163     // Allocation
00164     void            reserve             (int amount);
00165     void            ensure              (int amount) {if (mMaxLen<amount) reserve (amount);}
00166     void            ensure_spontane     (int amount) {if (mMaxLen<amount) reserve (amount+amount/2+4);}
00167     void            grow_spontane       () {reserve (mMaxLen+mMaxLen/2+4);}
00168 
00169     char            checksum            ();
00170     int             fast_isequal        (const String& other) const;
00171 
00172     // Encodings
00173     String&         hexcode             (const String& other);
00174     enum            quoteflags          {QUOTE_NORMAL=0, QUOTE_HTML=1};
00175     void            quote               (char quotechar='%', int flags=0);
00176     void            unquote             (char quotechar='%', int flags=0);
00177 
00178     // I/O
00179     TextOStream&    operator>>          (TextOStream&) const;
00180     TextIStream&    operator<<          (TextIStream&);
00181     DataOStream&    operator>>          (DataOStream&) const;
00182     DataIStream&    operator<<          (DataIStream&);
00183     ostream&        operator>>          (ostream&) const;
00184 
00185     // Conversions
00186     int             toInt               () const {if (this && mData) return atoi (mData); return 0;}
00187     uint            toUInt              () const {if (this && mData) return (unsigned int) atoi (mData); return 0;}
00188     long            toLong              () const {if (this && mData) return atol (mData); return 0;}
00189     float           toFloat             () const {if (this && mData) return atof (mData); return 0;}
00190     double          toDouble            () const {if (this && mData) return atof (mData); return 0;}
00191                     operator const char*() const {return this? mData : (const char*) NULL;}
00193     char*           getbuffer           () const {return mData;}
00194 
00195     // Implementations
00196     virtual String* clone               () const;
00197     virtual int     hashfunc            (int hashsize) const;
00198     
00199   private:
00200     int             mLen;           
00201     int             mMaxLen;        
00202     char*           mData;
00203     unsigned char   mChkSum;        
00205     friend String   MagiC::strformat    (const char* format, ...);
00206 };
00207 
00208 
00209 
00211 //                                     |                              o                  //
00212 //  ___       |   ___        _    ___  |     __         _    ___   |           _    ____ //
00213 // /   ) \ / -+- /   ) |/\ |/ \   ___| |    /   |   | |/ \  |   \ -+- |  __  |/ \  (     //
00214 // |---   X   |  |---  |   |   | (   | |    +-- |   | |   | |      |  | /  \ |   |  \__  //
00215 //  \__  / \   \  \__  |   |   |  \__| |    |    \__! |   |  \__/   \ | \__/ |   | ____) //
00216 //                                          |                                            //
00218 
00219 
00220 extern const String emptystring;
00221 inline bool     isempty     (const String& str) {return (!(&str) || str.isEmpty ());}
00222 char*           strnchr     (const char* str, int len, char c);
00223 char*           safedup     (const char* orig, int maxlen=-1);
00224 int             fgetS       (FILE* in, String& str);
00225 istream&        getS        (istream& in, String& str, char term='\n');
00226 void            loadString  (String& str, const String& filename);
00227 String          strformat   (const char* format, ...);
00228 String          vstrformat  (const char* format, va_list ap);
00229 
00230 #ifndef format
00231 #define format strformat
00232 #endif
00233 
00235 #define STRFORMAT_SMALL_BUFFER_SIZE 80
00236 
00237 
00238 
00240 //                ----              ----         o                           //
00241 //               (           |     (      |          _                       //
00242 //                ---  |   | |---   ---  -+- |/\ | |/ \   ___                //
00243 //                   ) |   | |   )     )  |  |   | |   | (   \               //
00244 //               ___/   \__! |__/  ___/    \ |   | |   |  ---/               //
00245 //                                                        __/                //
00247 
00248 // Virtual substring of a string.
00249 class SubString : public String {
00250     String*     str;
00251     int         start, end;
00252   public:
00253 };
00254 
00255 END_NAMESPACE;
00256 
00257 #include "magic/mi18n.h"
00258 
00259 #endif
00260 

Generated on Thu Feb 10 20:06:42 2005 for LibMagiC by doxygen1.2.18