📄 q3url.cpp
字号:
return false; if ( d->protocol == url.d->protocol && d->user == url.d->user && d->pass == url.d->pass && d->host == url.d->host && d->path == url.d->path && d->queryEncoded == url.d->queryEncoded && d->refEncoded == url.d->refEncoded && d->isValid == url.d->isValid && d->port == url.d->port ) return true; return false;}/*! \overload Compares this URL with \a url. \a url is parsed first. Returns true if \a url is equal to this url; otherwise returns false.*/bool Q3Url::operator==( const QString& url ) const{ Q3Url u( url ); return ( *this == u );}/*! Sets the file name of the URL to \a name. If this URL contains a fileName(), the original file name is replaced by \a name. See the documentation of fileName() for a more detailed discussion of what is handled as file name and what is handled as a directory path. \sa fileName()*/void Q3Url::setFileName( const QString& name ){ QString fn( name ); slashify( fn ); while ( fn[ 0 ] == QLatin1Char( '/' ) ) fn.remove( (uint)0, 1 ); QString p; if ( path().isEmpty() ) { p = QLatin1String("/"); } else { p = path(); int slash = p.findRev( QLatin1Char( '/' ) ); if ( slash == -1 ) { p = QLatin1String("/"); } else if ( p[ (int)p.length() - 1 ] != QLatin1Char( '/' ) ) { p.truncate( slash + 1 ); } } p += fn; if ( !d->queryEncoded.isEmpty() ) p += QLatin1String("?") + d->queryEncoded; setEncodedPathAndQuery( p );}/*! Returns the encoded path and query. \sa decode()*/QString Q3Url::encodedPathAndQuery(){ QString p = path(); if ( p.isEmpty() ) p = QLatin1String("/"); encode( p ); if ( !d->queryEncoded.isEmpty() ) { p += QLatin1String("?"); p += d->queryEncoded; } return p;}/*! Parses \a pathAndQuery for a path and query and sets those values. The whole string must be encoded. \sa encode()*/void Q3Url::setEncodedPathAndQuery( const QString& pathAndQuery ){ d->cleanPathDirty = true; int pos = pathAndQuery.find( QLatin1Char('?') ); if ( pos == -1 ) { d->path = pathAndQuery; d->queryEncoded = QLatin1String(""); } else { d->path = pathAndQuery.left( pos ); d->queryEncoded = pathAndQuery.mid( pos + 1 ); } decode( d->path ); d->cleanPathDirty = true;}/*! Returns the path of the URL. If \a correct is true, the path is cleaned (deals with too many or too few slashes, cleans things like "/../..", etc). Otherwise path() returns exactly the path that was parsed or set. \sa setPath() hasPath()*/QString Q3Url::path( bool correct ) const{ if ( !correct ) return d->path; if ( d->cleanPathDirty ) { bool check = true; if ( QDir::isRelativePath( d->path ) ) { d->cleanPath = d->path; } else if ( isLocalFile() ) {#if defined(Q_OS_WIN32) // hack for stuff like \\machine\path and //machine/path on windows if ( ( d->path.left( 1 ) == QLatin1String("/") || d->path.left( 1 ) == QLatin1String("\\") ) && d->path.length() > 1 ) { d->cleanPath = d->path; bool share = (d->cleanPath[0] == QLatin1Char('\\') && d->cleanPath[1] == QLatin1Char('\\')) || (d->cleanPath[0] == QLatin1Char('/') && d->cleanPath[1] == QLatin1Char('/')); slashify( d->cleanPath, false ); d->cleanPath = QDir::cleanDirPath( d->cleanPath ); if ( share ) { check = false; while (d->cleanPath.at(0) != QLatin1Char('/') || d->cleanPath.at(1) != QLatin1Char('/')) d->cleanPath.prepend(QLatin1String("/")); } }#endif if ( check ) { QFileInfo fi( d->path ); if ( !fi.exists() ) d->cleanPath = d->path; else if ( fi.isDir() ) { QString canPath = QDir( d->path ).canonicalPath(); QString dir; if ( qt_resolve_symlinks && !canPath.isNull() ) dir = QDir::cleanDirPath( canPath ); else dir = QDir::cleanDirPath( QDir( d->path ).absPath() ); dir += QLatin1String("/"); if ( dir == QLatin1String("//") ) d->cleanPath = QLatin1String("/"); else d->cleanPath = dir; } else { QString p = QDir::cleanDirPath( (qt_resolve_symlinks ? fi.dir().canonicalPath() : fi.dir().absPath()) ); d->cleanPath = p + QLatin1String("/") + fi.fileName(); } } } else { if ( d->path != QLatin1String("/") && d->path[ (int)d->path.length() - 1 ] == QLatin1Char('/') ) d->cleanPath = QDir::cleanDirPath( d->path ) + QLatin1String("/"); else d->cleanPath = QDir::cleanDirPath( d->path ); } if ( check ) slashify( d->cleanPath, false ); d->cleanPathDirty = false; } return d->cleanPath;}/*! Returns true if the URL is a local file; otherwise returns false.*/bool Q3Url::isLocalFile() const{ return d->protocol == QLatin1String("file");}/*! Returns the file name of the URL. If the path of the URL doesn't have a slash at the end, the part between the last slash and the end of the path string is considered to be the file name. If the path has a slash at the end, an empty string is returned here. \sa setFileName()*/QString Q3Url::fileName() const{ if ( d->path.isEmpty() || d->path.endsWith( QLatin1String("/") )#ifdef Q_WS_WIN || d->path.endsWith( QLatin1String("\\") )#endif ) return QString(); return QFileInfo( d->path ).fileName();}/*! Adds the path \a pa to the path of the URL. \sa setPath() hasPath()*/void Q3Url::addPath( const QString& pa ){ if ( pa.isEmpty() ) return; QString p( pa ); slashify( p ); if ( path().isEmpty() ) { if ( p[ 0 ] != QLatin1Char( '/' ) ) d->path = QLatin1String("/") + p; else d->path = p; } else { if ( p[ 0 ] != QLatin1Char( '/' ) && d->path[ (int)d->path.length() - 1 ] != QLatin1Char('/') ) d->path += QLatin1String("/") + p; else d->path += p; } d->cleanPathDirty = true;}/*! Returns the directory path of the URL. This is the part of the path of the URL without the fileName(). See the documentation of fileName() for a discussion of what is handled as file name and what is handled as directory path. \sa setPath() hasPath()*/QString Q3Url::dirPath() const{ if ( path().isEmpty() ) return QString(); QString s = path(); int pos = s.findRev( QLatin1Char('/') ); if ( pos == -1 ) { return QString::fromLatin1( "." ); } else { if ( pos == 0 ) return QString::fromLatin1( "/" ); return s.left( pos ); }}/*! Encodes the \a url in-place into UTF-8. For example \code QString url = http://www.trolltech.com Q3Url::encode( url ); // url is now "http%3A//www%20trolltech%20com" \endcode \sa decode()*/void Q3Url::encode( QString& url ){ if ( url.isEmpty() ) return; Q3CString curl = url.utf8(); int oldlen = curl.length(); const Q3CString special( "+<>#@\"&%$:,;?={}|^~[]\'`\\ \n\t\r" ); QString newUrl; int newlen = 0; for ( int i = 0; i < oldlen ;++i ) { uchar inCh = (uchar)curl[ i ]; if ( inCh >= 128 || special.contains(inCh) ) { newUrl[ newlen++ ] = QLatin1Char( '%' ); ushort c = inCh / 16; c += c > 9 ? 'A' - 10 : '0'; newUrl[ newlen++ ] = c; c = inCh % 16; c += c > 9 ? 'A' - 10 : '0'; newUrl[ newlen++ ] = c; } else { newUrl[ newlen++ ] = inCh; } } url = newUrl;}static uchar hex_to_int( uchar c ){ if ( c >= 'A' && c <= 'F' ) return c - 'A' + 10; if ( c >= 'a' && c <= 'f') return c - 'a' + 10; if ( c >= '0' && c <= '9') return c - '0'; return 0;}/*! Decodes the \a url in-place into UTF-8. For example \code QString url = "http%3A//www%20trolltech%20com" Q3Url::decode( url ); // url is now "http://www.trolltech.com" \endcode \sa encode()*/void Q3Url::decode( QString& url ){ if ( url.isEmpty() ) return; int newlen = 0; Q3CString curl = url.utf8(); int oldlen = curl.length(); Q3CString newUrl(oldlen); int i = 0; while ( i < oldlen ) { uchar c = (uchar)curl[ i++ ]; if ( c == '%' && i <= oldlen - 2 ) { c = hex_to_int( (uchar)curl[ i ] ) * 16 + hex_to_int( (uchar)curl[ i + 1 ] ); i += 2; } newUrl [ newlen++ ] = c; } newUrl.truncate( newlen ); url = QString::fromUtf8(newUrl.data());}/*! Composes a string version of the URL and returns it. If \a encodedPath is true the path in the returned string is encoded. If \a forcePrependProtocol is true and \a encodedPath looks like a local filename, the "file:/" protocol is also prepended. \sa encode() decode()*/QString Q3Url::toString( bool encodedPath, bool forcePrependProtocol ) const{ QString res, p = path(); if ( encodedPath ) encode( p ); if ( isLocalFile() ) { if ( forcePrependProtocol ) res = d->protocol + QLatin1String(":") + p; else res = p; } else if ( d->protocol == QLatin1String("mailto") ) { res = d->protocol + QLatin1String(":") + p; } else { res = d->protocol + QLatin1String("://"); if ( !d->user.isEmpty() || !d->pass.isEmpty() ) { QString tmp; if ( !d->user.isEmpty() ) { tmp = d->user; encode( tmp ); res += tmp; } if ( !d->pass.isEmpty() ) { tmp = d->pass; encode( tmp ); res += QLatin1String(":") + tmp; } res += QLatin1String("@"); } res += d->host; if ( d->port != -1 ) res += QLatin1String(":") + QString( QLatin1String("%1") ).arg( d->port ); if ( !p.isEmpty() ) { if ( !d->host.isEmpty() && p[0]!= QLatin1Char( '/' ) ) res += QLatin1String("/"); res += p; } } if ( !d->refEncoded.isEmpty() ) res += QLatin1String("#") + d->refEncoded; if ( !d->queryEncoded.isEmpty() ) res += QLatin1String("?") + d->queryEncoded; return res;}/*! Composes a string version of the URL and returns it. \sa Q3Url::toString()*/Q3Url::operator QString() const{ return toString();}/*! Changes the directory to one directory up. This function always returns true. \sa setPath()*/bool Q3Url::cdUp(){ d->path += QLatin1String("/.."); d->cleanPathDirty = true; return true;}#endif // QT_NO_URL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -