1 /******************************************************************************
3 * Copyright (C) 1997-2004 by Dimitri van Heesch.
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
22 #define ROUND_SIZE(x) ((x)+BLOCK_SIZE-1)&~(BLOCK_SIZE-1)
24 #define DBG_STR(x) do { } while(0)
25 //#define DBG_STR(x) printf x
27 QGString::QGString() // make null string
28 : m_data(0), m_len(0), m_memSize(0)
30 DBG_STR(("%p: QGString::QGString() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
33 QGString::QGString(uint size)
42 m_memSize = ROUND_SIZE(size+1);
43 m_data = (char*)malloc(m_memSize);
44 memset(m_data,' ',size);
48 DBG_STR(("%p: QGString::QGString(uint size=%d) %d:%s\n",
49 this,size,m_len,m_data?m_data:"<none>"));
52 QGString::QGString( const QGString &s )
62 m_data = (char *)malloc(s.m_memSize);
64 m_memSize = s.m_memSize;
65 qstrcpy(m_data,s.m_data);
67 DBG_STR(("%p: QGString::QGString(const QGString &) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
70 QGString::QGString( const char *str )
81 m_memSize = ROUND_SIZE(m_len+1);
82 assert(m_memSize>=m_len+1);
83 m_data = (char *)malloc(m_memSize);
86 DBG_STR(("%p: QGString::QGString(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
93 DBG_STR(("%p: QGString::~QGString() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
96 bool QGString::resize( uint newlen )
101 if (m_data) { free(m_data); m_data=0; }
103 DBG_STR(("%p: 1.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
106 m_memSize = ROUND_SIZE(newlen+1);
107 assert(m_memSize>=newlen+1);
110 m_data = (char *)malloc(m_memSize);
114 m_data = (char *)realloc(m_data,m_memSize);
118 DBG_STR(("%p: 2.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
122 m_len = qstrlen(m_data);
123 DBG_STR(("%p: 3.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>"));
127 bool QGString::enlarge( uint newlen )
131 if (m_data) { free(m_data); m_data=0; }
136 uint newMemSize = ROUND_SIZE(newlen+1);
137 if (newMemSize==m_memSize) return TRUE;
138 m_memSize = newMemSize;
141 m_data = (char *)malloc(m_memSize);
145 m_data = (char *)realloc(m_data,m_memSize);
151 m_data[newlen-1]='\0';
152 if (m_len>newlen) m_len=newlen;
156 void QGString::setLen( uint newlen )
158 m_len = newlen<=m_memSize ? newlen : m_memSize;
161 QGString &QGString::operator=( const QGString &s )
163 if (m_data) free(m_data);
164 if (s.m_memSize==0) // null string
173 m_memSize = s.m_memSize;
174 m_data = (char*)malloc(m_memSize);
175 qstrcpy(m_data,s.m_data);
177 DBG_STR(("%p: QGString::operator=(const QGString &%p) %d:%s\n",
178 this,&s,m_len,m_data?m_data:"<none>"));
182 QGString &QGString::operator=( const char *str )
184 if (m_data) free(m_data);
185 if (str==0) // null string
193 m_len = qstrlen(str);
194 m_memSize = ROUND_SIZE(m_len+1);
195 assert(m_memSize>=m_len+1);
196 m_data = (char*)malloc(m_memSize);
199 DBG_STR(("%p: QGString::operator=(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
203 QGString &QGString::operator+=( const QGString &s )
205 if (s.m_memSize==0) return *this;
206 uint len1 = length();
207 uint len2 = s.length();
208 uint memSize = ROUND_SIZE(len1 + len2 + 1);
209 assert(memSize>=len1+len2+1);
210 char *newData = memSize!=m_memSize ? (char*)realloc( m_data, memSize ) : m_data;
215 memcpy( m_data + len1, s, len2 + 1 );
218 DBG_STR(("%p: QGString::operator+=(const QGString &) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
222 QGString &QGString::operator+=( const char *str )
224 if (!str) return *this;
225 uint len1 = length();
226 uint len2 = qstrlen(str);
227 uint memSize = ROUND_SIZE(len1 + len2 + 1);
228 assert(memSize>=len1+len2+1);
229 char *newData = memSize!=m_memSize ? (char *)realloc( m_data, memSize ) : m_data;
234 memcpy( m_data + len1, str, len2 + 1 );
237 DBG_STR(("%p: QGString::operator+=(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>"));
241 QGString &QGString::operator+=( char c )
244 uint memSize = ROUND_SIZE(len+2);
245 assert(memSize>=len+2);
246 char *newData = memSize!=m_memSize ? (char *)realloc( m_data, memSize ) : m_data;
252 m_data[len+1] = '\0';
255 DBG_STR(("%p: QGString::operator+=(char s) %d:%s\n",this,m_len,m_data?m_data:"<none>"));