📄 qdir_win.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** This program is distributed in the hope that it will be useful, but** WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ** See the GNU General Public License for more details.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qplatformdefs.h"#include "qdir.h"#include "qdir_p.h"#include "qnamespace.h"#include "qfileinfo.h"#include "qfiledefs_p.h"#include "qregexp.h"#include "qstringlist.h"#ifdef QT_THREAD_SUPPORT# include <private/qmutexpool_p.h>#endif // QT_THREAD_SUPPORT#include <windows.h>#include <limits.h>#if defined(Q_OS_OS2EMX)extern "C" Q_UINT32 DosQueryCurrentDisk(Q_UINT32*,Q_UINT32*);#define NO_ERROR 0#endif#if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX)void QDir::slashify( QString& n ){ if ( n.isNull() ) return; for ( int i=0; i<(int)n.length(); i++ ) { if ( n[i] == '\\' ) n[i] = '/'; }}extern QCString qt_win95Name(const QString s);#elsestatic void QDir::slashify( QString& n){ return;}#endifQString QDir::homeDirPath(){ QString d; d = QString::fromLocal8Bit( getenv("HOME") ); if ( d.isEmpty() || !QFile::exists( d ) ) { d = QString::fromLocal8Bit( getenv("USERPROFILE") ); if ( d.isEmpty() || !QFile::exists( d ) ) { d = QString::fromLocal8Bit( getenv("HOMEDRIVE") ) + QString::fromLocal8Bit( getenv("HOMEPATH") ); if ( d.isEmpty() || !QFile::exists( d ) ) d = rootDirPath(); } } slashify( d ); return d;}/*! Returns the canonical path, i.e. a path without symbolic links or redundant "." or ".." elements. On systems that do not have symbolic links this function will always return the same string that absPath() returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalPath() returns QString::null. \sa path(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath(), QString::isNull()*/QString QDir::canonicalPath() const{ QString r; QT_WA( { TCHAR cur[PATH_MAX]; ::_wgetcwd( cur, PATH_MAX ); if ( ::_wchdir( (TCHAR*)dPath.ucs2() ) >= 0 ) { TCHAR tmp[PATH_MAX]; if ( ::_wgetcwd( tmp, PATH_MAX ) ) r = QString::fromUcs2( (ushort*)tmp ); } ::_wchdir( cur ); } , { char cur[PATH_MAX]; QT_GETCWD( cur, PATH_MAX ); if ( QT_CHDIR(qt_win95Name(dPath)) >= 0 ) { char tmp[PATH_MAX]; if ( QT_GETCWD( tmp, PATH_MAX ) ) r = QString::fromLocal8Bit( tmp ); } QT_CHDIR( cur ); } ); slashify( r ); return r;}/*! Creates a directory. If \a acceptAbsPath is TRUE a path starting with a separator ('/') will create the absolute directory; if \a acceptAbsPath is FALSE any number of separators at the beginning of \a dirName will be removed. Returns TRUE if successful; otherwise returns FALSE. \sa rmdir()*/bool QDir::mkdir( const QString &dirName, bool acceptAbsPath ) const{ QT_WA( { return ::_wmkdir( (TCHAR*)filePath(dirName,acceptAbsPath).ucs2() ) == 0; }, { return _mkdir(qt_win95Name(filePath(dirName,acceptAbsPath))) == 0; } );}/*! Removes a directory. If \a acceptAbsPath is TRUE a path starting with a separator ('/') will remove the absolute directory; if \a acceptAbsPath is FALSE any number of separators at the beginning of \a dirName will be removed. The directory must be empty for rmdir() to succeed. Returns TRUE if successful; otherwise returns FALSE. \sa mkdir()*/bool QDir::rmdir( const QString &dirName, bool acceptAbsPath ) const{ QT_WA( { return ::_wrmdir( (TCHAR*)filePath(dirName,acceptAbsPath).ucs2() ) == 0; } , { return _rmdir(qt_win95Name(filePath(dirName,acceptAbsPath))) == 0; } );}/*! Returns TRUE if the directory is readable \e and we can open files by name; otherwise returns FALSE. \warning A FALSE value from this function is not a guarantee that files in the directory are not accessible. \sa QFileInfo::isReadable()*/bool QDir::isReadable() const{ QT_WA( { return ::_waccess( (TCHAR*)dPath.ucs2(), R_OK ) == 0; } , { return QT_ACCESS(qt_win95Name(dPath), R_OK) == 0; } );}/*! Returns TRUE if the directory is the root directory; otherwise returns FALSE. Note: If the directory is a symbolic link to the root directory this function returns FALSE. If you want to test for this use canonicalPath(), e.g. \code QDir d( "/tmp/root_link" ); d = d.canonicalPath(); if ( d.isRoot() ) qWarning( "It is a root link" ); \endcode \sa root(), rootDirPath()*/bool QDir::isRoot() const{ return dPath == "/" || dPath == "//" || (dPath[0].isLetter() && dPath.mid(1,dPath.length()) == ":/");}/*! Renames a file or directory. If \a acceptAbsPaths is TRUE a path starting with a separator ('/') will rename the file with the absolute path; if \a acceptAbsPaths is FALSE any number of separators at the beginning of the names will be removed. Returns TRUE if successful; otherwise returns FALSE. On most file systems, rename() fails only if \a oldName does not exist or if \a newName and \a oldName are not on the same partition. On Windows, rename() will fail if \a newName 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, bool acceptAbsPaths ){ if ( oldName.isEmpty() || newName.isEmpty() ) {#if defined(QT_CHECK_NULL) qWarning( "QDir::rename: Empty or null file name" );#endif return FALSE; } QString fn1 = filePath( oldName, acceptAbsPaths ); QString fn2 = filePath( newName, acceptAbsPaths ); QT_WA( { return ::_wrename( (TCHAR*)fn1.ucs2(), (TCHAR*)fn2.ucs2() ) == 0; } , { return ::rename(qt_win95Name(fn1), qt_win95Name(fn2)) == 0; } );}/*! Sets the application's current working directory to \a path. Returns TRUE if the directory was successfully changed; otherwise returns FALSE.*/bool QDir::setCurrent( const QString &path ){ int r; QT_WA( { r = ::_wchdir( (TCHAR*)path.ucs2() ); } , { r = QT_CHDIR(qt_win95Name(path)); } ); return r >= 0;}/*! Returns the absolute path of the application's current directory. \sa current()*/QString QDir::currentDirPath(){ QString result; QT_WA( { TCHAR currentName[PATH_MAX]; if ( ::_wgetcwd(currentName,PATH_MAX) != 0 ) { result = QString::fromUcs2( (ushort*)currentName ); } } , { char currentName[PATH_MAX]; if ( QT_GETCWD(currentName,PATH_MAX) != 0 ) { result = QString::fromLocal8Bit(currentName); } } );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -