Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qcstring.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qcstring.h,v 1.2 2003/07/10 02:40:10 llornkcor Exp $
00003 **
00004 ** Definition of the extended char array operations,
00005 ** and QByteArray and QCString classes
00006 **
00007 ** Created : 920609
00008 **
00009 ** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
00010 **
00011 ** This file is part of the tools module of the Qt GUI Toolkit.
00012 **
00013 ** This file may be distributed under the terms of the Q Public License
00014 ** as defined by Trolltech AS of Norway and appearing in the file
00015 ** LICENSE.QPL included in the packaging of this file.
00016 **
00017 ** This file may be distributed and/or modified under the terms of the
00018 ** GNU General Public License version 2 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.GPL included in the
00020 ** packaging of this file.
00021 **
00022 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00023 ** licenses may use this file in accordance with the Qt Commercial License
00024 ** Agreement provided with the Software.
00025 **
00026 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00027 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00028 **
00029 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00030 **   information about Qt Commercial License Agreements.
00031 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00032 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00033 **
00034 ** Contact info@trolltech.com if any conditions of this licensing are
00035 ** not clear to you.
00036 **
00037 **********************************************************************/
00038 
00039 #ifndef QCSTRING_H
00040 #define QCSTRING_H
00041 
00042 #ifndef QT_H
00043 #include "qmemarray.h"
00044 #endif // QT_H
00045 
00046 #include <string.h>
00047 
00048 
00049 /*****************************************************************************
00050   Safe and portable C string functions; extensions to standard string.h
00051  *****************************************************************************/
00052 
00053 Q_EXPORT void *qmemmove( void *dst, const void *src, uint len );
00054 
00055 Q_EXPORT char *qstrdup( const char * );
00056 
00057 Q_EXPORT inline uint qstrlen( const char *str )
00058 { return str ? (uint)strlen(str) : 0; }
00059 
00060 Q_EXPORT inline char *qstrcpy( char *dst, const char *src )
00061 { return src ? strcpy(dst, src) : 0; }
00062 
00063 Q_EXPORT char *qstrncpy( char *dst, const char *src, uint len );
00064 
00065 Q_EXPORT inline int qstrcmp( const char *str1, const char *str2 )
00066 {
00067     return ( str1 && str2 ) ? strcmp( str1, str2 )
00068                             : ( str1 ? 1 : ( str2 ? -1 : 0 ) );
00069 }
00070 
00071 Q_EXPORT inline int qstrncmp( const char *str1, const char *str2, uint len )
00072 {
00073     return ( str1 && str2 ) ? strncmp( str1, str2, len )
00074                             : ( str1 ? 1 : ( str2 ? -1 : 0 ) );
00075 }
00076 
00077 Q_EXPORT int qstricmp( const char *, const char * );
00078 
00079 Q_EXPORT int qstrnicmp( const char *, const char *, uint len );
00080 
00081 #ifndef QT_CLEAN_NAMESPACE
00082 Q_EXPORT inline uint cstrlen( const char *str )
00083 { return (uint)strlen(str); }
00084 
00085 Q_EXPORT inline char *cstrcpy( char *dst, const char *src )
00086 { return strcpy(dst,src); }
00087 
00088 Q_EXPORT inline int cstrcmp( const char *str1, const char *str2 )
00089 { return strcmp(str1,str2); }
00090 
00091 Q_EXPORT inline int cstrncmp( const char *str1, const char *str2, uint len )
00092 { return strncmp(str1,str2,len); }
00093 #endif
00094 
00095 
00096 // qChecksum: Internet checksum
00097 
00098 Q_EXPORT Q_UINT16 qChecksum( const char *s, uint len );
00099 
00100 /*****************************************************************************
00101   QByteArray class
00102  *****************************************************************************/
00103 
00104 #ifndef QT_QWINEXPORT
00105 #if defined(Q_TEMPLATEDLL)
00106 Q_TEMPLATE_EXTERN template class Q_EXPORT QMemArray<char>;
00107 #endif
00108 #endif /* QT_QWINEXPORT */
00109 
00110 #if defined(Q_QDOC)
00111 /*
00112   We want qdoc to document QByteArray as a real class that inherits
00113   QMemArray<char> and that is inherited by QBitArray.
00114 */
00115 class QByteArray : public QMemArray<char>
00116 {
00117 public:
00118     QByteArray();
00119     QByteArray( int size );
00120 };
00121 #else
00122 typedef QMemArray<char> QByteArray;
00123 #endif
00124 
00125 #ifndef QT_NO_COMPRESS
00126 Q_EXPORT QByteArray qCompress( const uchar* data, int nbytes );
00127 Q_EXPORT QByteArray qUncompress( const uchar* data, int nbytes );
00128 Q_EXPORT inline QByteArray qCompress( const QByteArray& data)
00129 { return qCompress( (const uchar*)data.data(), data.size() ); }
00130 Q_EXPORT inline QByteArray qUncompress( const QByteArray& data )
00131 { return qUncompress( (const uchar*)data.data(), data.size() ); }
00132 #endif
00133 
00134 /*****************************************************************************
00135   QByteArray stream functions
00136  *****************************************************************************/
00137 #ifndef QT_NO_DATASTREAM
00138 Q_EXPORT QDataStream &operator<<( QDataStream &, const QByteArray & );
00139 Q_EXPORT QDataStream &operator>>( QDataStream &, QByteArray & );
00140 #endif
00141 
00142 /*****************************************************************************
00143   QCString class
00144  *****************************************************************************/
00145 
00146 class QRegExp;
00147 
00148 class Q_EXPORT QCString : public QByteArray     // C string class
00149 {
00150 public:
00151     QCString() {}                               // make null string
00152     QCString( int size );                       // allocate size incl. \0
00153     QCString( const QCString &s ) : QByteArray( s ) {}
00154     QCString( const char *str );                // deep copy
00155     QCString( const char *str, uint maxlen );   // deep copy, max length
00156     ~QCString();
00157 
00158     QCString    &operator=( const QCString &s );// shallow copy
00159     QCString    &operator=( const char *str );  // deep copy
00160 
00161     bool        isNull()        const;
00162     bool        isEmpty()       const;
00163     uint        length()        const;
00164     bool        resize( uint newlen );
00165     bool        truncate( uint pos );
00166     bool        fill( char c, int len = -1 );
00167 
00168     QCString    copy()  const;
00169 
00170     QCString    &sprintf( const char *format, ... );
00171 
00172     int         find( char c, int index=0, bool cs=TRUE ) const;
00173     int         find( const char *str, int index=0, bool cs=TRUE ) const;
00174 #ifndef QT_NO_REGEXP
00175     int         find( const QRegExp &, int index=0 ) const;
00176 #endif
00177     int         findRev( char c, int index=-1, bool cs=TRUE) const;
00178     int         findRev( const char *str, int index=-1, bool cs=TRUE) const;
00179 #ifndef QT_NO_REGEXP_CAPTURE
00180     int         findRev( const QRegExp &, int index=-1 ) const;
00181 #endif
00182     int         contains( char c, bool cs=TRUE ) const;
00183     int         contains( const char *str, bool cs=TRUE ) const;
00184 #ifndef QT_NO_REGEXP
00185     int         contains( const QRegExp & ) const;
00186 #endif
00187     QCString    left( uint len )  const;
00188     QCString    right( uint len ) const;
00189     QCString    mid( uint index, uint len=0xffffffff) const;
00190 
00191     QCString    leftJustify( uint width, char fill=' ', bool trunc=FALSE)const;
00192     QCString    rightJustify( uint width, char fill=' ',bool trunc=FALSE)const;
00193 
00194     QCString    lower() const;
00195     QCString    upper() const;
00196 
00197     QCString    stripWhiteSpace()       const;
00198     QCString    simplifyWhiteSpace()    const;
00199 
00200     QCString    &insert( uint index, const char * );
00201     QCString    &insert( uint index, char );
00202     QCString    &append( const char * );
00203     QCString    &prepend( const char * );
00204     QCString    &remove( uint index, uint len );
00205     QCString    &replace( uint index, uint len, const char * );
00206 #ifndef QT_NO_REGEXP
00207     QCString    &replace( const QRegExp &, const char * );
00208 #endif
00209     QCString    &replace( char c, const char *after );
00210     QCString    &replace( const char *, const char * );
00211     QCString    &replace( char, char );
00212 
00213     short       toShort( bool *ok=0 )   const;
00214     ushort      toUShort( bool *ok=0 )  const;
00215     int         toInt( bool *ok=0 )     const;
00216     uint        toUInt( bool *ok=0 )    const;
00217     long        toLong( bool *ok=0 )    const;
00218     ulong       toULong( bool *ok=0 )   const;
00219     float       toFloat( bool *ok=0 )   const;
00220     double      toDouble( bool *ok=0 )  const;
00221 
00222     QCString    &setStr( const char *s );
00223     QCString    &setNum( short );
00224     QCString    &setNum( ushort );
00225     QCString    &setNum( int );
00226     QCString    &setNum( uint );
00227     QCString    &setNum( long );
00228     QCString    &setNum( ulong );
00229     QCString    &setNum( float, char f='g', int prec=6 );
00230     QCString    &setNum( double, char f='g', int prec=6 );
00231 
00232     bool        setExpand( uint index, char c );
00233 
00234                 operator const char *() const;
00235     QCString    &operator+=( const char *str );
00236     QCString    &operator+=( char c );
00237 private:
00238     int find( const char *str, int index, bool cs, uint l ) const;
00239 };
00240 
00241 
00242 /*****************************************************************************
00243   QCString stream functions
00244  *****************************************************************************/
00245 #ifndef QT_NO_DATASTREAM
00246 Q_EXPORT QDataStream &operator<<( QDataStream &, const QCString & );
00247 Q_EXPORT QDataStream &operator>>( QDataStream &, QCString & );
00248 #endif
00249 
00250 /*****************************************************************************
00251   QCString inline functions
00252  *****************************************************************************/
00253 
00254 inline QCString &QCString::operator=( const QCString &s )
00255 { return (QCString&)assign( s ); }
00256 
00257 inline QCString &QCString::operator=( const char *str )
00258 { return (QCString&)duplicate( str, qstrlen(str)+1 ); }
00259 
00260 inline bool QCString::isNull() const
00261 { return data() == 0; }
00262 
00263 inline bool QCString::isEmpty() const
00264 { return data() == 0 || *data() == '\0'; }
00265 
00266 inline uint QCString::length() const
00267 { return qstrlen( data() ); }
00268 
00269 inline bool QCString::truncate( uint pos )
00270 { return resize(pos+1); }
00271 
00272 inline QCString QCString::copy() const
00273 { return QCString( data() ); }
00274 
00275 inline QCString &QCString::prepend( const char *s )
00276 { return insert(0,s); }
00277 
00278 inline QCString &QCString::append( const char *s )
00279 { return operator+=(s); }
00280 
00281 inline QCString &QCString::setNum( short n )
00282 { return setNum((long)n); }
00283 
00284 inline QCString &QCString::setNum( ushort n )
00285 { return setNum((ulong)n); }
00286 
00287 inline QCString &QCString::setNum( int n )
00288 { return setNum((long)n); }
00289 
00290 inline QCString &QCString::setNum( uint n )
00291 { return setNum((ulong)n); }
00292 
00293 inline QCString &QCString::setNum( float n, char f, int prec )
00294 { return setNum((double)n,f,prec); }
00295 
00296 inline QCString::operator const char *() const
00297 { return (const char *)data(); }
00298 
00299 
00300 /*****************************************************************************
00301   QCString non-member operators
00302  *****************************************************************************/
00303 
00304 Q_EXPORT inline bool operator==( const QCString &s1, const QCString &s2 )
00305 { return qstrcmp( s1.data(), s2.data() ) == 0; }
00306 
00307 Q_EXPORT inline bool operator==( const QCString &s1, const char *s2 )
00308 { return qstrcmp( s1.data(), s2 ) == 0; }
00309 
00310 Q_EXPORT inline bool operator==( const char *s1, const QCString &s2 )
00311 { return qstrcmp( s1, s2.data() ) == 0; }
00312 
00313 Q_EXPORT inline bool operator!=( const QCString &s1, const QCString &s2 )
00314 { return qstrcmp( s1.data(), s2.data() ) != 0; }
00315 
00316 Q_EXPORT inline bool operator!=( const QCString &s1, const char *s2 )
00317 { return qstrcmp( s1.data(), s2 ) != 0; }
00318 
00319 Q_EXPORT inline bool operator!=( const char *s1, const QCString &s2 )
00320 { return qstrcmp( s1, s2.data() ) != 0; }
00321 
00322 Q_EXPORT inline bool operator<( const QCString &s1, const QCString& s2 )
00323 { return qstrcmp( s1.data(), s2.data() ) < 0; }
00324 
00325 Q_EXPORT inline bool operator<( const QCString &s1, const char *s2 )
00326 { return qstrcmp( s1.data(), s2 ) < 0; }
00327 
00328 Q_EXPORT inline bool operator<( const char *s1, const QCString &s2 )
00329 { return qstrcmp( s1, s2.data() ) < 0; }
00330 
00331 Q_EXPORT inline bool operator<=( const QCString &s1, const QCString &s2 )
00332 { return qstrcmp( s1.data(), s2.data() ) <= 0; }
00333 
00334 Q_EXPORT inline bool operator<=( const QCString &s1, const char *s2 )
00335 { return qstrcmp( s1.data(), s2 ) <= 0; }
00336 
00337 Q_EXPORT inline bool operator<=( const char *s1, const QCString &s2 )
00338 { return qstrcmp( s1, s2.data() ) <= 0; }
00339 
00340 Q_EXPORT inline bool operator>( const QCString &s1, const QCString &s2 )
00341 { return qstrcmp( s1.data(), s2.data() ) > 0; }
00342 
00343 Q_EXPORT inline bool operator>( const QCString &s1, const char *s2 )
00344 { return qstrcmp( s1.data(), s2 ) > 0; }
00345 
00346 Q_EXPORT inline bool operator>( const char *s1, const QCString &s2 )
00347 { return qstrcmp( s1, s2.data() ) > 0; }
00348 
00349 Q_EXPORT inline bool operator>=( const QCString &s1, const QCString& s2 )
00350 { return qstrcmp( s1.data(), s2.data() ) >= 0; }
00351 
00352 Q_EXPORT inline bool operator>=( const QCString &s1, const char *s2 )
00353 { return qstrcmp( s1.data(), s2 ) >= 0; }
00354 
00355 Q_EXPORT inline bool operator>=( const char *s1, const QCString &s2 )
00356 { return qstrcmp( s1, s2.data() ) >= 0; }
00357 
00358 Q_EXPORT inline const QCString operator+( const QCString &s1,
00359                                           const QCString &s2 )
00360 {
00361     QCString tmp( s1.data() );
00362     tmp += s2;
00363     return tmp;
00364 }
00365 
00366 Q_EXPORT inline const QCString operator+( const QCString &s1, const char *s2 )
00367 {
00368     QCString tmp( s1.data() );
00369     tmp += s2;
00370     return tmp;
00371 }
00372 
00373 Q_EXPORT inline const QCString operator+( const char *s1, const QCString &s2 )
00374 {
00375     QCString tmp( s1 );
00376     tmp += s2;
00377     return tmp;
00378 }
00379 
00380 Q_EXPORT inline const QCString operator+( const QCString &s1, char c2 )
00381 {
00382     QCString tmp( s1.data() );
00383     tmp += c2;
00384     return tmp;
00385 }
00386 
00387 Q_EXPORT inline const QCString operator+( char c1, const QCString &s2 )
00388 {
00389     QCString tmp;
00390     tmp += c1;
00391     tmp += s2;
00392     return tmp;
00393 }
00394 
00395 #ifdef QT_QWINEXPORT
00396 #include <qwinexport.h>
00397 #endif /* QT_QWINEXPORT */
00398 #endif // QCSTRING_H

Generated on Sat Nov 5 16:18:23 2005 for OPIE by  doxygen 1.4.2