1 /****************************************************************************
4 ** Definition of the extended char array operations,
5 ** and QByteArray and QCString classes
9 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
11 ** This file is part of the tools module of the Qt GUI Toolkit.
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.
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.
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.
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.
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.
34 ** Contact info@trolltech.com if any conditions of this licensing are
37 **********************************************************************/
49 #if defined(_OS_SUN_) && defined(_CC_GNU_)
56 /*****************************************************************************
57 Fixes and workarounds for some platforms
58 *****************************************************************************/
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))
77 /*****************************************************************************
78 Safe and portable C string functions; extensions to standard string.h
79 *****************************************************************************/
81 Q_EXPORT void *qmemmove( void *dst, const void *src, uint len );
83 #if defined(_OS_SUN_) || defined(_CC_OC_)
84 #define memmove(s1,s2,n) qmemmove((s1),(s2),(n))
87 Q_EXPORT char *qstrdup( const char * );
89 Q_EXPORT inline uint cstrlen( const char *str )
90 { return (uint)strlen(str); }
92 Q_EXPORT inline uint qstrlen( const char *str )
93 { return str ? (uint)strlen(str) : 0; }
95 Q_EXPORT inline char *cstrcpy( char *dst, const char *src )
96 { return strcpy(dst,src); }
98 Q_EXPORT inline char *qstrcpy( char *dst, const char *src )
99 { return src ? strcpy(dst, src) : 0; }
101 Q_EXPORT char *qstrncpy( char *dst, const char *src, uint len );
103 Q_EXPORT inline int cstrcmp( const char *str1, const char *str2 )
104 { return strcmp(str1,str2); }
106 Q_EXPORT inline int qstrcmp( const char *str1, const char *str2 )
107 { return (str1 && str2) ? strcmp(str1,str2) : (int)((long)str2 - (long)str1); }
109 Q_EXPORT inline int cstrncmp( const char *str1, const char *str2, uint len )
110 { return strncmp(str1,str2,len); }
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); }
116 Q_EXPORT int qstricmp( const char *, const char * );
118 Q_EXPORT int qstrnicmp( const char *, const char *, uint len );
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
125 #if !defined(QT_GENUINE_STR)
128 #define strlen qstrlen
131 #define strcpy qstrcpy
134 #define strcmp qstrcmp
137 #define strncmp qstrncmp
140 #define stricmp qstricmp
143 #define strnicmp qstrnicmp
147 // qChecksum: Internet checksum
149 Q_EXPORT Q_UINT16 qChecksum( const char *s, uint len );
151 /*****************************************************************************
153 *****************************************************************************/
155 #if defined(Q_TEMPLATEDLL)
156 template class Q_EXPORT QArray<char>;
158 typedef QArray<char> QByteArray;
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 & );
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
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 );
190 QCString &operator=( const QCString &s );// deep copy
191 QCString &operator=( const char *str ); // deep copy
194 bool isEmpty() 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 );
202 QCString copy() const;
204 QCString &sprintf( const char *format, ... );
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 QCString &str, int index=0, bool cs=TRUE ) const;
209 int find( const QRegExp &, int index=0 ) const;
210 int findRev( char c, int index=-1, bool cs=TRUE) const;
211 int findRev( const char *str, int index=-1, bool cs=TRUE) const;
212 int findRev( const QRegExp &, int index=-1 ) const;
213 int contains( char c, bool cs=TRUE ) const;
214 int contains( const char *str, bool cs=TRUE ) const;
215 int contains( const QRegExp & ) const;
216 bool stripPrefix(const char *prefix);
218 QCString left( uint len ) const;
219 QCString right( uint len ) const;
220 QCString mid( uint index, uint len=0xffffffff) const;
222 QCString lower() const;
223 QCString upper() const;
225 QCString stripWhiteSpace() const;
226 QCString simplifyWhiteSpace() const;
228 QCString &assign( const char *str );
229 QCString &insert( uint index, const char * );
230 QCString &insert( uint index, char );
231 QCString &append( const char *s );
232 QCString &prepend( const char *s );
233 QCString &remove( uint index, uint len );
234 QCString &replace( uint index, uint len, const char * );
235 QCString &replace( const QRegExp &, const char * );
237 short toShort( bool *ok=0 ) const;
238 ushort toUShort( bool *ok=0 ) const;
239 int toInt( bool *ok=0 ) const;
240 uint toUInt( bool *ok=0 ) const;
241 long toLong( bool *ok=0 ) const;
242 ulong toULong( bool *ok=0 ) const;
244 QCString &setNum( short );
245 QCString &setNum( ushort );
246 QCString &setNum( int );
247 QCString &setNum( uint );
248 QCString &setNum( long );
249 QCString &setNum( ulong );
250 QCString &setNum( float, char f='g', int prec=6 );
251 QCString &setNum( double, char f='g', int prec=6 );
253 operator const char *() const;
254 QCString &operator+=( const char *str );
255 QCString &operator+=( char c );
256 char &at( uint index ) const;
257 char &operator[]( int i ) const { return at(i); }
260 static void msg_index( uint );
261 void duplicate( const QCString &s );
262 void duplicate( const char *str);
263 QCString &duplicate( const char *str, int);
268 inline char &QCString::at( uint index ) const
270 return m_data[index];
273 inline void QCString::duplicate( const QCString &s )
277 uint l = strlen(s.data());
278 m_data = (char *)malloc(l+1);
279 if (m_data) memcpy(m_data,s.data(),l+1);
285 inline void QCString::duplicate( const char *str)
287 if (str && str[0]!='\0')
289 uint l = strlen(str);
290 m_data = (char *)malloc(l+1);
291 if (m_data) memcpy(m_data,str,l+1);
297 inline QCString &QCString::duplicate( const char *str, int)
299 if (m_data==str) return *this;
300 if (m_data) free(m_data);
305 /*****************************************************************************
306 QCString stream functions
307 *****************************************************************************/
308 #ifndef QT_NO_DATASTREAM
309 Q_EXPORT QDataStream &operator<<( QDataStream &, const QCString & );
310 Q_EXPORT QDataStream &operator>>( QDataStream &, QCString & );
313 /*****************************************************************************
314 QCString inline functions
315 *****************************************************************************/
317 inline QCString &QCString::operator=( const QCString &s )
318 { return (QCString&)assign( s ); }
320 inline QCString &QCString::operator=( const char *str )
321 { return (QCString&)duplicate( str, qstrlen(str)+1 ); }
323 inline bool QCString::isNull() const
324 { return data() == 0; }
326 inline bool QCString::isEmpty() const
327 { return data() == 0 || *data() == '\0'; }
329 inline uint QCString::length() const
330 { return qstrlen( data() ); }
332 inline bool QCString::truncate( uint pos )
333 { return resize(pos+1); }
335 inline QCString QCString::copy() const
336 { return QCString( data() ); }
338 inline QCString &QCString::prepend( const char *s )
339 { return insert(0,s); }
341 inline QCString &QCString::append( const char *s )
342 { return operator+=(s); }
344 inline QCString &QCString::setNum( short n )
345 { return setNum((long)n); }
347 inline QCString &QCString::setNum( ushort n )
348 { return setNum((ulong)n); }
350 inline QCString &QCString::setNum( int n )
351 { return setNum((long)n); }
353 inline QCString &QCString::setNum( uint n )
354 { return setNum((ulong)n); }
356 inline QCString &QCString::setNum( float n, char f, int prec )
357 { return setNum((double)n,f,prec); }
359 inline QCString::operator const char *() const
360 { return (const char *)data(); }
363 /*****************************************************************************
364 QCString non-member operators
365 *****************************************************************************/
367 Q_EXPORT inline bool operator==( const QCString &s1, const QCString &s2 )
368 { return qstrcmp(s1.data(),s2.data()) == 0; }
370 Q_EXPORT inline bool operator==( const QCString &s1, const char *s2 )
371 { return qstrcmp(s1.data(),s2) == 0; }
373 Q_EXPORT inline bool operator==( const char *s1, const QCString &s2 )
374 { return qstrcmp(s1,s2.data()) == 0; }
376 Q_EXPORT inline bool operator!=( const QCString &s1, const QCString &s2 )
377 { return qstrcmp(s1.data(),s2.data()) != 0; }
379 Q_EXPORT inline bool operator!=( const QCString &s1, const char *s2 )
380 { return qstrcmp(s1.data(),s2) != 0; }
382 Q_EXPORT inline bool operator!=( const char *s1, const QCString &s2 )
383 { return qstrcmp(s1,s2.data()) != 0; }
385 Q_EXPORT inline bool operator<( const QCString &s1, const QCString& s2 )
386 { return qstrcmp(s1.data(),s2.data()) < 0; }
388 Q_EXPORT inline bool operator<( const QCString &s1, const char *s2 )
389 { return qstrcmp(s1.data(),s2) < 0; }
391 Q_EXPORT inline bool operator<( const char *s1, const QCString &s2 )
392 { return qstrcmp(s1,s2.data()) < 0; }
394 Q_EXPORT inline bool operator<=( const QCString &s1, const char *s2 )
395 { return qstrcmp(s1.data(),s2) <= 0; }
397 Q_EXPORT inline bool operator<=( const char *s1, const QCString &s2 )
398 { return qstrcmp(s1,s2.data()) <= 0; }
400 Q_EXPORT inline bool operator>( const QCString &s1, const char *s2 )
401 { return qstrcmp(s1.data(),s2) > 0; }
403 Q_EXPORT inline bool operator>( const char *s1, const QCString &s2 )
404 { return qstrcmp(s1,s2.data()) > 0; }
406 Q_EXPORT inline bool operator>=( const QCString &s1, const char *s2 )
407 { return qstrcmp(s1.data(),s2) >= 0; }
409 Q_EXPORT inline bool operator>=( const char *s1, const QCString &s2 )
410 { return qstrcmp(s1,s2.data()) >= 0; }
412 Q_EXPORT inline QCString operator+( const QCString &s1, const QCString &s2 )
420 inline QCString operator+( const QCString &s1, const QGString &s2 );
421 inline QCString operator+( const QGString &s1, const QCString &s2 );
424 Q_EXPORT inline QCString operator+( const QCString &s1, const char *s2 )
431 Q_EXPORT inline QCString operator+( const char *s1, const QCString &s2 )
438 Q_EXPORT inline QCString operator+( const QCString &s1, char c2 )
440 QCString tmp( s1.data() );
445 Q_EXPORT inline QCString operator+( char c1, const QCString &s2 )
453 inline const char *qPrint(const char *s)
455 if (s) return s; else return "";
458 inline const char *qPrint(const QCString &s)
460 if (!s.isEmpty()) return s.data(); else return "";