qdir_win32.cpp 11.9 KB
Newer Older
1 2
/******************************************************************************
 *
3
 * 
4
 *
5
 * Copyright (C) 1997-2001 by Dimitri van Heesch.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 * Based on qdir_unix.cpp 
 *
 * Copyright (C) 1992-2000 Trolltech AS.
 */


#include "qglobal.h"

#include "qdir.h"
#ifndef QT_NO_DIR


#include "qfileinfo.h"
#include "qfiledefs_p.h"
#include "qregexp.h"
#include "qstringlist.h"
#include <stdlib.h>
#include <ctype.h>
#if defined(_OS_WIN32_)
#if defined(_CC_BOOL_DEF_)
#undef  bool
#include <windows.h>
#define bool int
#else
#include <windows.h>
#endif
#endif
#if defined(_OS_OS2EMX_)
extern Q_UINT32 DosQueryCurrentDisk(Q_UINT32*,Q_UINT32*);
#define NO_ERROR 0
#endif

extern QStringList qt_makeFilterList( const QString &filter );

extern int qt_cmp_si_sortSpec;

#if defined(Q_C_CALLBACKS)
extern "C" {
#endif

extern int qt_cmp_si( const void *, const void * );

#if defined(Q_C_CALLBACKS)
}
#endif

Dimitri van Heesch's avatar
Dimitri van Heesch committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static QString p_getenv( QString name )
{
  DWORD len = GetEnvironmentVariableW( ( LPCWSTR ) qt_winTchar ( name, TRUE ), NULL, 0 );
  if ( len == 0 )
    return QString::null;
  /* ansi: we allocate too much memory, but this shouldn't be the problem here ... */
  LPWSTR buf = (LPWSTR)new WCHAR[ len ];
  len = GetEnvironmentVariableW ( ( LPCWSTR ) qt_winTchar ( name, TRUE ), buf, len );
  if ( len == 0 )
  {
    delete[] buf;
    return QString::null;
  }
  QString ret = qt_winQString ( buf );
  delete[] buf;
  return ret;
}

80 81 82 83 84 85 86 87 88 89 90 91

void QDir::slashify( QString& n )
{
  for ( int i=0; i<(int)n.length(); i++ ) 
  {
     if ( n[i] == '\\' )
          n[i] = '/';
  }
}

QString QDir::homeDirPath()
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  QString d = p_getenv ( "HOME" );
  if ( d.isNull () ) {
    d = p_getenv ( "USERPROFILE" );
    if ( d.isNull () ) {
      QString homeDrive = p_getenv ( "HOMEDRIVE" );
      QString homePath = p_getenv ( "HOMEPATH" );
      if ( !homeDrive.isNull () && !homePath.isNull () ) {
        d = homeDrive + homePath;
      } else {
        d = rootDirPath ();
      }
    }
  }
  slashify( d );
  return d;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

QString QDir::canonicalPath() const
{
    QString r;

    char cur[PATH_MAX];
    char tmp[PATH_MAX];
    GETCWD( cur, PATH_MAX );
    if ( CHDIR(QFile::encodeName(dPath)) >= 0 ) {
	GETCWD( tmp, PATH_MAX );
	r = QFile::decodeName(tmp);
    }
    CHDIR( cur );

    slashify( r );
    return r;
}

bool QDir::mkdir( const QString &dirName, bool acceptAbsPath ) const
{
#if defined(__CYGWIN32_)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
129
    return MKDIR( QFile::encodeName(filePath(dirName,acceptAbsPath)), 0777 ) == 0;
130
#else
Dimitri van Heesch's avatar
Dimitri van Heesch committed
131
    return _wmkdir( ( LPCWSTR ) filePath( dirName, acceptAbsPath ).ucs2() ) == 0;
132 133 134 135 136
#endif
}

bool QDir::rmdir( const QString &dirName, bool acceptAbsPath ) const
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
137
#if defined(__CYGWIN32_)
138
    return RMDIR( QFile::encodeName(filePath(dirName,acceptAbsPath)) ) == 0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
139 140 141
#else
    return _wrmdir( ( LPCWSTR ) filePath( dirName, acceptAbsPath ).ucs2() ) == 0;
#endif
142 143 144 145
}

bool QDir::isReadable() const
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
146 147 148 149
    QString path = dPath;
    if ( ( path[ 0 ] == '\\' ) || ( path[ 0 ] == '/' ) )
        path = rootDirPath() + path;
#if defined(__CYGWIN32_)
150
    return ACCESS( QFile::encodeName(dPath), R_OK ) == 0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
151 152 153
#else
    return ( _waccess( (wchar_t*) path.ucs2(), R_OK ) == 0 );
#endif
154 155 156 157
}

bool QDir::isRoot() const
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
158 159 160
    QString path = dPath;
    slashify( path );
    return path == rootDirPath ();
161 162 163 164 165 166 167 168 169 170 171 172 173
}

bool QDir::rename( const QString &name, const QString &newName,
		   bool acceptAbsPaths	)
{
    if ( name.isEmpty() || newName.isEmpty() ) {
#if defined(CHECK_NULL)
	qWarning( "QDir::rename: Empty or null file name(s)" );
#endif
	return FALSE;
    }
    QString fn1 = filePath( name, acceptAbsPaths );
    QString fn2 = filePath( newName, acceptAbsPaths );
Dimitri van Heesch's avatar
Dimitri van Heesch committed
174
#if defined(__CYGWIN32_)
175 176
    return ::rename( QFile::encodeName(fn1),
		     QFile::encodeName(fn2) ) == 0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
177 178 179
#else
    return MoveFileW( ( LPCWSTR ) fn1.ucs2(), ( LPCWSTR ) fn2.ucs2() ) != 0;    
#endif
180 181 182 183
}

bool QDir::setCurrent( const QString &path )
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
184
#if defined(__CYGWIN32_)
185 186 187
    int r;
    r = CHDIR( QFile::encodeName(path) );
    return r >= 0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
188 189 190 191 192
#else
    if ( !QDir( path ).exists() )
        return false;
    return ( SetCurrentDirectoryW( ( LPCWSTR ) path.ucs2() ) >= 0 );
#endif
193 194 195 196 197 198
}

QString QDir::currentDirPath()
{
    QString result;

Dimitri van Heesch's avatar
Dimitri van Heesch committed
199 200
#if defined(__CYGWIN32_)

201 202 203 204 205 206 207 208 209 210 211 212 213 214
    STATBUF st;
    if ( STAT( ".", &st ) == 0 ) {
	char currentName[PATH_MAX];
	if ( GETCWD( currentName, PATH_MAX ) != 0 )
	    result = QFile::decodeName(currentName);
#if defined(DEBUG)
	if ( result.isNull() )
	    qWarning( "QDir::currentDirPath: getcwd() failed" );
#endif
    } else {
#if defined(DEBUG)
	qWarning( "QDir::currentDirPath: stat(\".\") failed" );
#endif
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

#else

    DWORD size = 0;
    WCHAR currentName[ PATH_MAX ];
    size = ::GetCurrentDirectoryW( PATH_MAX, currentName );
    if ( size != 0 ) {
      if ( size > PATH_MAX ) {
        WCHAR * newCurrentName = new WCHAR[ size ];
        if ( ::GetCurrentDirectoryW( PATH_MAX, newCurrentName ) != 0 )
          result = QString::fromUcs2( ( ushort* ) newCurrentName );
        delete [] newCurrentName;
      } else {
        result = QString::fromUcs2( ( ushort* ) currentName );
      }
    }

    if ( result.length() >= 2 && result[ 1 ] == ':' )
          result[ 0 ] = result.at( 0 ).upper(); // Force uppercase drive letters.
#endif
235 236 237 238 239 240
    slashify( result );
    return result;
}

QString QDir::rootDirPath()
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
241 242 243 244 245
  QString d = p_getenv ( "SystemDrive" );
  if ( d.isNull () )
    d = QString::fromLatin1( "c:" );  // not "c:\\" !
  slashify ( d );
  return d;
246 247 248 249
}

bool QDir::isRelativePath( const QString &path )
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
250 251 252 253 254 255
  if ( path.isEmpty() )
    return TRUE;
  int p = 0;
  if ( path[ 0 ].isLetter() && path[ 1 ] == ':' )
    p = 2; // we have checked the first 2.
  return ( ( path[ p ] != '/' ) && ( path[ p ] != '\\' ) );
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
}

#undef  IS_SUBDIR
#undef  IS_RDONLY
#undef  IS_ARCH
#undef  IS_HIDDEN
#undef  IS_SYSTEM
#undef  FF_GETFIRST
#undef  FF_GETNEXT
#undef  FF_ERROR

#if defined(_OS_WIN32_)
#define IS_SUBDIR   FILE_ATTRIBUTE_DIRECTORY
#define IS_RDONLY   FILE_ATTRIBUTE_READONLY
#define IS_ARCH     FILE_ATTRIBUTE_ARCHIVE
#define IS_HIDDEN   FILE_ATTRIBUTE_HIDDEN
#define IS_SYSTEM   FILE_ATTRIBUTE_SYSTEM
#define FF_GETFIRST FindFirstFile
#define FF_GETNEXT  FindNextFile
#define FF_ERROR    INVALID_HANDLE_VALUE
#else
#define IS_SUBDIR   _A_SUBDIR
#define IS_RDONLY   _A_RDONLY
#define IS_ARCH     _A_ARCH
#define IS_HIDDEN   _A_HIDDEN
#define IS_SYSTEM   _A_SYSTEM
#define FF_GETFIRST _findfirst
#define FF_GETNEXT  _findnext
#define FF_ERROR    -1
#endif


bool QDir::readDirEntries( const QString &nameFilter,
			   int filterSpec, int sortSpec )
{
    int i;
    if ( !fList ) {
	fList  = new QStringList;
	CHECK_PTR( fList );
	fiList = new QFileInfoList;
	CHECK_PTR( fiList );
	fiList->setAutoDelete( TRUE );
    } else {
	fList->clear();
	fiList->clear();
    }

    QStringList filters = qt_makeFilterList( nameFilter );

    bool doDirs	    = (filterSpec & Dirs)	!= 0;
    bool doFiles    = (filterSpec & Files)	!= 0;
    bool noSymLinks = (filterSpec & NoSymLinks) != 0;
    bool doReadable = (filterSpec & Readable)	!= 0;
    bool doWritable = (filterSpec & Writable)	!= 0;
    bool doExecable = (filterSpec & Executable) != 0;
    bool doHidden   = (filterSpec & Hidden)	!= 0;
    // show hidden files if the user asks explicitly for e.g. .*
    if ( !doHidden && !nameFilter.isEmpty() && nameFilter[0] == '.' )
         doHidden = TRUE;
    bool doModified = (filterSpec & Modified)   != 0;
    bool doSystem   = (filterSpec & System)     != 0;

318
    QRegExp   wc( nameFilter.data(), FALSE, TRUE );    // wild card, case insensitive
319 320 321 322 323
    bool      first = TRUE;
    QString   p = dPath.copy();
    int       plen = p.length();
#if defined(_OS_WIN32_)
    HANDLE    ff;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
324
    WIN32_FIND_DATAW finfo;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
#else
    long      ff;
    _finddata_t finfo;
#endif
    QFileInfo fi;
    if ( plen == 0 )
    {
#if defined(CHECK_NULL)
      warning( "QDir::readDirEntries: No directory name specified" );
#endif
      return FALSE;
    }
    if ( p.at(plen-1) != '/' && p.at(plen-1) != '\\' )
          p += '/';
    p += "*.*";

Dimitri van Heesch's avatar
Dimitri van Heesch committed
341
#if defined(__CYGWIN32_)
342
    ff = FF_GETFIRST( p.data(), &finfo );
Dimitri van Heesch's avatar
Dimitri van Heesch committed
343 344 345
#else
    ff = FindFirstFileW ( ( LPCWSTR ) p.ucs2(), &finfo );
#endif
346 347 348 349
    if ( ff == FF_ERROR ) 
    {
#if defined(DEBUG)
      warning( "QDir::readDirEntries: Cannot read the directory: %s",
350
                           (const char *)dPath.utf8() );
351 352 353 354 355 356 357 358 359 360
#endif
    return FALSE;
    }
    
    while ( TRUE ) 
    {
	if ( first )
	    first = FALSE;
	else 
        {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
361
#if defined(__CYGWIN32_)
362 363
	    if ( FF_GETNEXT(ff,&finfo) == -1 )
		break;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
364 365 366 367 368
#else
	    //if ( !FF_GETNEXT(ff,&finfo) )
	    //	break;
            if (!FindNextFileW(ff, &finfo ))
                break;
369 370
#endif
	}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
371
#if defined(__CYGWIN32_)
372
	int  attrib = finfo.attrib;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
373 374
#else
	int  attrib = finfo.dwFileAttributes;
375 376 377 378 379 380 381 382 383 384 385
#endif
	bool isDir	= (attrib & IS_SUBDIR) != 0;
	bool isFile	= !isDir;
	bool isSymLink	= FALSE;
	bool isReadable = TRUE;
	bool isWritable = (attrib & IS_RDONLY) == 0;
	bool isExecable = FALSE;
	bool isModified = (attrib & IS_ARCH)   != 0;
	bool isHidden	= (attrib & IS_HIDDEN) != 0;
	bool isSystem	= (attrib & IS_SYSTEM) != 0;

Dimitri van Heesch's avatar
Dimitri van Heesch committed
386
#if defined(__CYGWIN32_)
387
	const char *fname = finfo.name;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
388 389 390
#else
	//const char *fname = finfo.cFileName;
        QString fname = QString::fromUcs2( ( const unsigned short* ) finfo.cFileName);
391
#endif
Dimitri van Heesch's avatar
Dimitri van Heesch committed
392
	if ( wc.match(fname.utf8()) == -1 && !(allDirs && isDir) )
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	    continue;

	QString name = fname;
	if ( doExecable ) 
        {
	    QString ext = name.right(4).lower();
	    if ( ext == ".exe" || ext == ".com" || ext == ".bat" ||
		 ext == ".pif" || ext == ".cmd" )
		isExecable = TRUE;
	}

	if  ( (doDirs && isDir) || (doFiles && isFile) ) 
        {
	    if ( noSymLinks && isSymLink )
		continue;
	    if ( (filterSpec & RWEMask) != 0 )
		if ( (doReadable && !isReadable) ||
		     (doWritable && !isWritable) ||
		     (doExecable && !isExecable) )
		    continue;
	    if ( doModified && !isModified )
		continue;
	    if ( !doHidden && isHidden )
		continue;
	    if ( !doSystem && isSystem )
		continue;
	    fi.setFile( *this, name );
	    fiList->append( new QFileInfo( fi ) );
	}
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
423
#if defined(__CYGWIN32_)
424
    _findclose( ff );
Dimitri van Heesch's avatar
Dimitri van Heesch committed
425 426
#else
    FindClose( ff );
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
#endif

    // Sort...
    QDirSortItem* si= new QDirSortItem[fiList->count()];
    QFileInfo* itm;
    i=0;
    for (itm = fiList->first(); itm; itm = fiList->next())
	si[i++].item = itm;
    qt_cmp_si_sortSpec = sortSpec;
    qsort( si, i, sizeof(si[0]), qt_cmp_si );
    // put them back in the list
    fiList->setAutoDelete( FALSE );
    fiList->clear();
    int j;
    for ( j=0; j<i; j++ ) {
	fiList->append( si[j].item );
	fList->append( si[j].item->fileName() );
    }
    delete [] si;
    fiList->setAutoDelete( TRUE );

    if ( filterSpec == (FilterSpec)filtS && sortSpec == (SortSpec)sortS &&
	 nameFilter == nameFilt )
	dirty = FALSE;
    else
	dirty = TRUE;
    return TRUE;
}

const QFileInfoList * QDir::drives()
{
    // at most one instance of QFileInfoList is leaked, and this variable
    // points to that list
    static QFileInfoList * knownMemoryLeak = 0;

    if ( !knownMemoryLeak ) {
	knownMemoryLeak = new QFileInfoList;

#if defined(_OS_WIN32_)
	Q_UINT32 driveBits = (Q_UINT32) GetLogicalDrives() & 0x3ffffff;
#elif defined(_OS_OS2EMX_)
	Q_UINT32 driveBits, cur;
	if (DosQueryCurrentDisk(&cur,&driveBits) != NO_ERROR)
	    exit(1);
	driveBits &= 0x3ffffff;
#endif
	char driveName[4];
	qstrcpy( driveName, "a:/" );
	while( driveBits ) {
	    if ( driveBits & 1 )
		knownMemoryLeak->append( new QFileInfo( driveName ) );
	    driveName[0]++;
	    driveBits = driveBits >> 1;
	}
    }

    return knownMemoryLeak;
}
#endif //QT_NO_DIR