📄 qdir.cpp
字号:
exist, if \a newName and \a oldName are not on the same partition or if a file with the new name already exists. However, there are also other reasons why rename() can fail. For example, on at least one file system rename() fails if \a newName points to an open file.*/bool QDir::rename(const QString &oldName, const QString &newName){ Q_D(QDir); if (oldName.isEmpty() || newName.isEmpty()) { qWarning("QDir::rename: Empty or null file name(s)"); return false; } if(!d->data->fileEngine) return false; QFile file(filePath(oldName)); if(!file.exists()) return false; return file.rename(filePath(newName));}/*! Returns true if the file called \a name exists; otherwise returns false. Unless \a name contains an absolute file path, the file name is assumed to be relative to the current directory. \sa QFileInfo::exists(), QFile::exists()*/bool QDir::exists(const QString &name) const{ if (name.isEmpty()) { qWarning("QDir::exists: Empty or null file name"); return false; } QString tmp = filePath(name); return QFile::exists(tmp);}/*! Returns a list of the root directories on this system. On Windows this returns a list of QFileInfo objects containing "C:/", "D:/", etc. On other operating systems, it returns a list containing just one root directory (i.e. "/"). \sa root(), rootPath()*/QFileInfoList QDir::drives(){ return QFSFileEngine::drives();}/*! Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows. You do not need to use this function to build file paths. If you always use "/", Qt will translate your paths to conform to the underlying operating system. If you want to display paths to the user using their operating system's separator use toNativeSeparators().*/QChar QDir::separator(){#if defined(Q_OS_UNIX) return QLatin1Char('/');#elif defined (Q_FS_FAT) || defined(Q_WS_WIN) return QLatin1Char('\\');#elif defined (Q_OS_MAC) return QLatin1Char(':');#else return QLatin1Char('/');#endif}/*! Sets the application's current working directory to \a path. Returns true if the directory was successfully changed; otherwise returns false. \sa current() currentPath() home() root() temp()*/bool QDir::setCurrent(const QString &path){ return QFSFileEngine::setCurrentPath(path);}/*! \fn QDir QDir::current() Returns the application's current directory. The directory is constructed using the absolute path of the current directory, ensuring that its path() will be the same as its absolutePath(). \sa currentPath(), home(), root(), temp()*//*! Returns the absolute path of the application's current directory. \sa current(), homePath(), rootPath(), tempPath()*/QString QDir::currentPath(){ return QFSFileEngine::currentPath();}/*! \fn QString QDir::currentDirPath() Returns the absolute path of the application's current directory. Use currentPath() instead. \sa currentPath()*//*! \fn QDir QDir::home() Returns the user's home directory. The directory is constructed using the absolute path of the home directory, ensuring that its path() will be the same as its absolutePath(). See homePath() for details. \sa drives(), current(), root(), temp()*//*! Returns the absolute path of the user's home directory. Under Windows this function will return the directory of the current user's profile. Typically, this is: \code C:/Documents and Settings/Username \endcode Use the toNativeSeparators() function to convert the separators to the ones that are appropriate for the underlying operating system. If the directory of the current user's profile does not exist or cannot be retrieved, the following alternatives will be checked (in the given order) until an existing and available path is found: \list 1 \o The path specified by the \c USERPROFILE environment variable. \o The path formed by concatenating the \c HOMEDRIVE and \c HOMEPATH environment variables. \o The path specified by the \c HOME environment variable. \o The path returned by the rootPath() function (which uses the \c SystemDrive environment variable) \o The \c{C:/} directory. \endlist Under non-Windows operating systems the \c HOME environment variable is used if it exists, otherwise the path returned by the rootPath() function is used. \sa home(), currentPath(), rootPath(), tempPath()*/QString QDir::homePath(){ return QFSFileEngine::homePath();}/*! \fn QString QDir::homeDirPath() Returns the absolute path of the user's home directory. Use homePath() instead. \sa homePath() *//*! \fn QDir QDir::temp() Returns the system's temporary directory. The directory is constructed using the absolute path of the temporary directory, ensuring that its path() will be the same as its absolutePath(). See tempPath() for details. \sa drives(), current(), home(), root()*//*! Returns the absolute path of the system's temporary directory. On Unix/Linux systems this is usually \c{/tmp}; on Windows this is usually the path in the \c TEMP or \c TMP environment variable. Whether a directory separator is added to the end or not, depends on the operating system. \sa temp(), currentPath(), homePath(), rootPath()*/QString QDir::tempPath(){ return QFSFileEngine::tempPath();}/*! \fn QDir QDir::root() Returns the root directory. The directory is constructed using the absolute path of the root directory, ensuring that its path() will be the same as its absolutePath(). See rootPath() for details. \sa drives(), current(), home(), temp()*//*! Returns the absolute path of the root directory. For Unix operating systems this returns "/". For Windows file systems this normally returns "c:/". \sa root(), drives(), currentPath(), homePath(), tempPath()*/QString QDir::rootPath(){ return QFSFileEngine::rootPath();}/*! \fn QString QDir::rootDirPath() Returns the absolute path of the root directory. Use rootPath() instead. \sa rootPath()*/#ifndef QT_NO_REGEXP/*! \overload Returns true if the \a fileName matches any of the wildcard (glob) patterns in the list of \a filters; otherwise returns false. The matching is case insensitive. \sa {QRegExp wildcard matching}, QRegExp::exactMatch() entryList() entryInfoList()*/bool QDir::match(const QStringList &filters, const QString &fileName){ for(QStringList::ConstIterator sit = filters.begin(); sit != filters.end(); ++sit) { QRegExp rx(*sit, Qt::CaseInsensitive, QRegExp::Wildcard); if (rx.exactMatch(fileName)) return true; } return false;}/*! Returns true if the \a fileName matches the wildcard (glob) pattern \a filter; otherwise returns false. The \a filter may contain multiple patterns separated by spaces or semicolons. The matching is case insensitive. \sa {QRegExp wildcard matching}, QRegExp::exactMatch() entryList() entryInfoList()*/bool QDir::match(const QString &filter, const QString &fileName){ return match(nameFiltersFromString(filter), fileName);}#endif/*! Removes all multiple directory separators "/" and resolves any "."s or ".."s found in the path, \a path. Symbolic links are kept. This function does not return the canonical path, but rather the simplest version of the input. For example, "./local" becomes "local", "local/../bin" becomes "bin" and "/local/usr/../bin" becomes "/local/bin". \sa absolutePath() canonicalPath()*/QString QDir::cleanPath(const QString &path){ if (path.isEmpty()) return path; QString name = path; QChar dir_separator = separator(); if(dir_separator != QLatin1Char('/')) name.replace(dir_separator, QLatin1Char('/')); int used = 0, levels = 0; const int len = name.length(); QVector<QChar> out(len); const QChar *p = name.unicode(); for(int i = 0, last = -1, iwrite = 0; i < len; i++) { if(p[i] == QLatin1Char('/')) { while(i < len-1 && p[i+1] == QLatin1Char('/')) {#ifdef Q_OS_WIN //allow unc paths if(!i) break;#endif i++; } bool eaten = false; if(i < len - 1 && p[i+1] == QLatin1Char('.')) { int dotcount = 1; if(i < len - 2 && p[i+2] == QLatin1Char('.')) dotcount++; if(i == len - dotcount - 1) { if(dotcount == 1) { break; } else if(levels) { if(last == -1) { for(int i2 = iwrite-1; i2 >= 0; i2--) { if(out[i2] == QLatin1Char('/')) { last = i2; break; } } } used -= iwrite - last - 1; break; } } else if(p[i+dotcount+1] == QLatin1Char('/')) { if(dotcount == 2 && levels) { if(last == -1 || iwrite - last == 1) { for(int i2 = (last == -1) ? (iwrite-1) : (last-1); i2 >= 0; i2--) { if(out[i2] == QLatin1Char('/')) { eaten = true; last = i2; break; } } } else { eaten = true; } if(eaten) { levels--; used -= iwrite - last; iwrite = last; last = -1; } } else if (dotcount == 2 && i > 0 && p[i - 1] != QLatin1Char('.')) { eaten = true; used -= iwrite - qMax(0, last); iwrite = qMax(0, last); last = -1; ++i; } else if(dotcount == 1) { eaten = true; } if(eaten) i += dotcount; } else { levels++; } } else if(last != -1 && iwrite - last == 1) {#ifdef Q_OS_WIN eaten = (iwrite > 2);#else eaten = true;#endif last = -1; } else if(last != -1 && i == len-1) { eaten = true; } else { levels++; } if(!eaten) last = i - (i - iwrite); else continue; } else if(!i && p[i] == QLatin1Char('.')) { int dotcount = 1; if(len >= 1 && p[1] == QLatin1Char('.')) dotcount++; if(len >= dotcount && p[dotcount] == QLatin1Char('/')) { if(dotcount == 1) { i++; while(i+1 < len-1 && p[i+1] == QLatin1Char('/')) i++; continue; } } } out[iwrite++] = p[i]; used++; } QString ret; if(used == len) ret = name; else ret = QString(out.data(), used); // Strip away last slash except for root directories if (ret.endsWith(QLatin1Char('/')) && !(ret.size() == 1 || (ret.size() == 3 && ret.at(1) == QLatin1Char(':')))) ret = ret.left(ret.length() - 1); return ret;}/*! Returns true if \a path is relative; returns false if it is absolute. \sa isRelative() isAbsolutePath() makeAbsolute()*/bool QDir::isRelativePath(const QString &path){ return QFileInfo(path).isRelative();}/*! Refreshes the directory information.*/void QDir::refresh() const{ Q_D(const QDir); d->data->clear();}/*! \internal Returns a list of n
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -