~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/include/util/BaseString.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef __UTIL_BASESTRING_HPP_INCLUDED__
 
17
#define __UTIL_BASESTRING_HPP_INCLUDED__
 
18
 
 
19
#include <ndb_global.h>
 
20
#include <Vector.hpp>
 
21
 
 
22
/**
 
23
 * @class BaseString
 
24
 * @brief Null terminated strings
 
25
 */
 
26
class BaseString {
 
27
public:
 
28
  /** @brief Constructs an empty string */
 
29
  BaseString();
 
30
 
 
31
  /** @brief Constructs a copy of a char * */
 
32
  BaseString(const char* s);
 
33
 
 
34
  /** @brief Constructs a copy of another BaseString */
 
35
  BaseString(const BaseString& str);
 
36
 
 
37
  /** @brief Destructor */
 
38
  ~BaseString();
 
39
 
 
40
  /** @brief Returns a C-style string */
 
41
  const char* c_str() const;
 
42
 
 
43
  /** @brief Returns the length of the string */
 
44
  unsigned length() const;
 
45
 
 
46
  /** @brief Checks if the string is empty */
 
47
  bool empty() const;
 
48
 
 
49
  /** @brief Clear a string */
 
50
  void clear();
 
51
 
 
52
  /** @brief Convert to uppercase */
 
53
  BaseString& ndb_toupper();
 
54
 
 
55
  /** @brief Convert to lowercase */
 
56
  BaseString& ndb_tolower();
 
57
 
 
58
  /** @brief Assigns from a char * */
 
59
  BaseString& assign(const char* s);
 
60
 
 
61
  /** @brief Assigns from another BaseString */
 
62
  BaseString& assign(const BaseString& str);
 
63
 
 
64
  /** @brief Assigns from char *s, with maximum length n */
 
65
  BaseString& assign(const char* s, size_t n);
 
66
 
 
67
  /** @brief Assigns from another BaseString, with maximum length n */
 
68
  BaseString& assign(const BaseString& str, size_t n);
 
69
 
 
70
  /** 
 
71
   * Assings from a Vector of BaseStrings, each Vector entry
 
72
   * separated by separator.
 
73
   *
 
74
   * @param vector Vector of BaseStrings to append
 
75
   * @param separator Separation between appended strings
 
76
   */
 
77
  BaseString& assign(const Vector<BaseString> &vector,
 
78
                     const BaseString &separator = BaseString(" "));
 
79
 
 
80
  /** @brief Appends a char * to the end */
 
81
  BaseString& append(const char* s);
 
82
 
 
83
  /** @brief Appends a char to the end */
 
84
  BaseString& append(char c);
 
85
 
 
86
  /** @brief Appends another BaseString to the end */
 
87
  BaseString& append(const BaseString& str);
 
88
 
 
89
  /** 
 
90
   * Appends a Vector of BaseStrings to the end, each Vector entry
 
91
   * separated by separator.
 
92
   *
 
93
   * @param vector Vector of BaseStrings to append
 
94
   * @param separator Separation between appended strings
 
95
   */
 
96
  BaseString& append(const Vector<BaseString> &vector,
 
97
                     const BaseString &separator = BaseString(" "));
 
98
 
 
99
  /** @brief Assigns from a format string a la printf() */
 
100
  BaseString& assfmt(const char* ftm, ...);
 
101
 
 
102
  /** @brief Appends a format string a la printf() to the end */
 
103
  BaseString& appfmt(const char* ftm, ...);
 
104
 
 
105
  /**
 
106
   * Split a string into a vector of strings. Separate the string where
 
107
   * any character included in separator exists.
 
108
   * Maximally maxSize entries are added to the vector, if more separators
 
109
   * exist in the string, the remainder of the string will be appended
 
110
   * to the last entry in the vector.
 
111
   * The vector will not be cleared, so any existing strings in the
 
112
   * vector will remain.
 
113
   *
 
114
   * @param separator characters separating the entries
 
115
   * @param vector where the separated strings will be stored
 
116
   * @param maximum number of strings extracted
 
117
   *
 
118
   * @returns the number of string added to the vector
 
119
   */
 
120
  int split(Vector<BaseString> &vector, 
 
121
            const BaseString &separator = BaseString(" "),
 
122
            int maxSize = -1) const;
 
123
 
 
124
  /**
 
125
   * Returns the index of the first occurance of the character c.
 
126
   *
 
127
   * @params c character to look for
 
128
   * @returns index of character, of -1 if no character found
 
129
   */
 
130
  ssize_t indexOf(char c);
 
131
 
 
132
  /**
 
133
   * Returns the index of the last occurance of the character c.
 
134
   *
 
135
   * @params c character to look for
 
136
   * @returns index of character, of -1 if no character found
 
137
   */
 
138
  ssize_t lastIndexOf(char c);
 
139
  
 
140
  /**
 
141
   * Returns a subset of a string
 
142
   *
 
143
   * @param start index of first character
 
144
   * @param stop index of last character
 
145
   * @return a new string
 
146
   */
 
147
  BaseString substr(ssize_t start, ssize_t stop = -1);
 
148
 
 
149
  /**
 
150
   *  @brief Assignment operator
 
151
   */
 
152
  BaseString& operator=(const BaseString& str);
 
153
 
 
154
  /** @brief Compare two strings */
 
155
  bool operator<(const BaseString& str) const;
 
156
  /** @brief Are two strings equal? */
 
157
  bool operator==(const BaseString& str) const;
 
158
  /** @brief Are two strings equal? */
 
159
  bool operator==(const char *str) const;
 
160
  /** @brief Are two strings not equal? */
 
161
  bool operator!=(const BaseString& str) const;
 
162
  /** @brief Are two strings not equal? */
 
163
  bool operator!=(const char *str) const;
 
164
 
 
165
  /**
 
166
   * Trim string from <i>delim</i>
 
167
   */
 
168
  BaseString& trim(const char * delim = " \t");
 
169
  
 
170
  /**
 
171
   * Return c-array with strings suitable for execve
 
172
   * When whitespace is detected, the characters '"' and '\' are honored,
 
173
   * to make it possible to give arguments containing whitespace.
 
174
   * The semantics of '"' and '\' match that of most Unix shells.
 
175
   */
 
176
  static char** argify(const char *argv0, const char *src);
 
177
 
 
178
  /**
 
179
   * Trim string from <i>delim</i>
 
180
   */
 
181
  static char* trim(char * src, const char * delim);
 
182
 
 
183
  /**
 
184
   * snprintf on some platforms need special treatment
 
185
   */
 
186
  static int snprintf(char *str, size_t size, const char *format, ...);
 
187
  static int vsnprintf(char *str, size_t size, const char *format, va_list ap);
 
188
private:
 
189
  char* m_chr;
 
190
  unsigned m_len;
 
191
  friend bool operator!(const BaseString& str);
 
192
};
 
193
 
 
194
inline const char*
 
195
BaseString::c_str() const
 
196
{
 
197
  return m_chr;
 
198
}
 
199
 
 
200
inline unsigned
 
201
BaseString::length() const
 
202
{
 
203
  return m_len;
 
204
}
 
205
 
 
206
inline bool
 
207
BaseString::empty() const
 
208
{
 
209
  return m_len == 0;
 
210
}
 
211
 
 
212
inline void
 
213
BaseString::clear()
 
214
{
 
215
  delete[] m_chr;
 
216
  m_chr = new char[1];
 
217
  m_chr[0] = 0;
 
218
  m_len = 0;
 
219
}
 
220
 
 
221
inline BaseString&
 
222
BaseString::ndb_toupper() {
 
223
  for(unsigned i = 0; i < length(); i++)
 
224
    m_chr[i] = toupper(m_chr[i]);
 
225
  return *this;
 
226
}
 
227
 
 
228
inline BaseString&
 
229
BaseString::ndb_tolower() {
 
230
  for(unsigned i = 0; i < length(); i++)
 
231
    m_chr[i] = tolower(m_chr[i]);
 
232
  return *this;
 
233
}
 
234
 
 
235
inline bool
 
236
BaseString::operator<(const BaseString& str) const
 
237
{
 
238
    return strcmp(m_chr, str.m_chr) < 0;
 
239
}
 
240
 
 
241
inline bool
 
242
BaseString::operator==(const BaseString& str) const
 
243
{
 
244
    return strcmp(m_chr, str.m_chr)  ==  0;
 
245
}
 
246
 
 
247
inline bool
 
248
BaseString::operator==(const char *str) const
 
249
{
 
250
    return strcmp(m_chr, str) == 0;
 
251
}
 
252
 
 
253
inline bool
 
254
BaseString::operator!=(const BaseString& str) const
 
255
{
 
256
    return strcmp(m_chr, str.m_chr)  !=  0;
 
257
}
 
258
 
 
259
inline bool
 
260
BaseString::operator!=(const char *str) const
 
261
{
 
262
    return strcmp(m_chr, str) != 0;
 
263
}
 
264
 
 
265
inline bool
 
266
operator!(const BaseString& str)
 
267
{
 
268
    return str.m_chr == NULL;
 
269
}
 
270
 
 
271
inline BaseString&
 
272
BaseString::assign(const BaseString& str)
 
273
{
 
274
    return assign(str.m_chr);
 
275
}
 
276
 
 
277
inline BaseString&
 
278
BaseString::assign(const Vector<BaseString> &vector,
 
279
                   const BaseString &separator) {
 
280
  assign("");
 
281
  return append(vector, separator);
 
282
}
 
283
 
 
284
#endif /* !__UTIL_BASESTRING_HPP_INCLUDED__ */