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

qstringlist.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qstringlist.cpp,v 1.2 2003/07/10 02:40:12 llornkcor Exp $
00003 **
00004 ** Implementation of QStringList
00005 **
00006 ** Created : 990406
00007 **
00008 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
00009 **
00010 ** This file is part of the tools module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech AS of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #include "qstringlist.h"
00039 
00040 #ifndef QT_NO_STRINGLIST
00041 #include "qregexp.h"
00042 #include "qstrlist.h"
00043 #include "qdatastream.h"
00044 #include "qtl.h"
00045 
00181 void QStringList::sort()
00182 {
00183     qHeapSort( *this );
00184 }
00185 
00195 QStringList QStringList::split( const QChar &sep, const QString &str,
00196                                 bool allowEmptyEntries )
00197 {
00198     return split( QString(sep), str, allowEmptyEntries );
00199 }
00200 
00218 QStringList QStringList::split( const QString &sep, const QString &str,
00219                                 bool allowEmptyEntries )
00220 {
00221     QStringList lst;
00222 
00223     int j = 0;
00224     int i = str.find( sep, j );
00225 
00226     while ( i != -1 ) {
00227         if ( i > j && i <= (int)str.length() )
00228             lst << str.mid( j, i - j );
00229         else if ( allowEmptyEntries )
00230             lst << QString::null;
00231         j = i + sep.length();
00232         i = str.find( sep, sep.length() > 0 ? j : j+1 );
00233     }
00234 
00235     int l = str.length() - 1;
00236     if ( str.mid( j, l - j + 1 ).length() > 0 )
00237         lst << str.mid( j, l - j + 1 );
00238     else if ( allowEmptyEntries )
00239         lst << QString::null;
00240 
00241     return lst;
00242 }
00243 
00244 #ifndef QT_NO_REGEXP
00245 
00264 QStringList QStringList::split( const QRegExp &sep, const QString &str,
00265                                 bool allowEmptyEntries )
00266 {
00267     QStringList lst;
00268 
00269     QRegExp tep = sep;
00270 
00271     int j = 0;
00272     int i = tep.search( str, j );
00273 
00274     while ( i != -1 ) {
00275         if ( str.mid( j, i - j ).length() > 0 )
00276             lst << str.mid( j, i - j );
00277         else if ( allowEmptyEntries )
00278             lst << QString::null;
00279         if ( tep.matchedLength() == 0 )
00280             j = i + 1;
00281         else
00282             j = i + tep.matchedLength();
00283         i = tep.search( str, j );
00284     }
00285 
00286     int l = str.length() - 1;
00287     if ( str.mid( j, l - j + 1 ).length() > 0 )
00288         lst << str.mid( j, l - j + 1 );
00289     else if ( allowEmptyEntries )
00290         lst << QString::null;
00291 
00292     return lst;
00293 }
00294 #endif
00295 
00303 QStringList QStringList::grep( const QString &str, bool cs ) const
00304 {
00305     QStringList res;
00306     for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
00307         if ( (*it).contains(str, cs) )
00308             res << *it;
00309 
00310     return res;
00311 }
00312 
00313 #ifndef QT_NO_REGEXP
00314 
00321 QStringList QStringList::grep( const QRegExp &expr ) const
00322 {
00323     QStringList res;
00324     for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
00325         if ( (*it).contains(expr) )
00326             res << *it;
00327 
00328     return res;
00329 }
00330 #endif
00331 
00338 QString QStringList::join( const QString &sep ) const
00339 {
00340     QString res;
00341     bool alredy = FALSE;
00342     for ( QStringList::ConstIterator it = begin(); it != end(); ++it ) {
00343         if ( alredy )
00344             res += sep;
00345         alredy = TRUE;
00346         res += *it;
00347     }
00348 
00349     return res;
00350 }
00351 
00352 #ifndef QT_NO_DATASTREAM
00353 Q_EXPORT QDataStream &operator>>( QDataStream & s, QStringList& l )
00354 {
00355     return s >> (QValueList<QString>&)l;
00356 }
00357 
00358 Q_EXPORT QDataStream &operator<<( QDataStream & s, const QStringList& l )
00359 {
00360     return s << (const QValueList<QString>&)l;
00361 }
00362 #endif
00363 
00367 QStringList QStringList::fromStrList(const QStrList& ascii)
00368 {
00369     QStringList res;
00370     const char * s;
00371     for ( QStrListIterator it(ascii); (s=it.current()); ++it )
00372         res << s;
00373     return res;
00374 }
00375 
00376 #endif //QT_NO_STRINGLIST

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