00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #ifndef __UTIL_BASESTRING_HPP_INCLUDED__ 00018 #define __UTIL_BASESTRING_HPP_INCLUDED__ 00019 00020 #include <ndb_global.h> 00021 #include <Vector.hpp> 00022 00027 class BaseString { 00028 public: 00030 BaseString(); 00031 00033 BaseString(const char* s); 00034 00036 BaseString(const BaseString& str); 00037 00039 ~BaseString(); 00040 00042 const char* c_str() const; 00043 00045 unsigned length() const; 00046 00048 bool empty() const; 00049 00051 void clear(); 00052 00054 BaseString& ndb_toupper(); 00055 00057 BaseString& ndb_tolower(); 00058 00060 BaseString& assign(const char* s); 00061 00063 BaseString& assign(const BaseString& str); 00064 00066 BaseString& assign(const char* s, size_t n); 00067 00069 BaseString& assign(const BaseString& str, size_t n); 00070 00078 BaseString& assign(const Vector<BaseString> &vector, 00079 const BaseString &separator = BaseString(" ")); 00080 00082 BaseString& append(const char* s); 00083 00085 BaseString& append(char c); 00086 00088 BaseString& append(const BaseString& str); 00089 00097 BaseString& append(const Vector<BaseString> &vector, 00098 const BaseString &separator = BaseString(" ")); 00099 00101 BaseString& assfmt(const char* ftm, ...); 00102 00104 BaseString& appfmt(const char* ftm, ...); 00105 00121 int split(Vector<BaseString> &vector, 00122 const BaseString &separator = BaseString(" "), 00123 int maxSize = -1) const; 00124 00131 ssize_t indexOf(char c); 00132 00139 ssize_t lastIndexOf(char c); 00140 00148 BaseString substr(ssize_t start, ssize_t stop = -1); 00149 00153 BaseString& operator=(const BaseString& str); 00154 00156 bool operator<(const BaseString& str) const; 00158 bool operator==(const BaseString& str) const; 00160 bool operator==(const char *str) const; 00162 bool operator!=(const BaseString& str) const; 00164 bool operator!=(const char *str) const; 00165 00169 BaseString& trim(const char * delim = " \t"); 00170 00177 static char** argify(const char *argv0, const char *src); 00178 00182 static char* trim(char * src, const char * delim); 00183 00187 static int snprintf(char *str, size_t size, const char *format, ...); 00188 static int vsnprintf(char *str, size_t size, const char *format, va_list ap); 00189 private: 00190 char* m_chr; 00191 unsigned m_len; 00192 }; 00193 00194 inline const char* 00195 BaseString::c_str() const 00196 { 00197 return m_chr; 00198 } 00199 00200 inline unsigned 00201 BaseString::length() const 00202 { 00203 return m_len; 00204 } 00205 00206 inline bool 00207 BaseString::empty() const 00208 { 00209 return m_len == 0; 00210 } 00211 00212 inline void 00213 BaseString::clear() 00214 { 00215 delete[] m_chr; 00216 m_chr = new char[1]; 00217 m_chr[0] = 0; 00218 m_len = 0; 00219 } 00220 00221 inline BaseString& 00222 BaseString::ndb_toupper() { 00223 for(unsigned i = 0; i < length(); i++) 00224 m_chr[i] = toupper(m_chr[i]); 00225 return *this; 00226 } 00227 00228 inline BaseString& 00229 BaseString::ndb_tolower() { 00230 for(unsigned i = 0; i < length(); i++) 00231 m_chr[i] = tolower(m_chr[i]); 00232 return *this; 00233 } 00234 00235 inline bool 00236 BaseString::operator<(const BaseString& str) const 00237 { 00238 return strcmp(m_chr, str.m_chr) < 0; 00239 } 00240 00241 inline bool 00242 BaseString::operator==(const BaseString& str) const 00243 { 00244 return strcmp(m_chr, str.m_chr) == 0; 00245 } 00246 00247 inline bool 00248 BaseString::operator==(const char *str) const 00249 { 00250 return strcmp(m_chr, str) == 0; 00251 } 00252 00253 inline bool 00254 BaseString::operator!=(const BaseString& str) const 00255 { 00256 return strcmp(m_chr, str.m_chr) != 0; 00257 } 00258 00259 inline bool 00260 BaseString::operator!=(const char *str) const 00261 { 00262 return strcmp(m_chr, str) != 0; 00263 } 00264 00265 inline BaseString& 00266 BaseString::assign(const BaseString& str) 00267 { 00268 return assign(str.m_chr); 00269 } 00270 00271 inline BaseString& 00272 BaseString::assign(const Vector<BaseString> &vector, 00273 const BaseString &separator) { 00274 assign(""); 00275 return append(vector, separator); 00276 } 00277 00278 #endif /* !__UTIL_BASESTRING_HPP_INCLUDED__ */
1.4.7

