|
1 /****************************************************************************** |
|
2 * |
|
3 * Copyright (C) 1997-2004 by Dimitri van Heesch. |
|
4 * |
|
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. |
|
10 * |
|
11 * Documents produced by Doxygen are derivative works derived from the |
|
12 * input used in their production; they are not affected by this license. |
|
13 * |
|
14 */ |
|
15 |
|
16 #include <stdio.h> |
|
17 #include "qgstring.h" |
|
18 |
|
19 #include <assert.h> |
|
20 |
|
21 #define BLOCK_SIZE 64 |
|
22 #define ROUND_SIZE(x) ((x)+BLOCK_SIZE-1)&~(BLOCK_SIZE-1) |
|
23 |
|
24 #define DBG_STR(x) do { } while(0) |
|
25 |
|
26 QGString::QGString() // make null string |
|
27 : m_data(0), m_len(0), m_memSize(0) |
|
28 { |
|
29 DBG_STR(("%p: QGString::QGString() %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
30 } |
|
31 |
|
32 QGString::QGString(uint size) |
|
33 { |
|
34 if (size==0) |
|
35 { |
|
36 m_data=0; |
|
37 m_len=0; |
|
38 } |
|
39 else |
|
40 { |
|
41 m_memSize = ROUND_SIZE(size+1); |
|
42 m_data = (char*)malloc(m_memSize); |
|
43 memset(m_data,' ',size); |
|
44 m_data[size]='\0'; |
|
45 m_len=size; |
|
46 } |
|
47 DBG_STR(("%p: QGString::QGString(uint size=%d) %d:%s\n", |
|
48 this,size,m_len,m_data?m_data:"<none>")); |
|
49 } |
|
50 |
|
51 QGString::QGString( const QGString &s ) |
|
52 { |
|
53 if (s.m_memSize==0) |
|
54 { |
|
55 m_data = 0; |
|
56 m_len = 0; |
|
57 m_memSize = 0; |
|
58 } |
|
59 else |
|
60 { |
|
61 m_data = (char *)malloc(s.m_memSize); |
|
62 m_len = s.m_len; |
|
63 m_memSize = s.m_memSize; |
|
64 qstrcpy(m_data,s.m_data); |
|
65 } |
|
66 DBG_STR(("%p: QGString::QGString(const QGString &) %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
67 } |
|
68 |
|
69 QGString::QGString( const char *str ) |
|
70 { |
|
71 if (str==0) |
|
72 { |
|
73 m_data=0; |
|
74 m_len=0; |
|
75 m_memSize=0; |
|
76 } |
|
77 else |
|
78 { |
|
79 m_len = qstrlen(str); |
|
80 m_memSize = ROUND_SIZE(m_len+1); |
|
81 assert(m_memSize>=m_len+1); |
|
82 m_data = (char *)malloc(m_memSize); |
|
83 qstrcpy(m_data,str); |
|
84 } |
|
85 DBG_STR(("%p: QGString::QGString(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
86 } |
|
87 |
|
88 QGString::~QGString() |
|
89 { |
|
90 free(m_data); |
|
91 m_data=0; |
|
92 DBG_STR(("%p: QGString::~QGString() %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
93 } |
|
94 |
|
95 bool QGString::resize( uint newlen ) |
|
96 { |
|
97 m_len = 0; |
|
98 if (newlen==0) |
|
99 { |
|
100 if (m_data) { free(m_data); m_data=0; } |
|
101 m_memSize=0; |
|
102 DBG_STR(("%p: 1.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
103 return TRUE; |
|
104 } |
|
105 m_memSize = ROUND_SIZE(newlen+1); |
|
106 assert(m_memSize>=newlen+1); |
|
107 if (m_data==0) |
|
108 { |
|
109 m_data = (char *)malloc(m_memSize); |
|
110 } |
|
111 else |
|
112 { |
|
113 m_data = (char *)realloc(m_data,m_memSize); |
|
114 } |
|
115 if (m_data==0) |
|
116 { |
|
117 DBG_STR(("%p: 2.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
118 return FALSE; |
|
119 } |
|
120 m_data[newlen-1]='\0'; |
|
121 m_len = qstrlen(m_data); |
|
122 DBG_STR(("%p: 3.QGString::resize() %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
123 return TRUE; |
|
124 } |
|
125 |
|
126 QGString &QGString::operator=( const QGString &s ) |
|
127 { |
|
128 if (m_data) free(m_data); |
|
129 if (s.m_memSize==0) // null string |
|
130 { |
|
131 m_data = 0; |
|
132 m_len = 0; |
|
133 m_memSize = 0; |
|
134 } |
|
135 else |
|
136 { |
|
137 m_len = s.m_len; |
|
138 m_memSize = s.m_memSize; |
|
139 m_data = (char*)malloc(m_memSize); |
|
140 qstrcpy(m_data,s.m_data); |
|
141 } |
|
142 DBG_STR(("%p: QGString::operator=(const QGString &%p) %d:%s\n", |
|
143 this,&s,m_len,m_data?m_data:"<none>")); |
|
144 return *this; |
|
145 } |
|
146 |
|
147 QGString &QGString::operator=( const char *str ) |
|
148 { |
|
149 if (m_data) free(m_data); |
|
150 if (str==0) // null string |
|
151 { |
|
152 m_data = 0; |
|
153 m_len = 0; |
|
154 m_memSize = 0; |
|
155 } |
|
156 else |
|
157 { |
|
158 m_len = qstrlen(str); |
|
159 m_memSize = ROUND_SIZE(m_len+1); |
|
160 assert(m_memSize>=m_len+1); |
|
161 m_data = (char*)malloc(m_memSize); |
|
162 qstrcpy(m_data,str); |
|
163 } |
|
164 DBG_STR(("%p: QGString::operator=(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
165 return *this; |
|
166 } |
|
167 |
|
168 QGString &QGString::operator+=( const QGString &s ) |
|
169 { |
|
170 if (s.m_memSize==0) return *this; |
|
171 uint len1 = length(); |
|
172 uint len2 = s.length(); |
|
173 uint memSize = ROUND_SIZE(len1 + len2 + 1); |
|
174 assert(memSize>=len1+len2+1); |
|
175 char *newData = memSize!=m_memSize ? (char*)realloc( m_data, memSize ) : m_data; |
|
176 m_memSize = memSize; |
|
177 if (m_data) |
|
178 { |
|
179 m_data = newData; |
|
180 memcpy( m_data + len1, s, len2 + 1 ); |
|
181 } |
|
182 m_len = len1+len2; |
|
183 DBG_STR(("%p: QGString::operator+=(const QGString &) %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
184 return *this; |
|
185 } |
|
186 |
|
187 QGString &QGString::operator+=( const char *str ) |
|
188 { |
|
189 if (!str) return *this; |
|
190 uint len1 = length(); |
|
191 uint len2 = qstrlen(str); |
|
192 uint memSize = ROUND_SIZE(len1 + len2 + 1); |
|
193 assert(memSize>=len1+len2+1); |
|
194 char *newData = memSize!=m_memSize ? (char *)realloc( m_data, memSize ) : m_data; |
|
195 m_memSize = memSize; |
|
196 if (newData) |
|
197 { |
|
198 m_data = newData; |
|
199 memcpy( m_data + len1, str, len2 + 1 ); |
|
200 } |
|
201 m_len+=len2; |
|
202 DBG_STR(("%p: QGString::operator+=(const char *) %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
203 return *this; |
|
204 } |
|
205 |
|
206 QGString &QGString::operator+=( char c ) |
|
207 { |
|
208 uint len = m_len; |
|
209 uint memSize = ROUND_SIZE(len+2); |
|
210 assert(memSize>=len+2); |
|
211 char *newData = memSize!=m_memSize ? (char *)realloc( m_data, memSize ) : m_data; |
|
212 m_memSize = memSize; |
|
213 if (newData) |
|
214 { |
|
215 m_data = newData; |
|
216 m_data[len] = c; |
|
217 m_data[len+1] = '\0'; |
|
218 } |
|
219 m_len++; |
|
220 DBG_STR(("%p: QGString::operator+=(char s) %d:%s\n",this,m_len,m_data?m_data:"<none>")); |
|
221 return *this; |
|
222 } |
|
223 |