Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:27:55

0001 #ifndef __OUC_STRING_H__
0002 #define __OUC_STRING_H__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                     X r d O u c S t r i n g . h h                          */
0006 /*                                                                            */
0007 /* (c) 2005 F. Furano (INFN Padova), G. Ganis (CERN)                          */
0008 /*                                                                            */
0009 /* This file is part of the XRootD software suite.                            */
0010 /*                                                                            */
0011 /* XRootD is free software: you can redistribute it and/or modify it under    */
0012 /* the terms of the GNU Lesser General Public License as published by the     */
0013 /* Free Software Foundation, either version 3 of the License, or (at your     */
0014 /* option) any later version.                                                 */
0015 /*                                                                            */
0016 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0017 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0018 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0019 /* License for more details.                                                  */
0020 /*                                                                            */
0021 /* You should have received a copy of the GNU Lesser General Public License   */
0022 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0023 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0024 /*                                                                            */
0025 /* The copyright holder's institutional names and contributor's names may not */
0026 /* be used to endorse or promote products derived from this software without  */
0027 /* specific prior written permission of the institution or contributor.       */
0028 /*     All Rights Reserved. See XrdInfo.cc for complete License Terms         */
0029 /******************************************************************************/
0030 
0031 /******************************************************************************/
0032 /*                                                                            */
0033 /*  Light string manipulation class                                           */
0034 /*                                                                            */
0035 /*  This class has three private members: a buffer (char *) and two integers, */
0036 /*  indicating the effective length of the null-terminated string (len), and  */
0037 /*  the buffer capacity (siz), i.e. the allocated size. The capacity is set   */
0038 /*  at construction either at the value needed by the initializing string or  */
0039 /*  to the value requested by the user; by default the capacity is never      */
0040 /*  decreased during manipulations (it is increased if required by the        */
0041 /*  operation). The capacity can be changed at any time by calling resize().  */
0042 /*  The user can choose a granularity other than 1 to increase the capacity   */
0043 /*  by calling XrdOucString::setblksize(nbs) with nbs > 1: this will make     */
0044 /*  new allocations to happen in blocks multiple of nbs bytes.                */
0045 /*                                                                            */
0046 /*  1. Constructors                                                           */
0047 /*                                                                            */
0048 /*     XrdOucString(int lmx = 0)                                              */
0049 /*      - create an empty string; set capacity to lmx+1 if lmx > 0            */
0050 /*     XrdOucString(const char *s, int lmx = 0)                               */
0051 /*      - create a string containing s; capacity is set to strlen(s)+1 or     */
0052 /*        to lmx+1 if lmx > 0; in the latter case truncation may occur.       */
0053 /*     XrdOucString(const char c, int lmx = 0)                                */
0054 /*      - create a string char c; capacity is set to 2 or to lmx+1 if lmx > 0.*/
0055 /*     XrdOucString(const XrdOucString &s)                                    */
0056 /*      - create string copying from XrdOucString s .                         */
0057 /*     XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0)    */
0058 /*      - create string copying a portion of XrdOucString s; portion is       */
0059 /*        defined by j to k inclusive; if k == -1 the portion copied will be  */
0060 /*        from j to end-of-string; if j or k are inconsistent they are taken  */
0061 /*        as 0 or len-1, respectively; capacity is set to k-j bytes or to     */
0062 /*        to lmx+1 if lmx > 0; in the latter case truncation of the portion   */
0063 /*        may occur.                                                          */
0064 /*                                                                            */
0065 /*  2. Access to information                                                  */
0066 /*                                                                            */
0067 /*     const char   *c_str() const                                            */
0068 /*      - return pointer to stored string                                     */
0069 /*     int           length() const                                           */
0070 /*      - return length stored string                                         */
0071 /*     int           capacity() const                                         */
0072 /*      - return capacity of the allocated buffer                             */
0073 /*                                                                            */
0074 /*     char         &operator[](int j)                                        */
0075 /*      - array-like operator returning char at position j; abort is invoked  */
0076 /*        if j is not in the correct range                                    */
0077 /*                                                                            */
0078 /*     int           find(const char c, int start = 0, bool forward = 1);     */
0079 /*      - find first occurence of char c starting from position start in      */
0080 /*        forward (forward == 1, default) or backward (forward == 0)          */
0081 /*        direction; returns STR_NPOS if nothing is found                     */
0082 /*     int           find(const char *s, int start = 0)                       */
0083 /*      - find first occurence of string s starting from position start in    */
0084 /*        forward direction; returns STR_NPOS if nothing is found             */
0085 /*     int           find(XrdOucString s, int start = 0)                      */
0086 /*      - find first occurence of XrdOucString s starting from position start */
0087 /*        in forward direction; returns STR_NPOS if nothing is found          */
0088 /*                                                                            */
0089 /*     int           rfind(const char c, int start = STR_NPOS)                */
0090 /*      - find first occurence of char c starting from position start in      */
0091 /*        backward direction; returns STR_NPOS if nothing is found.           */
0092 /*     int           rfind(const char *s, int start = STR_NPOS)               */
0093 /*      - find first occurence of string s starting from position start in    */
0094 /*        backward direction; returns STR_NPOS if nothing is found;           */
0095 /*        if start == STR_NPOS search starts from position len-strlen(s)      */
0096 /*     int           rfind(XrdOucString s, int start = STR_NPOS)              */
0097 /*      - find first occurence of XrdOucString s starting from position start */
0098 /*        in backward direction; returns STR_NPOS if nothing is found;        */
0099 /*        if start == STR_NPOS search starts from position len-s.lenght()     */
0100 /*                                                                            */
0101 /*     bool          beginswith(char c)                                       */
0102 /*      - returns 1 if the stored string starts with char c                   */
0103 /*     bool          beginswith(const char *s)                                */
0104 /*      - returns 1 if the stored string starts with string s                 */
0105 /*     bool          beginswith(XrdOucString s)                               */
0106 /*      - returns 1 if the stored string starts with XrdOucString s           */
0107 /*                                                                            */
0108 /*     bool          endswith(char c)                                         */
0109 /*      - returns 1 if the stored string ends with char c                     */
0110 /*     bool          endswith(const char *s)                                  */
0111 /*      - returns 1 if the stored string ends with string s                   */
0112 /*     bool          endswith(XrdOucString s)                                 */
0113 /*      - returns 1 if the stored string ends with XrdOucString s             */
0114 /*                                                                            */
0115 /*     int           matches(const char *s, char wch = '*')                   */
0116 /*      - check if stored string is compatible with s allowing for wild char  */
0117 /*        wch (default: '*'); return the number of matching characters.       */
0118 /*                                                                            */
0119 /*  3. Modifiers                                                              */
0120 /*                                                                            */
0121 /*     void          resize(int lmx = 0)                                      */
0122 /*      - resize buffer capacity to lmx+1 bytes; if lmx <= 0, free the buffer.*/
0123 /*                                                                            */
0124 /*     void          append(const int i)                                      */
0125 /*      - append to stored string the string representation of integer i,     */
0126 /*        e.g. if string is initially "x*", after append(5) it will be "x*5". */
0127 /*     void          append(const char c)                                     */
0128 /*      - append char c to stored string, e.g. if string is initially "pop",  */
0129 /*        after append('_') it will be "pop_".                                */
0130 /*     void          append(const char *s)                                    */
0131 /*      - append string s to stored string, e.g. if string is initially "pop",*/
0132 /*        after append("star") it will be "popstar".                          */
0133 /*     void          append(const XrdOucString s)                             */
0134 /*      - append s.c_str() to stored string, e.g. if string is initially      */
0135 /*        "anti", after append("star") it will be "antistar".                 */
0136 /*                                                                            */
0137 /*     void          assign(const char *s, int j, int k = -1)                 */
0138 /*      - copy to allocated buffer a portion of string s; portion is defined  */
0139 /*        by j to k inclusive; if k == -1 the portion copied will be from j   */
0140 /*        to end-of-string; if j or k are inconsistent they are taken as 0 or */
0141 /*        len-1, respectively; if necessary, capacity is increased to k-j     */
0142 /*        bytes.                                                              */
0143 /*     void          assign(const XrdOucString s, int j, int k = -1)          */
0144 /*      - copy to allocated buffer a portion of s.c_str(); portion is defined */
0145 /*        by j to k inclusive; if k == -1 the portion copied will be from j   */
0146 /*        to end-of-string; if j or k are inconsistent they are taken as 0 or */
0147 /*        len-1, respectively; if necessary, capacity is increased to k-j     */
0148 /*        bytes.                                                              */
0149 /*                                                                            */
0150 /*     int           keep(int start = 0, int size = 0)                        */
0151 /*      - drop chars outside the range of size bytes starting at start        */
0152 /*                                                                            */
0153 /*     void          insert(const int i, int start = -1)                      */
0154 /*      - insert the string representation of integer i at position start of  */
0155 /*        the stored string, e.g. if string is initially "*x", after          */
0156 /*        insert(5,0) it will be "5*x"; default action is append.             */
0157 /*     void          insert(const char c, int start = -1)                     */
0158 /*      - insert the char c at position start of the stored string, e.g.      */
0159 /*        if string is initially "pok", after insert('_',0) it will be "_poc";*/
0160 /*        default action is append.                                           */
0161 /*     void          insert(const char *s, int start = -1, int lmx = 0)       */
0162 /*      - insert string s at position start of the stored string, e.g.        */
0163 /*        if string is initially "forth", after insert("backand",0) it will be*/
0164 /*        "backandforth"; default action is append.                           */
0165 /*     void          insert(const XrdOucString s, int start = -1)             */
0166 /*      - insert string s.c_str() at position start of the stored string.     */
0167 /*                                                                            */
0168 /*     int           replace(const char *s1, const char *s2,                  */
0169 /*                           int from = 0, int to = -1);                      */
0170 /*      - replace all occurrencies of string s1 with string s2 in the region  */
0171 /*        from position 'from' to position 'to' inclusive; the method is      */
0172 /*        optimized to minimize the memory movements; with s2 == 0 or ""      */
0173 /*        removes all instances of s1 in the specified region.                */
0174 /*     int           replace(const XrdOucString s1, const char *s2,           */
0175 /*                           int from = 0, int to = -1);                      */
0176 /*     int           replace(const char *s1, const XrdOucString s2,           */
0177 /*                           int from = 0, int to = -1);                      */
0178 /*     int           replace(const XrdOucString s1, const XrdOucString s2,    */
0179 /*                           int from = 0, int to = -1);                      */
0180 /*      - interfaces to replace(const char *, const char *, int, int)         */
0181 /*                                                                            */
0182 /*     int           erase(int start = 0, int size = 0)                       */
0183 /*      - erase size bytes starting at start                                  */
0184 /*     int           erase(const char *s, int from = 0, int to = -1)          */
0185 /*      - erase occurences of string s within position 'from' and position    */
0186 /*        'to' (inclusive), e.g if stored string is "aabbccefccddgg", then    */
0187 /*        erase("cc",0,9) will result in string "aabbefccddgg".               */
0188 /*     int           erase(XrdOucString s, int from = 0, int to = -1)         */
0189 /*      - erase occurences of s.c_str() within position 'from' and position   */
0190 /*        'to' (inclusive).                                                   */
0191 /*     int           erasefromstart(int sz = 0)                               */
0192 /*      - erase sz bytes from the start.                                      */
0193 /*     int           erasefromend(int sz = 0)                                 */
0194 /*      - erase sz bytes from the end.                                        */
0195 /*                                                                            */
0196 /*     void          lower(int pos, int size = 0)                             */
0197 /*      - set to lower case size bytes from position start.                   */
0198 /*     void          upper(int pos, int size = 0)                             */
0199 /*      - set to upper case size bytes from position start.                   */
0200 /*                                                                            */
0201 /*     void          hardreset()                                              */
0202 /*      - memset to 0 the len meaningful bytes of the buffer.                 */
0203 /*                                                                            */
0204 /*     int           tokenize(XrdOucString &tok, int from, char del)          */
0205 /*      - search for tokens delimited by 'del' (def ':') in string s; search  */
0206 /*        starts from 'from' and the token is returned in 'tok'.              */
0207 /*                                                                            */
0208 /*  4. Assignement operators                                                  */
0209 /*     XrdOucString &operator=(const int i)                                   */
0210 /*     XrdOucString &operator=(const char c)                                  */
0211 /*     XrdOucString &operator=(const char *s)                                 */
0212 /*     XrdOucString &operator=(const XrdOucString s)                          */
0213 /*                                                                            */
0214 /*  5. Addition operators                                                     */
0215 /*     XrdOucString &operator+(const int i)                                   */
0216 /*     XrdOucString &operator+(const char c)                                  */
0217 /*     XrdOucString &operator+(const char *s)                                 */
0218 /*     XrdOucString &operator+(const XrdOucString s)                          */
0219 /*     XrdOucString &operator+=(const int i)                                  */
0220 /*     XrdOucString &operator+=(const char c)                                 */
0221 /*     XrdOucString &operator+=(const char *s)                                */
0222 /*     XrdOucString &operator+=(const XrdOucString s)                         */
0223 /*     XrdOucString const operator+(const char *s1, const XrdOucString s2)    */
0224 /*     XrdOucString const operator+(const char c, const XrdOucString s)       */
0225 /*     XrdOucString const operator+(const int i, const XrdOucString s)        */
0226 /*                                                                            */
0227 /*  6. Equality operators                                                     */
0228 /*     int operator==(const int i)                                            */
0229 /*     int operator==(const char c)                                           */
0230 /*     int operator==(const char *s)                                          */
0231 /*     int operator==(const XrdOucString s)                                   */
0232 /*                                                                            */
0233 /*  7. Inequality operators                                                   */
0234 /*     int operator!=(const int i)                                            */
0235 /*     int operator!=(const char c)                                           */
0236 /*     int operator!=(const char *s)                                          */
0237 /*     int operator!=(const XrdOucString s)                                   */
0238 /*                                                                            */
0239 /*  8. Static methods to change / monitor the blksize                         */
0240 /*     static int getblksize();                                               */
0241 /*     static void setblksize(const int bs);                                  */
0242 /*                                                                            */
0243 /******************************************************************************/
0244 #include "XrdSys/XrdSysHeaders.hh"
0245 
0246 #include <cstdio>
0247 #include <cstdlib>
0248 #include <cstdarg>
0249 
0250 #define STR_NPOS -1
0251 
0252 class XrdOucString {
0253 
0254 private:
0255    char *str;
0256    int   len;
0257    int   siz;
0258 
0259    // Mininal block size to be used in (re-)allocations
0260    // Option switched off by default; use XrdOucString::setblksize()
0261    // and XrdOucString::getblksize() to change / monitor 
0262    static int blksize;
0263 
0264    // Private methods
0265    int         adjust(int ls, int &j, int &k, int nmx = 0);
0266    char       *bufalloc(int nsz);
0267    inline void init() { str = 0; len = 0; siz = 0; }
0268 
0269 public:
0270    XrdOucString(int lmx = 0) { init(); if (lmx > 0) str = bufalloc(lmx+1); }
0271    XrdOucString(const char *s, int lmx = 0);
0272    XrdOucString(const char c, int lmx = 0);
0273    XrdOucString(const XrdOucString &s);
0274    XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0);
0275    virtual ~XrdOucString();
0276 
0277    // Info access
0278    const char   *c_str() const { return (const char *)str; }
0279    int           length() const { return len; }
0280    int           capacity() const { return siz; }
0281    char         &operator[](int j);
0282    int           find(const char c, int start = 0, bool forward = 1);
0283    int           find(const char *s, int start = 0);
0284    int           find(XrdOucString s, int start = 0);
0285    int           rfind(const char c, int start = STR_NPOS)
0286                                              { return find(c, start, 0); }
0287    int           rfind(const char *s, int start = STR_NPOS);
0288    int           rfind(XrdOucString s, int start = STR_NPOS);
0289    bool          beginswith(char c) { return (find(c) == 0); }
0290    bool          beginswith(const char *s) { return (find(s) == 0); }
0291    bool          beginswith(XrdOucString s) { return (find(s) == 0); }
0292    bool          endswith(char c);
0293    bool          endswith(const char *s);
0294    bool          endswith(XrdOucString s) { return (endswith(s.c_str())); }
0295    int           matches(const char *s, char wch = '*');
0296 
0297    // Tokenizer
0298    int           tokenize(XrdOucString &tok, int from, char del = ':');
0299 
0300    // Modifiers
0301    void          resize(int lmx = 0) { int ns = (lmx > 0) ? lmx + 1 : 0;
0302                                        str = bufalloc(ns); }
0303    void          append(const int i);
0304    void          append(const char c);
0305    void          append(const char *s);
0306    void          append(const XrdOucString s);
0307    void          assign(const char *s, int j, int k = -1);
0308    void          assign(const XrdOucString s, int j, int k = -1);
0309 #if !defined(WINDOWS)
0310    int           form(const char *fmt, ...);
0311 #endif
0312    int           keep(int start = 0, int size = 0);
0313    void          insert(const int i, int start = -1);
0314    void          insert(const char c, int start = -1);
0315    void          insert(const char *s, int start = -1, int lmx = 0);
0316    void          insert(const XrdOucString s, int start = -1);
0317    int           replace(const char *s1, const char *s2,
0318                                          int from = 0, int to = -1);
0319    int           replace(const XrdOucString s1, const XrdOucString s2,
0320                                                 int from = 0, int to = -1);
0321    int           replace(const XrdOucString s1, const char *s2,
0322                                                 int from = 0, int to = -1);
0323    int           replace(const char *s1, const XrdOucString s2,
0324                                                 int from = 0, int to = -1);
0325    int           erase(int start = 0, int size = 0);
0326    int           erase(const char *s, int from = 0, int to = -1);
0327    int           erase(XrdOucString s, int from = 0, int to = -1);
0328    int           erasefromstart(int sz = 0) { return erase(0,sz); }
0329    int           erasefromend(int sz = 0) { return erase(len-sz,sz); }
0330    void          lower(int pos, int size = 0);
0331    void          upper(int pos, int size = 0);
0332    void          reset(const char c, int j = 0, int k = -1);
0333    void          hardreset();
0334    void          setbuffer(char *buf);
0335 
0336    // Assignement operators
0337    XrdOucString &operator=(const int i);
0338    XrdOucString &operator=(const char c);
0339    XrdOucString &operator=(const char *s);
0340    XrdOucString &operator=(const XrdOucString s);
0341 
0342    // Add operators
0343    friend XrdOucString operator+(const XrdOucString &s1, const int i);
0344    friend XrdOucString operator+(const XrdOucString &s1, const char c);
0345    friend XrdOucString operator+(const XrdOucString &s1, const char *s);
0346    friend XrdOucString operator+(const XrdOucString &s1, const XrdOucString &s);
0347    XrdOucString &operator+=(const int i);
0348    XrdOucString &operator+=(const char c);
0349    XrdOucString &operator+=(const char *s);
0350    XrdOucString &operator+=(const XrdOucString s);   
0351 
0352    // Equality operators
0353    int operator==(const int i);
0354    int operator==(const char c);
0355    int operator==(const char *s);
0356    int operator==(const XrdOucString s);
0357 
0358    // Inequality operators
0359    int operator!=(const int i) { return !(*this == i); }
0360    int operator!=(const char c) { return !(*this == c); }
0361    int operator!=(const char *s) { return !(*this == s); }
0362    int operator!=(const XrdOucString s) { return !(*this == s); }
0363 
0364    // Miscellanea
0365    bool isdigit(int from = 0, int to = -1);
0366    long atoi(int from = 0, int to = -1);
0367 
0368    // Static methods to change / monitor the default blksize
0369    static int getblksize();
0370    static void setblksize(const int bs);
0371 
0372 #if !defined(WINDOWS)
0373    // format a string
0374    static int form(XrdOucString &str, const char *fmt, ...);
0375 #endif
0376 };
0377 
0378 // Operator << is useful to print a string into a stream
0379 std::ostream &operator<< (std::ostream &, const XrdOucString s);
0380 
0381 XrdOucString const operator+(const char *s1, const XrdOucString s2);
0382 XrdOucString const operator+(const char c, const XrdOucString s);
0383 XrdOucString const operator+(const int i, const XrdOucString s);
0384 
0385 #endif
0386