|
1 /**************************************************************************** |
|
2 ** |
|
3 ** |
|
4 ** Definition of the extended char array operations, |
|
5 ** and QByteArray and QCString classes |
|
6 ** |
|
7 ** Created : 920609 |
|
8 ** |
|
9 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. |
|
10 ** |
|
11 ** This file is part of the tools module of the Qt GUI Toolkit. |
|
12 ** |
|
13 ** This file may be distributed under the terms of the Q Public License |
|
14 ** as defined by Trolltech AS of Norway and appearing in the file |
|
15 ** LICENSE.QPL included in the packaging of this file. |
|
16 ** |
|
17 ** This file may be distributed and/or modified under the terms of the |
|
18 ** GNU General Public License version 2 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.GPL included in the |
|
20 ** packaging of this file. |
|
21 ** |
|
22 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition |
|
23 ** licenses may use this file in accordance with the Qt Commercial License |
|
24 ** Agreement provided with the Software. |
|
25 ** |
|
26 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
|
27 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
|
28 ** |
|
29 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for |
|
30 ** information about Qt Commercial License Agreements. |
|
31 ** See http://www.trolltech.com/qpl/ for QPL licensing information. |
|
32 ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
|
33 ** |
|
34 ** Contact info@trolltech.com if any conditions of this licensing are |
|
35 ** not clear to you. |
|
36 ** |
|
37 **********************************************************************/ |
|
38 |
|
39 #ifndef QCSTRING_H |
|
40 #define QCSTRING_H |
|
41 |
|
42 #ifndef QT_H |
|
43 #include "qarray.h" |
|
44 #endif // QT_H |
|
45 |
|
46 #include <stdlib.h> |
|
47 #include <string.h> |
|
48 |
|
49 #if defined(_OS_SUN_) && defined(_CC_GNU_) |
|
50 #include <strings.h> |
|
51 #endif |
|
52 |
|
53 |
|
54 class QGString; |
|
55 |
|
56 /***************************************************************************** |
|
57 Fixes and workarounds for some platforms |
|
58 *****************************************************************************/ |
|
59 |
|
60 #if defined(_OS_HPUX_) |
|
61 // HP-UX has badly defined strstr() etc. |
|
62 // ### fix in 3.0: change hack_* to qt_hack_* |
|
63 // by the way HP-UX is probably right, the standard has evolved and |
|
64 // we'll have to adapt to it |
|
65 inline char *hack_strstr( const char *s1, const char *s2 ) |
|
66 { return (char *)strstr(s1, s2); } |
|
67 inline char *hack_strchr( const char *s, int c ) |
|
68 { return (char *)strchr(s, c); } |
|
69 inline char *hack_strrchr( const char *s, int c ) |
|
70 { return (char *)strrchr(s, c); } |
|
71 #define strstr(s1,s2) hack_strstr((s1),(s2)) |
|
72 #define strchr(s,c) hack_strchr((s),(c)) |
|
73 #define strrchr(s,c) hack_strrchr((s),(c)) |
|
74 #endif |
|
75 |
|
76 |
|
77 /***************************************************************************** |
|
78 Safe and portable C string functions; extensions to standard string.h |
|
79 *****************************************************************************/ |
|
80 |
|
81 Q_EXPORT void *qmemmove( void *dst, const void *src, uint len ); |
|
82 |
|
83 #if defined(_OS_SUN_) || defined(_CC_OC_) |
|
84 #define memmove(s1,s2,n) qmemmove((s1),(s2),(n)) |
|
85 #endif |
|
86 |
|
87 Q_EXPORT char *qstrdup( const char * ); |
|
88 |
|
89 Q_EXPORT inline uint cstrlen( const char *str ) |
|
90 { return strlen(str); } |
|
91 |
|
92 Q_EXPORT inline uint qstrlen( const char *str ) |
|
93 { return str ? strlen(str) : 0; } |
|
94 |
|
95 Q_EXPORT inline char *cstrcpy( char *dst, const char *src ) |
|
96 { return strcpy(dst,src); } |
|
97 |
|
98 Q_EXPORT inline char *qstrcpy( char *dst, const char *src ) |
|
99 { return src ? strcpy(dst, src) : 0; } |
|
100 |
|
101 Q_EXPORT char *qstrncpy( char *dst, const char *src, uint len ); |
|
102 |
|
103 Q_EXPORT inline int cstrcmp( const char *str1, const char *str2 ) |
|
104 { return strcmp(str1,str2); } |
|
105 |
|
106 Q_EXPORT inline int qstrcmp( const char *str1, const char *str2 ) |
|
107 { return (str1 && str2) ? strcmp(str1,str2) : (int)((long)str2 - (long)str1); } |
|
108 |
|
109 Q_EXPORT inline int cstrncmp( const char *str1, const char *str2, uint len ) |
|
110 { return strncmp(str1,str2,len); } |
|
111 |
|
112 Q_EXPORT inline int qstrncmp( const char *str1, const char *str2, uint len ) |
|
113 { return (str1 && str2) ? strncmp(str1,str2,len) : |
|
114 (int)((long)str2 - (long)str1); } |
|
115 |
|
116 Q_EXPORT int qstricmp( const char *, const char * ); |
|
117 |
|
118 Q_EXPORT int qstrnicmp( const char *, const char *, uint len ); |
|
119 |
|
120 // ### TODO for 3.0: these and the cstr* functions should be used if |
|
121 // !defined(QT_CLEAN_NAMESPACE) |
|
122 // We want to keep source compatibility for 2.x |
|
123 // ### TODO for 4.0: completely remove these and the cstr* functions |
|
124 |
|
125 #if !defined(QT_GENUINE_STR) |
|
126 |
|
127 #undef strlen |
|
128 #define strlen qstrlen |
|
129 |
|
130 #undef strcpy |
|
131 #define strcpy qstrcpy |
|
132 |
|
133 #undef strcmp |
|
134 #define strcmp qstrcmp |
|
135 |
|
136 #undef strncmp |
|
137 #define strncmp qstrncmp |
|
138 |
|
139 #undef stricmp |
|
140 #define stricmp qstricmp |
|
141 |
|
142 #undef strnicmp |
|
143 #define strnicmp qstrnicmp |
|
144 |
|
145 #endif |
|
146 |
|
147 // qChecksum: Internet checksum |
|
148 |
|
149 Q_EXPORT Q_UINT16 qChecksum( const char *s, uint len ); |
|
150 |
|
151 /***************************************************************************** |
|
152 QByteArray class |
|
153 *****************************************************************************/ |
|
154 |
|
155 #if defined(Q_TEMPLATEDLL) |
|
156 template class Q_EXPORT QArray<char>; |
|
157 #endif |
|
158 typedef QArray<char> QByteArray; |
|
159 |
|
160 |
|
161 /***************************************************************************** |
|
162 QByteArray stream functions |
|
163 *****************************************************************************/ |
|
164 #ifndef QT_NO_DATASTREAM |
|
165 Q_EXPORT QDataStream &operator<<( QDataStream &, const QByteArray & ); |
|
166 Q_EXPORT QDataStream &operator>>( QDataStream &, QByteArray & ); |
|
167 #endif |
|
168 |
|
169 class QRegExp; |
|
170 |
|
171 /** This is an alternative implementation of QCString. It provides basically |
|
172 * the same functions but uses less memory for administration. This class |
|
173 * is just a wrapper around a plain C string requiring only 4 bytes "overhead". |
|
174 * QCString features sharing of data and stores the string length, but |
|
175 * requires 4 + 12 bytes for this (even for the empty string). As doxygen |
|
176 * uses a LOT of string during a run it saves a lot of memory to use a |
|
177 * more memory efficient implementation at the cost of relatively low |
|
178 * runtime overhead. |
|
179 */ |
|
180 class QCString |
|
181 { |
|
182 public: |
|
183 QCString() : m_data(0) {} // make null string |
|
184 QCString( const QCString &s ); |
|
185 QCString( int size ); |
|
186 QCString( const char *str ); |
|
187 QCString( const char *str, uint maxlen ); |
|
188 ~QCString(); |
|
189 |
|
190 QCString &operator=( const QCString &s );// deep copy |
|
191 QCString &operator=( const char *str ); // deep copy |
|
192 |
|
193 bool isNull() const; |
|
194 bool isEmpty() const; |
|
195 uint length() const; |
|
196 uint size() const { return m_data ? length()+1 : 0; } |
|
197 char * data() const { return m_data; } |
|
198 bool resize( uint newlen ); |
|
199 bool truncate( uint pos ); |
|
200 bool fill( char c, int len = -1 ); |
|
201 |
|
202 QCString copy() const; |
|
203 |
|
204 QCString &sprintf( const char *format, ... ); |
|
205 |
|
206 int find( char c, int index=0, bool cs=TRUE ) const; |
|
207 int find( const char *str, int index=0, bool cs=TRUE ) const; |
|
208 int find( const QRegExp &, int index=0 ) const; |
|
209 int findRev( char c, int index=-1, bool cs=TRUE) const; |
|
210 int findRev( const char *str, int index=-1, bool cs=TRUE) const; |
|
211 int findRev( const QRegExp &, int index=-1 ) const; |
|
212 int contains( char c, bool cs=TRUE ) const; |
|
213 int contains( const char *str, bool cs=TRUE ) const; |
|
214 int contains( const QRegExp & ) const; |
|
215 bool stripPrefix(const char *prefix); |
|
216 |
|
217 QCString left( uint len ) const; |
|
218 QCString right( uint len ) const; |
|
219 QCString mid( uint index, uint len=0xffffffff) const; |
|
220 |
|
221 QCString lower() const; |
|
222 QCString upper() const; |
|
223 |
|
224 QCString stripWhiteSpace() const; |
|
225 QCString simplifyWhiteSpace() const; |
|
226 |
|
227 QCString &assign( const char *str ); |
|
228 QCString &insert( uint index, const char * ); |
|
229 QCString &insert( uint index, char ); |
|
230 QCString &append( const char *s ); |
|
231 QCString &prepend( const char *s ); |
|
232 QCString &remove( uint index, uint len ); |
|
233 QCString &replace( uint index, uint len, const char * ); |
|
234 QCString &replace( const QRegExp &, const char * ); |
|
235 |
|
236 short toShort( bool *ok=0 ) const; |
|
237 ushort toUShort( bool *ok=0 ) const; |
|
238 int toInt( bool *ok=0 ) const; |
|
239 uint toUInt( bool *ok=0 ) const; |
|
240 long toLong( bool *ok=0 ) const; |
|
241 ulong toULong( bool *ok=0 ) const; |
|
242 |
|
243 QCString &setNum( short ); |
|
244 QCString &setNum( ushort ); |
|
245 QCString &setNum( int ); |
|
246 QCString &setNum( uint ); |
|
247 QCString &setNum( long ); |
|
248 QCString &setNum( ulong ); |
|
249 QCString &setNum( float, char f='g', int prec=6 ); |
|
250 QCString &setNum( double, char f='g', int prec=6 ); |
|
251 |
|
252 operator const char *() const; |
|
253 QCString &operator+=( const char *str ); |
|
254 QCString &operator+=( char c ); |
|
255 char &at( uint index ) const; |
|
256 char &operator[]( int i ) const { return at(i); } |
|
257 |
|
258 private: |
|
259 static void msg_index( uint ); |
|
260 void duplicate( const QCString &s ); |
|
261 void duplicate( const char *str); |
|
262 QCString &duplicate( const char *str, int); |
|
263 |
|
264 char * m_data; |
|
265 }; |
|
266 |
|
267 inline char &QCString::at( uint index ) const |
|
268 { |
|
269 return m_data[index]; |
|
270 } |
|
271 |
|
272 inline void QCString::duplicate( const QCString &s ) |
|
273 { |
|
274 if (!s.isEmpty()) |
|
275 { |
|
276 uint l = strlen(s.data()); |
|
277 m_data = (char *)malloc(l+1); |
|
278 if (m_data) memcpy(m_data,s.data(),l+1); |
|
279 } |
|
280 else |
|
281 m_data=0; |
|
282 } |
|
283 |
|
284 inline void QCString::duplicate( const char *str) |
|
285 { |
|
286 if (str && str[0]!='\0') |
|
287 { |
|
288 uint l = strlen(str); |
|
289 m_data = (char *)malloc(l+1); |
|
290 if (m_data) memcpy(m_data,str,l+1); |
|
291 } |
|
292 else |
|
293 m_data=0; |
|
294 } |
|
295 |
|
296 inline QCString &QCString::duplicate( const char *str, int) |
|
297 { |
|
298 if (m_data==str) return *this; |
|
299 if (m_data) free(m_data); |
|
300 duplicate(str); |
|
301 return *this; |
|
302 } |
|
303 |
|
304 /***************************************************************************** |
|
305 QCString stream functions |
|
306 *****************************************************************************/ |
|
307 #ifndef QT_NO_DATASTREAM |
|
308 Q_EXPORT QDataStream &operator<<( QDataStream &, const QCString & ); |
|
309 Q_EXPORT QDataStream &operator>>( QDataStream &, QCString & ); |
|
310 #endif |
|
311 |
|
312 /***************************************************************************** |
|
313 QCString inline functions |
|
314 *****************************************************************************/ |
|
315 |
|
316 inline QCString &QCString::operator=( const QCString &s ) |
|
317 { return (QCString&)assign( s ); } |
|
318 |
|
319 inline QCString &QCString::operator=( const char *str ) |
|
320 { return (QCString&)duplicate( str, qstrlen(str)+1 ); } |
|
321 |
|
322 inline bool QCString::isNull() const |
|
323 { return data() == 0; } |
|
324 |
|
325 inline bool QCString::isEmpty() const |
|
326 { return data() == 0 || *data() == '\0'; } |
|
327 |
|
328 inline uint QCString::length() const |
|
329 { return qstrlen( data() ); } |
|
330 |
|
331 inline bool QCString::truncate( uint pos ) |
|
332 { return resize(pos+1); } |
|
333 |
|
334 inline QCString QCString::copy() const |
|
335 { return QCString( data() ); } |
|
336 |
|
337 inline QCString &QCString::prepend( const char *s ) |
|
338 { return insert(0,s); } |
|
339 |
|
340 inline QCString &QCString::append( const char *s ) |
|
341 { return operator+=(s); } |
|
342 |
|
343 inline QCString &QCString::setNum( short n ) |
|
344 { return setNum((long)n); } |
|
345 |
|
346 inline QCString &QCString::setNum( ushort n ) |
|
347 { return setNum((ulong)n); } |
|
348 |
|
349 inline QCString &QCString::setNum( int n ) |
|
350 { return setNum((long)n); } |
|
351 |
|
352 inline QCString &QCString::setNum( uint n ) |
|
353 { return setNum((ulong)n); } |
|
354 |
|
355 inline QCString &QCString::setNum( float n, char f, int prec ) |
|
356 { return setNum((double)n,f,prec); } |
|
357 |
|
358 inline QCString::operator const char *() const |
|
359 { return (const char *)data(); } |
|
360 |
|
361 |
|
362 /***************************************************************************** |
|
363 QCString non-member operators |
|
364 *****************************************************************************/ |
|
365 |
|
366 Q_EXPORT inline bool operator==( const QCString &s1, const QCString &s2 ) |
|
367 { return qstrcmp(s1.data(),s2.data()) == 0; } |
|
368 |
|
369 Q_EXPORT inline bool operator==( const QCString &s1, const char *s2 ) |
|
370 { return qstrcmp(s1.data(),s2) == 0; } |
|
371 |
|
372 Q_EXPORT inline bool operator==( const char *s1, const QCString &s2 ) |
|
373 { return qstrcmp(s1,s2.data()) == 0; } |
|
374 |
|
375 Q_EXPORT inline bool operator!=( const QCString &s1, const QCString &s2 ) |
|
376 { return qstrcmp(s1.data(),s2.data()) != 0; } |
|
377 |
|
378 Q_EXPORT inline bool operator!=( const QCString &s1, const char *s2 ) |
|
379 { return qstrcmp(s1.data(),s2) != 0; } |
|
380 |
|
381 Q_EXPORT inline bool operator!=( const char *s1, const QCString &s2 ) |
|
382 { return qstrcmp(s1,s2.data()) != 0; } |
|
383 |
|
384 Q_EXPORT inline bool operator<( const QCString &s1, const QCString& s2 ) |
|
385 { return qstrcmp(s1.data(),s2.data()) < 0; } |
|
386 |
|
387 Q_EXPORT inline bool operator<( const QCString &s1, const char *s2 ) |
|
388 { return qstrcmp(s1.data(),s2) < 0; } |
|
389 |
|
390 Q_EXPORT inline bool operator<( const char *s1, const QCString &s2 ) |
|
391 { return qstrcmp(s1,s2.data()) < 0; } |
|
392 |
|
393 Q_EXPORT inline bool operator<=( const QCString &s1, const char *s2 ) |
|
394 { return qstrcmp(s1.data(),s2) <= 0; } |
|
395 |
|
396 Q_EXPORT inline bool operator<=( const char *s1, const QCString &s2 ) |
|
397 { return qstrcmp(s1,s2.data()) <= 0; } |
|
398 |
|
399 Q_EXPORT inline bool operator>( const QCString &s1, const char *s2 ) |
|
400 { return qstrcmp(s1.data(),s2) > 0; } |
|
401 |
|
402 Q_EXPORT inline bool operator>( const char *s1, const QCString &s2 ) |
|
403 { return qstrcmp(s1,s2.data()) > 0; } |
|
404 |
|
405 Q_EXPORT inline bool operator>=( const QCString &s1, const char *s2 ) |
|
406 { return qstrcmp(s1.data(),s2) >= 0; } |
|
407 |
|
408 Q_EXPORT inline bool operator>=( const char *s1, const QCString &s2 ) |
|
409 { return qstrcmp(s1,s2.data()) >= 0; } |
|
410 |
|
411 Q_EXPORT inline QCString operator+( const QCString &s1, const QCString &s2 ) |
|
412 { |
|
413 QCString tmp(s1); |
|
414 tmp += s2; |
|
415 return tmp; |
|
416 } |
|
417 |
|
418 |
|
419 inline QCString operator+( const QCString &s1, const QGString &s2 ); |
|
420 inline QCString operator+( const QGString &s1, const QCString &s2 ); |
|
421 |
|
422 |
|
423 Q_EXPORT inline QCString operator+( const QCString &s1, const char *s2 ) |
|
424 { |
|
425 QCString tmp(s1); |
|
426 tmp += s2; |
|
427 return tmp; |
|
428 } |
|
429 |
|
430 Q_EXPORT inline QCString operator+( const char *s1, const QCString &s2 ) |
|
431 { |
|
432 QCString tmp(s1); |
|
433 tmp += s2; |
|
434 return tmp; |
|
435 } |
|
436 |
|
437 Q_EXPORT inline QCString operator+( const QCString &s1, char c2 ) |
|
438 { |
|
439 QCString tmp( s1.data() ); |
|
440 tmp += c2; |
|
441 return tmp; |
|
442 } |
|
443 |
|
444 Q_EXPORT inline QCString operator+( char c1, const QCString &s2 ) |
|
445 { |
|
446 QCString tmp; |
|
447 tmp += c1; |
|
448 tmp += s2; |
|
449 return tmp; |
|
450 } |
|
451 |
|
452 |
|
453 #endif // QCSTRING_H |