00001 /* 00002 * Funambol is a mobile platform developed by Funambol, Inc. 00003 * Copyright (C) 2003 - 2007 Funambol, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Affero General Public License version 3 as published by 00007 * the Free Software Foundation with the addition of the following permission 00008 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED 00009 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 00010 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 00011 * 00012 * This program is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00014 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00015 * details. 00016 * 00017 * You should have received a copy of the GNU Affero General Public License 00018 * along with this program; if not, see http://www.gnu.org/licenses or write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00020 * MA 02110-1301 USA. 00021 * 00022 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 00023 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. 00024 * 00025 * The interactive user interfaces in modified source and object code versions 00026 * of this program must display Appropriate Legal Notices, as required under 00027 * Section 5 of the GNU Affero General Public License version 3. 00028 * 00029 * In accordance with Section 7(b) of the GNU Affero General Public License 00030 * version 3, these Appropriate Legal Notices must retain the display of the 00031 * "Powered by Funambol" logo. If the display of the logo is not reasonably 00032 * feasible for technical reasons, the Appropriate Legal Notices must display 00033 * the words "Powered by Funambol". 00034 */ 00035 00036 #ifndef INCL_BASE_STRING_BUFFER 00037 #define INCL_BASE_STRING_BUFFER 00038 /** @cond DEV */ 00039 00040 #include "base/globalsdef.h" 00041 #include "base/fscapi.h" 00042 #include "base/util/ArrayElement.h" 00043 00044 BEGIN_NAMESPACE 00045 00046 class ArrayList; 00047 00048 /** 00049 * Portable char string implementation. 00050 * 00051 * Characteristic: 00052 * <li> methods and operators similar to std::string 00053 * <li> based only on c-library 00054 * <li> a StringBuffer can be empty or null, and the two states can be tested 00055 * separately (keeping the same semantic of a char buf null or "". 00056 */ 00057 class StringBuffer: public ArrayElement { 00058 public: 00059 // Constant value for an invalid pos (returned by find and rfind) 00060 static const size_t npos; 00061 00062 /** 00063 * Creates a new StringBuffer with the given initial value. 00064 * The default is NULL, thus creating a string wiith a null internal 00065 * pointer. 00066 * Possible calls are: 00067 * <li> Stringbuffer s; // construct a string with NULL value 00068 * <li> Stringbuffer s(""); // construct a string with empty value 00069 * <li> Stringbuffer s("string"); // construct a string containing "string" 00070 * <li> Stringbuffer s(buf, len); // construct a string with the content of buf and lenght len. 00071 * 00072 * In the latter example, buf may or may not be null-terminated. 00073 * 00074 */ 00075 StringBuffer(const char* str = NULL, size_t len = npos); 00076 00077 /** 00078 * Copy constructor. 00079 */ 00080 StringBuffer(const StringBuffer &sb); 00081 00082 /** 00083 * Destructor 00084 */ 00085 ~StringBuffer(); 00086 00087 /** 00088 * Append a char string to the StringBuffer. See also operator+= 00089 */ 00090 StringBuffer& append(const char* ); 00091 00092 StringBuffer& append(unsigned long u, bool sign = true); 00093 00094 /** 00095 * Append the content of another StringBuffer to this one. See also operator+= 00096 */ 00097 StringBuffer& append(StringBuffer& s); 00098 00099 /** 00100 * Append the content of another StringBuffer to this one. See also operator+= 00101 */ 00102 StringBuffer& append(StringBuffer* str); 00103 00104 /** 00105 * Assign a new value to this StringBuffer. See also operator= 00106 */ 00107 StringBuffer& assign(const char* ); 00108 00109 /** @deprecated use assign instead */ 00110 StringBuffer& set(const char* other) { return assign(other);}; 00111 00112 /** 00113 * Convert the Wide char string wc, using the given encoding, and assign 00114 * the converted char string to this StringBuffer. 00115 * If WCHAR size is equal to char (i.e. wide chars are not used in the build, 00116 * no conversion is made and the value of wc is assigned to the StringBuffer. 00117 */ 00118 StringBuffer& convert(const WCHAR* wc, const char *encoding = 0); 00119 00120 /** 00121 * Executes a sprintf(), overwriting the current string buffer 00122 * and enlarging it as necessary. The parameters are that of a 00123 * normal sprintf(). 00124 */ 00125 StringBuffer& sprintf(const char* format, ...) 00126 #ifdef __GNUC__ 00127 /* enables GCC checking of format <-> parameter mismatches */ 00128 __attribute__ ((format (printf, 2, 3))) 00129 #endif 00130 ; 00131 00132 /** 00133 * Executes a vsprintf(), overwriting the current string 00134 * buffer and enlarging it as necessary. The parameters are 00135 * that of a normal vsprintf(). 00136 */ 00137 StringBuffer& vsprintf(const char* format, PLATFORM_VA_LIST ap) 00138 #ifdef __GNUC__ 00139 /* enables GCC checking of format <-> parameter mismatches */ 00140 __attribute__ ((format (printf, 2, 0))) 00141 #endif 00142 ; 00143 00144 00145 /** 00146 * Release the string buffer. 00147 */ 00148 StringBuffer& reset(); 00149 00150 /** 00151 * @deprecated user c_str() instead. 00152 */ 00153 const char* getChars() const { return c_str(); }; 00154 00155 /** 00156 * Return a const pointer to the internal char buffer. 00157 */ 00158 const char* c_str() const { return s; }; 00159 00160 /** 00161 * Find the first occurrence of substring str, starting from pos. 00162 * 00163 * @param - start position 00164 * @return - the position or StringBuffer::npos if not found. 00165 */ 00166 size_t find(const char *str, size_t pos = 0) const; 00167 00168 /** 00169 * Find the first occurrence of substring str, using case insensitive compare. 00170 * 00171 * @return - the position or StringBuffer::npos if not found. 00172 */ 00173 size_t ifind(const char *str, size_t pos = 0) const; 00174 00175 /** 00176 * Find the last occurrence of substring str. 00177 * 00178 * @return - the position or StringBuffer::npos if not found. 00179 */ 00180 size_t rfind(const char *str, size_t pos = 0) const; 00181 00182 /** 00183 * Replace the first occurrence of substring 'from' with string 'to'. 00184 * 00185 * @return - the position of the first token replaced or StringBuffer::npos if 00186 * not found. 00187 */ 00188 size_t replace(const char *from, const char *to, size_t pos = 0); 00189 /** 00190 * Replace all the occurrences of substring 'from' with string 'to'. 00191 * 00192 * @return - the number of tokens replaced 00193 */ 00194 int replaceAll(const char *from, const char *to, size_t pos = 0); 00195 00196 /** 00197 * Splits string on each occurrence of any of the characters in 00198 * delimiters. 00199 * 00200 * @return - the position or StringBuffer::npos if not found. 00201 */ 00202 ArrayList &split (ArrayList &tokens, 00203 const char *delimiters = " ") const; 00204 00205 /** 00206 * Joins all the tokens in the given ArrayList, using separator to 00207 * contatenate them, appending them to the StringBuffer 00208 * 00209 * @return - the StringBuffer 00210 */ 00211 StringBuffer& join(ArrayList &tokens, const char *separator); 00212 00213 /** 00214 * Return the substring between pos and pos+len. 00215 * If pos is greater then the string length, or len is 0, return an 00216 * empty string 00217 * If len is greater then the string length, the last is used. 00218 */ 00219 StringBuffer substr(size_t pos, size_t len = npos) const; 00220 00221 /** 00222 * Return the length of the string, or zero if the internal pointer 00223 * is NULL. 00224 */ 00225 unsigned long length() const; 00226 00227 /** 00228 * Reserve len amount of space for the string. 00229 */ 00230 void reserve(size_t len); 00231 00232 /** 00233 * Make the string upper case 00234 */ 00235 StringBuffer& upperCase() ; 00236 00237 /** 00238 * Make the string lower case 00239 */ 00240 StringBuffer& lowerCase() ; 00241 00242 /** 00243 * Perform case insensitive compare 00244 * @return true if equals 00245 */ 00246 bool icmp(const char *sc) const ; 00247 00248 /** 00249 * True if the string is null or empty 00250 */ 00251 bool empty() const; 00252 00253 /** 00254 * True if the string is null 00255 */ 00256 bool null() const; 00257 00258 /** 00259 * True if the string terminates with ch 00260 */ 00261 bool endsWith(char ch) const; 00262 00263 /** 00264 * True if the string terminates with str 00265 */ 00266 bool endsWith(const char* str) const; 00267 00268 /** 00269 * Clone the string 00270 */ 00271 ArrayElement* clone() ; 00272 00273 /** 00274 * Class operators 00275 */ 00276 StringBuffer& operator= (const char* sc) ; 00277 StringBuffer& operator= (const StringBuffer& s) ; 00278 StringBuffer& operator+= (const char* sc) ; 00279 StringBuffer& operator+= (const StringBuffer& s) ; 00280 bool operator== (const char* sc) const ; 00281 bool operator== (const StringBuffer& sb) const ; 00282 bool operator!= (const char* sc) const ; 00283 bool operator!= (const StringBuffer& s) const ; 00284 00285 operator const char* () const { return s; } ; 00286 00287 private: 00288 char* s; 00289 size_t size; 00290 00291 // Allocator 00292 void getmem(size_t len); 00293 // Deallocator 00294 void freemem(); 00295 }; 00296 00297 StringBuffer operator+(const StringBuffer& x, const char *y); 00298 00299 00300 END_NAMESPACE 00301 00302 /** @endcond */ 00303 #endif 00304