mimetypes/qmimedatabase.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
10 -
11static QMimeDatabasePrivate *staticQMimeDatabase() { static QGlobalStatic<QMimeDatabasePrivate > thisGlobalStatic = { { (0) }, false }; if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) { QMimeDatabasePrivate *x = new QMimeDatabasePrivate; if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) delete x; else static QGlobalStaticDeleter<QMimeDatabasePrivate > cleanup(thisGlobalStatic); } return thisGlobalStatic.pointer.load(); }
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:166
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:165
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-166
12 -
13QMimeDatabasePrivate *QMimeDatabasePrivate::instance() -
14{ -
15 return staticQMimeDatabase();
executed: return staticQMimeDatabase();
Execution Count:97
97
16} -
17 -
18QMimeDatabasePrivate::QMimeDatabasePrivate() -
19 : m_provider(0), m_defaultMimeType(QLatin1String("application/octet-stream")) -
20{ -
21}
executed: }
Execution Count:1
1
22 -
23QMimeDatabasePrivate::~QMimeDatabasePrivate() -
24{ -
25 delete m_provider; -
26 m_provider = 0; -
27}
executed: }
Execution Count:2
2
28 -
29QMimeProviderBase *QMimeDatabasePrivate::provider() -
30{ -
31 if (!m_provider) {
evaluated: !m_provider
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:4524
1-4524
32 QMimeProviderBase *binaryProvider = new QMimeBinaryProvider(this); -
33 if (binaryProvider->isValid()) {
partially evaluated: binaryProvider->isValid()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
34 m_provider = binaryProvider; -
35 } else {
executed: }
Execution Count:1
1
36 delete binaryProvider; -
37 m_provider = new QMimeXMLProvider(this); -
38 }
never executed: }
0
39 } -
40 return m_provider;
executed: return m_provider;
Execution Count:4525
4525
41} -
42 -
43void QMimeDatabasePrivate::setProvider(QMimeProviderBase *theProvider) -
44{ -
45 delete m_provider; -
46 m_provider = theProvider; -
47}
never executed: }
0
48 -
49 -
50 -
51 -
52 -
53QMimeType QMimeDatabasePrivate::mimeTypeForName(const QString &nameOrAlias) -
54{ -
55 return provider()->mimeTypeForName(provider()->resolveAlias(nameOrAlias));
executed: return provider()->mimeTypeForName(provider()->resolveAlias(nameOrAlias));
Execution Count:2138
2138
56} -
57 -
58QStringList QMimeDatabasePrivate::mimeTypeForFileName(const QString &fileName, QString *foundSuffix) -
59{ -
60 if (fileName.endsWith(QLatin1Char('/')))
evaluated: fileName.endsWith(QLatin1Char('/'))
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:95
2-95
61 return QStringList() << QLatin1String("inode/directory");
executed: return QStringList() << QLatin1String("inode/directory");
Execution Count:2
2
62 -
63 const QStringList matchingMimeTypes = provider()->findByFileName(QFileInfo(fileName).fileName(), foundSuffix); -
64 return matchingMimeTypes;
executed: return matchingMimeTypes;
Execution Count:95
95
65} -
66 -
67static inline bool isTextFile(const QByteArray &data) -
68{ -
69 -
70 static const char bigEndianBOM[] = "\xFE\xFF"; -
71 static const char littleEndianBOM[] = "\xFF\xFE"; -
72 if (data.startsWith(bigEndianBOM) || data.startsWith(littleEndianBOM))
partially evaluated: data.startsWith(bigEndianBOM)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
partially evaluated: data.startsWith(littleEndianBOM)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
73 return true;
never executed: return true;
0
74 -
75 -
76 const char *p = data.constData(); -
77 const char *e = p + qMin(32, data.size()); -
78 for ( ; p < e; ++p) {
evaluated: p < e
TRUEFALSE
yes
Evaluation Count:69
yes
Evaluation Count:6
6-69
79 if ((unsigned char)(*p) < 32 && *p != 9 && *p !=10 && *p != 13)
evaluated: (unsigned char)(*p) < 32
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:66
partially evaluated: *p != 9
TRUEFALSE
yes
Evaluation Count:3
no
Evaluation Count:0
partially evaluated: *p !=10
TRUEFALSE
yes
Evaluation Count:3
no
Evaluation Count:0
partially evaluated: *p != 13
TRUEFALSE
yes
Evaluation Count:3
no
Evaluation Count:0
0-66
80 return false;
executed: return false;
Execution Count:3
3
81 }
executed: }
Execution Count:66
66
82 -
83 return true;
executed: return true;
Execution Count:6
6
84} -
85 -
86QMimeType QMimeDatabasePrivate::findByData(const QByteArray &data, int *accuracyPtr) -
87{ -
88 if (data.isEmpty()) {
partially evaluated: data.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:27
0-27
89 *accuracyPtr = 100; -
90 return mimeTypeForName(QLatin1String("application/x-zerosize"));
never executed: return mimeTypeForName(QLatin1String("application/x-zerosize"));
0
91 } -
92 -
93 *accuracyPtr = 0; -
94 QMimeType candidate = provider()->findByMagic(data, accuracyPtr); -
95 -
96 if (candidate.isValid())
evaluated: candidate.isValid()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:9
9-18
97 return candidate;
executed: return candidate;
Execution Count:18
18
98 -
99 if (isTextFile(data)) {
evaluated: isTextFile(data)
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:3
3-6
100 *accuracyPtr = 5; -
101 return mimeTypeForName(QLatin1String("text/plain"));
executed: return mimeTypeForName(QLatin1String("text/plain"));
Execution Count:6
6
102 } -
103 -
104 return mimeTypeForName(defaultMimeType());
executed: return mimeTypeForName(defaultMimeType());
Execution Count:3
3
105} -
106 -
107QMimeType QMimeDatabasePrivate::mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device, int *accuracyPtr) -
108{ -
109 -
110 -
111 -
112 -
113 -
114 *accuracyPtr = 0; -
115 -
116 -
117 QStringList candidatesByName = mimeTypeForFileName(fileName); -
118 if (candidatesByName.count() == 1) {
evaluated: candidatesByName.count() == 1
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:16
12-16
119 *accuracyPtr = 100; -
120 const QMimeType mime = mimeTypeForName(candidatesByName.at(0)); -
121 if (mime.isValid())
partially evaluated: mime.isValid()
TRUEFALSE
yes
Evaluation Count:12
no
Evaluation Count:0
0-12
122 return mime;
executed: return mime;
Execution Count:12
12
123 candidatesByName.clear(); -
124 }
never executed: }
0
125 -
126 -
127 -
128 if (device->isOpen()) {
evaluated: device->isOpen()
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:3
3-13
129 -
130 -
131 -
132 const QByteArray data = device->peek(16384); -
133 -
134 int magicAccuracy = 0; -
135 QMimeType candidateByData(findByData(data, &magicAccuracy)); -
136 -
137 -
138 if (candidateByData.isValid() && magicAccuracy > 0) {
partially evaluated: candidateByData.isValid()
TRUEFALSE
yes
Evaluation Count:13
no
Evaluation Count:0
partially evaluated: magicAccuracy > 0
TRUEFALSE
yes
Evaluation Count:13
no
Evaluation Count:0
0-13
139 -
140 -
141 const QString sniffedMime = candidateByData.name(); -
142 for (QForeachContainer<__typeof__(candidatesByName)> _container_(candidatesByName); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &m = *_container_.i;; __extension__ ({--_container_.brk; break;})) { -
143 if (inherits(m, sniffedMime)) {
never evaluated: inherits(m, sniffedMime)
0
144 -
145 *accuracyPtr = 100; -
146 return mimeTypeForName(m);
never executed: return mimeTypeForName(m);
0
147 } -
148 }
never executed: }
0
149 *accuracyPtr = magicAccuracy; -
150 return candidateByData;
executed: return candidateByData;
Execution Count:13
13
151 } -
152 }
never executed: }
0
153 -
154 if (candidatesByName.count() > 1) {
partially evaluated: candidatesByName.count() > 1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:3
0-3
155 *accuracyPtr = 20; -
156 candidatesByName.sort(); -
157 const QMimeType mime = mimeTypeForName(candidatesByName.at(0)); -
158 if (mime.isValid())
never evaluated: mime.isValid()
0
159 return mime;
never executed: return mime;
0
160 }
never executed: }
0
161 -
162 return mimeTypeForName(defaultMimeType());
executed: return mimeTypeForName(defaultMimeType());
Execution Count:3
3
163} -
164 -
165QList<QMimeType> QMimeDatabasePrivate::allMimeTypes() -
166{ -
167 return provider()->allMimeTypes();
executed: return provider()->allMimeTypes();
Execution Count:5
5
168} -
169 -
170bool QMimeDatabasePrivate::inherits(const QString &mime, const QString &parent) -
171{ -
172 const QString resolvedParent = provider()->resolveAlias(parent); -
173 -
174 QStack<QString> toCheck; -
175 toCheck.push(mime); -
176 while (!toCheck.isEmpty()) {
evaluated: !toCheck.isEmpty()
TRUEFALSE
yes
Evaluation Count:58
yes
Evaluation Count:2
2-58
177 const QString current = toCheck.pop(); -
178 if (current == resolvedParent)
evaluated: current == resolvedParent
TRUEFALSE
yes
Evaluation Count:24
yes
Evaluation Count:34
24-34
179 return true;
executed: return true;
Execution Count:24
24
180 for (QForeachContainer<__typeof__(provider()->parents(current))> _container_(provider()->parents(current)); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &par = *_container_.i;; __extension__ ({--_container_.brk; break;})) -
181 toCheck.push(par);
executed: toCheck.push(par);
Execution Count:34
34
182 }
executed: }
Execution Count:34
34
183 return false;
executed: return false;
Execution Count:2
2
184} -
185QMimeDatabase::QMimeDatabase() : -
186 d(staticQMimeDatabase()) -
187{ -
188}
executed: }
Execution Count:69
69
189 -
190 -
191 -
192 -
193 -
194QMimeDatabase::~QMimeDatabase() -
195{ -
196 d = 0; -
197}
executed: }
Execution Count:69
69
198 -
199 -
200 -
201 -
202 -
203QMimeType QMimeDatabase::mimeTypeForName(const QString &nameOrAlias) const -
204{ -
205 QMutexLocker locker(&d->mutex); -
206 -
207 return d->mimeTypeForName(nameOrAlias);
executed: return d->mimeTypeForName(nameOrAlias);
Execution Count:2050
2050
208} -
209QMimeType QMimeDatabase::mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const -
210{ -
211 QMutexLocker locker(&d->mutex); -
212 -
213 if (fileInfo.isDir())
evaluated: fileInfo.isDir()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:13
2-13
214 return d->mimeTypeForName(QLatin1String("inode/directory"));
executed: return d->mimeTypeForName(QLatin1String("inode/directory"));
Execution Count:2
2
215 -
216 QFile file(fileInfo.absoluteFilePath()); -
217 -
218 -
219 -
220 const QByteArray nativeFilePath = QFile::encodeName(file.fileName()); -
221 struct stat64 statBuffer; -
222 if (::lstat64(nativeFilePath.constData(), &statBuffer) == 0) {
evaluated: ::lstat64(nativeFilePath.constData(), &statBuffer) == 0
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:4
4-9
223 if (((((statBuffer.st_mode)) & 0170000) == (0020000)))
partially evaluated: ((((statBuffer.st_mode)) & 0170000) == (0020000))
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
224 return d->mimeTypeForName(QLatin1String("inode/chardevice"));
never executed: return d->mimeTypeForName(QLatin1String("inode/chardevice"));
0
225 if (((((statBuffer.st_mode)) & 0170000) == (0060000)))
partially evaluated: ((((statBuffer.st_mode)) & 0170000) == (0060000))
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
226 return d->mimeTypeForName(QLatin1String("inode/blockdevice"));
never executed: return d->mimeTypeForName(QLatin1String("inode/blockdevice"));
0
227 if (((((statBuffer.st_mode)) & 0170000) == (0010000)))
partially evaluated: ((((statBuffer.st_mode)) & 0170000) == (0010000))
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
228 return d->mimeTypeForName(QLatin1String("inode/fifo"));
never executed: return d->mimeTypeForName(QLatin1String("inode/fifo"));
0
229 if (((((statBuffer.st_mode)) & 0170000) == (0140000)))
partially evaluated: ((((statBuffer.st_mode)) & 0170000) == (0140000))
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
230 return d->mimeTypeForName(QLatin1String("inode/socket"));
never executed: return d->mimeTypeForName(QLatin1String("inode/socket"));
0
231 }
executed: }
Execution Count:9
9
232 -
233 -
234 int priority = 0; -
235 switch (mode) { -
236 case MatchDefault: -
237 file.open(QIODevice::ReadOnly); -
238 return d->mimeTypeForFileNameAndData(fileInfo.absoluteFilePath(), &file, &priority);
executed: return d->mimeTypeForFileNameAndData(fileInfo.absoluteFilePath(), &file, &priority);
Execution Count:13
13
239 case MatchExtension: -
240 locker.unlock(); -
241 return mimeTypeForFile(fileInfo.absoluteFilePath(), mode);
never executed: return mimeTypeForFile(fileInfo.absoluteFilePath(), mode);
0
242 case MatchContent: -
243 if (file.open(QIODevice::ReadOnly)) {
never evaluated: file.open(QIODevice::ReadOnly)
0
244 locker.unlock(); -
245 return mimeTypeForData(&file);
never executed: return mimeTypeForData(&file);
0
246 } else { -
247 return d->mimeTypeForName(d->defaultMimeType());
never executed: return d->mimeTypeForName(d->defaultMimeType());
0
248 } -
249 default: -
250 qt_noop(); -
251 }
never executed: }
0
252 return d->mimeTypeForName(d->defaultMimeType());
never executed: return d->mimeTypeForName(d->defaultMimeType());
0
253} -
254 -
255 -
256 -
257 -
258 -
259 -
260QMimeType QMimeDatabase::mimeTypeForFile(const QString &fileName, MatchMode mode) const -
261{ -
262 if (mode == MatchExtension) {
evaluated: mode == MatchExtension
TRUEFALSE
yes
Evaluation Count:34
yes
Evaluation Count:15
15-34
263 QMutexLocker locker(&d->mutex); -
264 QStringList matches = d->mimeTypeForFileName(fileName); -
265 const int matchCount = matches.count(); -
266 if (matchCount == 0) {
evaluated: matchCount == 0
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:26
8-26
267 return d->mimeTypeForName(d->defaultMimeType());
executed: return d->mimeTypeForName(d->defaultMimeType());
Execution Count:8
8
268 } else if (matchCount == 1) {
partially evaluated: matchCount == 1
TRUEFALSE
yes
Evaluation Count:26
no
Evaluation Count:0
0-26
269 return d->mimeTypeForName(matches.first());
executed: return d->mimeTypeForName(matches.first());
Execution Count:26
26
270 } else { -
271 -
272 matches.sort(); -
273 return d->mimeTypeForName(matches.first());
never executed: return d->mimeTypeForName(matches.first());
0
274 } -
275 } else { -
276 -
277 QFileInfo fileInfo(fileName); -
278 return mimeTypeForFile(fileInfo);
executed: return mimeTypeForFile(fileInfo);
Execution Count:15
15
279 } -
280} -
281QList<QMimeType> QMimeDatabase::mimeTypesForFileName(const QString &fileName) const -
282{ -
283 QMutexLocker locker(&d->mutex); -
284 -
285 QStringList matches = d->mimeTypeForFileName(fileName); -
286 QList<QMimeType> mimes; -
287 matches.sort(); -
288 for (QForeachContainer<__typeof__(matches)> _container_(matches); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &mime = *_container_.i;; __extension__ ({--_container_.brk; break;})) -
289 mimes.append(d->mimeTypeForName(mime));
executed: mimes.append(d->mimeTypeForName(mime));
Execution Count:26
26
290 return mimes;
executed: return mimes;
Execution Count:27
27
291} -
292 -
293 -
294 -
295 -
296 -
297 -
298QString QMimeDatabase::suffixForFileName(const QString &fileName) const -
299{ -
300 QMutexLocker locker(&d->mutex); -
301 QString foundSuffix; -
302 d->mimeTypeForFileName(fileName, &foundSuffix); -
303 return foundSuffix;
executed: return foundSuffix;
Execution Count:8
8
304} -
305QMimeType QMimeDatabase::mimeTypeForData(const QByteArray &data) const -
306{ -
307 QMutexLocker locker(&d->mutex); -
308 -
309 int accuracy = 0; -
310 return d->findByData(data, &accuracy);
executed: return d->findByData(data, &accuracy);
Execution Count:4
4
311} -
312QMimeType QMimeDatabase::mimeTypeForData(QIODevice *device) const -
313{ -
314 QMutexLocker locker(&d->mutex); -
315 -
316 int accuracy = 0; -
317 const bool openedByUs = !device->isOpen() && device->open(QIODevice::ReadOnly);
evaluated: !device->isOpen()
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:4
evaluated: device->open(QIODevice::ReadOnly)
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:2
2-8
318 if (device->isOpen()) {
evaluated: device->isOpen()
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:2
2-10
319 -
320 -
321 const QByteArray data = device->peek(16384); -
322 const QMimeType result = d->findByData(data, &accuracy); -
323 if (openedByUs)
evaluated: openedByUs
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:4
4-6
324 device->close();
executed: device->close();
Execution Count:6
6
325 return result;
executed: return result;
Execution Count:10
10
326 } -
327 return d->mimeTypeForName(d->defaultMimeType());
executed: return d->mimeTypeForName(d->defaultMimeType());
Execution Count:2
2
328} -
329QMimeType QMimeDatabase::mimeTypeForUrl(const QUrl &url) const -
330{ -
331 if (url.isLocalFile())
partially evaluated: url.isLocalFile()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:3
0-3
332 return mimeTypeForFile(url.toLocalFile());
never executed: return mimeTypeForFile(url.toLocalFile());
0
333 -
334 const QString scheme = url.scheme(); -
335 if (scheme.startsWith(QLatin1String("http")))
evaluated: scheme.startsWith(QLatin1String("http"))
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:2
1-2
336 return mimeTypeForName(d->defaultMimeType());
executed: return mimeTypeForName(d->defaultMimeType());
Execution Count:1
1
337 -
338 return mimeTypeForFile(url.path());
executed: return mimeTypeForFile(url.path());
Execution Count:2
2
339} -
340QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const -
341{ -
342 int accuracy = 0; -
343 const bool openedByUs = !device->isOpen() && device->open(QIODevice::ReadOnly);
evaluated: !device->isOpen()
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:5
partially evaluated: device->open(QIODevice::ReadOnly)
TRUEFALSE
yes
Evaluation Count:5
no
Evaluation Count:0
0-5
344 const QMimeType result = d->mimeTypeForFileNameAndData(fileName, device, &accuracy); -
345 if (openedByUs)
evaluated: openedByUs
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:5
5
346 device->close();
executed: device->close();
Execution Count:5
5
347 return result;
executed: return result;
Execution Count:10
10
348} -
349QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString &fileName, const QByteArray &data) const -
350{ -
351 QBuffer buffer(const_cast<QByteArray *>(&data)); -
352 buffer.open(QIODevice::ReadOnly); -
353 int accuracy = 0; -
354 return d->mimeTypeForFileNameAndData(fileName, &buffer, &accuracy);
executed: return d->mimeTypeForFileNameAndData(fileName, &buffer, &accuracy);
Execution Count:5
5
355} -
356QList<QMimeType> QMimeDatabase::allMimeTypes() const -
357{ -
358 QMutexLocker locker(&d->mutex); -
359 -
360 return d->allMimeTypes();
executed: return d->allMimeTypes();
Execution Count:5
5
361} -
362 -
363 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial