|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite 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 #include <QDebug> |
|
42 #include <QDirIterator> |
|
43 #include <QString> |
|
44 |
|
45 #ifdef Q_OS_WIN |
|
46 # include <windows.h> |
|
47 # include <atlbase.h> |
|
48 #else |
|
49 # include <sys/stat.h> |
|
50 # include <sys/types.h> |
|
51 # include <dirent.h> |
|
52 # include <errno.h> |
|
53 # include <string.h> |
|
54 #endif |
|
55 |
|
56 #include <qtest.h> |
|
57 |
|
58 #include "qfilesystemiterator.h" |
|
59 |
|
60 class tst_qdiriterator : public QObject |
|
61 { |
|
62 Q_OBJECT |
|
63 private slots: |
|
64 void posix(); |
|
65 void posix_data() { data(); } |
|
66 void diriterator(); |
|
67 void diriterator_data() { data(); } |
|
68 void fsiterator(); |
|
69 void fsiterator_data() { data(); } |
|
70 void data(); |
|
71 }; |
|
72 |
|
73 |
|
74 void tst_qdiriterator::data() |
|
75 { |
|
76 #ifdef Q_OS_WINCE |
|
77 QByteArray qtdir = qPrintable(QCoreApplication::applicationDirPath()); |
|
78 qtdir += "/depot"; |
|
79 #else |
|
80 #if defined(Q_OS_WIN) |
|
81 const char *qtdir = "C:\\depot\\qt\\main"; |
|
82 #else |
|
83 const char *qtdir = ::getenv("QTDIR"); |
|
84 #endif |
|
85 if (!qtdir) { |
|
86 fprintf(stderr, "QTDIR not set\n"); |
|
87 exit(1); |
|
88 } |
|
89 #endif |
|
90 |
|
91 QTest::addColumn<QByteArray>("dirpath"); |
|
92 QByteArray ba = QByteArray(qtdir) + "/src/corelib"; |
|
93 QByteArray ba1 = ba + "/io"; |
|
94 QTest::newRow(ba) << ba; |
|
95 //QTest::newRow(ba1) << ba1; |
|
96 } |
|
97 |
|
98 #ifdef Q_OS_WIN |
|
99 static int posix_helper(const wchar_t *dirpath) |
|
100 { |
|
101 int count = 0; |
|
102 HANDLE hSearch; |
|
103 WIN32_FIND_DATA fd; |
|
104 |
|
105 const size_t origDirPathLength = wcslen(dirpath); |
|
106 |
|
107 wchar_t appendedPath[MAX_PATH]; |
|
108 wcscpy(appendedPath, dirpath); |
|
109 wcscat(appendedPath, L"\\*"); |
|
110 hSearch = FindFirstFile(appendedPath, &fd); |
|
111 appendedPath[origDirPathLength] = 0; |
|
112 |
|
113 if (hSearch == INVALID_HANDLE_VALUE) { |
|
114 qWarning("FindFirstFile failed"); |
|
115 return count; |
|
116 } |
|
117 |
|
118 do { |
|
119 if (!(fd.cFileName[0] == L'.' && fd.cFileName[1] == 0) && |
|
120 !(fd.cFileName[0] == L'.' && fd.cFileName[1] == L'.' && fd.cFileName[2] == 0)) |
|
121 { |
|
122 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
|
123 wcscat(appendedPath, L"\\"); |
|
124 wcscat(appendedPath, fd.cFileName); |
|
125 count += posix_helper(appendedPath); |
|
126 appendedPath[origDirPathLength] = 0; |
|
127 } |
|
128 else { |
|
129 ++count; |
|
130 } |
|
131 } |
|
132 } while (FindNextFile(hSearch, &fd)); |
|
133 FindClose(hSearch); |
|
134 |
|
135 return count; |
|
136 } |
|
137 |
|
138 #else |
|
139 |
|
140 static int posix_helper(const char *dirpath) |
|
141 { |
|
142 //qDebug() << "DIR" << dirpath; |
|
143 DIR *dir = ::opendir(dirpath); |
|
144 if (!dir) |
|
145 return 0; |
|
146 |
|
147 dirent *entry = 0; |
|
148 |
|
149 int count = 0; |
|
150 while ((entry = ::readdir(dir))) { |
|
151 if (qstrcmp(entry->d_name, ".") == 0) |
|
152 continue; |
|
153 if (qstrcmp(entry->d_name, "..") == 0) |
|
154 continue; |
|
155 ++count; |
|
156 QByteArray ba = dirpath; |
|
157 ba += '/'; |
|
158 ba += entry->d_name; |
|
159 struct stat st; |
|
160 lstat(ba.constData(), &st); |
|
161 if (S_ISDIR(st.st_mode)) |
|
162 count += posix_helper(ba.constData()); |
|
163 } |
|
164 |
|
165 ::closedir(dir); |
|
166 return count; |
|
167 } |
|
168 #endif |
|
169 |
|
170 |
|
171 void tst_qdiriterator::posix() |
|
172 { |
|
173 QFETCH(QByteArray, dirpath); |
|
174 |
|
175 int count = 0; |
|
176 QString path(dirpath); |
|
177 QBENCHMARK { |
|
178 #ifdef Q_OS_WIN |
|
179 count = posix_helper(path.utf16()); |
|
180 #else |
|
181 count = posix_helper(dirpath.constData()); |
|
182 #endif |
|
183 } |
|
184 qDebug() << count; |
|
185 } |
|
186 |
|
187 void tst_qdiriterator::diriterator() |
|
188 { |
|
189 QFETCH(QByteArray, dirpath); |
|
190 |
|
191 int count = 0; |
|
192 |
|
193 QBENCHMARK { |
|
194 int c = 0; |
|
195 |
|
196 QDirIterator dir(dirpath, |
|
197 //QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot, |
|
198 //QDir::AllEntries | QDir::Hidden, |
|
199 QDir::Files, |
|
200 QDirIterator::Subdirectories); |
|
201 |
|
202 while (dir.hasNext()) { |
|
203 dir.next(); |
|
204 //printf("%s\n", qPrintable(dir.fileName())); |
|
205 0 && printf("%d %s\n", |
|
206 dir.fileInfo().isDir(), |
|
207 //qPrintable(dir.fileInfo().absoluteFilePath()), |
|
208 //qPrintable(dir.path()), |
|
209 qPrintable(dir.filePath())); |
|
210 ++c; |
|
211 } |
|
212 count = c; |
|
213 } |
|
214 qDebug() << count; |
|
215 } |
|
216 |
|
217 void tst_qdiriterator::fsiterator() |
|
218 { |
|
219 QFETCH(QByteArray, dirpath); |
|
220 |
|
221 int count = 0; |
|
222 int dump = 0; |
|
223 |
|
224 QBENCHMARK { |
|
225 int c = 0; |
|
226 |
|
227 dump && printf("\n\n\n\n"); |
|
228 QFileSystemIterator dir(dirpath, |
|
229 //QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot, |
|
230 //QDir::AllEntries | QDir::Hidden, |
|
231 //QDir::Files | QDir::NoDotAndDotDot, |
|
232 QDir::Files, |
|
233 QFileSystemIterator::Subdirectories); |
|
234 |
|
235 for (; !dir.atEnd(); dir.next()) { |
|
236 dump && printf("%d %s\n", |
|
237 dir.fileInfo().isDir(), |
|
238 //qPrintable(dir.fileInfo().absoluteFilePath()), |
|
239 //qPrintable(dir.path()), |
|
240 qPrintable(dir.filePath()) |
|
241 ); |
|
242 ++c; |
|
243 } |
|
244 count = c; |
|
245 } |
|
246 qDebug() << count; |
|
247 } |
|
248 |
|
249 QTEST_MAIN(tst_qdiriterator) |
|
250 |
|
251 #include "main.moc" |