xrootd
XrdOucString.hh
Go to the documentation of this file.
1 #ifndef __OUC_STRING_H__
2 #define __OUC_STRING_H__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c S t r i n g . h h */
6 /* */
7 /* (c) 2005 F. Furano (INFN Padova), G. Ganis (CERN) */
8 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */
9 /******************************************************************************/
10 
11 // $Id$
12 
13 /******************************************************************************/
14 /* */
15 /* Light string manipulation class */
16 /* */
17 /* This class has three private members: a buffer (char *) and two integers, */
18 /* indicating the effective length of the null-terminated string (len), and */
19 /* the buffer capacity (siz), i.e. the allocated size. The capacity is set */
20 /* at construction either at the value needed by the initializing string or */
21 /* to the value requested by the user; by default the capacity is never */
22 /* decreased during manipulations (it is increased if required by the */
23 /* operation). The capacity can be changed at any time by calling resize(). */
24 /* The user can choose a granularity other than 1 to increase the capacity */
25 /* by calling XrdOucString::setblksize(nbs) with nbs > 1: this will make */
26 /* new allocations to happen in blocks multiple of nbs bytes. */
27 /* */
28 /* 1. Constructors */
29 /* */
30 /* XrdOucString(int lmx = 0) */
31 /* - create an empty string; set capacity to lmx+1 if lmx > 0 */
32 /* XrdOucString(const char *s, int lmx = 0) */
33 /* - create a string containing s; capacity is set to strlen(s)+1 or */
34 /* to lmx+1 if lmx > 0; in the latter case truncation may occur. */
35 /* XrdOucString(const char c, int lmx = 0) */
36 /* - create a string char c; capacity is set to 2 or to lmx+1 if lmx > 0.*/
37 /* XrdOucString(const XrdOucString &s) */
38 /* - create string copying from XrdOucString s . */
39 /* XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0) */
40 /* - create string copying a portion of XrdOucString s; portion is */
41 /* defined by j to k inclusive; if k == -1 the portion copied will be */
42 /* from j to end-of-string; if j or k are inconsistent they are taken */
43 /* as 0 or len-1, respectively; capacity is set to k-j bytes or to */
44 /* to lmx+1 if lmx > 0; in the latter case truncation of the portion */
45 /* may occur. */
46 /* */
47 /* 2. Access to information */
48 /* */
49 /* const char *c_str() const */
50 /* - return pointer to stored string */
51 /* int length() const */
52 /* - return length stored string */
53 /* int capacity() const */
54 /* - return capacity of the allocated buffer */
55 /* */
56 /* char &operator[](int j) */
57 /* - array-like operator returning char at position j; abort is invoked */
58 /* if j is not in the correct range */
59 /* */
60 /* int find(const char c, int start = 0, bool forward = 1); */
61 /* - find first occurence of char c starting from position start in */
62 /* forward (forward == 1, default) or backward (forward == 0) */
63 /* direction; returns STR_NPOS if nothing is found */
64 /* int find(const char *s, int start = 0) */
65 /* - find first occurence of string s starting from position start in */
66 /* forward direction; returns STR_NPOS if nothing is found */
67 /* int find(XrdOucString s, int start = 0) */
68 /* - find first occurence of XrdOucString s starting from position start */
69 /* in forward direction; returns STR_NPOS if nothing is found */
70 /* */
71 /* int rfind(const char c, int start = STR_NPOS) */
72 /* - find first occurence of char c starting from position start in */
73 /* backward direction; returns STR_NPOS if nothing is found. */
74 /* int rfind(const char *s, int start = STR_NPOS) */
75 /* - find first occurence of string s starting from position start in */
76 /* backward direction; returns STR_NPOS if nothing is found; */
77 /* if start == STR_NPOS search starts from position len-strlen(s) */
78 /* int rfind(XrdOucString s, int start = STR_NPOS) */
79 /* - find first occurence of XrdOucString s starting from position start */
80 /* in backward direction; returns STR_NPOS if nothing is found; */
81 /* if start == STR_NPOS search starts from position len-s.lenght() */
82 /* */
83 /* bool beginswith(char c) */
84 /* - returns 1 if the stored string starts with char c */
85 /* bool beginswith(const char *s) */
86 /* - returns 1 if the stored string starts with string s */
87 /* bool beginswith(XrdOucString s) */
88 /* - returns 1 if the stored string starts with XrdOucString s */
89 /* */
90 /* bool endswith(char c) */
91 /* - returns 1 if the stored string ends with char c */
92 /* bool endswith(const char *s) */
93 /* - returns 1 if the stored string ends with string s */
94 /* bool endswith(XrdOucString s) */
95 /* - returns 1 if the stored string ends with XrdOucString s */
96 /* */
97 /* int matches(const char *s, char wch = '*') */
98 /* - check if stored string is compatible with s allowing for wild char */
99 /* wch (default: '*'); return the number of matching characters. */
100 /* */
101 /* 3. Modifiers */
102 /* */
103 /* void resize(int lmx = 0) */
104 /* - resize buffer capacity to lmx+1 bytes; if lmx <= 0, free the buffer.*/
105 /* */
106 /* void append(const int i) */
107 /* - append to stored string the string representation of integer i, */
108 /* e.g. if string is initially "x*", after append(5) it will be "x*5". */
109 /* void append(const char c) */
110 /* - append char c to stored string, e.g. if string is initially "pop", */
111 /* after append('_') it will be "pop_". */
112 /* void append(const char *s) */
113 /* - append string s to stored string, e.g. if string is initially "pop",*/
114 /* after append("star") it will be "popstar". */
115 /* void append(const XrdOucString s) */
116 /* - append s.c_str() to stored string, e.g. if string is initially */
117 /* "anti", after append("star") it will be "antistar". */
118 /* */
119 /* void assign(const char *s, int j, int k = -1) */
120 /* - copy to allocated buffer a portion of string s; portion is defined */
121 /* by j to k inclusive; if k == -1 the portion copied will be from j */
122 /* to end-of-string; if j or k are inconsistent they are taken as 0 or */
123 /* len-1, respectively; if necessary, capacity is increased to k-j */
124 /* bytes. */
125 /* void assign(const XrdOucString s, int j, int k = -1) */
126 /* - copy to allocated buffer a portion of s.c_str(); portion is defined */
127 /* by j to k inclusive; if k == -1 the portion copied will be from j */
128 /* to end-of-string; if j or k are inconsistent they are taken as 0 or */
129 /* len-1, respectively; if necessary, capacity is increased to k-j */
130 /* bytes. */
131 /* */
132 /* int keep(int start = 0, int size = 0) */
133 /* - drop chars outside the range of size bytes starting at start */
134 /* */
135 /* void insert(const int i, int start = -1) */
136 /* - insert the string representation of integer i at position start of */
137 /* the stored string, e.g. if string is initially "*x", after */
138 /* insert(5,0) it will be "5*x"; default action is append. */
139 /* void insert(const char c, int start = -1) */
140 /* - insert the char c at position start of the stored string, e.g. */
141 /* if string is initially "pok", after insert('_',0) it will be "_poc";*/
142 /* default action is append. */
143 /* void insert(const char *s, int start = -1, int lmx = 0) */
144 /* - insert string s at position start of the stored string, e.g. */
145 /* if string is initially "forth", after insert("backand",0) it will be*/
146 /* "backandforth"; default action is append. */
147 /* void insert(const XrdOucString s, int start = -1) */
148 /* - insert string s.c_str() at position start of the stored string. */
149 /* */
150 /* int replace(const char *s1, const char *s2, */
151 /* int from = 0, int to = -1); */
152 /* - replace all occurrencies of string s1 with string s2 in the region */
153 /* from position 'from' to position 'to' inclusive; the method is */
154 /* optimized to minimize the memory movements; with s2 == 0 or "" */
155 /* removes all instances of s1 in the specified region. */
156 /* int replace(const XrdOucString s1, const char *s2, */
157 /* int from = 0, int to = -1); */
158 /* int replace(const char *s1, const XrdOucString s2, */
159 /* int from = 0, int to = -1); */
160 /* int replace(const XrdOucString s1, const XrdOucString s2, */
161 /* int from = 0, int to = -1); */
162 /* - interfaces to replace(const char *, const char *, int, int) */
163 /* */
164 /* int erase(int start = 0, int size = 0) */
165 /* - erase size bytes starting at start */
166 /* int erase(const char *s, int from = 0, int to = -1) */
167 /* - erase occurences of string s within position 'from' and position */
168 /* 'to' (inclusive), e.g if stored string is "aabbccefccddgg", then */
169 /* erase("cc",0,9) will result in string "aabbefccddgg". */
170 /* int erase(XrdOucString s, int from = 0, int to = -1) */
171 /* - erase occurences of s.c_str() within position 'from' and position */
172 /* 'to' (inclusive). */
173 /* int erasefromstart(int sz = 0) */
174 /* - erase sz bytes from the start. */
175 /* int erasefromend(int sz = 0) */
176 /* - erase sz bytes from the end. */
177 /* */
178 /* void lower(int pos, int size = 0) */
179 /* - set to lower case size bytes from position start. */
180 /* void upper(int pos, int size = 0) */
181 /* - set to upper case size bytes from position start. */
182 /* */
183 /* void hardreset() */
184 /* - memset to 0 the len meaningful bytes of the buffer. */
185 /* */
186 /* int tokenize(XrdOucString &tok, int from, char del) */
187 /* - search for tokens delimited by 'del' (def ':') in string s; search */
188 /* starts from 'from' and the token is returned in 'tok'. */
189 /* */
190 /* 4. Assignement operators */
191 /* XrdOucString &operator=(const int i) */
192 /* XrdOucString &operator=(const char c) */
193 /* XrdOucString &operator=(const char *s) */
194 /* XrdOucString &operator=(const XrdOucString s) */
195 /* */
196 /* 5. Addition operators */
197 /* XrdOucString &operator+(const int i) */
198 /* XrdOucString &operator+(const char c) */
199 /* XrdOucString &operator+(const char *s) */
200 /* XrdOucString &operator+(const XrdOucString s) */
201 /* XrdOucString &operator+=(const int i) */
202 /* XrdOucString &operator+=(const char c) */
203 /* XrdOucString &operator+=(const char *s) */
204 /* XrdOucString &operator+=(const XrdOucString s) */
205 /* XrdOucString const operator+(const char *s1, const XrdOucString s2) */
206 /* XrdOucString const operator+(const char c, const XrdOucString s) */
207 /* XrdOucString const operator+(const int i, const XrdOucString s) */
208 /* */
209 /* 6. Equality operators */
210 /* int operator==(const int i) */
211 /* int operator==(const char c) */
212 /* int operator==(const char *s) */
213 /* int operator==(const XrdOucString s) */
214 /* */
215 /* 7. Inequality operators */
216 /* int operator!=(const int i) */
217 /* int operator!=(const char c) */
218 /* int operator!=(const char *s) */
219 /* int operator!=(const XrdOucString s) */
220 /* */
221 /* 8. Static methods to change / monitor the blksize */
222 /* static int getblksize(); */
223 /* static void setblksize(const int bs); */
224 /* */
225 /******************************************************************************/
226 #include "XrdSys/XrdSysHeaders.hh"
227 
228 #include <stdio.h>
229 #include <stdlib.h>
230 #include <stdarg.h>
231 
232 using namespace std;
233 
234 #define STR_NPOS -1
235 
237 
238 private:
239  char *str;
240  int len;
241  int siz;
242 
243  // Mininal block size to be used in (re-)allocations
244  // Option switched off by default; use XrdOucString::setblksize()
245  // and XrdOucString::getblksize() to change / monitor
246  static int blksize;
247 
248  // Private methods
249  int adjust(int ls, int &j, int &k, int nmx = 0);
250  char *bufalloc(int nsz);
251  inline void init() { str = 0; len = 0; siz = 0; }
252 
253 public:
254  XrdOucString(int lmx = 0) { init(); if (lmx > 0) str = bufalloc(lmx+1); }
255  XrdOucString(const char *s, int lmx = 0);
256  XrdOucString(const char c, int lmx = 0);
257  XrdOucString(const XrdOucString &s);
258  XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0);
259  virtual ~XrdOucString();
260 
261  // Info access
262  const char *c_str() const { return (const char *)str; }
263  int length() const { return len; }
264  int capacity() const { return siz; }
265  char &operator[](int j);
266  int find(const char c, int start = 0, bool forward = 1);
267  int find(const char *s, int start = 0);
268  int find(XrdOucString s, int start = 0);
269  int rfind(const char c, int start = STR_NPOS)
270  { return find(c, start, 0); }
271  int rfind(const char *s, int start = STR_NPOS);
272  int rfind(XrdOucString s, int start = STR_NPOS);
273  bool beginswith(char c) { return (find(c) == 0); }
274  bool beginswith(const char *s) { return (find(s) == 0); }
275  bool beginswith(XrdOucString s) { return (find(s) == 0); }
276  bool endswith(char c);
277  bool endswith(const char *s);
278  bool endswith(XrdOucString s) { return (endswith(s.c_str())); }
279  int matches(const char *s, char wch = '*');
280 
281  // Tokenizer
282  int tokenize(XrdOucString &tok, int from, char del = ':');
283 
284  // Modifiers
285  void resize(int lmx = 0) { int ns = (lmx > 0) ? lmx + 1 : 0;
286  str = bufalloc(ns); }
287  void append(const int i);
288  void append(const char c);
289  void append(const char *s);
290  void append(const XrdOucString s);
291  void assign(const char *s, int j, int k = -1);
292  void assign(const XrdOucString s, int j, int k = -1);
293 #if !defined(WINDOWS)
294  int form(const char *fmt, ...);
295 #endif
296  int keep(int start = 0, int size = 0);
297  void insert(const int i, int start = -1);
298  void insert(const char c, int start = -1);
299  void insert(const char *s, int start = -1, int lmx = 0);
300  void insert(const XrdOucString s, int start = -1);
301  int replace(const char *s1, const char *s2,
302  int from = 0, int to = -1);
303  int replace(const XrdOucString s1, const XrdOucString s2,
304  int from = 0, int to = -1);
305  int replace(const XrdOucString s1, const char *s2,
306  int from = 0, int to = -1);
307  int replace(const char *s1, const XrdOucString s2,
308  int from = 0, int to = -1);
309  int erase(int start = 0, int size = 0);
310  int erase(const char *s, int from = 0, int to = -1);
311  int erase(XrdOucString s, int from = 0, int to = -1);
312  int erasefromstart(int sz = 0) { return erase(0,sz); }
313  int erasefromend(int sz = 0) { return erase(len-sz,sz); }
314  void lower(int pos, int size = 0);
315  void upper(int pos, int size = 0);
316  void reset(const char c, int j = 0, int k = -1);
317  void hardreset();
318  void setbuffer(char *buf);
319 
320  // Assignement operators
321  XrdOucString &operator=(const int i);
322  XrdOucString &operator=(const char c);
323  XrdOucString &operator=(const char *s);
324  XrdOucString &operator=(const XrdOucString s);
325 
326  // Add operators
327  friend XrdOucString operator+(const XrdOucString &s1, const int i);
328  friend XrdOucString operator+(const XrdOucString &s1, const char c);
329  friend XrdOucString operator+(const XrdOucString &s1, const char *s);
330  friend XrdOucString operator+(const XrdOucString &s1, const XrdOucString &s);
331  XrdOucString &operator+=(const int i);
332  XrdOucString &operator+=(const char c);
333  XrdOucString &operator+=(const char *s);
334  XrdOucString &operator+=(const XrdOucString s);
335 
336  // Equality operators
337  int operator==(const int i);
338  int operator==(const char c);
339  int operator==(const char *s);
340  int operator==(const XrdOucString s);
341 
342  // Inequality operators
343  int operator!=(const int i) { return !(*this == i); }
344  int operator!=(const char c) { return !(*this == c); }
345  int operator!=(const char *s) { return !(*this == s); }
346  int operator!=(const XrdOucString s) { return !(*this == s); }
347 
348  // Miscellanea
349  bool isdigit(int from = 0, int to = -1);
350  long atoi(int from = 0, int to = -1);
351 
352  // Static methods to change / monitor the default blksize
353  static int getblksize();
354  static void setblksize(const int bs);
355 
356 #if !defined(WINDOWS)
357  // format a string
358  static int form(XrdOucString &str, const char *fmt, ...);
359 #endif
360 };
361 
362 // Operator << is useful to print a string into a stream
363 ostream &operator<< (ostream &, const XrdOucString s);
364 
365 XrdOucString const operator+(const char *s1, const XrdOucString s2);
366 XrdOucString const operator+(const char c, const XrdOucString s);
367 XrdOucString const operator+(const int i, const XrdOucString s);
368 
369 #endif
370