Main Page   Class Hierarchy   Compound List   File List   Compound Members  

mstring.h

00001 /***************************************************************************
00002  *   This file is part of the MagiC++ library.                             *
00003  *                                                                         *
00004  *   Copyright (C) 1998-2001 Marko Grönroos <magi@iki.fi>                  *
00005  *                                                                         *
00006  ***************************************************************************
00007  *                                                                         *
00008  *  This library is free software; you can redistribute it and/or          *
00009  *  modify it under the terms of the GNU Library General Public            *
00010  *  License as published by the Free Software Foundation; either           *
00011  *  version 2 of the License, or (at your option) any later version.       *
00012  *                                                                         *
00013  *  This library is distributed in the hope that it will be useful,        *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
00016  *  Library General Public License for more details.                       *
00017  *                                                                         *
00018  *  You should have received a copy of the GNU Library General Public      *
00019  *  License along with this library; see the file COPYING.LIB.  If         *
00020  *  not, write to the Free Software Foundation, Inc., 59 Temple Place      *
00021  *  - Suite 330, Boston, MA 02111-1307, USA.                               *
00022  *                                                                         *
00023  ***************************************************************************/
00024 
00025 #ifndef __MAGIC_MSTRING_H__
00026 #define __MAGIC_MSTRING_H__
00027 
00028 #include <string.h> // fgetS and safedup require
00029 #include <stdlib.h> // atoi() requires
00030 
00031 #include "magic/mtypes.h"
00032 #include "magic/mobject.h"
00033 #include "magic/mmagisupp.h"
00034 
00035 
00036 namespace MagiC {
00037     // Externals
00038     class RegExp;
00039 
00040     class String;
00041     class SubString;
00042 
00043     String  strformat   (const char* format, ...);
00044 }
00045 using namespace MagiC;
00046 
00047 
00048 
00050 //                         ----         o                                    //
00051 //                        (      |          _                                //
00052 //                         ---  -+- |/\ | |/ \   ___                         //
00053 //                            )  |  |   | |   | (   \                        //
00054 //                        ___/    \ |   | |   |  ---/                        //
00055 //                                               __/                         //
00057 
00058 // Obey the global no-bounds-checking option
00059 #ifdef NOCHECKBOUNDS
00060 #define STRING_NOCHECKBOUNDS 1
00061 #endif
00062 
00067 #ifdef STRING_NOCHECKBOUNDS // TODO!
00068 #define STRING_GETCHAR(n) return mData[n];
00069 #else
00070 #define STRING_GETCHAR(n) return mData[n];
00071 #endif
00072 
00077 class MagiC::String : public Comparable {
00078     decl_dynamic (String);
00079   public:
00080                     String              ();
00081                     String              (const String& orig);
00082                     String              (const char*);
00083                     String              (char* str, bool own);
00084                     String              (const char*, int n);
00085                     String              (char*, uint n, bool own);
00086     explicit        String              (int i, int base=10);
00087     explicit        String              (uint i, int base=10);
00088     explicit        String              (long i, int base=10);
00089     explicit        String              (float f, char fmt='g', int prec=6);
00090     explicit        String              (double f, char fmt='g', int prec=6);
00091     virtual         ~String             ();
00092 
00093     // Operators
00094     String&         assign              (char c);
00095     String&         operator=           (const String&);
00096     String&         operator=           (const char*);
00097     String&         operator=           (char c) {return assign(c);}
00098     int             operator!=          (const char* b) const {return !operator== (b);}
00099     int             operator==          (const char*) const;
00100     int             operator==          (const String& b) const {return (*this) == b.mData;}
00101     int             operator==          (const Comparable& other) const;
00102     String&         operator+=          (const String& str);
00103     String&         operator+=          (char c);
00104     String          operator+           (const String& str) const;
00105     String          operator+           (const char* str) const;
00106     const char      operator[]          (uint n) const {STRING_GETCHAR(n)} // Varies on bounds checking, see above
00107     char&           operator[]          (uint n) {STRING_GETCHAR(n)} // Varies on bounds checking, see above
00108 
00109     // Information
00110     bool            isNull              () const {if (this) return !mData; return 0;}
00111     void            nullify             ();
00112     bool            isEmpty             () const {return (!this || !mData || !mLen);}
00113     void            empty               ();
00114     uint            length              () const {return mLen;}
00115     int             maxLength           () const {return mMaxLen;}
00116     //void          truncate            (uint pos);
00117     //void          fill                (char c, int len=-1)
00118 
00119     // Formatting
00120     String          arg                 (const String& str, int fieldwidth=0) const;
00121     String          arg                 (const char* str, int fieldwidth=0) const {return arg (String (str), fieldwidth);}
00122     String          arg                 (const char c, int fieldwidth=0) const {return arg (String (c), fieldwidth);}
00123     String          arg                 (float x, int fieldwidth=0, char fmt='g', int prec=-1) const {return arg (String (x, fmt, prec), fieldwidth);}
00124     String          arg                 (double x, int fieldwidth=0, char fmt='g', int prec=-1) const {return arg (String (x, fmt, prec), fieldwidth);}
00125     String          arg                 (int x, int fieldwidth=0, int base=10) const {return arg (String (x, base), fieldwidth);}
00126     String          arg                 (long x, int fieldwidth=0, int base=10) const {return arg (String (x, base), fieldwidth);}
00127     //String&       sprintf             (const char* format, ...);
00128 
00129     // Searching
00130     int             find                (const String&, uint start=0) const;
00131     int             find                (const char c, uint start=0) const;
00132     int             findRev             (const String&, int start=-1) const;
00133     int             regmatch            (const char* regexpr) const;
00134     int             regmatch            (const char* regexpr, Array<String>& target) const;
00135     int             regmatch            (RegExp& compiled, Array<String>& target) const;
00136 
00137     // Manipulations
00138     String          mid                 (uint start, int len=-1) const;
00139     String          left                (uint n) const;
00140     String          right               (uint n) const;
00141     void            upper               () const;
00142     void            lower               () const;
00143     void            split               (Array<String>& target, const char delim=' ') const;
00144     void            join                (const Array<String>& source, const char delim=' ');
00145     String&         dellast             (uint n);
00146     void            chop                ();
00147     String          stripWhiteSpace     () const;
00148     String          simplifyWhiteSpace  () const;
00149 
00150     // Allocation
00151     void            reserve             (int amount);
00152     void            ensure              (int amount) {if (mMaxLen<amount) reserve (amount);}
00153     void            ensure_spontane     (int amount) {if (mMaxLen<amount) reserve (amount+amount/2+4);}
00154     void            grow_spontane       () {reserve (mMaxLen+mMaxLen/2+4);}
00155 
00156     char            checksum            ();
00157     int             fast_isequal        (const String& other) const;
00158 
00159     // Encodings
00160     String&         hexcode             (const String& other);
00161     enum            quoteflags          {QUOTE_NORMAL=0, QUOTE_HTML=1};
00162     void            quote               (char quotechar='%', int flags=0);
00163     void            unquote             (char quotechar='%', int flags=0);
00164 
00165     // I/O
00166     ostream&        dump                (ostream&) const;
00167     ostream&        operator>>          (ostream&) const;
00168     istream&        operator<<          (istream&);
00169     CArchive&       operator>>          (CArchive&) const;
00170     IStream&        operator<<          (IStream&);
00171 
00172     // Conversions
00173     int             toInt               () const {if (this && mData) return atoi (mData); return 0;}
00174     uint            toUInt              () const {if (this && mData) return (unsigned int) atoi (mData); return 0;}
00175     long            toLong              () const {if (this && mData) return atol (mData); return 0;}
00176     float           toFloat             () const {if (this && mData) return atof (mData); return 0;}
00177     double          toDouble            () const {if (this && mData) return atof (mData); return 0;}
00178                     operator const char*() const {return this? mData : (const char*) NULL;}
00180     char*           getbuffer           () const {return mData;}
00181 
00182     // Implementations
00183     virtual String* clone               () const;
00184     virtual int     hashfunc            (int hashsize) const;
00185     
00186   private:
00187     int             mLen;           
00188     int             mMaxLen;        
00189     char*           mData;
00190     unsigned char   mChkSum;        
00192     friend String   MagiC::strformat    (const char* format, ...);
00193 };
00194 
00195 
00196 
00198 //                                     |                              o                  //
00199 //  ___       |   ___        _    ___  |     __         _    ___   |           _    ____ //
00200 // /   ) \ / -+- /   ) |/\ |/ \   ___| |    /   |   | |/ \  |   \ -+- |  __  |/ \  (     //
00201 // |---   X   |  |---  |   |   | (   | |    +-- |   | |   | |      |  | /  \ |   |  \__  //
00202 //  \__  / \   \  \__  |   |   |  \__| |    |    \__! |   |  \__/   \ | \__/ |   | ____) //
00203 //                                          |                                            //
00205 
00206 namespace MagiC {
00207     extern const String emptystring;
00208     inline bool     isempty     (const String& str) {return (!(&str) || str.isEmpty ());}
00209     char*           strnchr     (const char* str, int len, char c);
00210     char*           safedup     (const char* orig, int maxlen=-1);
00211     int             fgetS       (FILE* in, String& str);
00212     istream&        getS        (istream& in, String& str, char term='\n');
00213     void            loadString  (String& str, const String& filename);
00214     String          strformat   (const char* format, ...);
00215 
00216 #ifndef format
00217 #define format strformat
00218 #endif
00219 }
00220 
00221 
00222 
00224 //                ----              ----         o                           //
00225 //               (           |     (      |          _                       //
00226 //                ---  |   | |---   ---  -+- |/\ | |/ \   ___                //
00227 //                   ) |   | |   )     )  |  |   | |   | (   \               //
00228 //               ___/   \__! |__/  ___/    \ |   | |   |  ---/               //
00229 //                                                        __/                //
00231 
00232 // Virtual substring of a string.
00233 class MagiC::SubString : public String {
00234     String*     str;
00235     int         start, end;
00236   public:
00237 };
00238 
00239 #endif

Generated at Tue Dec 4 19:53:28 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001