| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qcstring.h 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Definition of the extended char array operations,
|
|---|
| 5 | ** and QByteArray and QCString classes
|
|---|
| 6 | **
|
|---|
| 7 | ** Created : 920609
|
|---|
| 8 | **
|
|---|
| 9 | ** Copyright (C) 1992-2002 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 "qmemarray.h"
|
|---|
| 44 | #endif // QT_H
|
|---|
| 45 |
|
|---|
| 46 | #include <string.h>
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /*****************************************************************************
|
|---|
| 50 | Safe and portable C string functions; extensions to standard string.h
|
|---|
| 51 | *****************************************************************************/
|
|---|
| 52 |
|
|---|
| 53 | Q_EXPORT void *qmemmove( void *dst, const void *src, uint len );
|
|---|
| 54 |
|
|---|
| 55 | Q_EXPORT char *qstrdup( const char * );
|
|---|
| 56 |
|
|---|
| 57 | Q_EXPORT inline uint qstrlen( const char *str )
|
|---|
| 58 | { return str ? (uint)strlen(str) : 0u; }
|
|---|
| 59 |
|
|---|
| 60 | Q_EXPORT inline char *qstrcpy( char *dst, const char *src )
|
|---|
| 61 | { return src ? strcpy(dst, src) : 0; }
|
|---|
| 62 |
|
|---|
| 63 | Q_EXPORT char *qstrncpy( char *dst, const char *src, uint len );
|
|---|
| 64 |
|
|---|
| 65 | Q_EXPORT inline int qstrcmp( const char *str1, const char *str2 )
|
|---|
| 66 | {
|
|---|
| 67 | return ( str1 && str2 ) ? strcmp( str1, str2 )
|
|---|
| 68 | : ( str1 ? 1 : ( str2 ? -1 : 0 ) );
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | Q_EXPORT inline int qstrncmp( const char *str1, const char *str2, uint len )
|
|---|
| 72 | {
|
|---|
| 73 | return ( str1 && str2 ) ? strncmp( str1, str2, len )
|
|---|
| 74 | : ( str1 ? 1 : ( str2 ? -1 : 0 ) );
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | Q_EXPORT int qstricmp( const char *, const char * );
|
|---|
| 78 |
|
|---|
| 79 | Q_EXPORT int qstrnicmp( const char *, const char *, uint len );
|
|---|
| 80 |
|
|---|
| 81 | #ifndef QT_CLEAN_NAMESPACE
|
|---|
| 82 | Q_EXPORT inline uint cstrlen( const char *str )
|
|---|
| 83 | { return (uint)strlen(str); }
|
|---|
| 84 |
|
|---|
| 85 | Q_EXPORT inline char *cstrcpy( char *dst, const char *src )
|
|---|
| 86 | { return strcpy(dst,src); }
|
|---|
| 87 |
|
|---|
| 88 | Q_EXPORT inline int cstrcmp( const char *str1, const char *str2 )
|
|---|
| 89 | { return strcmp(str1,str2); }
|
|---|
| 90 |
|
|---|
| 91 | Q_EXPORT inline int cstrncmp( const char *str1, const char *str2, uint len )
|
|---|
| 92 | { return strncmp(str1,str2,len); }
|
|---|
| 93 | #endif
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 | // qChecksum: Internet checksum
|
|---|
| 97 |
|
|---|
| 98 | Q_EXPORT Q_UINT16 qChecksum( const char *s, uint len );
|
|---|
| 99 |
|
|---|
| 100 | /*****************************************************************************
|
|---|
| 101 | QByteArray class
|
|---|
| 102 | *****************************************************************************/
|
|---|
| 103 |
|
|---|
| 104 | #if defined(Q_QDOC)
|
|---|
| 105 | /*
|
|---|
| 106 | We want qdoc to document QByteArray as a real class that inherits
|
|---|
| 107 | QMemArray<char> and that is inherited by QBitArray.
|
|---|
| 108 | */
|
|---|
| 109 | class QByteArray : public QMemArray<char>
|
|---|
| 110 | {
|
|---|
| 111 | public:
|
|---|
| 112 | QByteArray();
|
|---|
| 113 | QByteArray( int size );
|
|---|
| 114 | };
|
|---|
| 115 | #else
|
|---|
| 116 | typedef QMemArray<char> QByteArray;
|
|---|
| 117 | #endif
|
|---|
| 118 |
|
|---|
| 119 | #ifndef QT_NO_COMPRESS
|
|---|
| 120 | Q_EXPORT QByteArray qCompress( const uchar* data, int nbytes );
|
|---|
| 121 | Q_EXPORT QByteArray qUncompress( const uchar* data, int nbytes );
|
|---|
| 122 | Q_EXPORT inline QByteArray qCompress( const QByteArray& data)
|
|---|
| 123 | { return qCompress( (const uchar*)data.data(), data.size() ); }
|
|---|
| 124 | Q_EXPORT inline QByteArray qUncompress( const QByteArray& data )
|
|---|
| 125 | { return qUncompress( (const uchar*)data.data(), data.size() ); }
|
|---|
| 126 | #endif
|
|---|
| 127 |
|
|---|
| 128 | /*****************************************************************************
|
|---|
| 129 | QByteArray stream functions
|
|---|
| 130 | *****************************************************************************/
|
|---|
| 131 | #ifndef QT_NO_DATASTREAM
|
|---|
| 132 | Q_EXPORT QDataStream &operator<<( QDataStream &, const QByteArray & );
|
|---|
| 133 | Q_EXPORT QDataStream &operator>>( QDataStream &, QByteArray & );
|
|---|
| 134 | #endif
|
|---|
| 135 |
|
|---|
| 136 | /*****************************************************************************
|
|---|
| 137 | QCString class
|
|---|
| 138 | *****************************************************************************/
|
|---|
| 139 |
|
|---|
| 140 | class QRegExp;
|
|---|
| 141 |
|
|---|
| 142 | class Q_EXPORT QCString : public QByteArray // C string class
|
|---|
| 143 | {
|
|---|
| 144 | public:
|
|---|
| 145 | QCString() {} // make null string
|
|---|
| 146 | QCString( int size ); // allocate size incl. \0
|
|---|
| 147 | QCString( const QCString &s ) : QByteArray( s ) {}
|
|---|
| 148 | QCString( const char *str ); // deep copy
|
|---|
| 149 | QCString( const char *str, uint maxlen ); // deep copy, max length
|
|---|
| 150 | ~QCString();
|
|---|
| 151 |
|
|---|
| 152 | QCString &operator=( const QCString &s );// shallow copy
|
|---|
| 153 | QCString &operator=( const char *str ); // deep copy
|
|---|
| 154 |
|
|---|
| 155 | bool isNull() const;
|
|---|
| 156 | bool isEmpty() const;
|
|---|
| 157 | uint length() const;
|
|---|
| 158 | bool resize( uint newlen );
|
|---|
| 159 | bool truncate( uint pos );
|
|---|
| 160 | bool fill( char c, int len = -1 );
|
|---|
| 161 |
|
|---|
| 162 | QCString copy() const;
|
|---|
| 163 |
|
|---|
| 164 | QCString &sprintf( const char *format, ... );
|
|---|
| 165 |
|
|---|
| 166 | int find( char c, int index=0, bool cs=TRUE ) const;
|
|---|
| 167 | int find( const char *str, int index=0, bool cs=TRUE ) const;
|
|---|
| 168 | #ifndef QT_NO_REGEXP
|
|---|
| 169 | int find( const QRegExp &, int index=0 ) const;
|
|---|
| 170 | #endif
|
|---|
| 171 | int findRev( char c, int index=-1, bool cs=TRUE) const;
|
|---|
| 172 | int findRev( const char *str, int index=-1, bool cs=TRUE) const;
|
|---|
| 173 | #ifndef QT_NO_REGEXP_CAPTURE
|
|---|
| 174 | int findRev( const QRegExp &, int index=-1 ) const;
|
|---|
| 175 | #endif
|
|---|
| 176 | int contains( char c, bool cs=TRUE ) const;
|
|---|
| 177 | int contains( const char *str, bool cs=TRUE ) const;
|
|---|
| 178 | #ifndef QT_NO_REGEXP
|
|---|
| 179 | int contains( const QRegExp & ) const;
|
|---|
| 180 | #endif
|
|---|
| 181 | QCString left( uint len ) const;
|
|---|
| 182 | QCString right( uint len ) const;
|
|---|
| 183 | QCString mid( uint index, uint len=0xffffffff) const;
|
|---|
| 184 |
|
|---|
| 185 | QCString leftJustify( uint width, char fill=' ', bool trunc=FALSE)const;
|
|---|
| 186 | QCString rightJustify( uint width, char fill=' ',bool trunc=FALSE)const;
|
|---|
| 187 |
|
|---|
| 188 | QCString lower() const;
|
|---|
| 189 | QCString upper() const;
|
|---|
| 190 |
|
|---|
| 191 | QCString stripWhiteSpace() const;
|
|---|
| 192 | QCString simplifyWhiteSpace() const;
|
|---|
| 193 |
|
|---|
| 194 | QCString &insert( uint index, const char * );
|
|---|
| 195 | QCString &insert( uint index, char );
|
|---|
| 196 | QCString &append( const char * );
|
|---|
| 197 | QCString &prepend( const char * );
|
|---|
| 198 | QCString &remove( uint index, uint len );
|
|---|
| 199 | QCString &replace( uint index, uint len, const char * );
|
|---|
| 200 | #ifndef QT_NO_REGEXP
|
|---|
| 201 | QCString &replace( const QRegExp &, const char * );
|
|---|
| 202 | #endif
|
|---|
| 203 | QCString &replace( char c, const char *after );
|
|---|
| 204 | QCString &replace( const char *, const char * );
|
|---|
| 205 | QCString &replace( char, char );
|
|---|
| 206 |
|
|---|
| 207 | short toShort( bool *ok=0 ) const;
|
|---|
| 208 | ushort toUShort( bool *ok=0 ) const;
|
|---|
| 209 | int toInt( bool *ok=0 ) const;
|
|---|
| 210 | uint toUInt( bool *ok=0 ) const;
|
|---|
| 211 | long toLong( bool *ok=0 ) const;
|
|---|
| 212 | ulong toULong( bool *ok=0 ) const;
|
|---|
| 213 | float toFloat( bool *ok=0 ) const;
|
|---|
| 214 | double toDouble( bool *ok=0 ) const;
|
|---|
| 215 |
|
|---|
| 216 | QCString &setStr( const char *s );
|
|---|
| 217 | QCString &setNum( short );
|
|---|
| 218 | QCString &setNum( ushort );
|
|---|
| 219 | QCString &setNum( int );
|
|---|
| 220 | QCString &setNum( uint );
|
|---|
| 221 | QCString &setNum( long );
|
|---|
| 222 | QCString &setNum( ulong );
|
|---|
| 223 | QCString &setNum( float, char f='g', int prec=6 );
|
|---|
| 224 | QCString &setNum( double, char f='g', int prec=6 );
|
|---|
| 225 |
|
|---|
| 226 | bool setExpand( uint index, char c );
|
|---|
| 227 |
|
|---|
| 228 | operator const char *() const;
|
|---|
| 229 | QCString &operator+=( const char *str );
|
|---|
| 230 | QCString &operator+=( char c );
|
|---|
| 231 | private:
|
|---|
| 232 | int find( const char *str, int index, bool cs, uint l ) const;
|
|---|
| 233 | };
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 | /*****************************************************************************
|
|---|
| 237 | QCString stream functions
|
|---|
| 238 | *****************************************************************************/
|
|---|
| 239 | #ifndef QT_NO_DATASTREAM
|
|---|
| 240 | Q_EXPORT QDataStream &operator<<( QDataStream &, const QCString & );
|
|---|
| 241 | Q_EXPORT QDataStream &operator>>( QDataStream &, QCString & );
|
|---|
| 242 | #endif
|
|---|
| 243 |
|
|---|
| 244 | /*****************************************************************************
|
|---|
| 245 | QCString inline functions
|
|---|
| 246 | *****************************************************************************/
|
|---|
| 247 |
|
|---|
| 248 | inline QCString &QCString::operator=( const QCString &s )
|
|---|
| 249 | { return (QCString&)assign( s ); }
|
|---|
| 250 |
|
|---|
| 251 | inline QCString &QCString::operator=( const char *str )
|
|---|
| 252 | { return (QCString&)duplicate( str, qstrlen(str)+1 ); }
|
|---|
| 253 |
|
|---|
| 254 | inline bool QCString::isNull() const
|
|---|
| 255 | { return data() == 0; }
|
|---|
| 256 |
|
|---|
| 257 | inline bool QCString::isEmpty() const
|
|---|
| 258 | { return data() == 0 || *data() == '\0'; }
|
|---|
| 259 |
|
|---|
| 260 | inline uint QCString::length() const
|
|---|
| 261 | { return qstrlen( data() ); }
|
|---|
| 262 |
|
|---|
| 263 | inline bool QCString::truncate( uint pos )
|
|---|
| 264 | { return resize(pos+1); }
|
|---|
| 265 |
|
|---|
| 266 | inline QCString QCString::copy() const
|
|---|
| 267 | { return QCString( data() ); }
|
|---|
| 268 |
|
|---|
| 269 | inline QCString &QCString::prepend( const char *s )
|
|---|
| 270 | { return insert(0,s); }
|
|---|
| 271 |
|
|---|
| 272 | inline QCString &QCString::append( const char *s )
|
|---|
| 273 | { return operator+=(s); }
|
|---|
| 274 |
|
|---|
| 275 | inline QCString &QCString::setNum( short n )
|
|---|
| 276 | { return setNum((long)n); }
|
|---|
| 277 |
|
|---|
| 278 | inline QCString &QCString::setNum( ushort n )
|
|---|
| 279 | { return setNum((ulong)n); }
|
|---|
| 280 |
|
|---|
| 281 | inline QCString &QCString::setNum( int n )
|
|---|
| 282 | { return setNum((long)n); }
|
|---|
| 283 |
|
|---|
| 284 | inline QCString &QCString::setNum( uint n )
|
|---|
| 285 | { return setNum((ulong)n); }
|
|---|
| 286 |
|
|---|
| 287 | inline QCString &QCString::setNum( float n, char f, int prec )
|
|---|
| 288 | { return setNum((double)n,f,prec); }
|
|---|
| 289 |
|
|---|
| 290 | inline QCString::operator const char *() const
|
|---|
| 291 | { return (const char *)data(); }
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 | /*****************************************************************************
|
|---|
| 295 | QCString non-member operators
|
|---|
| 296 | *****************************************************************************/
|
|---|
| 297 |
|
|---|
| 298 | Q_EXPORT inline bool operator==( const QCString &s1, const QCString &s2 )
|
|---|
| 299 | { return qstrcmp( s1.data(), s2.data() ) == 0; }
|
|---|
| 300 |
|
|---|
| 301 | Q_EXPORT inline bool operator==( const QCString &s1, const char *s2 )
|
|---|
| 302 | { return qstrcmp( s1.data(), s2 ) == 0; }
|
|---|
| 303 |
|
|---|
| 304 | Q_EXPORT inline bool operator==( const char *s1, const QCString &s2 )
|
|---|
| 305 | { return qstrcmp( s1, s2.data() ) == 0; }
|
|---|
| 306 |
|
|---|
| 307 | Q_EXPORT inline bool operator!=( const QCString &s1, const QCString &s2 )
|
|---|
| 308 | { return qstrcmp( s1.data(), s2.data() ) != 0; }
|
|---|
| 309 |
|
|---|
| 310 | Q_EXPORT inline bool operator!=( const QCString &s1, const char *s2 )
|
|---|
| 311 | { return qstrcmp( s1.data(), s2 ) != 0; }
|
|---|
| 312 |
|
|---|
| 313 | Q_EXPORT inline bool operator!=( const char *s1, const QCString &s2 )
|
|---|
| 314 | { return qstrcmp( s1, s2.data() ) != 0; }
|
|---|
| 315 |
|
|---|
| 316 | Q_EXPORT inline bool operator<( const QCString &s1, const QCString& s2 )
|
|---|
| 317 | { return qstrcmp( s1.data(), s2.data() ) < 0; }
|
|---|
| 318 |
|
|---|
| 319 | Q_EXPORT inline bool operator<( const QCString &s1, const char *s2 )
|
|---|
| 320 | { return qstrcmp( s1.data(), s2 ) < 0; }
|
|---|
| 321 |
|
|---|
| 322 | Q_EXPORT inline bool operator<( const char *s1, const QCString &s2 )
|
|---|
| 323 | { return qstrcmp( s1, s2.data() ) < 0; }
|
|---|
| 324 |
|
|---|
| 325 | Q_EXPORT inline bool operator<=( const QCString &s1, const QCString &s2 )
|
|---|
| 326 | { return qstrcmp( s1.data(), s2.data() ) <= 0; }
|
|---|
| 327 |
|
|---|
| 328 | Q_EXPORT inline bool operator<=( const QCString &s1, const char *s2 )
|
|---|
| 329 | { return qstrcmp( s1.data(), s2 ) <= 0; }
|
|---|
| 330 |
|
|---|
| 331 | Q_EXPORT inline bool operator<=( const char *s1, const QCString &s2 )
|
|---|
| 332 | { return qstrcmp( s1, s2.data() ) <= 0; }
|
|---|
| 333 |
|
|---|
| 334 | Q_EXPORT inline bool operator>( const QCString &s1, const QCString &s2 )
|
|---|
| 335 | { return qstrcmp( s1.data(), s2.data() ) > 0; }
|
|---|
| 336 |
|
|---|
| 337 | Q_EXPORT inline bool operator>( const QCString &s1, const char *s2 )
|
|---|
| 338 | { return qstrcmp( s1.data(), s2 ) > 0; }
|
|---|
| 339 |
|
|---|
| 340 | Q_EXPORT inline bool operator>( const char *s1, const QCString &s2 )
|
|---|
| 341 | { return qstrcmp( s1, s2.data() ) > 0; }
|
|---|
| 342 |
|
|---|
| 343 | Q_EXPORT inline bool operator>=( const QCString &s1, const QCString& s2 )
|
|---|
| 344 | { return qstrcmp( s1.data(), s2.data() ) >= 0; }
|
|---|
| 345 |
|
|---|
| 346 | Q_EXPORT inline bool operator>=( const QCString &s1, const char *s2 )
|
|---|
| 347 | { return qstrcmp( s1.data(), s2 ) >= 0; }
|
|---|
| 348 |
|
|---|
| 349 | Q_EXPORT inline bool operator>=( const char *s1, const QCString &s2 )
|
|---|
| 350 | { return qstrcmp( s1, s2.data() ) >= 0; }
|
|---|
| 351 |
|
|---|
| 352 | Q_EXPORT inline const QCString operator+( const QCString &s1,
|
|---|
| 353 | const QCString &s2 )
|
|---|
| 354 | {
|
|---|
| 355 | QCString tmp( s1.data() );
|
|---|
| 356 | tmp += s2;
|
|---|
| 357 | return tmp;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | Q_EXPORT inline const QCString operator+( const QCString &s1, const char *s2 )
|
|---|
| 361 | {
|
|---|
| 362 | QCString tmp( s1.data() );
|
|---|
| 363 | tmp += s2;
|
|---|
| 364 | return tmp;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | Q_EXPORT inline const QCString operator+( const char *s1, const QCString &s2 )
|
|---|
| 368 | {
|
|---|
| 369 | QCString tmp( s1 );
|
|---|
| 370 | tmp += s2;
|
|---|
| 371 | return tmp;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | Q_EXPORT inline const QCString operator+( const QCString &s1, char c2 )
|
|---|
| 375 | {
|
|---|
| 376 | QCString tmp( s1.data() );
|
|---|
| 377 | tmp += c2;
|
|---|
| 378 | return tmp;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | Q_EXPORT inline const QCString operator+( char c1, const QCString &s2 )
|
|---|
| 382 | {
|
|---|
| 383 | QCString tmp;
|
|---|
| 384 | tmp += c1;
|
|---|
| 385 | tmp += s2;
|
|---|
| 386 | return tmp;
|
|---|
| 387 | }
|
|---|
| 388 | #include "qwinexport.h"
|
|---|
| 389 | #endif // QCSTRING_H
|
|---|