author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Mon, 19 Apr 2010 10:15:19 +0300 | |
branch | RCL_3 |
changeset 9 | b5b118452c7d |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qplatformdefs.h" |
|
43 |
#include "qdir.h" |
|
44 |
#include "qabstractfileengine.h" |
|
45 |
#ifndef QT_NO_DEBUG_STREAM |
|
46 |
#include "qdebug.h" |
|
47 |
#endif |
|
48 |
#include "qdiriterator.h" |
|
49 |
#include "qfsfileengine.h" |
|
50 |
#include "qdatetime.h" |
|
51 |
#include "qstring.h" |
|
52 |
#include "qregexp.h" |
|
53 |
#include "qvector.h" |
|
54 |
#include "qalgorithms.h" |
|
55 |
#ifdef QT_BUILD_CORE_LIB |
|
56 |
# include "qresource.h" |
|
57 |
#endif |
|
58 |
||
59 |
#include "qvarlengtharray.h" |
|
60 |
||
61 |
#include "private/qcoreglobaldata_p.h" |
|
62 |
#include <stdlib.h> |
|
63 |
||
64 |
QT_BEGIN_NAMESPACE |
|
65 |
||
66 |
static QString driveSpec(const QString &path) |
|
67 |
{ |
|
68 |
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) |
|
69 |
if (path.size() < 2) |
|
70 |
return QString(); |
|
71 |
char c = path.at(0).toAscii(); |
|
72 |
if (c < 'a' && c > 'z' && c < 'A' && c > 'Z') |
|
73 |
return QString(); |
|
74 |
if (path.at(1).toAscii() != ':') |
|
75 |
return QString(); |
|
76 |
return path.mid(0, 2); |
|
77 |
#else |
|
78 |
Q_UNUSED(path); |
|
79 |
return QString(); |
|
80 |
#endif |
|
81 |
} |
|
82 |
||
83 |
//************* QDirPrivate |
|
84 |
class QDirPrivate |
|
85 |
{ |
|
86 |
QDir *q_ptr; |
|
87 |
Q_DECLARE_PUBLIC(QDir) |
|
88 |
||
89 |
friend struct QScopedPointerDeleter<QDirPrivate>; |
|
90 |
protected: |
|
91 |
QDirPrivate(QDir*, const QDir *copy=0); |
|
92 |
~QDirPrivate(); |
|
93 |
||
94 |
QString initFileEngine(const QString &file); |
|
95 |
||
96 |
void updateFileLists() const; |
|
97 |
void sortFileList(QDir::SortFlags, QFileInfoList &, QStringList *, QFileInfoList *) const; |
|
98 |
||
99 |
private: |
|
100 |
#ifdef QT3_SUPPORT |
|
101 |
QChar filterSepChar; |
|
102 |
bool matchAllDirs; |
|
103 |
#endif |
|
104 |
static inline QChar getFilterSepChar(const QString &nameFilter) |
|
105 |
{ |
|
106 |
QChar sep(QLatin1Char(';')); |
|
107 |
int i = nameFilter.indexOf(sep, 0); |
|
108 |
if (i == -1 && nameFilter.indexOf(QLatin1Char(' '), 0) != -1) |
|
109 |
sep = QChar(QLatin1Char(' ')); |
|
110 |
return sep; |
|
111 |
} |
|
112 |
static inline QStringList splitFilters(const QString &nameFilter, QChar sep=0) { |
|
113 |
if(sep == 0) |
|
114 |
sep = getFilterSepChar(nameFilter); |
|
115 |
QStringList ret = nameFilter.split(sep); |
|
116 |
for(int i = 0; i < ret.count(); i++) |
|
117 |
ret[i] = ret[i].trimmed(); |
|
118 |
return ret; |
|
119 |
} |
|
120 |
||
121 |
struct Data { |
|
122 |
inline Data() |
|
123 |
: ref(1), fileEngine(0) |
|
124 |
{ clear(); } |
|
125 |
inline Data(const Data ©) |
|
126 |
: ref(1), path(copy.path), nameFilters(copy.nameFilters), sort(copy.sort), |
|
127 |
filters(copy.filters), fileEngine(0) |
|
128 |
{ clear(); } |
|
129 |
inline ~Data() |
|
130 |
{ delete fileEngine; } |
|
131 |
||
132 |
inline void clear() { |
|
133 |
listsDirty = 1; |
|
134 |
} |
|
135 |
mutable QAtomicInt ref; |
|
136 |
||
137 |
QString path; |
|
138 |
QStringList nameFilters; |
|
139 |
QDir::SortFlags sort; |
|
140 |
QDir::Filters filters; |
|
141 |
||
142 |
mutable QAbstractFileEngine *fileEngine; |
|
143 |
||
144 |
mutable uint listsDirty : 1; |
|
145 |
mutable QStringList files; |
|
146 |
mutable QFileInfoList fileInfos; |
|
147 |
} *data; |
|
148 |
inline void setPath(const QString &p) |
|
149 |
{ |
|
150 |
detach(false); |
|
151 |
QString path = p; |
|
152 |
if ((path.endsWith(QLatin1Char('/')) || path.endsWith(QLatin1Char('\\'))) |
|
153 |
&& path.length() > 1) { |
|
154 |
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) |
|
155 |
if (!(path.length() == 3 && path.at(1) == QLatin1Char(':'))) |
|
156 |
#endif |
|
157 |
path.truncate(path.length() - 1); |
|
158 |
} |
|
159 |
if(!data->fileEngine || !QDir::isRelativePath(path)) |
|
160 |
path = initFileEngine(path); |
|
161 |
data->fileEngine->setFileName(path); |
|
162 |
// set the path to be the qt friendly version so then we can operate on it using just / |
|
163 |
data->path = data->fileEngine->fileName(QAbstractFileEngine::DefaultName); |
|
164 |
data->clear(); |
|
165 |
} |
|
166 |
inline void reset() { |
|
167 |
detach(); |
|
168 |
data->clear(); |
|
169 |
} |
|
170 |
void detach(bool createFileEngine = true); |
|
171 |
}; |
|
172 |
||
173 |
QDirPrivate::QDirPrivate(QDir *qq, const QDir *copy) : q_ptr(qq) |
|
174 |
#ifdef QT3_SUPPORT |
|
175 |
, filterSepChar(0) |
|
176 |
, matchAllDirs(false) |
|
177 |
#endif |
|
178 |
{ |
|
179 |
if(copy) { |
|
180 |
copy->d_func()->data->ref.ref(); |
|
181 |
data = copy->d_func()->data; |
|
182 |
} else { |
|
183 |
data = new QDirPrivate::Data; |
|
184 |
data->clear(); |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
QDirPrivate::~QDirPrivate() |
|
189 |
{ |
|
190 |
if (!data->ref.deref()) |
|
191 |
delete data; |
|
192 |
data = 0; |
|
193 |
q_ptr = 0; |
|
194 |
} |
|
195 |
||
196 |
/* For sorting */ |
|
197 |
struct QDirSortItem { |
|
198 |
mutable QString filename_cache; |
|
199 |
mutable QString suffix_cache; |
|
200 |
QFileInfo item; |
|
201 |
}; |
|
202 |
||
203 |
||
204 |
class QDirSortItemComparator { |
|
205 |
int qt_cmp_si_sort_flags; |
|
206 |
public: |
|
207 |
QDirSortItemComparator(int flags) : qt_cmp_si_sort_flags(flags) {} |
|
208 |
bool operator()(const QDirSortItem &, const QDirSortItem &); |
|
209 |
}; |
|
210 |
||
211 |
bool QDirSortItemComparator::operator()(const QDirSortItem &n1, const QDirSortItem &n2) |
|
212 |
{ |
|
213 |
const QDirSortItem* f1 = &n1; |
|
214 |
const QDirSortItem* f2 = &n2; |
|
215 |
||
216 |
if ((qt_cmp_si_sort_flags & QDir::DirsFirst) && (f1->item.isDir() != f2->item.isDir())) |
|
217 |
return f1->item.isDir(); |
|
218 |
if ((qt_cmp_si_sort_flags & QDir::DirsLast) && (f1->item.isDir() != f2->item.isDir())) |
|
219 |
return !f1->item.isDir(); |
|
220 |
||
221 |
int r = 0; |
|
222 |
int sortBy = (qt_cmp_si_sort_flags & QDir::SortByMask) |
|
223 |
| (qt_cmp_si_sort_flags & QDir::Type); |
|
224 |
||
225 |
switch (sortBy) { |
|
226 |
case QDir::Time: |
|
227 |
r = f1->item.lastModified().secsTo(f2->item.lastModified()); |
|
228 |
break; |
|
229 |
case QDir::Size: |
|
230 |
r = int(qBound<qint64>(-1, f2->item.size() - f1->item.size(), 1)); |
|
231 |
break; |
|
232 |
case QDir::Type: |
|
233 |
{ |
|
234 |
bool ic = qt_cmp_si_sort_flags & QDir::IgnoreCase; |
|
235 |
||
236 |
if (f1->suffix_cache.isNull()) |
|
237 |
f1->suffix_cache = ic ? f1->item.suffix().toLower() |
|
238 |
: f1->item.suffix(); |
|
239 |
if (f2->suffix_cache.isNull()) |
|
240 |
f2->suffix_cache = ic ? f2->item.suffix().toLower() |
|
241 |
: f2->item.suffix(); |
|
242 |
||
243 |
r = qt_cmp_si_sort_flags & QDir::LocaleAware |
|
244 |
? f1->suffix_cache.localeAwareCompare(f2->suffix_cache) |
|
245 |
: f1->suffix_cache.compare(f2->suffix_cache); |
|
246 |
} |
|
247 |
break; |
|
248 |
default: |
|
249 |
; |
|
250 |
} |
|
251 |
||
252 |
if (r == 0 && sortBy != QDir::Unsorted) { |
|
253 |
// Still not sorted - sort by name |
|
254 |
bool ic = qt_cmp_si_sort_flags & QDir::IgnoreCase; |
|
255 |
||
256 |
if (f1->filename_cache.isNull()) |
|
257 |
f1->filename_cache = ic ? f1->item.fileName().toLower() |
|
258 |
: f1->item.fileName(); |
|
259 |
if (f2->filename_cache.isNull()) |
|
260 |
f2->filename_cache = ic ? f2->item.fileName().toLower() |
|
261 |
: f2->item.fileName(); |
|
262 |
||
263 |
r = qt_cmp_si_sort_flags & QDir::LocaleAware |
|
264 |
? f1->filename_cache.localeAwareCompare(f2->filename_cache) |
|
265 |
: f1->filename_cache.compare(f2->filename_cache); |
|
266 |
} |
|
267 |
if (r == 0) // Enforce an order - the order the items appear in the array |
|
268 |
r = (&n1) - (&n2); |
|
269 |
if (qt_cmp_si_sort_flags & QDir::Reversed) |
|
270 |
return r > 0; |
|
271 |
return r < 0; |
|
272 |
} |
|
273 |
||
274 |
inline void QDirPrivate::sortFileList(QDir::SortFlags sort, QFileInfoList &l, |
|
275 |
QStringList *names, QFileInfoList *infos) const |
|
276 |
{ |
|
277 |
if(names) |
|
278 |
names->clear(); |
|
279 |
if(infos) |
|
280 |
infos->clear(); |
|
281 |
int n = l.size(); |
|
282 |
if(n > 0) { |
|
283 |
if (n == 1 || (sort & QDir::SortByMask) == QDir::Unsorted) { |
|
284 |
if(infos) |
|
285 |
*infos = l; |
|
286 |
if(names) { |
|
287 |
for (int i = 0; i < n; ++i) |
|
288 |
names->append(l.at(i).fileName()); |
|
289 |
} |
|
290 |
} else { |
|
291 |
QScopedArrayPointer<QDirSortItem> si(new QDirSortItem[n]); |
|
292 |
for (int i = 0; i < n; ++i) |
|
293 |
si[i].item = l.at(i); |
|
294 |
qSort(si.data(), si.data()+n, QDirSortItemComparator(sort)); |
|
295 |
// put them back in the list(s) |
|
296 |
if(infos) { |
|
297 |
for (int i = 0; i < n; ++i) |
|
298 |
infos->append(si[i].item); |
|
299 |
} |
|
300 |
if(names) { |
|
301 |
for (int i = 0; i < n; ++i) |
|
302 |
names->append(si[i].item.fileName()); |
|
303 |
} |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
inline void QDirPrivate::updateFileLists() const |
|
309 |
{ |
|
310 |
if(data->listsDirty) { |
|
311 |
QFileInfoList l; |
|
312 |
QDirIterator it(data->path, data->nameFilters, data->filters); |
|
313 |
while (it.hasNext()) { |
|
314 |
it.next(); |
|
315 |
l.append(it.fileInfo()); |
|
316 |
} |
|
317 |
sortFileList(data->sort, l, &data->files, &data->fileInfos); |
|
318 |
data->listsDirty = 0; |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
QString QDirPrivate::initFileEngine(const QString &path) |
|
323 |
{ |
|
324 |
detach(false); |
|
325 |
delete data->fileEngine; |
|
326 |
data->fileEngine = 0; |
|
327 |
data->clear(); |
|
328 |
data->fileEngine = QAbstractFileEngine::create(path); |
|
329 |
return data->fileEngine->fileName(QAbstractFileEngine::DefaultName); |
|
330 |
} |
|
331 |
||
332 |
void QDirPrivate::detach(bool createFileEngine) |
|
333 |
{ |
|
334 |
qAtomicDetach(data); |
|
335 |
if (createFileEngine) { |
|
336 |
QAbstractFileEngine *newFileEngine = QAbstractFileEngine::create(data->path); |
|
337 |
delete data->fileEngine; |
|
338 |
data->fileEngine = newFileEngine; |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
/*! |
|
343 |
\class QDir |
|
344 |
\brief The QDir class provides access to directory structures and their contents. |
|
345 |
||
346 |
\ingroup io |
|
347 |
\ingroup shared |
|
348 |
\reentrant |
|
349 |
||
350 |
||
351 |
A QDir is used to manipulate path names, access information |
|
352 |
regarding paths and files, and manipulate the underlying file |
|
353 |
system. It can also be used to access Qt's \l{resource system}. |
|
354 |
||
355 |
Qt uses "/" as a universal directory separator in the same way |
|
356 |
that "/" is used as a path separator in URLs. If you always use |
|
357 |
"/" as a directory separator, Qt will translate your paths to |
|
358 |
conform to the underlying operating system. |
|
359 |
||
360 |
A QDir can point to a file using either a relative or an absolute |
|
361 |
path. Absolute paths begin with the directory separator |
|
362 |
(optionally preceded by a drive specification under Windows). |
|
363 |
Relative file names begin with a directory name or a file name and |
|
364 |
specify a path relative to the current directory. |
|
365 |
||
366 |
Examples of absolute paths: |
|
367 |
||
368 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 0 |
|
369 |
||
370 |
On Windows, the second example above will be translated to |
|
371 |
\c{C:\Documents and Settings} when used to access files. |
|
372 |
||
373 |
Examples of relative paths: |
|
374 |
||
375 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 1 |
|
376 |
||
377 |
You can use the isRelative() or isAbsolute() functions to check if |
|
378 |
a QDir is using a relative or an absolute file path. Call |
|
379 |
makeAbsolute() to convert a relative QDir to an absolute one. |
|
380 |
||
381 |
\section1 Navigation and Directory Operations |
|
382 |
||
383 |
A directory's path can be obtained with the path() function, and |
|
384 |
a new path set with the setPath() function. The absolute path to |
|
385 |
a directory is found by calling absolutePath(). |
|
386 |
||
387 |
The name of a directory is found using the dirName() function. This |
|
388 |
typically returns the last element in the absolute path that specifies |
|
389 |
the location of the directory. However, it can also return "." if |
|
390 |
the QDir represents the current directory. |
|
391 |
||
392 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 2 |
|
393 |
||
394 |
The path for a directory can also be changed with the cd() and cdUp() |
|
395 |
functions, both of which operate like familiar shell commands. |
|
396 |
When cd() is called with the name of an existing directory, the QDir |
|
397 |
object changes directory so that it represents that directory instead. |
|
398 |
The cdUp() function changes the directory of the QDir object so that |
|
399 |
it refers to its parent directory; i.e. cd("..") is equivalent to |
|
400 |
cdUp(). |
|
401 |
||
402 |
Directories can be created with mkdir(), renamed with rename(), and |
|
403 |
removed with rmdir(). |
|
404 |
||
405 |
You can test for the presence of a directory with a given name by |
|
406 |
using exists(), and the properties of a directory can be tested with |
|
407 |
isReadable(), isAbsolute(), isRelative(), and isRoot(). |
|
408 |
||
409 |
The refresh() function re-reads the directory's data from disk. |
|
410 |
||
411 |
\section1 Files and Directory Contents |
|
412 |
||
413 |
Directories contain a number of entries, representing files, |
|
414 |
directories, and symbolic links. The number of entries in a |
|
415 |
directory is returned by count(). |
|
416 |
A string list of the names of all the entries in a directory can be |
|
417 |
obtained with entryList(). If you need information about each |
|
418 |
entry, use entryInfoList() to obtain a list of QFileInfo objects. |
|
419 |
||
420 |
Paths to files and directories within a directory can be |
|
421 |
constructed using filePath() and absoluteFilePath(). |
|
422 |
The filePath() function returns a path to the specified file |
|
423 |
or directory relative to the path of the QDir object; |
|
424 |
absoluteFilePath() returns an absolute path to the specified |
|
425 |
file or directory. Neither of these functions checks for the |
|
426 |
existence of files or directory; they only construct paths. |
|
427 |
||
428 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 3 |
|
429 |
||
430 |
Files can be removed by using the remove() function. Directories |
|
431 |
cannot be removed in the same way as files; use rmdir() to remove |
|
432 |
them instead. |
|
433 |
||
434 |
It is possible to reduce the number of entries returned by |
|
435 |
entryList() and entryInfoList() by applying filters to a QDir object. |
|
436 |
You can apply a name filter to specify a pattern with wildcards that |
|
437 |
file names need to match, an attribute filter that selects properties |
|
438 |
of entries and can distinguish between files and directories, and a |
|
439 |
sort order. |
|
440 |
||
441 |
Name filters are lists of strings that are passed to setNameFilters(). |
|
442 |
Attribute filters consist of a bitwise OR combination of Filters, and |
|
443 |
these are specified when calling setFilter(). |
|
444 |
The sort order is specified using setSorting() with a bitwise OR |
|
445 |
combination of SortFlags. |
|
446 |
||
447 |
You can test to see if a filename matches a filter using the match() |
|
448 |
function. |
|
449 |
||
450 |
Filter and sort order flags may also be specified when calling |
|
451 |
entryList() and entryInfoList() in order to override previously defined |
|
452 |
behavior. |
|
453 |
||
454 |
\section1 The Current Directory and Other Special Paths |
|
455 |
||
456 |
Access to some common directories is provided with a number of static |
|
457 |
functions that return QDir objects. There are also corresponding functions |
|
458 |
for these that return strings: |
|
459 |
||
460 |
\table |
|
461 |
\header \o QDir \o QString \o Return Value |
|
462 |
\row \o current() \o currentPath() \o The application's working directory |
|
463 |
\row \o home() \o homePath() \o The user's home directory |
|
464 |
\row \o root() \o rootPath() \o The root directory |
|
465 |
\row \o temp() \o tempPath() \o The system's temporary directory |
|
466 |
\endtable |
|
467 |
||
468 |
The setCurrent() static function can also be used to set the application's |
|
469 |
working directory. |
|
470 |
||
471 |
If you want to find the directory containing the application's executable, |
|
472 |
see \l{QCoreApplication::applicationDirPath()}. |
|
473 |
||
474 |
The drives() static function provides a list of root directories for each |
|
475 |
device that contains a filing system. On Unix systems this returns a list |
|
476 |
containing a single root directory "/"; on Windows the list will usually |
|
477 |
contain \c{C:/}, and possibly other drive letters such as \c{D:/}, depending |
|
478 |
on the configuration of the user's system. |
|
479 |
||
480 |
\section1 Path Manipulation and Strings |
|
481 |
||
482 |
Paths containing "." elements that reference the current directory at that |
|
483 |
point in the path, ".." elements that reference the parent directory, and |
|
484 |
symbolic links can be reduced to a canonical form using the canonicalPath() |
|
485 |
function. |
|
486 |
||
487 |
Paths can also be simplified by using cleanPath() to remove redundant "/" |
|
488 |
and ".." elements. |
|
489 |
||
490 |
It is sometimes necessary to be able to show a path in the native |
|
491 |
representation for the user's platform. The static toNativeSeparators() |
|
492 |
function returns a copy of the specified path in which each directory |
|
493 |
separator is replaced by the appropriate separator for the underlying |
|
494 |
operating system. |
|
495 |
||
496 |
\section1 Examples |
|
497 |
||
498 |
Check if a directory exists: |
|
499 |
||
500 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 4 |
|
501 |
||
502 |
(We could also use the static convenience function |
|
503 |
QFile::exists().) |
|
504 |
||
505 |
Traversing directories and reading a file: |
|
506 |
||
507 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 5 |
|
508 |
||
509 |
A program that lists all the files in the current directory |
|
510 |
(excluding symbolic links), sorted by size, smallest first: |
|
511 |
||
512 |
\snippet doc/src/snippets/qdir-listfiles/main.cpp 0 |
|
513 |
||
514 |
\sa QFileInfo, QFile, QFileDialog, QApplication::applicationDirPath(), {Find Files Example} |
|
515 |
*/ |
|
516 |
||
517 |
/*! |
|
518 |
Constructs a QDir pointing to the given directory \a path. If path |
|
519 |
is empty the program's working directory, ("."), is used. |
|
520 |
||
521 |
\sa currentPath() |
|
522 |
*/ |
|
523 |
||
524 |
QDir::QDir(const QString &path) : d_ptr(new QDirPrivate(this)) |
|
525 |
{ |
|
526 |
Q_D(QDir); |
|
527 |
d->setPath(path.isEmpty() ? QString::fromLatin1(".") : path); |
|
528 |
d->data->nameFilters = QStringList(QString::fromLatin1("*")); |
|
529 |
d->data->filters = AllEntries; |
|
530 |
d->data->sort = SortFlags(Name | IgnoreCase); |
|
531 |
} |
|
532 |
||
533 |
/*! |
|
534 |
Constructs a QDir with path \a path, that filters its entries by |
|
535 |
name using \a nameFilter and by attributes using \a filters. It |
|
536 |
also sorts the names using \a sort. |
|
537 |
||
538 |
The default \a nameFilter is an empty string, which excludes |
|
539 |
nothing; the default \a filters is \l AllEntries, which also means |
|
540 |
exclude nothing. The default \a sort is \l Name | \l IgnoreCase, |
|
541 |
i.e. sort by name case-insensitively. |
|
542 |
||
543 |
If \a path is an empty string, QDir uses "." (the current |
|
544 |
directory). If \a nameFilter is an empty string, QDir uses the |
|
545 |
name filter "*" (all files). |
|
546 |
||
547 |
Note that \a path need not exist. |
|
548 |
||
549 |
\sa exists(), setPath(), setNameFilter(), setFilter(), setSorting() |
|
550 |
*/ |
|
551 |
||
552 |
QDir::QDir(const QString &path, const QString &nameFilter, |
|
553 |
SortFlags sort, Filters filters) : d_ptr(new QDirPrivate(this)) |
|
554 |
{ |
|
555 |
Q_D(QDir); |
|
556 |
d->setPath(path.isEmpty() ? QString::fromLatin1(".") : path); |
|
557 |
d->data->nameFilters = QDir::nameFiltersFromString(nameFilter); |
|
558 |
bool empty = d->data->nameFilters.isEmpty(); |
|
559 |
if(!empty) { |
|
560 |
empty = true; |
|
561 |
for(int i = 0; i < d->data->nameFilters.size(); ++i) { |
|
562 |
if(!d->data->nameFilters.at(i).isEmpty()) { |
|
563 |
empty = false; |
|
564 |
break; |
|
565 |
} |
|
566 |
} |
|
567 |
} |
|
568 |
if (empty) |
|
569 |
d->data->nameFilters = QStringList(QString::fromLatin1("*")); |
|
570 |
d->data->sort = sort; |
|
571 |
d->data->filters = filters; |
|
572 |
} |
|
573 |
||
574 |
/*! |
|
575 |
Constructs a QDir object that is a copy of the QDir object for |
|
576 |
directory \a dir. |
|
577 |
||
578 |
\sa operator=() |
|
579 |
*/ |
|
580 |
||
581 |
QDir::QDir(const QDir &dir) : d_ptr(new QDirPrivate(this, &dir)) |
|
582 |
{ |
|
583 |
} |
|
584 |
||
585 |
/*! |
|
586 |
Destroys the QDir object frees up its resources. This has no |
|
587 |
effect on the underlying directory in the file system. |
|
588 |
*/ |
|
589 |
||
590 |
QDir::~QDir() |
|
591 |
{ |
|
592 |
} |
|
593 |
||
594 |
/*! |
|
595 |
Sets the path of the directory to \a path. The path is cleaned of |
|
596 |
redundant ".", ".." and of multiple separators. No check is made |
|
597 |
to see whether a directory with this path actually exists; but you |
|
598 |
can check for yourself using exists(). |
|
599 |
||
600 |
The path can be either absolute or relative. Absolute paths begin |
|
601 |
with the directory separator "/" (optionally preceded by a drive |
|
602 |
specification under Windows). Relative file names begin with a |
|
603 |
directory name or a file name and specify a path relative to the |
|
604 |
current directory. An example of an absolute path is the string |
|
605 |
"/tmp/quartz", a relative path might look like "src/fatlib". |
|
606 |
||
607 |
\sa path(), absolutePath(), exists(), cleanPath(), dirName(), |
|
608 |
absoluteFilePath(), isRelative(), makeAbsolute() |
|
609 |
*/ |
|
610 |
||
611 |
void QDir::setPath(const QString &path) |
|
612 |
{ |
|
613 |
Q_D(QDir); |
|
614 |
d->setPath(path); |
|
615 |
} |
|
616 |
||
617 |
/*! |
|
618 |
Returns the path. This may contain symbolic links, but never |
|
619 |
contains redundant ".", ".." or multiple separators. |
|
620 |
||
621 |
The returned path can be either absolute or relative (see |
|
622 |
setPath()). |
|
623 |
||
624 |
\sa setPath(), absolutePath(), exists(), cleanPath(), dirName(), |
|
625 |
absoluteFilePath(), toNativeSeparators(), makeAbsolute() |
|
626 |
*/ |
|
627 |
||
628 |
QString QDir::path() const |
|
629 |
{ |
|
630 |
Q_D(const QDir); |
|
631 |
return d->data->path; |
|
632 |
} |
|
633 |
||
634 |
/*! |
|
635 |
Returns the absolute path (a path that starts with "/" or with a |
|
636 |
drive specification), which may contain symbolic links, but never |
|
637 |
contains redundant ".", ".." or multiple separators. |
|
638 |
||
639 |
\sa setPath(), canonicalPath(), exists(), cleanPath(), |
|
640 |
dirName(), absoluteFilePath() |
|
641 |
*/ |
|
642 |
||
643 |
QString QDir::absolutePath() const |
|
644 |
{ |
|
645 |
Q_D(const QDir); |
|
646 |
QString ret = d->data->path; |
|
647 |
if (QDir::isRelativePath(ret)) |
|
648 |
ret = absoluteFilePath(QString::fromLatin1("")); |
|
649 |
return cleanPath(ret); |
|
650 |
} |
|
651 |
||
652 |
||
653 |
/*! |
|
654 |
Returns the canonical path, i.e. a path without symbolic links or |
|
655 |
redundant "." or ".." elements. |
|
656 |
||
657 |
On systems that do not have symbolic links this function will |
|
658 |
always return the same string that absolutePath() returns. If the |
|
659 |
canonical path does not exist (normally due to dangling symbolic |
|
660 |
links) canonicalPath() returns an empty string. |
|
661 |
||
662 |
Example: |
|
663 |
||
664 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 6 |
|
665 |
||
666 |
\sa path(), absolutePath(), exists(), cleanPath(), dirName(), |
|
667 |
absoluteFilePath() |
|
668 |
*/ |
|
669 |
||
670 |
QString QDir::canonicalPath() const |
|
671 |
{ |
|
672 |
Q_D(const QDir); |
|
673 |
||
674 |
if(!d->data->fileEngine) |
|
675 |
return QLatin1String(""); |
|
676 |
return cleanPath(d->data->fileEngine->fileName(QAbstractFileEngine::CanonicalName)); |
|
677 |
} |
|
678 |
||
679 |
/*! |
|
680 |
Returns the name of the directory; this is \e not the same as the |
|
681 |
path, e.g. a directory with the name "mail", might have the path |
|
682 |
"/var/spool/mail". If the directory has no name (e.g. it is the |
|
683 |
root directory) an empty string is returned. |
|
684 |
||
685 |
No check is made to ensure that a directory with this name |
|
686 |
actually exists; but see exists(). |
|
687 |
||
688 |
\sa path(), filePath(), absolutePath(), absoluteFilePath() |
|
689 |
*/ |
|
690 |
||
691 |
QString QDir::dirName() const |
|
692 |
{ |
|
693 |
Q_D(const QDir); |
|
694 |
int pos = d->data->path.lastIndexOf(QLatin1Char('/')); |
|
695 |
if (pos == -1) |
|
696 |
return d->data->path; |
|
697 |
return d->data->path.mid(pos + 1); |
|
698 |
} |
|
699 |
||
700 |
/*! |
|
701 |
Returns the path name of a file in the directory. Does \e not |
|
702 |
check if the file actually exists in the directory; but see |
|
703 |
exists(). If the QDir is relative the returned path name will also |
|
704 |
be relative. Redundant multiple separators or "." and ".." |
|
705 |
directories in \a fileName are not removed (see cleanPath()). |
|
706 |
||
707 |
\sa dirName() absoluteFilePath(), isRelative(), canonicalPath() |
|
708 |
*/ |
|
709 |
||
710 |
QString QDir::filePath(const QString &fileName) const |
|
711 |
{ |
|
712 |
Q_D(const QDir); |
|
713 |
if (isAbsolutePath(fileName)) |
|
714 |
return QString(fileName); |
|
715 |
||
716 |
QString ret = d->data->path; |
|
717 |
if(!fileName.isEmpty()) { |
|
718 |
if (!ret.isEmpty() && ret[(int)ret.length()-1] != QLatin1Char('/') && fileName[0] != QLatin1Char('/')) |
|
719 |
ret += QLatin1Char('/'); |
|
720 |
ret += fileName; |
|
721 |
} |
|
722 |
return ret; |
|
723 |
} |
|
724 |
||
725 |
/*! |
|
726 |
Returns the absolute path name of a file in the directory. Does \e |
|
727 |
not check if the file actually exists in the directory; but see |
|
728 |
exists(). Redundant multiple separators or "." and ".." |
|
729 |
directories in \a fileName are not removed (see cleanPath()). |
|
730 |
||
731 |
\sa relativeFilePath() filePath() canonicalPath() |
|
732 |
*/ |
|
733 |
||
734 |
QString QDir::absoluteFilePath(const QString &fileName) const |
|
735 |
{ |
|
736 |
Q_D(const QDir); |
|
737 |
if (isAbsolutePath(fileName)) |
|
738 |
return fileName; |
|
739 |
if(!d->data->fileEngine) |
|
740 |
return fileName; |
|
741 |
||
742 |
QString ret; |
|
743 |
#ifndef QT_NO_FSFILEENGINE |
|
744 |
if (isRelativePath(d->data->path)) //get pwd |
|
745 |
ret = QFSFileEngine::currentPath(fileName); |
|
746 |
#endif |
|
747 |
if(!d->data->path.isEmpty() && d->data->path != QLatin1String(".")) { |
|
748 |
if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) |
|
749 |
ret += QLatin1Char('/'); |
|
750 |
ret += d->data->path; |
|
751 |
} |
|
752 |
if (!fileName.isEmpty()) { |
|
753 |
if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) |
|
754 |
ret += QLatin1Char('/'); |
|
755 |
ret += fileName; |
|
756 |
} |
|
757 |
return ret; |
|
758 |
} |
|
759 |
||
760 |
/*! |
|
761 |
Returns the path to \a fileName relative to the directory. |
|
762 |
||
763 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 7 |
|
764 |
||
765 |
\sa absoluteFilePath() filePath() canonicalPath() |
|
766 |
*/ |
|
767 |
||
768 |
QString QDir::relativeFilePath(const QString &fileName) const |
|
769 |
{ |
|
770 |
QString dir = absolutePath(); |
|
771 |
QString file = cleanPath(fileName); |
|
772 |
||
773 |
if (isRelativePath(file) || isRelativePath(dir)) |
|
774 |
return file; |
|
775 |
||
776 |
QString dirDrive = driveSpec(dir); |
|
777 |
QString fileDrive = driveSpec(file); |
|
778 |
||
779 |
bool fileDriveMissing = false; |
|
780 |
if (fileDrive.isEmpty()) { |
|
781 |
fileDrive = dirDrive; |
|
782 |
fileDriveMissing = true; |
|
783 |
} |
|
784 |
||
785 |
#ifdef Q_OS_WIN |
|
786 |
if (fileDrive.toLower() != dirDrive.toLower() |
|
787 |
|| (file.startsWith(QLatin1String("//")) |
|
788 |
&& !dir.startsWith(QLatin1String("//")))) |
|
789 |
#elif defined(Q_OS_SYMBIAN) |
|
790 |
if (fileDrive.toLower() != dirDrive.toLower()) |
|
791 |
#else |
|
792 |
if (fileDrive != dirDrive) |
|
793 |
#endif |
|
794 |
return file; |
|
795 |
||
796 |
dir.remove(0, dirDrive.size()); |
|
797 |
if (!fileDriveMissing) |
|
798 |
file.remove(0, fileDrive.size()); |
|
799 |
||
800 |
QString result; |
|
801 |
QStringList dirElts = dir.split(QLatin1Char('/'), QString::SkipEmptyParts); |
|
802 |
QStringList fileElts = file.split(QLatin1Char('/'), QString::SkipEmptyParts); |
|
803 |
||
804 |
int i = 0; |
|
805 |
while (i < dirElts.size() && i < fileElts.size() && |
|
806 |
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) |
|
807 |
dirElts.at(i).toLower() == fileElts.at(i).toLower()) |
|
808 |
#else |
|
809 |
dirElts.at(i) == fileElts.at(i)) |
|
810 |
#endif |
|
811 |
++i; |
|
812 |
||
813 |
for (int j = 0; j < dirElts.size() - i; ++j) |
|
814 |
result += QLatin1String("../"); |
|
815 |
||
816 |
for (int j = i; j < fileElts.size(); ++j) { |
|
817 |
result += fileElts.at(j); |
|
818 |
if (j < fileElts.size() - 1) |
|
819 |
result += QLatin1Char('/'); |
|
820 |
} |
|
821 |
||
822 |
return result; |
|
823 |
} |
|
824 |
||
825 |
/*! |
|
826 |
\obsolete |
|
827 |
||
828 |
Use QDir::toNativeSeparators() instead. |
|
829 |
*/ |
|
830 |
QString QDir::convertSeparators(const QString &pathName) |
|
831 |
{ |
|
832 |
return toNativeSeparators(pathName); |
|
833 |
} |
|
834 |
||
835 |
/*! |
|
836 |
\since 4.2 |
|
837 |
||
838 |
Returns \a pathName with the '/' separators converted to |
|
839 |
separators that are appropriate for the underlying operating |
|
840 |
system. |
|
841 |
||
842 |
On Windows, toNativeSeparators("c:/winnt/system32") returns |
|
843 |
"c:\\winnt\\system32". |
|
844 |
||
845 |
The returned string may be the same as the argument on some |
|
846 |
operating systems, for example on Unix. |
|
847 |
||
848 |
\sa fromNativeSeparators(), separator() |
|
849 |
*/ |
|
850 |
QString QDir::toNativeSeparators(const QString &pathName) |
|
851 |
{ |
|
852 |
QString n(pathName); |
|
853 |
#if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN) |
|
854 |
for (int i=0; i<(int)n.length(); i++) { |
|
855 |
if (n[i] == QLatin1Char('/')) |
|
856 |
n[i] = QLatin1Char('\\'); |
|
857 |
} |
|
858 |
#endif |
|
859 |
return n; |
|
860 |
} |
|
861 |
||
862 |
/*! |
|
863 |
\since 4.2 |
|
864 |
||
865 |
Returns \a pathName using '/' as file separator. On Windows, |
|
866 |
for instance, fromNativeSeparators("\c{c:\\winnt\\system32}") returns |
|
867 |
"c:/winnt/system32". |
|
868 |
||
869 |
The returned string may be the same as the argument on some |
|
870 |
operating systems, for example on Unix. |
|
871 |
||
872 |
\sa toNativeSeparators(), separator() |
|
873 |
*/ |
|
874 |
QString QDir::fromNativeSeparators(const QString &pathName) |
|
875 |
{ |
|
876 |
QString n(pathName); |
|
877 |
#if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN) |
|
878 |
for (int i=0; i<(int)n.length(); i++) { |
|
879 |
if (n[i] == QLatin1Char('\\')) |
|
880 |
n[i] = QLatin1Char('/'); |
|
881 |
} |
|
882 |
#endif |
|
883 |
return n; |
|
884 |
} |
|
885 |
||
886 |
/*! |
|
887 |
Changes the QDir's directory to \a dirName. |
|
888 |
||
889 |
Returns true if the new directory exists and is readable; |
|
890 |
otherwise returns false. Note that the logical cd() operation is |
|
891 |
not performed if the new directory does not exist. |
|
892 |
||
893 |
Calling cd("..") is equivalent to calling cdUp(). |
|
894 |
||
895 |
\sa cdUp(), isReadable(), exists(), path() |
|
896 |
*/ |
|
897 |
||
898 |
bool QDir::cd(const QString &dirName) |
|
899 |
{ |
|
900 |
Q_D(QDir); |
|
901 |
||
902 |
if (dirName.isEmpty() || dirName == QLatin1String(".")) |
|
903 |
return true; |
|
904 |
QString newPath = d->data->path; |
|
905 |
if (isAbsolutePath(dirName)) { |
|
906 |
newPath = cleanPath(dirName); |
|
907 |
} else { |
|
908 |
if (isRoot()) { |
|
909 |
if (dirName == QLatin1String("..")) |
|
910 |
return false; |
|
911 |
} else { |
|
912 |
newPath += QLatin1Char('/'); |
|
913 |
} |
|
914 |
||
915 |
newPath += dirName; |
|
916 |
if (dirName.indexOf(QLatin1Char('/')) >= 0 |
|
917 |
|| d->data->path == QLatin1String(".") |
|
918 |
|| dirName == QLatin1String("..")) { |
|
919 |
newPath = cleanPath(newPath); |
|
920 |
/* |
|
921 |
If newPath starts with .., we convert it to absolute to |
|
922 |
avoid infinite looping on |
|
923 |
||
924 |
QDir dir("."); |
|
925 |
while (dir.cdUp()) |
|
926 |
; |
|
927 |
*/ |
|
928 |
if (newPath.startsWith(QLatin1String(".."))) { |
|
929 |
newPath = QFileInfo(newPath).absoluteFilePath(); |
|
930 |
} |
|
931 |
} |
|
932 |
} |
|
933 |
{ |
|
934 |
QFileInfo fi(newPath); |
|
935 |
if (!(fi.exists() && fi.isDir())) |
|
936 |
return false; |
|
937 |
} |
|
938 |
||
939 |
d->setPath(newPath); |
|
940 |
refresh(); |
|
941 |
return true; |
|
942 |
} |
|
943 |
||
944 |
/*! |
|
945 |
Changes directory by moving one directory up from the QDir's |
|
946 |
current directory. |
|
947 |
||
948 |
Returns true if the new directory exists and is readable; |
|
949 |
otherwise returns false. Note that the logical cdUp() operation is |
|
950 |
not performed if the new directory does not exist. |
|
951 |
||
952 |
\sa cd(), isReadable(), exists(), path() |
|
953 |
*/ |
|
954 |
||
955 |
bool QDir::cdUp() |
|
956 |
{ |
|
957 |
return cd(QString::fromLatin1("..")); |
|
958 |
} |
|
959 |
||
960 |
/*! |
|
961 |
Returns the string list set by setNameFilters() |
|
962 |
*/ |
|
963 |
||
964 |
QStringList QDir::nameFilters() const |
|
965 |
{ |
|
966 |
Q_D(const QDir); |
|
967 |
||
968 |
return d->data->nameFilters; |
|
969 |
} |
|
970 |
||
971 |
/*! |
|
972 |
Sets the name filters used by entryList() and entryInfoList() to the |
|
973 |
list of filters specified by \a nameFilters. |
|
974 |
||
975 |
Each name filter is a wildcard (globbing) filter that understands |
|
976 |
\c{*} and \c{?} wildcards. (See \l{QRegExp wildcard matching}.) |
|
977 |
||
978 |
For example, the following code sets three name filters on a QDir |
|
979 |
to ensure that only files with extensions typically used for C++ |
|
980 |
source files are listed: |
|
981 |
||
982 |
\snippet doc/src/snippets/qdir-namefilters/main.cpp 0 |
|
983 |
||
984 |
\sa nameFilters(), setFilter() |
|
985 |
*/ |
|
986 |
||
987 |
void QDir::setNameFilters(const QStringList &nameFilters) |
|
988 |
{ |
|
989 |
Q_D(QDir); |
|
990 |
d->detach(); |
|
991 |
d->data->nameFilters = nameFilters; |
|
992 |
} |
|
993 |
||
994 |
/*! |
|
995 |
\obsolete |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
996 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
997 |
Use QDir::addSearchPath() with a prefix instead. |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
998 |
|
0 | 999 |
Adds \a path to the search paths searched in to find resources |
1000 |
that are not specified with an absolute path. The default search |
|
1001 |
path is to search only in the root (\c{:/}). |
|
1002 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1003 |
\sa {The Qt Resource System} |
0 | 1004 |
*/ |
1005 |
void QDir::addResourceSearchPath(const QString &path) |
|
1006 |
{ |
|
1007 |
#ifdef QT_BUILD_CORE_LIB |
|
1008 |
QResource::addSearchPath(path); |
|
1009 |
#else |
|
1010 |
Q_UNUSED(path) |
|
1011 |
#endif |
|
1012 |
} |
|
1013 |
||
1014 |
#ifdef QT_BUILD_CORE_LIB |
|
1015 |
/*! |
|
1016 |
\since 4.3 |
|
1017 |
||
1018 |
Sets or replaces Qt's search paths for file names with the prefix \a prefix |
|
1019 |
to \a searchPaths. |
|
1020 |
||
1021 |
To specify a prefix for a file name, prepend the prefix followed by a single |
|
1022 |
colon (e.g., "images:undo.png", "xmldocs:books.xml"). \a prefix can only |
|
1023 |
contain letters or numbers (e.g., it cannot contain a colon, nor a slash). |
|
1024 |
||
1025 |
Qt uses this search path to locate files with a known prefix. The search |
|
1026 |
path entries are tested in order, starting with the first entry. |
|
1027 |
||
1028 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 8 |
|
1029 |
||
1030 |
File name prefix must be at least 2 characters long to avoid conflicts with |
|
1031 |
Windows drive letters. |
|
1032 |
||
1033 |
Search paths may contain paths to \l{The Qt Resource System}. |
|
1034 |
*/ |
|
1035 |
void QDir::setSearchPaths(const QString &prefix, const QStringList &searchPaths) |
|
1036 |
{ |
|
1037 |
if (prefix.length() < 2) { |
|
1038 |
qWarning("QDir::setSearchPaths: Prefix must be longer than 1 character"); |
|
1039 |
return; |
|
1040 |
} |
|
1041 |
||
1042 |
for (int i = 0; i < prefix.count(); i++) { |
|
1043 |
if (!prefix.at(i).isLetterOrNumber()) { |
|
1044 |
qWarning("QDir::setSearchPaths: Prefix can only contain letters or numbers"); |
|
1045 |
return; |
|
1046 |
} |
|
1047 |
} |
|
1048 |
||
1049 |
QWriteLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock); |
|
1050 |
QMap<QString, QStringList> &paths = QCoreGlobalData::instance()->dirSearchPaths; |
|
1051 |
if (searchPaths.isEmpty()) { |
|
1052 |
paths.remove(prefix); |
|
1053 |
} else { |
|
1054 |
paths.insert(prefix, searchPaths); |
|
1055 |
} |
|
1056 |
} |
|
1057 |
||
1058 |
/*! |
|
1059 |
\since 4.3 |
|
1060 |
||
1061 |
Adds \a path to the search path for \a prefix. |
|
1062 |
||
1063 |
\sa setSearchPaths() |
|
1064 |
*/ |
|
1065 |
void QDir::addSearchPath(const QString &prefix, const QString &path) |
|
1066 |
{ |
|
1067 |
if (path.isEmpty()) |
|
1068 |
return; |
|
1069 |
||
1070 |
QWriteLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock); |
|
1071 |
QCoreGlobalData::instance()->dirSearchPaths[prefix] += path; |
|
1072 |
} |
|
1073 |
||
1074 |
/*! |
|
1075 |
\since 4.3 |
|
1076 |
||
1077 |
Returns the search paths for \a prefix. |
|
1078 |
||
1079 |
\sa setSearchPaths(), addSearchPath() |
|
1080 |
*/ |
|
1081 |
QStringList QDir::searchPaths(const QString &prefix) |
|
1082 |
{ |
|
1083 |
QReadLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock); |
|
1084 |
return QCoreGlobalData::instance()->dirSearchPaths.value(prefix); |
|
1085 |
} |
|
1086 |
||
1087 |
#endif // QT_BUILD_CORE_LIB |
|
1088 |
||
1089 |
/*! |
|
1090 |
Returns the value set by setFilter() |
|
1091 |
*/ |
|
1092 |
||
1093 |
QDir::Filters QDir::filter() const |
|
1094 |
{ |
|
1095 |
Q_D(const QDir); |
|
1096 |
||
1097 |
return d->data->filters; |
|
1098 |
} |
|
1099 |
||
1100 |
/*! |
|
1101 |
\enum QDir::Filter |
|
1102 |
||
1103 |
This enum describes the filtering options available to QDir; e.g. |
|
1104 |
for entryList() and entryInfoList(). The filter value is specified |
|
1105 |
by combining values from the following list using the bitwise OR |
|
1106 |
operator: |
|
1107 |
||
1108 |
\value Dirs List directories that match the filters. |
|
1109 |
\value AllDirs List all directories; i.e. don't apply the filters |
|
1110 |
to directory names. |
|
1111 |
\value Files List files. |
|
1112 |
\value Drives List disk drives (ignored under Unix). |
|
1113 |
\value NoSymLinks Do not list symbolic links (ignored by operating |
|
1114 |
systems that don't support symbolic links). |
|
1115 |
\value NoDotAndDotDot Do not list the special entries "." and "..". |
|
1116 |
\value AllEntries List directories, files, drives and symlinks (this does not list |
|
1117 |
broken symlinks unless you specify System). |
|
1118 |
\value Readable List files for which the application has read |
|
1119 |
access. The Readable value needs to be combined |
|
1120 |
with Dirs or Files. |
|
1121 |
\value Writable List files for which the application has write |
|
1122 |
access. The Writable value needs to be combined |
|
1123 |
with Dirs or Files. |
|
1124 |
\value Executable List files for which the application has |
|
1125 |
execute access. The Executable value needs to be |
|
1126 |
combined with Dirs or Files. |
|
1127 |
\value Modified Only list files that have been modified (ignored |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1128 |
on Unix). |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1129 |
\value Hidden List hidden files (on Unix, files starting with a "."). |
0 | 1130 |
\value System List system files (on Unix, FIFOs, sockets and |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1131 |
device files are included; on Windows, \c {.lnk} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1132 |
files are included) |
0 | 1133 |
\value CaseSensitive The filter should be case sensitive. |
1134 |
||
1135 |
\omitvalue DefaultFilter |
|
1136 |
\omitvalue TypeMask |
|
1137 |
\omitvalue All |
|
1138 |
\omitvalue RWEMask |
|
1139 |
\omitvalue AccessMask |
|
1140 |
\omitvalue PermissionMask |
|
1141 |
\omitvalue NoFilter |
|
1142 |
||
1143 |
Functions that use Filter enum values to filter lists of files |
|
1144 |
and directories will include symbolic links to files and directories |
|
1145 |
unless you set the NoSymLinks value. |
|
1146 |
||
1147 |
A default constructed QDir will not filter out files based on |
|
1148 |
their permissions, so entryList() and entryInfoList() will return |
|
1149 |
all files that are readable, writable, executable, or any |
|
1150 |
combination of the three. This makes the default easy to write, |
|
1151 |
and at the same time useful. |
|
1152 |
||
1153 |
For example, setting the \c Readable, \c Writable, and \c Files |
|
1154 |
flags allows all files to be listed for which the application has read |
|
1155 |
access, write access or both. If the \c Dirs and \c Drives flags are |
|
1156 |
also included in this combination then all drives, directories, all |
|
1157 |
files that the application can read, write, or execute, and symlinks |
|
1158 |
to such files/directories can be listed. |
|
1159 |
||
1160 |
To retrieve the permissons for a directory, use the |
|
1161 |
entryInfoList() function to get the associated QFileInfo objects |
|
1162 |
and then use the QFileInfo::permissons() to obtain the permissions |
|
1163 |
and ownership for each file. |
|
1164 |
*/ |
|
1165 |
||
1166 |
/*! |
|
1167 |
Sets the filter used by entryList() and entryInfoList() to \a |
|
1168 |
filters. The filter is used to specify the kind of files that |
|
1169 |
should be returned by entryList() and entryInfoList(). See |
|
1170 |
\l{QDir::Filter}. |
|
1171 |
||
1172 |
\sa filter(), setNameFilters() |
|
1173 |
*/ |
|
1174 |
||
1175 |
void QDir::setFilter(Filters filters) |
|
1176 |
{ |
|
1177 |
Q_D(QDir); |
|
1178 |
||
1179 |
d->detach(); |
|
1180 |
d->data->filters = filters; |
|
1181 |
} |
|
1182 |
||
1183 |
/*! |
|
1184 |
Returns the value set by setSorting() |
|
1185 |
||
1186 |
\sa setSorting() SortFlag |
|
1187 |
*/ |
|
1188 |
||
1189 |
QDir::SortFlags QDir::sorting() const |
|
1190 |
{ |
|
1191 |
Q_D(const QDir); |
|
1192 |
||
1193 |
return d->data->sort; |
|
1194 |
} |
|
1195 |
||
1196 |
/*! |
|
1197 |
\enum QDir::SortFlag |
|
1198 |
||
1199 |
This enum describes the sort options available to QDir, e.g. for |
|
1200 |
entryList() and entryInfoList(). The sort value is specified by |
|
1201 |
OR-ing together values from the following list: |
|
1202 |
||
1203 |
\value Name Sort by name. |
|
1204 |
\value Time Sort by time (modification time). |
|
1205 |
\value Size Sort by file size. |
|
1206 |
\value Type Sort by file type (extension). |
|
1207 |
\value Unsorted Do not sort. |
|
1208 |
\value NoSort Not sorted by default. |
|
1209 |
||
1210 |
\value DirsFirst Put the directories first, then the files. |
|
1211 |
\value DirsLast Put the files first, then the directories. |
|
1212 |
\value Reversed Reverse the sort order. |
|
1213 |
\value IgnoreCase Sort case-insensitively. |
|
1214 |
\value LocaleAware Sort items appropriately using the current locale settings. |
|
1215 |
||
1216 |
\omitvalue SortByMask |
|
1217 |
\omitvalue DefaultSort |
|
1218 |
||
1219 |
You can only specify one of the first four. |
|
1220 |
||
1221 |
If you specify both DirsFirst and Reversed, directories are |
|
1222 |
still put first, but in reverse order; the files will be listed |
|
1223 |
after the directories, again in reverse order. |
|
1224 |
*/ |
|
1225 |
||
1226 |
/*! |
|
1227 |
Sets the sort order used by entryList() and entryInfoList(). |
|
1228 |
||
1229 |
The \a sort is specified by OR-ing values from the enum |
|
1230 |
\l{QDir::SortFlag}. |
|
1231 |
||
1232 |
\sa sorting() SortFlag |
|
1233 |
*/ |
|
1234 |
||
1235 |
void QDir::setSorting(SortFlags sort) |
|
1236 |
{ |
|
1237 |
Q_D(QDir); |
|
1238 |
||
1239 |
d->detach(); |
|
1240 |
d->data->sort = sort; |
|
1241 |
} |
|
1242 |
||
1243 |
||
1244 |
/*! |
|
1245 |
Returns the total number of directories and files in the directory. |
|
1246 |
||
1247 |
Equivalent to entryList().count(). |
|
1248 |
||
1249 |
\sa operator[](), entryList() |
|
1250 |
*/ |
|
1251 |
||
1252 |
uint QDir::count() const |
|
1253 |
{ |
|
1254 |
Q_D(const QDir); |
|
1255 |
||
1256 |
d->updateFileLists(); |
|
1257 |
return d->data->files.count(); |
|
1258 |
} |
|
1259 |
||
1260 |
/*! |
|
1261 |
Returns the file name at position \a pos in the list of file |
|
1262 |
names. Equivalent to entryList().at(index). |
|
1263 |
||
1264 |
Returns an empty string if \a pos is out of range or if the |
|
1265 |
entryList() function failed. |
|
1266 |
||
1267 |
\sa count(), entryList() |
|
1268 |
*/ |
|
1269 |
||
1270 |
QString QDir::operator[](int pos) const |
|
1271 |
{ |
|
1272 |
Q_D(const QDir); |
|
1273 |
||
1274 |
d->updateFileLists(); |
|
1275 |
return d->data->files[pos]; |
|
1276 |
} |
|
1277 |
||
1278 |
/*! |
|
1279 |
\overload |
|
1280 |
||
1281 |
Returns a list of the names of all the files and directories in |
|
1282 |
the directory, ordered according to the name and attribute filters |
|
1283 |
previously set with setNameFilters() and setFilter(), and sorted according |
|
1284 |
to the flags set with setSorting(). |
|
1285 |
||
1286 |
The attribute filter and sorting specifications can be overridden using the |
|
1287 |
\a filters and \a sort arguments. |
|
1288 |
||
1289 |
Returns an empty list if the directory is unreadable, does not |
|
1290 |
exist, or if nothing matches the specification. |
|
1291 |
||
1292 |
\note To list symlinks that point to non existing files, \l System must be |
|
1293 |
passed to the filter. |
|
1294 |
||
1295 |
\sa entryInfoList(), setNameFilters(), setSorting(), setFilter() |
|
1296 |
*/ |
|
1297 |
||
1298 |
QStringList QDir::entryList(Filters filters, SortFlags sort) const |
|
1299 |
{ |
|
1300 |
Q_D(const QDir); |
|
1301 |
||
1302 |
return entryList(d->data->nameFilters, filters, sort); |
|
1303 |
} |
|
1304 |
||
1305 |
||
1306 |
/*! |
|
1307 |
\overload |
|
1308 |
||
1309 |
Returns a list of QFileInfo objects for all the files and directories in |
|
1310 |
the directory, ordered according to the name and attribute filters |
|
1311 |
previously set with setNameFilters() and setFilter(), and sorted according |
|
1312 |
to the flags set with setSorting(). |
|
1313 |
||
1314 |
The attribute filter and sorting specifications can be overridden using the |
|
1315 |
\a filters and \a sort arguments. |
|
1316 |
||
1317 |
Returns an empty list if the directory is unreadable, does not |
|
1318 |
exist, or if nothing matches the specification. |
|
1319 |
||
1320 |
\sa entryList(), setNameFilters(), setSorting(), setFilter(), isReadable(), exists() |
|
1321 |
*/ |
|
1322 |
QFileInfoList QDir::entryInfoList(Filters filters, SortFlags sort) const |
|
1323 |
{ |
|
1324 |
Q_D(const QDir); |
|
1325 |
||
1326 |
return entryInfoList(d->data->nameFilters, filters, sort); |
|
1327 |
} |
|
1328 |
||
1329 |
/*! |
|
1330 |
Returns a list of the names of all the files and |
|
1331 |
directories in the directory, ordered according to the name |
|
1332 |
and attribute filters previously set with setNameFilters() |
|
1333 |
and setFilter(), and sorted according to the flags set with |
|
1334 |
setSorting(). |
|
1335 |
||
1336 |
The name filter, file attribute filter, and sorting specification |
|
1337 |
can be overridden using the \a nameFilters, \a filters, and \a sort |
|
1338 |
arguments. |
|
1339 |
||
1340 |
Returns an empty list if the directory is unreadable, does not |
|
1341 |
exist, or if nothing matches the specification. |
|
1342 |
||
1343 |
\sa entryInfoList(), setNameFilters(), setSorting(), setFilter() |
|
1344 |
*/ |
|
1345 |
||
1346 |
QStringList QDir::entryList(const QStringList &nameFilters, Filters filters, |
|
1347 |
SortFlags sort) const |
|
1348 |
{ |
|
1349 |
Q_D(const QDir); |
|
1350 |
||
1351 |
if (filters == NoFilter) |
|
1352 |
filters = d->data->filters; |
|
1353 |
#ifdef QT3_SUPPORT |
|
1354 |
if (d->matchAllDirs) |
|
1355 |
filters |= AllDirs; |
|
1356 |
#endif |
|
1357 |
if (sort == NoSort) |
|
1358 |
sort = d->data->sort; |
|
1359 |
if (filters == NoFilter && sort == NoSort && nameFilters == d->data->nameFilters) { |
|
1360 |
d->updateFileLists(); |
|
1361 |
return d->data->files; |
|
1362 |
} |
|
1363 |
QFileInfoList l; |
|
1364 |
QDirIterator it(d->data->path, nameFilters, filters); |
|
1365 |
while (it.hasNext()) { |
|
1366 |
it.next(); |
|
1367 |
l.append(it.fileInfo()); |
|
1368 |
} |
|
1369 |
QStringList ret; |
|
1370 |
d->sortFileList(sort, l, &ret, 0); |
|
1371 |
return ret; |
|
1372 |
} |
|
1373 |
||
1374 |
/*! |
|
1375 |
Returns a list of QFileInfo objects for all the files and |
|
1376 |
directories in the directory, ordered according to the name |
|
1377 |
and attribute filters previously set with setNameFilters() |
|
1378 |
and setFilter(), and sorted according to the flags set with |
|
1379 |
setSorting(). |
|
1380 |
||
1381 |
The name filter, file attribute filter, and sorting specification |
|
1382 |
can be overridden using the \a nameFilters, \a filters, and \a sort |
|
1383 |
arguments. |
|
1384 |
||
1385 |
Returns an empty list if the directory is unreadable, does not |
|
1386 |
exist, or if nothing matches the specification. |
|
1387 |
||
1388 |
\sa entryList(), setNameFilters(), setSorting(), setFilter(), isReadable(), exists() |
|
1389 |
*/ |
|
1390 |
||
1391 |
QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, Filters filters, |
|
1392 |
SortFlags sort) const |
|
1393 |
{ |
|
1394 |
Q_D(const QDir); |
|
1395 |
||
1396 |
if (filters == NoFilter) |
|
1397 |
filters = d->data->filters; |
|
1398 |
#ifdef QT3_SUPPORT |
|
1399 |
if (d->matchAllDirs) |
|
1400 |
filters |= AllDirs; |
|
1401 |
#endif |
|
1402 |
if (sort == NoSort) |
|
1403 |
sort = d->data->sort; |
|
1404 |
if (filters == NoFilter && sort == NoSort && nameFilters == d->data->nameFilters) { |
|
1405 |
d->updateFileLists(); |
|
1406 |
return d->data->fileInfos; |
|
1407 |
} |
|
1408 |
QFileInfoList l; |
|
1409 |
QDirIterator it(d->data->path, nameFilters, filters); |
|
1410 |
while (it.hasNext()) { |
|
1411 |
it.next(); |
|
1412 |
l.append(it.fileInfo()); |
|
1413 |
} |
|
1414 |
QFileInfoList ret; |
|
1415 |
d->sortFileList(sort, l, 0, &ret); |
|
1416 |
return ret; |
|
1417 |
} |
|
1418 |
||
1419 |
/*! |
|
1420 |
Creates a sub-directory called \a dirName. |
|
1421 |
||
1422 |
Returns true on success; otherwise returns false. |
|
1423 |
||
1424 |
\sa rmdir() |
|
1425 |
*/ |
|
1426 |
||
1427 |
bool QDir::mkdir(const QString &dirName) const |
|
1428 |
{ |
|
1429 |
Q_D(const QDir); |
|
1430 |
||
1431 |
if (dirName.isEmpty()) { |
|
1432 |
qWarning("QDir::mkdir: Empty or null file name(s)"); |
|
1433 |
return false; |
|
1434 |
} |
|
1435 |
if(!d->data->fileEngine) |
|
1436 |
return false; |
|
1437 |
||
1438 |
QString fn = filePath(dirName); |
|
1439 |
return d->data->fileEngine->mkdir(fn, false); |
|
1440 |
} |
|
1441 |
||
1442 |
/*! |
|
1443 |
Removes the directory specified by \a dirName. |
|
1444 |
||
1445 |
The directory must be empty for rmdir() to succeed. |
|
1446 |
||
1447 |
Returns true if successful; otherwise returns false. |
|
1448 |
||
1449 |
\sa mkdir() |
|
1450 |
*/ |
|
1451 |
||
1452 |
bool QDir::rmdir(const QString &dirName) const |
|
1453 |
{ |
|
1454 |
Q_D(const QDir); |
|
1455 |
||
1456 |
if (dirName.isEmpty()) { |
|
1457 |
qWarning("QDir::rmdir: Empty or null file name(s)"); |
|
1458 |
return false; |
|
1459 |
} |
|
1460 |
if(!d->data->fileEngine) |
|
1461 |
return false; |
|
1462 |
||
1463 |
QString fn = filePath(dirName); |
|
1464 |
return d->data->fileEngine->rmdir(fn, false); |
|
1465 |
} |
|
1466 |
||
1467 |
/*! |
|
1468 |
Creates the directory path \a dirPath. |
|
1469 |
||
1470 |
The function will create all parent directories necessary to |
|
1471 |
create the directory. |
|
1472 |
||
1473 |
Returns true if successful; otherwise returns false. |
|
1474 |
||
1475 |
\sa rmpath() |
|
1476 |
*/ |
|
1477 |
||
1478 |
bool QDir::mkpath(const QString &dirPath) const |
|
1479 |
{ |
|
1480 |
Q_D(const QDir); |
|
1481 |
||
1482 |
if (dirPath.isEmpty()) { |
|
1483 |
qWarning("QDir::mkpath: Empty or null file name(s)"); |
|
1484 |
return false; |
|
1485 |
} |
|
1486 |
if(!d->data->fileEngine) |
|
1487 |
return false; |
|
1488 |
||
1489 |
QString fn = filePath(dirPath); |
|
1490 |
return d->data->fileEngine->mkdir(fn, true); |
|
1491 |
} |
|
1492 |
||
1493 |
/*! |
|
1494 |
Removes the directory path \a dirPath. |
|
1495 |
||
1496 |
The function will remove all parent directories in \a dirPath, |
|
1497 |
provided that they are empty. This is the opposite of |
|
1498 |
mkpath(dirPath). |
|
1499 |
||
1500 |
Returns true if successful; otherwise returns false. |
|
1501 |
||
1502 |
\sa mkpath() |
|
1503 |
*/ |
|
1504 |
bool QDir::rmpath(const QString &dirPath) const |
|
1505 |
{ |
|
1506 |
Q_D(const QDir); |
|
1507 |
||
1508 |
if (dirPath.isEmpty()) { |
|
1509 |
qWarning("QDir::rmpath: Empty or null file name(s)"); |
|
1510 |
return false; |
|
1511 |
} |
|
1512 |
if(!d->data->fileEngine) |
|
1513 |
return false; |
|
1514 |
||
1515 |
QString fn = filePath(dirPath); |
|
1516 |
return d->data->fileEngine->rmdir(fn, true); |
|
1517 |
} |
|
1518 |
||
1519 |
/*! |
|
1520 |
Returns true if the directory is readable \e and we can open files |
|
1521 |
by name; otherwise returns false. |
|
1522 |
||
1523 |
\warning A false value from this function is not a guarantee that |
|
1524 |
files in the directory are not accessible. |
|
1525 |
||
1526 |
\sa QFileInfo::isReadable() |
|
1527 |
*/ |
|
1528 |
||
1529 |
||
1530 |
bool QDir::isReadable() const |
|
1531 |
{ |
|
1532 |
Q_D(const QDir); |
|
1533 |
||
1534 |
if(!d->data->fileEngine) |
|
1535 |
return false; |
|
1536 |
const QAbstractFileEngine::FileFlags info = d->data->fileEngine->fileFlags(QAbstractFileEngine::DirectoryType |
|
1537 |
|QAbstractFileEngine::PermsMask); |
|
1538 |
if(!(info & QAbstractFileEngine::DirectoryType)) |
|
1539 |
return false; |
|
1540 |
return info & QAbstractFileEngine::ReadUserPerm; |
|
1541 |
} |
|
1542 |
||
1543 |
/*! |
|
1544 |
\overload |
|
1545 |
||
1546 |
Returns true if the directory exists; otherwise returns false. |
|
1547 |
(If a file with the same name is found this function will return false). |
|
1548 |
||
1549 |
The overload of this function that accepts an argument is used to test |
|
1550 |
for the presence of files and directories within a directory. |
|
1551 |
||
1552 |
\sa QFileInfo::exists(), QFile::exists() |
|
1553 |
*/ |
|
1554 |
||
1555 |
bool QDir::exists() const |
|
1556 |
{ |
|
1557 |
Q_D(const QDir); |
|
1558 |
||
1559 |
if(!d->data->fileEngine) |
|
1560 |
return false; |
|
1561 |
const QAbstractFileEngine::FileFlags info = |
|
1562 |
d->data->fileEngine->fileFlags( |
|
1563 |
QAbstractFileEngine::DirectoryType |
|
1564 |
| QAbstractFileEngine::ExistsFlag |
|
1565 |
| QAbstractFileEngine::Refresh); |
|
1566 |
if(!(info & QAbstractFileEngine::DirectoryType)) |
|
1567 |
return false; |
|
1568 |
return info & QAbstractFileEngine::ExistsFlag; |
|
1569 |
} |
|
1570 |
||
1571 |
/*! |
|
1572 |
Returns true if the directory is the root directory; otherwise |
|
1573 |
returns false. |
|
1574 |
||
1575 |
Note: If the directory is a symbolic link to the root directory |
|
1576 |
this function returns false. If you want to test for this use |
|
1577 |
canonicalPath(), e.g. |
|
1578 |
||
1579 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 9 |
|
1580 |
||
1581 |
\sa root(), rootPath() |
|
1582 |
*/ |
|
1583 |
||
1584 |
bool QDir::isRoot() const |
|
1585 |
{ |
|
1586 |
Q_D(const QDir); |
|
1587 |
||
1588 |
if(!d->data->fileEngine) |
|
1589 |
return true; |
|
1590 |
return d->data->fileEngine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::RootFlag; |
|
1591 |
} |
|
1592 |
||
1593 |
/*! |
|
1594 |
\fn bool QDir::isAbsolute() const |
|
1595 |
||
1596 |
Returns true if the directory's path is absolute; otherwise |
|
1597 |
returns false. See isAbsolutePath(). |
|
1598 |
||
1599 |
\sa isRelative() makeAbsolute() cleanPath() |
|
1600 |
*/ |
|
1601 |
||
1602 |
/*! |
|
1603 |
\fn bool QDir::isAbsolutePath(const QString &) |
|
1604 |
||
1605 |
Returns true if \a path is absolute; returns false if it is |
|
1606 |
relative. |
|
1607 |
||
1608 |
\sa isAbsolute() isRelativePath() makeAbsolute() cleanPath() |
|
1609 |
*/ |
|
1610 |
||
1611 |
/*! |
|
1612 |
Returns true if the directory path is relative; otherwise returns |
|
1613 |
false. (Under Unix a path is relative if it does not start with a |
|
1614 |
"/"). |
|
1615 |
||
1616 |
\sa makeAbsolute() isAbsolute() isAbsolutePath() cleanPath() |
|
1617 |
*/ |
|
1618 |
||
1619 |
bool QDir::isRelative() const |
|
1620 |
{ |
|
1621 |
Q_D(const QDir); |
|
1622 |
||
1623 |
if(!d->data->fileEngine) |
|
1624 |
return false; |
|
1625 |
return d->data->fileEngine->isRelativePath(); |
|
1626 |
} |
|
1627 |
||
1628 |
||
1629 |
/*! |
|
1630 |
Converts the directory path to an absolute path. If it is already |
|
1631 |
absolute nothing happens. Returns true if the conversion |
|
1632 |
succeeded; otherwise returns false. |
|
1633 |
||
1634 |
\sa isAbsolute() isAbsolutePath() isRelative() cleanPath() |
|
1635 |
*/ |
|
1636 |
||
1637 |
bool QDir::makeAbsolute() // ### What do the return values signify? |
|
1638 |
{ |
|
1639 |
Q_D(QDir); |
|
1640 |
||
1641 |
if(!d->data->fileEngine) |
|
1642 |
return false; |
|
1643 |
QString absolutePath = d->data->fileEngine->fileName(QAbstractFileEngine::AbsoluteName); |
|
1644 |
if(QDir::isRelativePath(absolutePath)) |
|
1645 |
return false; |
|
1646 |
d->detach(); |
|
1647 |
d->data->path = absolutePath; |
|
1648 |
d->data->fileEngine->setFileName(absolutePath); |
|
1649 |
if(!(d->data->fileEngine->fileFlags(QAbstractFileEngine::TypesMask) & QAbstractFileEngine::DirectoryType)) |
|
1650 |
return false; |
|
1651 |
return true; |
|
1652 |
} |
|
1653 |
||
1654 |
/*! |
|
1655 |
Returns true if directory \a dir and this directory have the same |
|
1656 |
path and their sort and filter settings are the same; otherwise |
|
1657 |
returns false. |
|
1658 |
||
1659 |
Example: |
|
1660 |
||
1661 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 10 |
|
1662 |
*/ |
|
1663 |
||
1664 |
bool QDir::operator==(const QDir &dir) const |
|
1665 |
{ |
|
1666 |
const QDirPrivate *d = d_func(); |
|
1667 |
const QDirPrivate *other = dir.d_func(); |
|
1668 |
||
1669 |
if(d->data == other->data) |
|
1670 |
return true; |
|
1671 |
Q_ASSERT(d->data->fileEngine && other->data->fileEngine); |
|
1672 |
if(d->data->fileEngine->caseSensitive() != other->data->fileEngine->caseSensitive()) |
|
1673 |
return false; |
|
1674 |
if(d->data->filters == other->data->filters |
|
1675 |
&& d->data->sort == other->data->sort |
|
1676 |
&& d->data->nameFilters == other->data->nameFilters) { |
|
1677 |
QString dir1 = absolutePath(), dir2 = dir.absolutePath(); |
|
1678 |
if(!other->data->fileEngine->caseSensitive()) |
|
1679 |
return (dir1.toLower() == dir2.toLower()); |
|
1680 |
||
1681 |
return (dir1 == dir2); |
|
1682 |
||
1683 |
} |
|
1684 |
return false; |
|
1685 |
} |
|
1686 |
||
1687 |
/*! |
|
1688 |
Makes a copy of the \a dir object and assigns it to this QDir |
|
1689 |
object. |
|
1690 |
*/ |
|
1691 |
||
1692 |
QDir &QDir::operator=(const QDir &dir) |
|
1693 |
{ |
|
1694 |
if (this == &dir) |
|
1695 |
return *this; |
|
1696 |
||
1697 |
Q_D(QDir); |
|
1698 |
qAtomicAssign(d->data, dir.d_func()->data); |
|
1699 |
return *this; |
|
1700 |
} |
|
1701 |
||
1702 |
/*! |
|
1703 |
\overload |
|
1704 |
\obsolete |
|
1705 |
||
1706 |
Sets the directory path to the given \a path. |
|
1707 |
||
1708 |
Use setPath() instead. |
|
1709 |
*/ |
|
1710 |
||
1711 |
QDir &QDir::operator=(const QString &path) |
|
1712 |
{ |
|
1713 |
Q_D(QDir); |
|
1714 |
||
1715 |
d->setPath(path); |
|
1716 |
return *this; |
|
1717 |
} |
|
1718 |
||
1719 |
/*! |
|
1720 |
\fn bool QDir::operator!=(const QDir &dir) const |
|
1721 |
||
1722 |
Returns true if directory \a dir and this directory have different |
|
1723 |
paths or different sort or filter settings; otherwise returns |
|
1724 |
false. |
|
1725 |
||
1726 |
Example: |
|
1727 |
||
1728 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 11 |
|
1729 |
*/ |
|
1730 |
||
1731 |
||
1732 |
/*! |
|
1733 |
Removes the file, \a fileName. |
|
1734 |
||
1735 |
Returns true if the file is removed successfully; otherwise |
|
1736 |
returns false. |
|
1737 |
*/ |
|
1738 |
||
1739 |
bool QDir::remove(const QString &fileName) |
|
1740 |
{ |
|
1741 |
if (fileName.isEmpty()) { |
|
1742 |
qWarning("QDir::remove: Empty or null file name"); |
|
1743 |
return false; |
|
1744 |
} |
|
1745 |
QString p = filePath(fileName); |
|
1746 |
return QFile::remove(p); |
|
1747 |
} |
|
1748 |
||
1749 |
/*! |
|
1750 |
Renames a file or directory from \a oldName to \a newName, and returns |
|
1751 |
true if successful; otherwise returns false. |
|
1752 |
||
1753 |
On most file systems, rename() fails only if \a oldName does not |
|
1754 |
exist, if \a newName and \a oldName are not on the same |
|
1755 |
partition or if a file with the new name already exists. |
|
1756 |
However, there are also other reasons why rename() can |
|
1757 |
fail. For example, on at least one file system rename() fails if |
|
1758 |
\a newName points to an open file. |
|
1759 |
*/ |
|
1760 |
||
1761 |
bool QDir::rename(const QString &oldName, const QString &newName) |
|
1762 |
{ |
|
1763 |
Q_D(QDir); |
|
1764 |
||
1765 |
if (oldName.isEmpty() || newName.isEmpty()) { |
|
1766 |
qWarning("QDir::rename: Empty or null file name(s)"); |
|
1767 |
return false; |
|
1768 |
} |
|
1769 |
if(!d->data->fileEngine) |
|
1770 |
return false; |
|
1771 |
||
1772 |
QFile file(filePath(oldName)); |
|
1773 |
if(!file.exists()) |
|
1774 |
return false; |
|
1775 |
return file.rename(filePath(newName)); |
|
1776 |
} |
|
1777 |
||
1778 |
/*! |
|
1779 |
Returns true if the file called \a name exists; otherwise returns |
|
1780 |
false. |
|
1781 |
||
1782 |
Unless \a name contains an absolute file path, the file name is assumed |
|
1783 |
to be relative to the directory itself, so this function is typically used |
|
1784 |
to check for the presence of files within a directory. |
|
1785 |
||
1786 |
\sa QFileInfo::exists(), QFile::exists() |
|
1787 |
*/ |
|
1788 |
||
1789 |
bool QDir::exists(const QString &name) const |
|
1790 |
{ |
|
1791 |
if (name.isEmpty()) { |
|
1792 |
qWarning("QDir::exists: Empty or null file name"); |
|
1793 |
return false; |
|
1794 |
} |
|
1795 |
QString tmp = filePath(name); |
|
1796 |
return QFile::exists(tmp); |
|
1797 |
} |
|
1798 |
||
1799 |
/*! |
|
1800 |
Returns a list of the root directories on this system. |
|
1801 |
||
1802 |
On Windows this returns a list of QFileInfo objects containing "C:/", |
|
1803 |
"D:/", etc. On other operating systems, it returns a list containing |
|
1804 |
just one root directory (i.e. "/"). |
|
1805 |
||
1806 |
\sa root(), rootPath() |
|
1807 |
*/ |
|
1808 |
||
1809 |
QFileInfoList QDir::drives() |
|
1810 |
{ |
|
1811 |
#ifdef QT_NO_FSFILEENGINE |
|
1812 |
return QFileInfoList(); |
|
1813 |
#else |
|
1814 |
return QFSFileEngine::drives(); |
|
1815 |
#endif |
|
1816 |
} |
|
1817 |
||
1818 |
/*! |
|
1819 |
Returns the native directory separator: "/" under Unix (including |
|
1820 |
Mac OS X) and "\\" under Windows. |
|
1821 |
||
1822 |
You do not need to use this function to build file paths. If you |
|
1823 |
always use "/", Qt will translate your paths to conform to the |
|
1824 |
underlying operating system. If you want to display paths to the |
|
1825 |
user using their operating system's separator use |
|
1826 |
toNativeSeparators(). |
|
1827 |
*/ |
|
1828 |
||
1829 |
QChar QDir::separator() |
|
1830 |
{ |
|
1831 |
#if defined (Q_FS_FAT) || defined(Q_WS_WIN) || defined(Q_OS_SYMBIAN) |
|
1832 |
return QLatin1Char('\\'); |
|
1833 |
#elif defined(Q_OS_UNIX) |
|
1834 |
return QLatin1Char('/'); |
|
1835 |
#elif defined (Q_OS_MAC) |
|
1836 |
return QLatin1Char(':'); |
|
1837 |
#else |
|
1838 |
return QLatin1Char('/'); |
|
1839 |
#endif |
|
1840 |
} |
|
1841 |
||
1842 |
/*! |
|
1843 |
Sets the application's current working directory to \a path. |
|
1844 |
Returns true if the directory was successfully changed; otherwise |
|
1845 |
returns false. |
|
1846 |
||
1847 |
\sa current() currentPath() home() root() temp() |
|
1848 |
*/ |
|
1849 |
||
1850 |
bool QDir::setCurrent(const QString &path) |
|
1851 |
{ |
|
1852 |
#ifdef QT_NO_FSFILEENGINE |
|
1853 |
Q_UNUSED(path); |
|
1854 |
return false; |
|
1855 |
#else |
|
1856 |
return QFSFileEngine::setCurrentPath(path); |
|
1857 |
#endif |
|
1858 |
} |
|
1859 |
||
1860 |
/*! |
|
1861 |
\fn QDir QDir::current() |
|
1862 |
||
1863 |
Returns the application's current directory. |
|
1864 |
||
1865 |
The directory is constructed using the absolute path of the current directory, |
|
1866 |
ensuring that its path() will be the same as its absolutePath(). |
|
1867 |
||
1868 |
\sa currentPath(), home(), root(), temp() |
|
1869 |
*/ |
|
1870 |
||
1871 |
/*! |
|
1872 |
Returns the absolute path of the application's current directory. |
|
1873 |
||
1874 |
\sa current(), homePath(), rootPath(), tempPath() |
|
1875 |
*/ |
|
1876 |
QString QDir::currentPath() |
|
1877 |
{ |
|
1878 |
#ifdef QT_NO_FSFILEENGINE |
|
1879 |
return QString(); |
|
1880 |
#else |
|
1881 |
return QFSFileEngine::currentPath(); |
|
1882 |
#endif |
|
1883 |
} |
|
1884 |
||
1885 |
/*! |
|
1886 |
\fn QString QDir::currentDirPath() |
|
1887 |
Returns the absolute path of the application's current directory. |
|
1888 |
||
1889 |
Use currentPath() instead. |
|
1890 |
||
1891 |
\sa currentPath() |
|
1892 |
*/ |
|
1893 |
||
1894 |
/*! |
|
1895 |
\fn QDir QDir::home() |
|
1896 |
||
1897 |
Returns the user's home directory. |
|
1898 |
||
1899 |
The directory is constructed using the absolute path of the home directory, |
|
1900 |
ensuring that its path() will be the same as its absolutePath(). |
|
1901 |
||
1902 |
See homePath() for details. |
|
1903 |
||
1904 |
\sa drives(), current(), root(), temp() |
|
1905 |
*/ |
|
1906 |
||
1907 |
/*! |
|
1908 |
Returns the absolute path of the user's home directory. |
|
1909 |
||
1910 |
Under Windows this function will return the directory of the |
|
1911 |
current user's profile. Typically, this is: |
|
1912 |
||
1913 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 12 |
|
1914 |
||
1915 |
Use the toNativeSeparators() function to convert the separators to |
|
1916 |
the ones that are appropriate for the underlying operating system. |
|
1917 |
||
1918 |
If the directory of the current user's profile does not exist or |
|
1919 |
cannot be retrieved, the following alternatives will be checked (in |
|
1920 |
the given order) until an existing and available path is found: |
|
1921 |
||
1922 |
\list 1 |
|
1923 |
\o The path specified by the \c USERPROFILE environment variable. |
|
1924 |
\o The path formed by concatenating the \c HOMEDRIVE and \c HOMEPATH |
|
1925 |
environment variables. |
|
1926 |
\o The path specified by the \c HOME environment variable. |
|
1927 |
\o The path returned by the rootPath() function (which uses the \c SystemDrive |
|
1928 |
environment variable) |
|
1929 |
\o The \c{C:/} directory. |
|
1930 |
\endlist |
|
1931 |
||
1932 |
Under non-Windows operating systems the \c HOME environment |
|
1933 |
variable is used if it exists, otherwise the path returned by the |
|
1934 |
rootPath(). On Symbian always the same as the path returned by the rootPath(). |
|
1935 |
||
1936 |
\sa home(), currentPath(), rootPath(), tempPath() |
|
1937 |
*/ |
|
1938 |
QString QDir::homePath() |
|
1939 |
{ |
|
1940 |
#ifdef QT_NO_FSFILEENGINE |
|
1941 |
return QString(); |
|
1942 |
#else |
|
1943 |
return cleanPath(QFSFileEngine::homePath()); |
|
1944 |
#endif |
|
1945 |
} |
|
1946 |
||
1947 |
/*! |
|
1948 |
\fn QString QDir::homeDirPath() |
|
1949 |
||
1950 |
Returns the absolute path of the user's home directory. |
|
1951 |
||
1952 |
Use homePath() instead. |
|
1953 |
||
1954 |
\sa homePath() |
|
1955 |
*/ |
|
1956 |
||
1957 |
/*! |
|
1958 |
\fn QDir QDir::temp() |
|
1959 |
||
1960 |
Returns the system's temporary directory. |
|
1961 |
||
1962 |
The directory is constructed using the absolute path of the temporary directory, |
|
1963 |
ensuring that its path() will be the same as its absolutePath(). |
|
1964 |
||
1965 |
See tempPath() for details. |
|
1966 |
||
1967 |
\sa drives(), current(), home(), root() |
|
1968 |
*/ |
|
1969 |
||
1970 |
/*! |
|
1971 |
Returns the absolute path of the system's temporary directory. |
|
1972 |
||
1973 |
On Unix/Linux systems this is usually \c{/tmp}; on Windows this is |
|
1974 |
usually the path in the \c TEMP or \c TMP environment |
|
1975 |
variable. Whether a directory separator is added to the end or |
|
1976 |
not, depends on the operating system. |
|
1977 |
||
1978 |
\sa temp(), currentPath(), homePath(), rootPath() |
|
1979 |
*/ |
|
1980 |
QString QDir::tempPath() |
|
1981 |
{ |
|
1982 |
#ifdef QT_NO_FSFILEENGINE |
|
1983 |
return QString(); |
|
1984 |
#else |
|
1985 |
return cleanPath(QFSFileEngine::tempPath()); |
|
1986 |
#endif |
|
1987 |
} |
|
1988 |
||
1989 |
/*! |
|
1990 |
\fn QDir QDir::root() |
|
1991 |
||
1992 |
Returns the root directory. |
|
1993 |
||
1994 |
The directory is constructed using the absolute path of the root directory, |
|
1995 |
ensuring that its path() will be the same as its absolutePath(). |
|
1996 |
||
1997 |
See rootPath() for details. |
|
1998 |
||
1999 |
\sa drives(), current(), home(), temp() |
|
2000 |
*/ |
|
2001 |
||
2002 |
/*! |
|
2003 |
Returns the absolute path of the root directory. |
|
2004 |
||
2005 |
For Unix operating systems this returns "/". For Windows file |
|
2006 |
systems this normally returns "c:/". On Symbian this typically returns |
|
2007 |
"c:/data", i.e. the same as native PathInfo::PhoneMemoryRootPath(). |
|
2008 |
||
2009 |
\sa root(), drives(), currentPath(), homePath(), tempPath() |
|
2010 |
*/ |
|
2011 |
QString QDir::rootPath() |
|
2012 |
{ |
|
2013 |
#ifdef QT_NO_FSFILEENGINE |
|
2014 |
return QString(); |
|
2015 |
#else |
|
2016 |
return QFSFileEngine::rootPath(); |
|
2017 |
#endif |
|
2018 |
} |
|
2019 |
||
2020 |
/*! |
|
2021 |
\fn QString QDir::rootDirPath() |
|
2022 |
||
2023 |
Returns the absolute path of the root directory. |
|
2024 |
||
2025 |
Use rootPath() instead. |
|
2026 |
||
2027 |
\sa rootPath() |
|
2028 |
*/ |
|
2029 |
||
2030 |
#ifndef QT_NO_REGEXP |
|
2031 |
/*! |
|
2032 |
\overload |
|
2033 |
||
2034 |
Returns true if the \a fileName matches any of the wildcard (glob) |
|
2035 |
patterns in the list of \a filters; otherwise returns false. The |
|
2036 |
matching is case insensitive. |
|
2037 |
||
2038 |
\sa {QRegExp wildcard matching}, QRegExp::exactMatch() entryList() entryInfoList() |
|
2039 |
*/ |
|
2040 |
||
2041 |
||
2042 |
bool QDir::match(const QStringList &filters, const QString &fileName) |
|
2043 |
{ |
|
2044 |
for(QStringList::ConstIterator sit = filters.begin(); sit != filters.end(); ++sit) { |
|
2045 |
QRegExp rx(*sit, Qt::CaseInsensitive, QRegExp::Wildcard); |
|
2046 |
if (rx.exactMatch(fileName)) |
|
2047 |
return true; |
|
2048 |
} |
|
2049 |
return false; |
|
2050 |
} |
|
2051 |
||
2052 |
/*! |
|
2053 |
Returns true if the \a fileName matches the wildcard (glob) |
|
2054 |
pattern \a filter; otherwise returns false. The \a filter may |
|
2055 |
contain multiple patterns separated by spaces or semicolons. |
|
2056 |
The matching is case insensitive. |
|
2057 |
||
2058 |
\sa {QRegExp wildcard matching}, QRegExp::exactMatch() entryList() entryInfoList() |
|
2059 |
*/ |
|
2060 |
||
2061 |
bool QDir::match(const QString &filter, const QString &fileName) |
|
2062 |
{ |
|
2063 |
return match(nameFiltersFromString(filter), fileName); |
|
2064 |
} |
|
2065 |
#endif |
|
2066 |
||
2067 |
/*! |
|
2068 |
Removes all multiple directory separators "/" and resolves any |
|
2069 |
"."s or ".."s found in the path, \a path. |
|
2070 |
||
2071 |
Symbolic links are kept. This function does not return the |
|
2072 |
canonical path, but rather the simplest version of the input. |
|
2073 |
For example, "./local" becomes "local", "local/../bin" becomes |
|
2074 |
"bin" and "/local/usr/../bin" becomes "/local/bin". |
|
2075 |
||
2076 |
\sa absolutePath() canonicalPath() |
|
2077 |
*/ |
|
2078 |
||
2079 |
QString QDir::cleanPath(const QString &path) |
|
2080 |
{ |
|
2081 |
if (path.isEmpty()) |
|
2082 |
return path; |
|
2083 |
QString name = path; |
|
2084 |
QChar dir_separator = separator(); |
|
2085 |
if(dir_separator != QLatin1Char('/')) |
|
2086 |
name.replace(dir_separator, QLatin1Char('/')); |
|
2087 |
||
2088 |
int used = 0, levels = 0; |
|
2089 |
const int len = name.length(); |
|
2090 |
QVarLengthArray<QChar> outVector(len); |
|
2091 |
QChar *out = outVector.data(); |
|
2092 |
||
2093 |
const QChar *p = name.unicode(); |
|
2094 |
for(int i = 0, last = -1, iwrite = 0; i < len; i++) { |
|
2095 |
if(p[i] == QLatin1Char('/')) { |
|
2096 |
while(i < len-1 && p[i+1] == QLatin1Char('/')) { |
|
2097 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) //allow unc paths |
|
2098 |
if(!i) |
|
2099 |
break; |
|
2100 |
#endif |
|
2101 |
i++; |
|
2102 |
} |
|
2103 |
bool eaten = false; |
|
2104 |
if(i < len - 1 && p[i+1] == QLatin1Char('.')) { |
|
2105 |
int dotcount = 1; |
|
2106 |
if(i < len - 2 && p[i+2] == QLatin1Char('.')) |
|
2107 |
dotcount++; |
|
2108 |
if(i == len - dotcount - 1) { |
|
2109 |
if(dotcount == 1) { |
|
2110 |
break; |
|
2111 |
} else if(levels) { |
|
2112 |
if(last == -1) { |
|
2113 |
for(int i2 = iwrite-1; i2 >= 0; i2--) { |
|
2114 |
if(out[i2] == QLatin1Char('/')) { |
|
2115 |
last = i2; |
|
2116 |
break; |
|
2117 |
} |
|
2118 |
} |
|
2119 |
} |
|
2120 |
used -= iwrite - last - 1; |
|
2121 |
break; |
|
2122 |
} |
|
2123 |
} else if(p[i+dotcount+1] == QLatin1Char('/')) { |
|
2124 |
if(dotcount == 2 && levels) { |
|
2125 |
if(last == -1 || iwrite - last == 1) { |
|
2126 |
for(int i2 = (last == -1) ? (iwrite-1) : (last-1); i2 >= 0; i2--) { |
|
2127 |
if(out[i2] == QLatin1Char('/')) { |
|
2128 |
eaten = true; |
|
2129 |
last = i2; |
|
2130 |
break; |
|
2131 |
} |
|
2132 |
} |
|
2133 |
} else { |
|
2134 |
eaten = true; |
|
2135 |
} |
|
2136 |
if(eaten) { |
|
2137 |
levels--; |
|
2138 |
used -= iwrite - last; |
|
2139 |
iwrite = last; |
|
2140 |
last = -1; |
|
2141 |
} |
|
2142 |
} else if (dotcount == 2 && i > 0 && p[i - 1] != QLatin1Char('.')) { |
|
2143 |
eaten = true; |
|
2144 |
used -= iwrite - qMax(0, last); |
|
2145 |
iwrite = qMax(0, last); |
|
2146 |
last = -1; |
|
2147 |
++i; |
|
2148 |
} else if(dotcount == 1) { |
|
2149 |
eaten = true; |
|
2150 |
} |
|
2151 |
if(eaten) |
|
2152 |
i += dotcount; |
|
2153 |
} else { |
|
2154 |
levels++; |
|
2155 |
} |
|
2156 |
} else if(last != -1 && iwrite - last == 1) { |
|
2157 |
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) |
|
2158 |
eaten = (iwrite > 2); |
|
2159 |
#else |
|
2160 |
eaten = true; |
|
2161 |
#endif |
|
2162 |
last = -1; |
|
2163 |
} else if(last != -1 && i == len-1) { |
|
2164 |
eaten = true; |
|
2165 |
} else { |
|
2166 |
levels++; |
|
2167 |
} |
|
2168 |
if(!eaten) |
|
2169 |
last = i - (i - iwrite); |
|
2170 |
else |
|
2171 |
continue; |
|
2172 |
} else if(!i && p[i] == QLatin1Char('.')) { |
|
2173 |
int dotcount = 1; |
|
2174 |
if(len >= 1 && p[1] == QLatin1Char('.')) |
|
2175 |
dotcount++; |
|
2176 |
if(len >= dotcount && p[dotcount] == QLatin1Char('/')) { |
|
2177 |
if(dotcount == 1) { |
|
2178 |
i++; |
|
2179 |
while(i+1 < len-1 && p[i+1] == QLatin1Char('/')) |
|
2180 |
i++; |
|
2181 |
continue; |
|
2182 |
} |
|
2183 |
} |
|
2184 |
} |
|
2185 |
out[iwrite++] = p[i]; |
|
2186 |
used++; |
|
2187 |
} |
|
2188 |
QString ret; |
|
2189 |
if(used == len) |
|
2190 |
ret = name; |
|
2191 |
else |
|
2192 |
ret = QString(out, used); |
|
2193 |
||
2194 |
// Strip away last slash except for root directories |
|
2195 |
if (ret.endsWith(QLatin1Char('/')) |
|
2196 |
&& !(ret.size() == 1 || (ret.size() == 3 && ret.at(1) == QLatin1Char(':')))) |
|
2197 |
ret = ret.left(ret.length() - 1); |
|
2198 |
||
2199 |
return ret; |
|
2200 |
} |
|
2201 |
||
2202 |
/*! |
|
2203 |
Returns true if \a path is relative; returns false if it is |
|
2204 |
absolute. |
|
2205 |
||
2206 |
\sa isRelative() isAbsolutePath() makeAbsolute() |
|
2207 |
*/ |
|
2208 |
||
2209 |
bool QDir::isRelativePath(const QString &path) |
|
2210 |
{ |
|
2211 |
return QFileInfo(path).isRelative(); |
|
2212 |
} |
|
2213 |
||
2214 |
/*! |
|
2215 |
Refreshes the directory information. |
|
2216 |
*/ |
|
2217 |
||
2218 |
void QDir::refresh() const |
|
2219 |
{ |
|
2220 |
Q_D(const QDir); |
|
2221 |
||
2222 |
d->data->clear(); |
|
2223 |
} |
|
2224 |
||
2225 |
/*! |
|
2226 |
\internal |
|
2227 |
||
2228 |
Returns a list of name filters from the given \a nameFilter. (If |
|
2229 |
there is more than one filter, each pair of filters is separated |
|
2230 |
by a space or by a semicolon.) |
|
2231 |
*/ |
|
2232 |
||
2233 |
QStringList QDir::nameFiltersFromString(const QString &nameFilter) |
|
2234 |
{ |
|
2235 |
return QDirPrivate::splitFilters(nameFilter); |
|
2236 |
} |
|
2237 |
||
2238 |
/*! |
|
2239 |
\macro void Q_INIT_RESOURCE(name) |
|
2240 |
\relates QDir |
|
2241 |
||
2242 |
Initializes the resources specified by the \c .qrc file with the |
|
2243 |
specified base \a name. Normally, Qt resources are loaded |
|
2244 |
automatically at startup. The Q_INIT_RESOURCE() macro is |
|
2245 |
necessary on some platforms for resources stored in a static |
|
2246 |
library. |
|
2247 |
||
2248 |
For example, if your application's resources are listed in a file |
|
2249 |
called \c myapp.qrc, you can ensure that the resources are |
|
2250 |
initialized at startup by adding this line to your \c main() |
|
2251 |
function: |
|
2252 |
||
2253 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 13 |
|
2254 |
||
2255 |
If the file name contains characters that cannot be part of a valid C++ function name |
|
2256 |
(such as '-'), they have to be replaced by the underscore character ('_'). |
|
2257 |
||
2258 |
Note: This macro cannot be used in a namespace. It should be called from |
|
2259 |
main(). If that is not possible, the following workaround can be used |
|
2260 |
to init the resource \c myapp from the function \c{MyNamespace::myFunction}: |
|
2261 |
||
2262 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 14 |
|
2263 |
||
2264 |
\sa Q_CLEANUP_RESOURCE(), {The Qt Resource System} |
|
2265 |
*/ |
|
2266 |
||
2267 |
/*! |
|
2268 |
\since 4.1 |
|
2269 |
\macro void Q_CLEANUP_RESOURCE(name) |
|
2270 |
\relates QDir |
|
2271 |
||
2272 |
Unloads the resources specified by the \c .qrc file with the base |
|
2273 |
name \a name. |
|
2274 |
||
2275 |
Normally, Qt resources are unloaded automatically when the |
|
2276 |
application terminates, but if the resources are located in a |
|
2277 |
plugin that is being unloaded, call Q_CLEANUP_RESOURCE() to force |
|
2278 |
removal of your resources. |
|
2279 |
||
2280 |
Note: This macro cannot be used in a namespace. Please see the |
|
2281 |
Q_INIT_RESOURCE documentation for a workaround. |
|
2282 |
||
2283 |
Example: |
|
2284 |
||
2285 |
\snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 15 |
|
2286 |
||
2287 |
\sa Q_INIT_RESOURCE(), {The Qt Resource System} |
|
2288 |
*/ |
|
2289 |
||
2290 |
#ifdef QT3_SUPPORT |
|
2291 |
||
2292 |
/*! |
|
2293 |
\fn bool QDir::matchAllDirs() const |
|
2294 |
||
2295 |
Use filter() & AllDirs instead. |
|
2296 |
*/ |
|
2297 |
bool QDir::matchAllDirs() const |
|
2298 |
{ |
|
2299 |
Q_D(const QDir); |
|
2300 |
return d->matchAllDirs; |
|
2301 |
} |
|
2302 |
||
2303 |
||
2304 |
/*! |
|
2305 |
\fn void QDir::setMatchAllDirs(bool on) |
|
2306 |
||
2307 |
Use setFilter() instead. |
|
2308 |
*/ |
|
2309 |
void QDir::setMatchAllDirs(bool on) |
|
2310 |
{ |
|
2311 |
Q_D(QDir); |
|
2312 |
d->matchAllDirs = on; |
|
2313 |
} |
|
2314 |
||
2315 |
/*! |
|
2316 |
Use nameFilters() instead. |
|
2317 |
*/ |
|
2318 |
QString QDir::nameFilter() const |
|
2319 |
{ |
|
2320 |
Q_D(const QDir); |
|
2321 |
||
2322 |
return nameFilters().join(QString(d->filterSepChar)); |
|
2323 |
} |
|
2324 |
||
2325 |
/*! |
|
2326 |
Use setNameFilters() instead. |
|
2327 |
||
2328 |
The \a nameFilter is a wildcard (globbing) filter that understands |
|
2329 |
"*" and "?" wildcards. (See \l{QRegExp wildcard matching}.) You may |
|
2330 |
specify several filter entries, each separated by spaces or by |
|
2331 |
semicolons. |
|
2332 |
||
2333 |
For example, if you want entryList() and entryInfoList() to list |
|
2334 |
all files ending with either ".cpp" or ".h", you would use either |
|
2335 |
dir.setNameFilters("*.cpp *.h") or dir.setNameFilters("*.cpp;*.h"). |
|
2336 |
||
2337 |
\oldcode |
|
2338 |
QString filter = "*.cpp *.cxx *.cc"; |
|
2339 |
dir.setNameFilter(filter); |
|
2340 |
\newcode |
|
2341 |
QString filter = "*.cpp *.cxx *.cc"; |
|
2342 |
dir.setNameFilters(filter.split(' ')); |
|
2343 |
\endcode |
|
2344 |
*/ |
|
2345 |
void QDir::setNameFilter(const QString &nameFilter) |
|
2346 |
{ |
|
2347 |
Q_D(QDir); |
|
2348 |
||
2349 |
d->filterSepChar = QDirPrivate::getFilterSepChar(nameFilter); |
|
2350 |
setNameFilters(QDirPrivate::splitFilters(nameFilter, d->filterSepChar)); |
|
2351 |
} |
|
2352 |
||
2353 |
/*! |
|
2354 |
\fn QString QDir::absPath() const |
|
2355 |
||
2356 |
Use absolutePath() instead. |
|
2357 |
*/ |
|
2358 |
||
2359 |
/*! |
|
2360 |
\fn QString QDir::absFilePath(const QString &fileName, bool acceptAbsPath) const |
|
2361 |
||
2362 |
Use absoluteFilePath(\a fileName) instead. |
|
2363 |
||
2364 |
The \a acceptAbsPath parameter is ignored. |
|
2365 |
*/ |
|
2366 |
||
2367 |
/*! |
|
2368 |
\fn bool QDir::mkdir(const QString &dirName, bool acceptAbsPath) const |
|
2369 |
||
2370 |
Use mkdir(\a dirName) instead. |
|
2371 |
||
2372 |
The \a acceptAbsPath parameter is ignored. |
|
2373 |
*/ |
|
2374 |
||
2375 |
/*! |
|
2376 |
\fn bool QDir::rmdir(const QString &dirName, bool acceptAbsPath) const |
|
2377 |
||
2378 |
Use rmdir(\a dirName) instead. |
|
2379 |
||
2380 |
The \a acceptAbsPath parameter is ignored. |
|
2381 |
*/ |
|
2382 |
||
2383 |
/*! |
|
2384 |
\fn QStringList QDir::entryList(const QString &nameFilter, Filters filters, |
|
2385 |
SortFlags sort) const |
|
2386 |
\overload |
|
2387 |
||
2388 |
Use the overload that takes a name filter string list as first |
|
2389 |
argument instead of a combination of attribute filter flags. |
|
2390 |
*/ |
|
2391 |
||
2392 |
/*! |
|
2393 |
\fn QFileInfoList QDir::entryInfoList(const QString &nameFilter, Filters filters, |
|
2394 |
SortFlags sort) const |
|
2395 |
\overload |
|
2396 |
||
2397 |
Use the overload that takes a name filter string list as first |
|
2398 |
argument instead of a combination of attribute filter flags. |
|
2399 |
*/ |
|
2400 |
||
2401 |
/*! |
|
2402 |
\fn void QDir::convertToAbs() |
|
2403 |
||
2404 |
Use makeAbsolute() instead. |
|
2405 |
*/ |
|
2406 |
||
2407 |
/*! |
|
2408 |
\fn QString QDir::cleanDirPath(const QString &name) |
|
2409 |
||
2410 |
Use cleanPath() instead. |
|
2411 |
*/ |
|
2412 |
||
2413 |
/*! |
|
2414 |
\typedef QDir::FilterSpec |
|
2415 |
||
2416 |
Use QDir::Filters instead. |
|
2417 |
*/ |
|
2418 |
||
2419 |
/*! |
|
2420 |
\typedef QDir::SortSpec |
|
2421 |
||
2422 |
Use QDir::SortFlags instead. |
|
2423 |
*/ |
|
2424 |
#endif |
|
2425 |
#ifndef QT_NO_DEBUG_STREAM |
|
2426 |
QDebug operator<<(QDebug debug, QDir::Filters filters) |
|
2427 |
{ |
|
2428 |
QStringList flags; |
|
2429 |
if (filters == QDir::NoFilter) { |
|
2430 |
flags << QLatin1String("NoFilter"); |
|
2431 |
} else { |
|
2432 |
if (filters & QDir::Dirs) flags << QLatin1String("Dirs"); |
|
2433 |
if (filters & QDir::AllDirs) flags << QLatin1String("AllDirs"); |
|
2434 |
if (filters & QDir::Files) flags << QLatin1String("Files"); |
|
2435 |
if (filters & QDir::Drives) flags << QLatin1String("Drives"); |
|
2436 |
if (filters & QDir::NoSymLinks) flags << QLatin1String("NoSymLinks"); |
|
2437 |
if (filters & QDir::NoDotAndDotDot) flags << QLatin1String("NoDotAndDotDot"); |
|
2438 |
if ((filters & QDir::AllEntries) == QDir::AllEntries) flags << QLatin1String("AllEntries"); |
|
2439 |
if (filters & QDir::Readable) flags << QLatin1String("Readable"); |
|
2440 |
if (filters & QDir::Writable) flags << QLatin1String("Writable"); |
|
2441 |
if (filters & QDir::Executable) flags << QLatin1String("Executable"); |
|
2442 |
if (filters & QDir::Modified) flags << QLatin1String("Modified"); |
|
2443 |
if (filters & QDir::Hidden) flags << QLatin1String("Hidden"); |
|
2444 |
if (filters & QDir::System) flags << QLatin1String("System"); |
|
2445 |
if (filters & QDir::CaseSensitive) flags << QLatin1String("CaseSensitive"); |
|
2446 |
} |
|
2447 |
debug << "QDir::Filters(" << qPrintable(flags.join(QLatin1String("|"))) << ')'; |
|
2448 |
return debug; |
|
2449 |
} |
|
2450 |
||
2451 |
static QDebug operator<<(QDebug debug, QDir::SortFlags sorting) |
|
2452 |
{ |
|
2453 |
if (sorting == QDir::NoSort) { |
|
2454 |
debug << "QDir::SortFlags(NoSort)"; |
|
2455 |
} else { |
|
2456 |
QString type; |
|
2457 |
if ((sorting & 3) == QDir::Name) type = QLatin1String("Name"); |
|
2458 |
if ((sorting & 3) == QDir::Time) type = QLatin1String("Time"); |
|
2459 |
if ((sorting & 3) == QDir::Size) type = QLatin1String("Size"); |
|
2460 |
if ((sorting & 3) == QDir::Unsorted) type = QLatin1String("Unsorted"); |
|
2461 |
||
2462 |
QStringList flags; |
|
2463 |
if (sorting & QDir::DirsFirst) flags << QLatin1String("DirsFirst"); |
|
2464 |
if (sorting & QDir::DirsLast) flags << QLatin1String("DirsLast"); |
|
2465 |
if (sorting & QDir::IgnoreCase) flags << QLatin1String("IgnoreCase"); |
|
2466 |
if (sorting & QDir::LocaleAware) flags << QLatin1String("LocaleAware"); |
|
2467 |
if (sorting & QDir::Type) flags << QLatin1String("Type"); |
|
2468 |
debug << "QDir::SortFlags(" << qPrintable(type) |
|
2469 |
<< '|' |
|
2470 |
<< qPrintable(flags.join(QLatin1String("|"))) << ')'; |
|
2471 |
} |
|
2472 |
return debug; |
|
2473 |
} |
|
2474 |
||
2475 |
QDebug operator<<(QDebug debug, const QDir &dir) |
|
2476 |
{ |
|
2477 |
debug.maybeSpace() << "QDir(" << dir.path() |
|
2478 |
<< ", nameFilters = {" |
|
2479 |
<< qPrintable(dir.nameFilters().join(QLatin1String(","))) |
|
2480 |
<< "}, " |
|
2481 |
<< dir.sorting() |
|
2482 |
<< ',' |
|
2483 |
<< dir.filter() |
|
2484 |
<< ')'; |
|
2485 |
return debug.space(); |
|
2486 |
} |
|
2487 |
||
2488 |
||
2489 |
||
2490 |
#endif |
|
2491 |
||
2492 |
QT_END_NAMESPACE |