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

Generated on 13 Mar 2017 for xrootd by  doxygen 1.4.7