home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qcstring.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  12KB  |  371 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qcstring.h   3.0.0   edited Sep 21 17:37 $
  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 "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) : 0; }
  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_TEMPLATEDLL)
  105. template class Q_EXPORT QMemArray<char>;
  106. #endif
  107.  
  108. #if defined(Q_QDOC)
  109. /*
  110.   We want qdoc to document QByteArray as a real class that inherits
  111.   QMemArray<char> and that is inherited by QBitArray.
  112. */
  113. class QByteArray : public QMemArray<char>
  114. {
  115. public:
  116.     QByteArray();
  117.     QByteArray( int size );
  118. };
  119. #else
  120. typedef QMemArray<char> QByteArray;
  121. #endif
  122.  
  123. /*****************************************************************************
  124.   QByteArray stream functions
  125.  *****************************************************************************/
  126. #ifndef QT_NO_DATASTREAM
  127. Q_EXPORT QDataStream &operator<<( QDataStream &, const QByteArray & );
  128. Q_EXPORT QDataStream &operator>>( QDataStream &, QByteArray & );
  129. #endif
  130.  
  131.  
  132.  
  133. /*****************************************************************************
  134.   QCString class
  135.  *****************************************************************************/
  136.  
  137. class QRegExp;
  138.  
  139. class Q_EXPORT QCString : public QByteArray    // C string class
  140. {
  141. public:
  142.     QCString() {}                // make null string
  143.     QCString( int size );            // allocate size incl. \0
  144.     QCString( const QCString &s ) : QByteArray( s ) {}
  145.     QCString( const char *str );        // deep copy
  146.     QCString( const char *str, uint maxlen );    // deep copy, max length
  147.     ~QCString();
  148.  
  149.     QCString    &operator=( const QCString &s );// shallow copy
  150.     QCString    &operator=( const char *str );    // deep copy
  151.  
  152.     bool    isNull()    const;
  153.     bool    isEmpty()    const;
  154.     uint    length()    const;
  155.     bool    resize( uint newlen );
  156.     bool    truncate( uint pos );
  157.     bool    fill( char c, int len = -1 );
  158.  
  159.     QCString    copy()    const;
  160.  
  161.     QCString    &sprintf( const char *format, ... );
  162.  
  163.     int        find( char c, int index=0, bool cs=TRUE ) const;
  164.     int        find( const char *str, int index=0, bool cs=TRUE ) const;
  165. #ifndef QT_NO_REGEXP
  166.     int        find( const QRegExp &, int index=0 ) const;
  167. #endif
  168.     int        findRev( char c, int index=-1, bool cs=TRUE) const;
  169.     int        findRev( const char *str, int index=-1, bool cs=TRUE) const;
  170. #ifndef QT_NO_REGEXP
  171.     int        findRev( const QRegExp &, int index=-1 ) const;
  172. #endif
  173.     int        contains( char c, bool cs=TRUE ) const;
  174.     int        contains( const char *str, bool cs=TRUE ) const;
  175. #ifndef QT_NO_REGEXP
  176.     int        contains( const QRegExp & ) const;
  177. #endif
  178.     QCString    left( uint len )  const;
  179.     QCString    right( uint len ) const;
  180.     QCString    mid( uint index, uint len=0xffffffff) const;
  181.  
  182.     QCString    leftJustify( uint width, char fill=' ', bool trunc=FALSE)const;
  183.     QCString    rightJustify( uint width, char fill=' ',bool trunc=FALSE)const;
  184.  
  185.     QCString    lower() const;
  186.     QCString    upper() const;
  187.  
  188.     QCString    stripWhiteSpace()    const;
  189.     QCString    simplifyWhiteSpace()    const;
  190.  
  191.     QCString    &insert( uint index, const char * );
  192.     QCString    &insert( uint index, char );
  193.     QCString    &append( const char * );
  194.     QCString    &prepend( const char * );
  195.     QCString    &remove( uint index, uint len );
  196.     QCString    &replace( uint index, uint len, const char * );
  197. #ifndef QT_NO_REGEXP
  198.     QCString    &replace( const QRegExp &, const char * );
  199. #endif
  200.     short    toShort( bool *ok=0 )    const;
  201.     ushort    toUShort( bool *ok=0 )    const;
  202.     int        toInt( bool *ok=0 )    const;
  203.     uint    toUInt( bool *ok=0 )    const;
  204.     long    toLong( bool *ok=0 )    const;
  205.     ulong    toULong( bool *ok=0 )    const;
  206.     float    toFloat( bool *ok=0 )    const;
  207.     double    toDouble( bool *ok=0 )    const;
  208.  
  209.     QCString    &setStr( const char *s );
  210.     QCString    &setNum( short );
  211.     QCString    &setNum( ushort );
  212.     QCString    &setNum( int );
  213.     QCString    &setNum( uint );
  214.     QCString    &setNum( long );
  215.     QCString    &setNum( ulong );
  216.     QCString    &setNum( float, char f='g', int prec=6 );
  217.     QCString    &setNum( double, char f='g', int prec=6 );
  218.  
  219.     bool    setExpand( uint index, char c );
  220.  
  221.         operator const char *() const;
  222.     QCString    &operator+=( const char *str );
  223.     QCString    &operator+=( char c );
  224. };
  225.  
  226.  
  227. /*****************************************************************************
  228.   QCString stream functions
  229.  *****************************************************************************/
  230. #ifndef QT_NO_DATASTREAM
  231. Q_EXPORT QDataStream &operator<<( QDataStream &, const QCString & );
  232. Q_EXPORT QDataStream &operator>>( QDataStream &, QCString & );
  233. #endif
  234.  
  235. /*****************************************************************************
  236.   QCString inline functions
  237.  *****************************************************************************/
  238.  
  239. inline QCString &QCString::operator=( const QCString &s )
  240. { return (QCString&)assign( s ); }
  241.  
  242. inline QCString &QCString::operator=( const char *str )
  243. { return (QCString&)duplicate( str, qstrlen(str)+1 ); }
  244.  
  245. inline bool QCString::isNull() const
  246. { return data() == 0; }
  247.  
  248. inline bool QCString::isEmpty() const
  249. { return data() == 0 || *data() == '\0'; }
  250.  
  251. inline uint QCString::length() const
  252. { return qstrlen( data() ); }
  253.  
  254. inline bool QCString::truncate( uint pos )
  255. { return resize(pos+1); }
  256.  
  257. inline QCString QCString::copy() const
  258. { return QCString( data() ); }
  259.  
  260. inline QCString &QCString::prepend( const char *s )
  261. { return insert(0,s); }
  262.  
  263. inline QCString &QCString::append( const char *s )
  264. { return operator+=(s); }
  265.  
  266. inline QCString &QCString::setNum( short n )
  267. { return setNum((long)n); }
  268.  
  269. inline QCString &QCString::setNum( ushort n )
  270. { return setNum((ulong)n); }
  271.  
  272. inline QCString &QCString::setNum( int n )
  273. { return setNum((long)n); }
  274.  
  275. inline QCString &QCString::setNum( uint n )
  276. { return setNum((ulong)n); }
  277.  
  278. inline QCString &QCString::setNum( float n, char f, int prec )
  279. { return setNum((double)n,f,prec); }
  280.  
  281. inline QCString::operator const char *() const
  282. { return (const char *)data(); }
  283.  
  284.  
  285. /*****************************************************************************
  286.   QCString non-member operators
  287.  *****************************************************************************/
  288.  
  289. Q_EXPORT inline bool operator==( const QCString &s1, const QCString &s2 )
  290. { return qstrcmp(s1.data(),s2.data()) == 0; }
  291.  
  292. Q_EXPORT inline bool operator==( const QCString &s1, const char *s2 )
  293. { return qstrcmp(s1.data(),s2) == 0; }
  294.  
  295. Q_EXPORT inline bool operator==( const char *s1, const QCString &s2 )
  296. { return qstrcmp(s1,s2.data()) == 0; }
  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 char *s2 )
  317. { return qstrcmp(s1.data(),s2) <= 0; }
  318.  
  319. Q_EXPORT inline bool operator<=( const char *s1, const QCString &s2 )
  320. { return qstrcmp(s1,s2.data()) <= 0; }
  321.  
  322. Q_EXPORT inline bool operator>( const QCString &s1, const char *s2 )
  323. { return qstrcmp(s1.data(),s2) > 0; }
  324.  
  325. Q_EXPORT inline bool operator>( const char *s1, const QCString &s2 )
  326. { return qstrcmp(s1,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 const QCString operator+( const QCString &s1, const QCString &s2 )
  335. {
  336.     QCString tmp( s1.data() );
  337.     tmp += s2;
  338.     return tmp;
  339. }
  340.  
  341. Q_EXPORT inline const QCString operator+( const QCString &s1, const char *s2 )
  342. {
  343.     QCString tmp( s1.data() );
  344.     tmp += s2;
  345.     return tmp;
  346. }
  347.  
  348. Q_EXPORT inline const QCString operator+( const char *s1, const QCString &s2 )
  349. {
  350.     QCString tmp( s1 );
  351.     tmp += s2;
  352.     return tmp;
  353. }
  354.  
  355. Q_EXPORT inline const QCString operator+( const QCString &s1, char c2 )
  356. {
  357.     QCString tmp( s1.data() );
  358.     tmp += c2;
  359.     return tmp;
  360. }
  361.  
  362. Q_EXPORT inline const QCString operator+( char c1, const QCString &s2 )
  363. {
  364.     QCString tmp;
  365.     tmp += c1;
  366.     tmp += s2;
  367.     return tmp;
  368. }
  369.  
  370. #endif // QCSTRING_H
  371.