XrdOucString.hh

Go to the documentation of this file.
00001 #ifndef __OUC_STRING_H__
00002 #define __OUC_STRING_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                     X r d O u c S t r i n g . h h                          */
00006 /*                                                                            */
00007 /* (c) 2005 F. Furano (INFN Padova), G. Ganis (CERN)                          */
00008 /*     All Rights Reserved. See XrdInfo.cc for complete License Terms         */
00009 /******************************************************************************/
00010 
00011 //           $Id: XrdOucString.hh 32231 2010-02-05 18:24:46Z ganis $
00012 
00013 /******************************************************************************/
00014 /*                                                                            */
00015 /*  Light string manipulation class                                           */
00016 /*                                                                            */
00017 /*  This class has three private members: a buffer (char *) and two integers, */
00018 /*  indicating the effective length of the null-terminated string (len), and  */
00019 /*  the buffer capacity (siz), i.e. the allocated size. The capacity is set   */
00020 /*  at construction either at the value needed by the initializing string or  */
00021 /*  to the value requested by the user; by default the capacity is never      */
00022 /*  decreased during manipulations (it is increased if required by the        */
00023 /*  operation). The capacity can be changed at any time by calling resize().  */
00024 /*  The user can choose a granularity other than 1 to increase the capacity   */
00025 /*  by calling XrdOucString::setblksize(nbs) with nbs > 1: this will make     */
00026 /*  new allocations to happen in blocks multiple of nbs bytes.                */
00027 /*                                                                            */
00028 /*  1. Constructors                                                           */
00029 /*                                                                            */
00030 /*     XrdOucString(int lmx = 0)                                              */
00031 /*      - create an empty string; set capacity to lmx+1 if lmx > 0            */
00032 /*     XrdOucString(const char *s, int lmx = 0)                               */
00033 /*      - create a string containing s; capacity is set to strlen(s)+1 or     */
00034 /*        to lmx+1 if lmx > 0; in the latter case truncation may occur.       */
00035 /*     XrdOucString(const char c, int lmx = 0)                                */
00036 /*      - create a string char c; capacity is set to 2 or to lmx+1 if lmx > 0.*/
00037 /*     XrdOucString(const XrdOucString &s)                                    */
00038 /*      - create string copying from XrdOucString s .                         */
00039 /*     XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0)    */
00040 /*      - create string copying a portion of XrdOucString s; portion is       */
00041 /*        defined by j to k inclusive; if k == -1 the portion copied will be  */
00042 /*        from j to end-of-string; if j or k are inconsistent they are taken  */
00043 /*        as 0 or len-1, respectively; capacity is set to k-j bytes or to     */
00044 /*        to lmx+1 if lmx > 0; in the latter case truncation of the portion   */
00045 /*        may occur.                                                          */
00046 /*                                                                            */
00047 /*  2. Access to information                                                  */
00048 /*                                                                            */
00049 /*     const char   *c_str() const                                            */
00050 /*      - return pointer to stored string                                     */
00051 /*     int           length() const                                           */
00052 /*      - return length stored string                                         */
00053 /*     int           capacity() const                                         */
00054 /*      - return capacity of the allocated buffer                             */
00055 /*                                                                            */
00056 /*     char         &operator[](int j)                                        */
00057 /*      - array-like operator returning char at position j; abort is invoked  */
00058 /*        if j is not in the correct range                                    */
00059 /*                                                                            */
00060 /*     int           find(const char c, int start = 0, bool forward = 1);     */
00061 /*      - find first occurence of char c starting from position start in      */
00062 /*        forward (forward == 1, default) or backward (forward == 0)          */
00063 /*        direction; returns STR_NPOS if nothing is found                     */
00064 /*     int           find(const char *s, int start = 0)                       */
00065 /*      - find first occurence of string s starting from position start in    */
00066 /*        forward direction; returns STR_NPOS if nothing is found             */
00067 /*     int           find(XrdOucString s, int start = 0)                      */
00068 /*      - find first occurence of XrdOucString s starting from position start */
00069 /*        in forward direction; returns STR_NPOS if nothing is found          */
00070 /*                                                                            */
00071 /*     int           rfind(const char c, int start = STR_NPOS)                */
00072 /*      - find first occurence of char c starting from position start in      */
00073 /*        backward direction; returns STR_NPOS if nothing is found.           */
00074 /*     int           rfind(const char *s, int start = STR_NPOS)               */
00075 /*      - find first occurence of string s starting from position start in    */
00076 /*        backward direction; returns STR_NPOS if nothing is found;           */
00077 /*        if start == STR_NPOS search starts from position len-strlen(s)      */
00078 /*     int           rfind(XrdOucString s, int start = STR_NPOS)              */
00079 /*      - find first occurence of XrdOucString s starting from position start */
00080 /*        in backward direction; returns STR_NPOS if nothing is found;        */
00081 /*        if start == STR_NPOS search starts from position len-s.lenght()     */
00082 /*                                                                            */
00083 /*     bool          beginswith(char c)                                       */
00084 /*      - returns 1 if the stored string starts with char c                   */
00085 /*     bool          beginswith(const char *s)                                */
00086 /*      - returns 1 if the stored string starts with string s                 */
00087 /*     bool          beginswith(XrdOucString s)                               */
00088 /*      - returns 1 if the stored string starts with XrdOucString s           */
00089 /*                                                                            */
00090 /*     bool          endswith(char c)                                         */
00091 /*      - returns 1 if the stored string ends with char c                     */
00092 /*     bool          endswith(const char *s)                                  */
00093 /*      - returns 1 if the stored string ends with string s                   */
00094 /*     bool          endswith(XrdOucString s)                                 */
00095 /*      - returns 1 if the stored string ends with XrdOucString s             */
00096 /*                                                                            */
00097 /*     int           matches(const char *s, char wch = '*')                   */
00098 /*      - check if stored string is compatible with s allowing for wild char  */
00099 /*        wch (default: '*'); return the number of matching characters.       */
00100 /*                                                                            */
00101 /*  3. Modifiers                                                              */
00102 /*                                                                            */
00103 /*     void          resize(int lmx = 0)                                      */
00104 /*      - resize buffer capacity to lmx+1 bytes; if lmx <= 0, free the buffer.*/
00105 /*                                                                            */
00106 /*     void          append(const int i)                                      */
00107 /*      - append to stored string the string representation of integer i,     */
00108 /*        e.g. if string is initially "x*", after append(5) it will be "x*5". */
00109 /*     void          append(const char c)                                     */
00110 /*      - append char c to stored string, e.g. if string is initially "pop",  */
00111 /*        after append('_') it will be "pop_".                                */
00112 /*     void          append(const char *s)                                    */
00113 /*      - append string s to stored string, e.g. if string is initially "pop",*/
00114 /*        after append("star") it will be "popstar".                          */
00115 /*     void          append(const XrdOucString s)                             */
00116 /*      - append s.c_str() to stored string, e.g. if string is initially      */
00117 /*        "anti", after append("star") it will be "antistar".                 */
00118 /*                                                                            */
00119 /*     void          assign(const char *s, int j, int k = -1)                 */
00120 /*      - copy to allocated buffer a portion of string s; portion is defined  */
00121 /*        by j to k inclusive; if k == -1 the portion copied will be from j   */
00122 /*        to end-of-string; if j or k are inconsistent they are taken as 0 or */
00123 /*        len-1, respectively; if necessary, capacity is increased to k-j     */
00124 /*        bytes.                                                              */
00125 /*     void          assign(const XrdOucString s, int j, int k = -1)          */
00126 /*      - copy to allocated buffer a portion of s.c_str(); portion is defined */
00127 /*        by j to k inclusive; if k == -1 the portion copied will be from j   */
00128 /*        to end-of-string; if j or k are inconsistent they are taken as 0 or */
00129 /*        len-1, respectively; if necessary, capacity is increased to k-j     */
00130 /*        bytes.                                                              */
00131 /*                                                                            */
00132 /*     int           keep(int start = 0, int size = 0)                        */
00133 /*      - drop chars outside the range of size bytes starting at start        */
00134 /*                                                                            */
00135 /*     void          insert(const int i, int start = -1)                      */
00136 /*      - insert the string representation of integer i at position start of  */
00137 /*        the stored string, e.g. if string is initially "*x", after          */
00138 /*        insert(5,0) it will be "5*x"; default action is append.             */
00139 /*     void          insert(const char c, int start = -1)                     */
00140 /*      - insert the char c at position start of the stored string, e.g.      */
00141 /*        if string is initially "pok", after insert('_',0) it will be "_poc";*/
00142 /*        default action is append.                                           */
00143 /*     void          insert(const char *s, int start = -1, int lmx = 0)       */
00144 /*      - insert string s at position start of the stored string, e.g.        */
00145 /*        if string is initially "forth", after insert("backand",0) it will be*/
00146 /*        "backandforth"; default action is append.                           */
00147 /*     void          insert(const XrdOucString s, int start = -1)             */
00148 /*      - insert string s.c_str() at position start of the stored string.     */
00149 /*                                                                            */
00150 /*     int           replace(const char *s1, const char *s2,                  */
00151 /*                           int from = 0, int to = -1);                      */
00152 /*      - replace all occurrencies of string s1 with string s2 in the region  */
00153 /*        from position 'from' to position 'to' inclusive; the method is      */
00154 /*        optimized to minimize the memory movements; with s2 == 0 or ""      */
00155 /*        removes all instances of s1 in the specified region.                */
00156 /*     int           replace(const XrdOucString s1, const char *s2,           */
00157 /*                           int from = 0, int to = -1);                      */
00158 /*     int           replace(const char *s1, const XrdOucString s2,           */
00159 /*                           int from = 0, int to = -1);                      */
00160 /*     int           replace(const XrdOucString s1, const XrdOucString s2,    */
00161 /*                           int from = 0, int to = -1);                      */
00162 /*      - interfaces to replace(const char *, const char *, int, int)         */
00163 /*                                                                            */
00164 /*     int           erase(int start = 0, int size = 0)                       */
00165 /*      - erase size bytes starting at start                                  */
00166 /*     int           erase(const char *s, int from = 0, int to = -1)          */
00167 /*      - erase occurences of string s within position 'from' and position    */
00168 /*        'to' (inclusive), e.g if stored string is "aabbccefccddgg", then    */
00169 /*        erase("cc",0,9) will result in string "aabbefccddgg".               */
00170 /*     int           erase(XrdOucString s, int from = 0, int to = -1)         */
00171 /*      - erase occurences of s.c_str() within position 'from' and position   */
00172 /*        'to' (inclusive).                                                   */
00173 /*     int           erasefromstart(int sz = 0)                               */
00174 /*      - erase sz bytes from the start.                                      */
00175 /*     int           erasefromend(int sz = 0)                                 */
00176 /*      - erase sz bytes from the end.                                        */
00177 /*                                                                            */
00178 /*     void          lower(int pos, int size = 0)                             */
00179 /*      - set to lower case size bytes from position start.                   */
00180 /*     void          upper(int pos, int size = 0)                             */
00181 /*      - set to upper case size bytes from position start.                   */
00182 /*                                                                            */
00183 /*     void          hardreset()                                              */
00184 /*      - memset to 0 the len meaningful bytes of the buffer.                 */
00185 /*                                                                            */
00186 /*     int           tokenize(XrdOucString &tok, int from, char del)          */
00187 /*      - search for tokens delimited by 'del' (def ':') in string s; search  */
00188 /*        starts from 'from' and the token is returned in 'tok'.              */
00189 /*                                                                            */
00190 /*  4. Assignement operators                                                  */
00191 /*     XrdOucString &operator=(const int i)                                   */
00192 /*     XrdOucString &operator=(const char c)                                  */
00193 /*     XrdOucString &operator=(const char *s)                                 */
00194 /*     XrdOucString &operator=(const XrdOucString s)                          */
00195 /*                                                                            */
00196 /*  5. Addition operators                                                     */
00197 /*     XrdOucString &operator+(const int i)                                   */
00198 /*     XrdOucString &operator+(const char c)                                  */
00199 /*     XrdOucString &operator+(const char *s)                                 */
00200 /*     XrdOucString &operator+(const XrdOucString s)                          */
00201 /*     XrdOucString &operator+=(const int i)                                  */
00202 /*     XrdOucString &operator+=(const char c)                                 */
00203 /*     XrdOucString &operator+=(const char *s)                                */
00204 /*     XrdOucString &operator+=(const XrdOucString s)                         */
00205 /*     XrdOucString const operator+(const char *s1, const XrdOucString s2)    */
00206 /*     XrdOucString const operator+(const char c, const XrdOucString s)       */
00207 /*     XrdOucString const operator+(const int i, const XrdOucString s)        */
00208 /*                                                                            */
00209 /*  6. Equality operators                                                     */
00210 /*     int operator==(const int i)                                            */
00211 /*     int operator==(const char c)                                           */
00212 /*     int operator==(const char *s)                                          */
00213 /*     int operator==(const XrdOucString s)                                   */
00214 /*                                                                            */
00215 /*  7. Inequality operators                                                   */
00216 /*     int operator!=(const int i)                                            */
00217 /*     int operator!=(const char c)                                           */
00218 /*     int operator!=(const char *s)                                          */
00219 /*     int operator!=(const XrdOucString s)                                   */
00220 /*                                                                            */
00221 /*  8. Static methods to change / monitor the blksize                         */
00222 /*     static int getblksize();                                               */
00223 /*     static void setblksize(const int bs);                                  */
00224 /*                                                                            */
00225 /******************************************************************************/
00226 #include "XrdSys/XrdSysHeaders.hh"
00227 
00228 #include <stdio.h>
00229 #include <stdlib.h>
00230 #include <stdarg.h>
00231 
00232 using namespace std;
00233 
00234 #define STR_NPOS -1
00235 
00236 class XrdOucString {
00237 
00238 private:
00239    char *str;
00240    int   len;
00241    int   siz;
00242 
00243    // Mininal block size to be used in (re-)allocations
00244    // Option switched off by default; use XrdOucString::setblksize()
00245    // and XrdOucString::getblksize() to change / monitor 
00246    static int blksize;
00247 
00248    // Private methods
00249    int         adjust(int ls, int &j, int &k, int nmx = 0);
00250    char       *bufalloc(int nsz);
00251    inline void init() { str = 0; len = 0; siz = 0; }
00252 
00253 public:
00254    XrdOucString(int lmx = 0) { init(); if (lmx > 0) str = bufalloc(lmx+1); }
00255    XrdOucString(const char *s, int lmx = 0);
00256    XrdOucString(const char c, int lmx = 0);
00257    XrdOucString(const XrdOucString &s);
00258    XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0);
00259    virtual ~XrdOucString();
00260 
00261    // Info access
00262    const char   *c_str() const { return (const char *)str; }
00263    int           length() const { return len; }
00264    int           capacity() const { return siz; }
00265    char         &operator[](int j);
00266    int           find(const char c, int start = 0, bool forward = 1);
00267    int           find(const char *s, int start = 0);
00268    int           find(XrdOucString s, int start = 0);
00269    int           rfind(const char c, int start = STR_NPOS)
00270                                              { return find(c, start, 0); }
00271    int           rfind(const char *s, int start = STR_NPOS);
00272    int           rfind(XrdOucString s, int start = STR_NPOS);
00273    bool          beginswith(char c) { return (find(c) == 0); }
00274    bool          beginswith(const char *s) { return (find(s) == 0); }
00275    bool          beginswith(XrdOucString s) { return (find(s) == 0); }
00276    bool          endswith(char c);
00277    bool          endswith(const char *s);
00278    bool          endswith(XrdOucString s) { return (endswith(s.c_str())); }
00279    int           matches(const char *s, char wch = '*');
00280 
00281    // Tokenizer
00282    int           tokenize(XrdOucString &tok, int from, char del = ':');
00283 
00284    // Modifiers
00285    void          resize(int lmx = 0) { int ns = (lmx > 0) ? lmx + 1 : 0;
00286                                        str = bufalloc(ns); }
00287    void          append(const int i);
00288    void          append(const char c);
00289    void          append(const char *s);
00290    void          append(const XrdOucString s);
00291    void          assign(const char *s, int j, int k = -1);
00292    void          assign(const XrdOucString s, int j, int k = -1);
00293 #if !defined(WINDOWS)
00294    int           form(const char *fmt, ...);
00295 #endif
00296    int           keep(int start = 0, int size = 0);
00297    void          insert(const int i, int start = -1);
00298    void          insert(const char c, int start = -1);
00299    void          insert(const char *s, int start = -1, int lmx = 0);
00300    void          insert(const XrdOucString s, int start = -1);
00301    int           replace(const char *s1, const char *s2,
00302                                          int from = 0, int to = -1);
00303    int           replace(const XrdOucString s1, const XrdOucString s2,
00304                                                 int from = 0, int to = -1);
00305    int           replace(const XrdOucString s1, const char *s2,
00306                                                 int from = 0, int to = -1);
00307    int           replace(const char *s1, const XrdOucString s2,
00308                                                 int from = 0, int to = -1);
00309    int           erase(int start = 0, int size = 0);
00310    int           erase(const char *s, int from = 0, int to = -1);
00311    int           erase(XrdOucString s, int from = 0, int to = -1);
00312    int           erasefromstart(int sz = 0) { return erase(0,sz); }
00313    int           erasefromend(int sz = 0) { return erase(len-sz,sz); }
00314    void          lower(int pos, int size = 0);
00315    void          upper(int pos, int size = 0);
00316    void          reset(const char c, int j = 0, int k = -1);
00317    void          hardreset();
00318    void          setbuffer(char *buf);
00319 
00320    // Assignement operators
00321    XrdOucString &operator=(const int i);
00322    XrdOucString &operator=(const char c);
00323    XrdOucString &operator=(const char *s);
00324    XrdOucString &operator=(const XrdOucString s);
00325 
00326    // Add operators
00327    friend XrdOucString operator+(const XrdOucString &s1, const int i);
00328    friend XrdOucString operator+(const XrdOucString &s1, const char c);
00329    friend XrdOucString operator+(const XrdOucString &s1, const char *s);
00330    friend XrdOucString operator+(const XrdOucString &s1, const XrdOucString &s);
00331    XrdOucString &operator+=(const int i);
00332    XrdOucString &operator+=(const char c);
00333    XrdOucString &operator+=(const char *s);
00334    XrdOucString &operator+=(const XrdOucString s);   
00335 
00336    // Equality operators
00337    int operator==(const int i);
00338    int operator==(const char c);
00339    int operator==(const char *s);
00340    int operator==(const XrdOucString s);
00341 
00342    // Inequality operators
00343    int operator!=(const int i) { return !(*this == i); }
00344    int operator!=(const char c) { return !(*this == c); }
00345    int operator!=(const char *s) { return !(*this == s); }
00346    int operator!=(const XrdOucString s) { return !(*this == s); }
00347 
00348    // Miscellanea
00349    bool isdigit(int from = 0, int to = -1);
00350    long atoi(int from = 0, int to = -1);
00351 
00352    // Static methods to change / monitor the default blksize
00353    static int getblksize();
00354    static void setblksize(const int bs);
00355 
00356 #if !defined(WINDOWS)
00357    // format a string
00358    static int form(XrdOucString &str, const char *fmt, ...);
00359 #endif
00360 };
00361 
00362 // Operator << is useful to print a string into a stream
00363 ostream &operator<< (ostream &, const XrdOucString s);
00364 
00365 XrdOucString const operator+(const char *s1, const XrdOucString s2);
00366 XrdOucString const operator+(const char c, const XrdOucString s);
00367 XrdOucString const operator+(const int i, const XrdOucString s);
00368 
00369 #endif
00370 

Generated on Tue Jul 5 14:46:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1