00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "stringbuffer.h"
00037
00038 #include <string.h>
00039 #include "memory.h"
00040 #include "strings.h"
00041 #include "exceptions.h"
00042
00043 using namespace zmm;
00044
00045 StringBuffer::StringBuffer()
00046 {
00047 capacity = DEFAULT_STRINGBUFFER_CAPACITY;
00048 data = (char *)MALLOC((capacity + 1) * sizeof(char));
00049 len = 0;
00050 data[len] = 0;
00051 }
00052
00053 StringBuffer::StringBuffer(int capacity)
00054 {
00055 this->capacity = capacity;
00056 data = (char *)MALLOC((capacity + 1) * sizeof(char));
00057 len = 0;
00058 data[len] = 0;
00059 }
00060
00061 StringBuffer::~StringBuffer()
00062 {
00063 FREE(data);
00064 }
00065
00066 StringBuffer &StringBuffer::operator<<(String other)
00067 {
00068 int otherLen = other.length();
00069 if(otherLen > 0)
00070 {
00071 addCapacity(otherLen);
00072 strcpy(data + len, other.base->data);
00073 len += otherLen;
00074 }
00075 return *this;
00076 }
00077
00078 StringBuffer &StringBuffer::operator<<(Ref<StringBuffer> other)
00079 {
00080 concat(other, 0);
00081 return *this;
00082 }
00083
00084
00085 StringBuffer &StringBuffer::operator<<(const char *str)
00086 {
00087 if(! str)
00088 return *this;
00089 int otherLen = (int)strlen(str);
00090 if(otherLen)
00091 {
00092 addCapacity(otherLen);
00093 strcpy(data + len, str);
00094 len += otherLen;
00095 }
00096 return *this;
00097 }
00098
00099 StringBuffer &StringBuffer::operator<<(signed char *str)
00100 {
00101 return operator<<((char *)str);
00102 }
00103
00104 StringBuffer &StringBuffer::operator<<(char chr)
00105 {
00106 addCapacity(1);
00107 data[len] = chr;
00108 len++;
00109 data[len] = 0;
00110 return *this;
00111 }
00112
00113 StringBuffer &StringBuffer::operator<<(signed char chr)
00114 {
00115 return operator<<((char)chr);
00116 }
00117
00118 StringBuffer &StringBuffer::operator<<(int x)
00119 {
00120 addCapacity(MAX_INT_STRING_LENGTH);
00121 char *dest = data + len;
00122 sprintf(dest, "%d", x);
00123 len += (int)strlen(dest);
00124 return *this;
00125 }
00126
00127 StringBuffer &StringBuffer::operator<<(unsigned int x)
00128 {
00129 addCapacity(MAX_INT_STRING_LENGTH);
00130 char *dest = data + len;
00131 sprintf(dest, "%u", x);
00132 len += (int)strlen(dest);
00133 return *this;
00134 }
00135
00136 void StringBuffer::concat(Ref<StringBuffer> other, int offset)
00137 {
00138 int otherLen = other->length();
00139 if(otherLen > 0)
00140 {
00141 if (offset >= otherLen)
00142 throw _Exception(_("illegal offset"));
00143 otherLen -= offset;
00144 addCapacity(otherLen);
00145 strcpy(data + len, other->c_str() + offset);
00146 len += otherLen;
00147 }
00148 }
00149
00150 void StringBuffer::concat(char *str, int length)
00151 {
00152 if(str && length)
00153 {
00154 addCapacity(length);
00155 strncpy(data + len, str, length);
00156 len += length;
00157 data[len] = 0;
00158 }
00159 }
00160
00161 int StringBuffer::length()
00162 {
00163 return len;
00164 }
00165
00166 void StringBuffer::setLength(int newLength)
00167 {
00168 if (newLength > len)
00169 ensureCapacity(newLength);
00170
00171 this->len = newLength;
00172 data[len] = 0;
00173 }
00174
00175 char *StringBuffer::c_str(int offset)
00176 {
00177 if (offset > len || offset < 0)
00178 throw _Exception(_("illegal offset"));
00179 return data + offset;
00180 }
00181
00182 String StringBuffer::toString()
00183 {
00184 return String(data, len);
00185 }
00186
00187 String StringBuffer::toString(int offset)
00188 {
00189 if (offset > len || offset < 0)
00190 throw _Exception(_("illegal offset"));
00191 return String(data + offset, len - offset);
00192 }
00193
00194 void StringBuffer::setCharAt(int index, char c)
00195 {
00196 if (index > len || index < 0)
00197 throw _Exception(_("illegal index"));
00198 data[index] = c;
00199 }
00200
00201 void StringBuffer::clear()
00202 {
00203 len = 0;
00204 data[len] = 0;
00205 }
00206
00207 void StringBuffer::ensureCapacity(int neededCapacity)
00208 {
00209 if(neededCapacity > capacity)
00210 {
00211 int newCapacity = (int)(capacity * STRINGBUFFER_CAPACITY_INCREMENT);
00212 if(neededCapacity > newCapacity)
00213 newCapacity = neededCapacity;
00214 capacity = newCapacity;
00215 data = (char *)REALLOC(data, (capacity + 1) * sizeof(char));
00216 }
00217 }