qdir_unix.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 **
4 ** Implementation of QDirclass
5 **
6 ** Created : 950628
7 **
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9 **
10 ** This file is part of the tools module of the Qt GUI Toolkit.
11 **
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
15 **
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
20 **
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses for Unix/X11 or for Qt/Embedded may use this file in accordance
23 ** with the Qt Commercial License Agreement provided with the Software.
24 **
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 **
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
32 **
33 ** Contact info@trolltech.com if any conditions of this licensing are
34 ** not clear to you.
35 **
36 **********************************************************************/
37 
38 #include "qglobal.h"
39 
40 #include "qdir.h"
41 #ifndef QT_NO_DIR
42 
43 #include "qfileinfo.h"
44 #include "qfiledefs_p.h"
45 #include "qregexp.h"
46 #include "qstringlist.h"
47 #include <stdlib.h>
48 #include <ctype.h>
49 
51 
52 extern int qt_cmp_si_sortSpec;
53 
54 #if defined(Q_C_CALLBACKS)
55 extern "C" {
56 #endif
57 
58 extern int qt_cmp_si( const void *, const void * );
59 
60 #if defined(Q_C_CALLBACKS)
61 }
62 #endif
63 
64 
66 {
67 }
68 
70 {
71  QString d;
72  d = QFile::decodeName(getenv("HOME"));
73  slashify( d );
74  if ( d.isNull() )
75  d = rootDirPath();
76  return d;
77 }
78 
80 {
81  QString r;
82 
83  char cur[PATH_MAX];
84  char tmp[PATH_MAX];
85  if (GETCWD( cur, PATH_MAX )) {
86  if ( CHDIR(QFile::encodeName(dPath)) >= 0 ) {
87  if (GETCWD( tmp, PATH_MAX )) {
88  r = QFile::decodeName(tmp);
89  }
90  (void)CHDIR( cur );
91  }
92  }
93 
94  slashify( r );
95  return r;
96 }
97 
98 bool QDir::mkdir( const QString &dirName, bool acceptAbsPath ) const
99 {
100  return MKDIR( QFile::encodeName(filePath(dirName,acceptAbsPath)), 0777 )
101  == 0;
102 }
103 
104 bool QDir::rmdir( const QString &dirName, bool acceptAbsPath ) const
105 {
106  return RMDIR( QFile::encodeName(filePath(dirName,acceptAbsPath)) ) == 0;
107 }
108 
109 bool QDir::isReadable() const
110 {
111  return ACCESS( QFile::encodeName(dPath), R_OK | X_OK ) == 0;
112 }
113 
114 bool QDir::isRoot() const
115 {
116  return dPath == QString::fromLatin1("/");
117 }
118 
119 bool QDir::rename( const QString &name, const QString &newName,
120  bool acceptAbsPaths )
121 {
122  if ( name.isEmpty() || newName.isEmpty() ) {
123 #if defined(CHECK_NULL)
124  qWarning( "QDir::rename: Empty or null file name(s)" );
125 #endif
126  return FALSE;
127  }
128  QString fn1 = filePath( name, acceptAbsPaths );
129  QString fn2 = filePath( newName, acceptAbsPaths );
131  QFile::encodeName(fn2) ) == 0;
132 }
133 
135 {
136  int r;
137  r = CHDIR( QFile::encodeName(path) );
138  return r >= 0;
139 }
140 
142 {
143  QString result;
144 
145  STATBUF st;
146  if ( STAT( ".", &st ) == 0 ) {
147  char currentName[PATH_MAX];
148  if ( GETCWD( currentName, PATH_MAX ) != 0 )
149  result = QFile::decodeName(currentName);
150 #if defined(DEBUG)
151  if ( result.isNull() )
152  qWarning( "QDir::currentDirPath: getcwd() failed" );
153 #endif
154  } else {
155 #if defined(DEBUG)
156  qWarning( "QDir::currentDirPath: stat(\".\") failed" );
157 #endif
158  }
159  slashify( result );
160  return result;
161 }
162 
164 {
165  QString d = QString::fromLatin1( "/" );
166  return d;
167 }
168 
170 {
171  int len = path.length();
172  if ( len == 0 )
173  return TRUE;
174  return path[0] != '/';
175 }
176 
178  int filterSpec, int sortSpec )
179 {
180  int i;
181  if ( !fList ) {
182  fList = new QStringList;
183  CHECK_PTR( fList );
184  fiList = new QFileInfoList;
185  CHECK_PTR( fiList );
187  } else {
188  fList->clear();
189  fiList->clear();
190  }
191 
192  QStringList filters = qt_makeFilterList( nameFilter );
193 
194  bool doDirs = (filterSpec & Dirs) != 0;
195  bool doFiles = (filterSpec & Files) != 0;
196  bool noSymLinks = (filterSpec & NoSymLinks) != 0;
197  bool doReadable = (filterSpec & Readable) != 0;
198  bool doWritable = (filterSpec & Writable) != 0;
199  bool doExecable = (filterSpec & Executable) != 0;
200  bool doHidden = (filterSpec & Hidden) != 0;
201 
202 #if defined(_OS_OS2EMX_)
203  //QRegExp wc( nameFilter, FALSE, TRUE ); // wild card, case insensitive
204 #else
205  //QRegExp wc( nameFilter, TRUE, TRUE ); // wild card, case sensitive
206 #endif
207  QFileInfo fi;
208  DIR *dir;
209  dirent *file;
210 
211  dir = opendir( QFile::encodeName(dPath) );
212  if ( !dir ) {
213 #if defined(CHECK_NULL)
214  qWarning( "QDir::readDirEntries: Cannot read the directory: %s",
216 #endif
217  return FALSE;
218  }
219 
220  while ( (file = readdir(dir)) ) {
221  QString fn = QFile::decodeName(file->d_name);
222  fi.setFile( *this, fn );
223  if ( !match( filters, fn ) && !(allDirs && fi.isDir()) )
224  continue;
225  if ( (doDirs && fi.isDir()) || (doFiles && fi.isFile()) ) {
226  if ( noSymLinks && fi.isSymLink() )
227  continue;
228  if ( (filterSpec & RWEMask) != 0 )
229  if ( (doReadable && !fi.isReadable()) ||
230  (doWritable && !fi.isWritable()) ||
231  (doExecable && !fi.isExecutable()) )
232  continue;
233  if ( !doHidden && fn[0] == '.' &&
234  fn != QString::fromLatin1(".")
235  && fn != QString::fromLatin1("..") )
236  continue;
237  fiList->append( new QFileInfo( fi ) );
238  }
239  }
240  if ( closedir(dir) != 0 ) {
241 #if defined(CHECK_NULL)
242  qWarning( "QDir::readDirEntries: Cannot close the directory: %s",
243  dPath.local8Bit().data() );
244 #endif
245  }
246 
247  // Sort...
248  if(fiList->count()) {
249  QDirSortItem* si= new QDirSortItem[fiList->count()];
250  QFileInfo* itm;
251  i=0;
252  for (itm = fiList->first(); itm; itm = fiList->next())
253  si[i++].item = itm;
254  qt_cmp_si_sortSpec = sortSpec;
255  qsort( si, i, sizeof(si[0]), qt_cmp_si );
256  // put them back in the list
258  fiList->clear();
259  int j;
260  for ( j=0; j<i; j++ ) {
261  fiList->append( si[j].item );
262  fList->append( si[j].item->fileName() );
263  }
264  delete [] si;
266  }
267 
268  if ( filterSpec == (FilterSpec)filtS && sortSpec == (SortSpec)sortS &&
269  nameFilter == nameFilt )
270  dirty = FALSE;
271  else
272  dirty = TRUE;
273  return TRUE;
274 }
275 
277 {
278  // at most one instance of QFileInfoList is leaked, and this variable
279  // points to that list
280  static QFileInfoList * knownMemoryLeak = 0;
281 
282  if ( !knownMemoryLeak ) {
283  knownMemoryLeak = new QFileInfoList;
284  // non-win32 versions both use just one root directory
285  knownMemoryLeak->append( new QFileInfo( rootDirPath() ) );
286  }
287 
288  return knownMemoryLeak;
289 }
290 #endif //QT_NO_DIR
static QCString name
Definition: declinfo.cpp:673
QString nameFilt
Definition: qdir.h:190
static void slashify(QString &)
Definition: qdir_unix.cpp:65
QString nameFilter() const
Definition: qdir.h:203
static bool match(const QStringList &filters, const QString &fileName)
Definition: qdir.cpp:1036
bool isEmpty() const
Definition: qstring.h:682
#define STATBUF
Definition: qfiledefs_p.h:199
Iterator append(const T &x)
Definition: qvaluelist.h:372
static QCString result
#define CHDIR
Definition: qfiledefs_p.h:222
static QString homeDirPath()
Definition: qdir_unix.cpp:69
SortSpec sortS
Definition: qdir.h:192
static const QFileInfoList * drives()
Definition: qdir_unix.cpp:276
virtual QString dirName() const
Definition: qdir.cpp:300
#define PATH_MAX
Definition: qfiledefs_p.h:92
FilterSpec filtS
Definition: qdir.h:191
#define RMDIR
Definition: qfiledefs_p.h:225
int qt_cmp_si(const void *, const void *)
Definition: qdir.cpp:1145
#define STAT
Definition: qfiledefs_p.h:201
type * first()
Definition: qinternallist.h:87
SortSpec
Definition: qdir.h:74
void clear()
Definition: qvaluelist.h:396
const bool FALSE
Definition: qglobal.h:370
QString dPath
Definition: qdir.h:187
static bool isRelativePath(const QString &path)
Definition: qdir_unix.cpp:169
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
virtual bool isRoot() const
Definition: qdir_unix.cpp:114
string dir
static QString fromLatin1(const char *, int len=-1)
Definition: qstring.cpp:14539
bool isFile() const
QCString local8Bit() const
Definition: qstring.cpp:14569
bool isSymLink() const
QInternalList< QFileInfo > QFileInfoList
Definition: qdir.h:47
virtual bool mkdir(const QString &dirName, bool acceptAbsPath=TRUE) const
Definition: qdir_unix.cpp:98
QAsciiDict< Entry > fn
A list of strings.
Definition: qstringlist.h:51
void setFile(const QString &file)
Definition: qfileinfo.cpp:219
virtual bool rename(const QString &name, const QString &newName, bool acceptAbsPaths=TRUE)
Definition: qdir_unix.cpp:119
virtual bool readDirEntries(const QString &nameFilter, int FilterSpec, int SortSpec)
Definition: qdir_unix.cpp:177
uint length() const
Definition: qstring.h:679
std::string getenv(std::string const &name)
Definition: getenv.cc:15
static QString currentDirPath()
Definition: qdir_unix.cpp:141
static QString rootDirPath()
Definition: qdir_unix.cpp:163
#define MKDIR
Definition: qfiledefs_p.h:224
#define GETCWD
Definition: qfiledefs_p.h:221
void append(const type *d)
Definition: qinternallist.h:61
virtual QString canonicalPath() const
Definition: qdir_unix.cpp:79
const char * data() const
Definition: qcstring.h:207
string tmp
Definition: languages.py:63
static QCString encodeName(const QString &fileName)
Definition: qfile.cpp:494
type * next()
Definition: qinternallist.h:89
static QString decodeName(const QCString &localFileName)
Definition: qfile.cpp:529
bool isWritable() const
Definition: qfileinfo.cpp:415
void setAutoDelete(bool enable)
Definition: qcollection.h:55
QStringList * fList
Definition: qdir.h:188
#define CHECK_PTR(p)
Definition: qglobal.h:601
virtual bool rmdir(const QString &dirName, bool acceptAbsPath=TRUE) const
Definition: qdir_unix.cpp:104
int qt_cmp_si_sortSpec
Definition: qdir.cpp:1139
QStringList qt_makeFilterList(const QString &filter)
Definition: qdir.cpp:1010
virtual QString path() const
Definition: qdir.h:198
uint allDirs
Definition: qdir.h:194
bool isNull() const
Definition: qstring.h:379
virtual QString filePath(const QString &fileName, bool acceptAbsPath=TRUE) const
Definition: qdir.cpp:323
bool isExecutable() const
Definition: qfileinfo.cpp:425
uint dirty
Definition: qdir.h:193
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:51
uint count() const
Definition: qinternallist.h:56
#define ACCESS
Definition: qfiledefs_p.h:216
FilterSpec
Definition: qdir.h:55
def filters(nticks=9600, tick=0.5 *units.us, npitches=3000, pitch=1.0)
Definition: __init__.py:555
bool isDir() const
const bool TRUE
Definition: qglobal.h:371
QFileInfoList * fiList
Definition: qdir.h:189
static bool setCurrent(const QString &path)
Definition: qdir_unix.cpp:134
virtual bool isReadable() const
Definition: qdir_unix.cpp:109
bool isReadable() const
Definition: qfileinfo.cpp:405
def rename(src, dest)