Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/mimetypes/qmimeglobpattern.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qmimeglobpattern_p.h" | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #ifndef QT_NO_MIMETYPE | - | ||||||||||||||||||
37 | - | |||||||||||||||||||
38 | #include <QRegExp> | - | ||||||||||||||||||
39 | #include <QStringList> | - | ||||||||||||||||||
40 | #include <QDebug> | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
43 | - | |||||||||||||||||||
44 | /*! | - | ||||||||||||||||||
45 | \internal | - | ||||||||||||||||||
46 | \class QMimeGlobMatchResult | - | ||||||||||||||||||
47 | \inmodule QtCore | - | ||||||||||||||||||
48 | \brief The QMimeGlobMatchResult class accumulates results from glob matching. | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | Handles glob weights, and preferring longer matches over shorter matches. | - | ||||||||||||||||||
51 | */ | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const QString &pattern) | - | ||||||||||||||||||
54 | { | - | ||||||||||||||||||
55 | // Is this a lower-weight pattern than the last match? Skip this match then. | - | ||||||||||||||||||
56 | if (weight < m_weight)
| 6-155 | ||||||||||||||||||
57 | return; executed 6 times by 1 test: return; Executed by:
| 6 | ||||||||||||||||||
58 | bool replace = weight > m_weight; | - | ||||||||||||||||||
59 | if (!replace) {
| 13-142 | ||||||||||||||||||
60 | // Compare the length of the match | - | ||||||||||||||||||
61 | if (pattern.length() < m_matchingPatternLength)
| 0-13 | ||||||||||||||||||
62 | return; // too short, ignore never executed: return; | 0 | ||||||||||||||||||
63 | else if (pattern.length() > m_matchingPatternLength) {
| 6-7 | ||||||||||||||||||
64 | // longer: clear any previous match (like *.bz2, when pattern is *.tar.bz2) | - | ||||||||||||||||||
65 | replace = true; | - | ||||||||||||||||||
66 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||
67 | } executed 13 times by 1 test: end of block Executed by:
| 13 | ||||||||||||||||||
68 | if (replace) {
| 7-148 | ||||||||||||||||||
69 | m_matchingMimeTypes.clear(); | - | ||||||||||||||||||
70 | // remember the new "longer" length | - | ||||||||||||||||||
71 | m_matchingPatternLength = pattern.length(); | - | ||||||||||||||||||
72 | m_weight = weight; | - | ||||||||||||||||||
73 | } executed 148 times by 1 test: end of block Executed by:
| 148 | ||||||||||||||||||
74 | if (!m_matchingMimeTypes.contains(mimeType)) {
| 1-154 | ||||||||||||||||||
75 | m_matchingMimeTypes.append(mimeType); | - | ||||||||||||||||||
76 | if (pattern.startsWith(QLatin1String("*.")))
| 26-128 | ||||||||||||||||||
77 | m_foundSuffix = pattern.mid(2); executed 128 times by 1 test: m_foundSuffix = pattern.mid(2); Executed by:
| 128 | ||||||||||||||||||
78 | } executed 154 times by 1 test: end of block Executed by:
| 154 | ||||||||||||||||||
79 | } executed 155 times by 1 test: end of block Executed by:
| 155 | ||||||||||||||||||
80 | - | |||||||||||||||||||
81 | /*! | - | ||||||||||||||||||
82 | \internal | - | ||||||||||||||||||
83 | \class QMimeGlobPattern | - | ||||||||||||||||||
84 | \inmodule QtCore | - | ||||||||||||||||||
85 | \brief The QMimeGlobPattern class contains the glob pattern for file names for MIME type matching. | - | ||||||||||||||||||
86 | - | |||||||||||||||||||
87 | \sa QMimeType, QMimeDatabase, QMimeMagicRuleMatcher, QMimeMagicRule | - | ||||||||||||||||||
88 | */ | - | ||||||||||||||||||
89 | - | |||||||||||||||||||
90 | bool QMimeGlobPattern::matchFileName(const QString &inputFilename) const | - | ||||||||||||||||||
91 | { | - | ||||||||||||||||||
92 | // "Applications MUST match globs case-insensitively, except when the case-sensitive | - | ||||||||||||||||||
93 | // attribute is set to true." | - | ||||||||||||||||||
94 | // The constructor takes care of putting case-insensitive patterns in lowercase. | - | ||||||||||||||||||
95 | const QString filename = m_caseSensitivity == Qt::CaseInsensitive ? inputFilename.toLower() : inputFilename;
| 382-6116 | ||||||||||||||||||
96 | - | |||||||||||||||||||
97 | const int pattern_len = m_pattern.length(); | - | ||||||||||||||||||
98 | if (!pattern_len)
| 0-6498 | ||||||||||||||||||
99 | return false; never executed: return false; | 0 | ||||||||||||||||||
100 | const int len = filename.length(); | - | ||||||||||||||||||
101 | - | |||||||||||||||||||
102 | const int starCount = m_pattern.count(QLatin1Char('*')); | - | ||||||||||||||||||
103 | - | |||||||||||||||||||
104 | // Patterns like "*~", "*.extension" | - | ||||||||||||||||||
105 | if (m_pattern[0] == QLatin1Char('*') && m_pattern.indexOf(QLatin1Char('[')) == -1 && starCount == 1)
| 0-3454 | ||||||||||||||||||
106 | { | - | ||||||||||||||||||
107 | if (len + 1 < pattern_len) return false; executed 686 times by 1 test: return false; Executed by:
| 686-2578 | ||||||||||||||||||
108 | - | |||||||||||||||||||
109 | const QChar *c1 = m_pattern.unicode() + pattern_len - 1; | - | ||||||||||||||||||
110 | const QChar *c2 = filename.unicode() + len - 1; | - | ||||||||||||||||||
111 | int cnt = 1; | - | ||||||||||||||||||
112 | while (cnt < pattern_len && *c1-- == *c2--)
| 10-2907 | ||||||||||||||||||
113 | ++cnt; executed 339 times by 1 test: ++cnt; Executed by:
| 339 | ||||||||||||||||||
114 | return cnt == pattern_len; executed 2578 times by 1 test: return cnt == pattern_len; Executed by:
| 2578 | ||||||||||||||||||
115 | } | - | ||||||||||||||||||
116 | - | |||||||||||||||||||
117 | // Patterns like "README*" (well this is currently the only one like that...) | - | ||||||||||||||||||
118 | if (starCount == 1 && m_pattern.at(pattern_len - 1) == QLatin1Char('*')) {
| 190-2664 | ||||||||||||||||||
119 | if (len + 1 < pattern_len) return false; executed 100 times by 1 test: return false; Executed by:
| 100-280 | ||||||||||||||||||
120 | if (m_pattern.at(0) == QLatin1Char('*'))
| 0-280 | ||||||||||||||||||
121 | return filename.indexOf(m_pattern.mid(1, pattern_len - 2)) != -1; never executed: return filename.indexOf(m_pattern.mid(1, pattern_len - 2)) != -1; | 0 | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | const QChar *c1 = m_pattern.unicode(); | - | ||||||||||||||||||
124 | const QChar *c2 = filename.unicode(); | - | ||||||||||||||||||
125 | int cnt = 1; | - | ||||||||||||||||||
126 | while (cnt < pattern_len && *c1++ == *c2++)
| 20-386 | ||||||||||||||||||
127 | ++cnt; executed 126 times by 1 test: ++cnt; Executed by:
| 126 | ||||||||||||||||||
128 | return cnt == pattern_len; executed 280 times by 1 test: return cnt == pattern_len; Executed by:
| 280 | ||||||||||||||||||
129 | } | - | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | // Names without any wildcards like "README" | - | ||||||||||||||||||
132 | if (m_pattern.indexOf(QLatin1Char('[')) == -1 && starCount == 0 && m_pattern.indexOf(QLatin1Char('?')))
| 0-2474 | ||||||||||||||||||
133 | return (m_pattern == filename); executed 2474 times by 1 test: return (m_pattern == filename); Executed by:
| 2474 | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | // Other (quite rare) patterns, like "*.anim[1-9j]": use slow but correct method | - | ||||||||||||||||||
136 | QRegExp rx(m_pattern, Qt::CaseSensitive, QRegExp::WildcardUnix); | - | ||||||||||||||||||
137 | return rx.exactMatch(filename); executed 380 times by 1 test: return rx.exactMatch(filename); Executed by:
| 380 | ||||||||||||||||||
138 | } | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | static bool isFastPattern(const QString &pattern) | - | ||||||||||||||||||
141 | { | - | ||||||||||||||||||
142 | // starts with "*.", has no other '*' and no other '.' | - | ||||||||||||||||||
143 | return pattern.lastIndexOf(QLatin1Char('*')) == 0 executed 4126 times by 1 test: return pattern.lastIndexOf(QLatin1Char('*')) == 0 && pattern.lastIndexOf(QLatin1Char('.')) == 1 && !pattern.contains(QLatin1Char('?')) && !pattern.contains(QLatin1Char('[')) ; Executed by:
| 70-4126 | ||||||||||||||||||
144 | && pattern.lastIndexOf(QLatin1Char('.')) == 1 executed 4126 times by 1 test: return pattern.lastIndexOf(QLatin1Char('*')) == 0 && pattern.lastIndexOf(QLatin1Char('.')) == 1 && !pattern.contains(QLatin1Char('?')) && !pattern.contains(QLatin1Char('[')) ; Executed by:
| 160-4126 | ||||||||||||||||||
145 | // and contains no other special character executed 4126 times by 1 test: return pattern.lastIndexOf(QLatin1Char('*')) == 0 && pattern.lastIndexOf(QLatin1Char('.')) == 1 && !pattern.contains(QLatin1Char('?')) && !pattern.contains(QLatin1Char('[')) ; Executed by:
| 4126 | ||||||||||||||||||
146 | && !pattern.contains(QLatin1Char('?')) executed 4126 times by 1 test: return pattern.lastIndexOf(QLatin1Char('*')) == 0 && pattern.lastIndexOf(QLatin1Char('.')) == 1 && !pattern.contains(QLatin1Char('?')) && !pattern.contains(QLatin1Char('[')) ; Executed by:
| 0-4126 | ||||||||||||||||||
147 | && !pattern.contains(QLatin1Char('[')) executed 4126 times by 1 test: return pattern.lastIndexOf(QLatin1Char('*')) == 0 && pattern.lastIndexOf(QLatin1Char('.')) == 1 && !pattern.contains(QLatin1Char('?')) && !pattern.contains(QLatin1Char('[')) ; Executed by:
| 5-4126 | ||||||||||||||||||
148 | ; executed 4126 times by 1 test: return pattern.lastIndexOf(QLatin1Char('*')) == 0 && pattern.lastIndexOf(QLatin1Char('.')) == 1 && !pattern.contains(QLatin1Char('?')) && !pattern.contains(QLatin1Char('[')) ; Executed by:
| 4126 | ||||||||||||||||||
149 | } | - | ||||||||||||||||||
150 | - | |||||||||||||||||||
151 | void QMimeAllGlobPatterns::addGlob(const QMimeGlobPattern &glob) | - | ||||||||||||||||||
152 | { | - | ||||||||||||||||||
153 | const QString &pattern = glob.pattern(); | - | ||||||||||||||||||
154 | Q_ASSERT(!pattern.isEmpty()); | - | ||||||||||||||||||
155 | - | |||||||||||||||||||
156 | // Store each patterns into either m_fastPatternDict (*.txt, *.html etc. with default weight 50) | - | ||||||||||||||||||
157 | // or for the rest, like core.*, *.tar.bz2, *~, into highWeightPatternOffset (>50) | - | ||||||||||||||||||
158 | // or lowWeightPatternOffset (<=50) | - | ||||||||||||||||||
159 | - | |||||||||||||||||||
160 | if (glob.weight() == 50 && isFastPattern(pattern) && !glob.isCaseSensitive()) {
| 10-4126 | ||||||||||||||||||
161 | // The bulk of the patterns is *.foo with weight 50 --> those go into the fast patterns hash. | - | ||||||||||||||||||
162 | const QString extension = pattern.mid(2).toLower(); | - | ||||||||||||||||||
163 | QStringList &patterns = m_fastPatterns[extension]; // find or create | - | ||||||||||||||||||
164 | if (!patterns.contains(glob.mimeType()))
| 12-3869 | ||||||||||||||||||
165 | patterns.append(glob.mimeType()); executed 3869 times by 1 test: patterns.append(glob.mimeType()); Executed by:
| 3869 | ||||||||||||||||||
166 | } else { executed 3881 times by 1 test: end of block Executed by:
| 3881 | ||||||||||||||||||
167 | if (glob.weight() > 50) {
| 0-255 | ||||||||||||||||||
168 | if (!m_highWeightGlobs.hasPattern(glob.mimeType(), glob.pattern()))
| 0 | ||||||||||||||||||
169 | m_highWeightGlobs.append(glob); never executed: m_highWeightGlobs.append(glob); | 0 | ||||||||||||||||||
170 | } else { never executed: end of block | 0 | ||||||||||||||||||
171 | if (!m_lowWeightGlobs.hasPattern(glob.mimeType(), glob.pattern()))
| 0-255 | ||||||||||||||||||
172 | m_lowWeightGlobs.append(glob); executed 255 times by 1 test: m_lowWeightGlobs.append(glob); Executed by:
| 255 | ||||||||||||||||||
173 | } executed 255 times by 1 test: end of block Executed by:
| 255 | ||||||||||||||||||
174 | } | - | ||||||||||||||||||
175 | } | - | ||||||||||||||||||
176 | - | |||||||||||||||||||
177 | void QMimeAllGlobPatterns::removeMimeType(const QString &mimeType) | - | ||||||||||||||||||
178 | { | - | ||||||||||||||||||
179 | QMutableHashIterator<QString, QStringList> it(m_fastPatterns); | - | ||||||||||||||||||
180 | while (it.hasNext()) {
| 0 | ||||||||||||||||||
181 | it.next().value().removeAll(mimeType); | - | ||||||||||||||||||
182 | } never executed: end of block | 0 | ||||||||||||||||||
183 | m_highWeightGlobs.removeMimeType(mimeType); | - | ||||||||||||||||||
184 | m_lowWeightGlobs.removeMimeType(mimeType); | - | ||||||||||||||||||
185 | } never executed: end of block | 0 | ||||||||||||||||||
186 | - | |||||||||||||||||||
187 | void QMimeGlobPatternList::match(QMimeGlobMatchResult &result, | - | ||||||||||||||||||
188 | const QString &fileName) const | - | ||||||||||||||||||
189 | { | - | ||||||||||||||||||
190 | - | |||||||||||||||||||
191 | QMimeGlobPatternList::const_iterator it = this->constBegin(); | - | ||||||||||||||||||
192 | const QMimeGlobPatternList::const_iterator endIt = this->constEnd(); | - | ||||||||||||||||||
193 | for (; it != endIt; ++it) {
| 192-4896 | ||||||||||||||||||
194 | const QMimeGlobPattern &glob = *it; | - | ||||||||||||||||||
195 | if (glob.matchFileName(fileName))
| 26-4870 | ||||||||||||||||||
196 | result.addMatch(glob.mimeType(), glob.weight(), glob.pattern()); executed 26 times by 1 test: result.addMatch(glob.mimeType(), glob.weight(), glob.pattern()); Executed by:
| 26 | ||||||||||||||||||
197 | } executed 4896 times by 1 test: end of block Executed by:
| 4896 | ||||||||||||||||||
198 | } executed 192 times by 1 test: end of block Executed by:
| 192 | ||||||||||||||||||
199 | - | |||||||||||||||||||
200 | QStringList QMimeAllGlobPatterns::matchingGlobs(const QString &fileName, QString *foundSuffix) const | - | ||||||||||||||||||
201 | { | - | ||||||||||||||||||
202 | // First try the high weight matches (>50), if any. | - | ||||||||||||||||||
203 | QMimeGlobMatchResult result; | - | ||||||||||||||||||
204 | m_highWeightGlobs.match(result, fileName); | - | ||||||||||||||||||
205 | if (result.m_matchingMimeTypes.isEmpty()) {
| 0-96 | ||||||||||||||||||
206 | - | |||||||||||||||||||
207 | // Now use the "fast patterns" dict, for simple *.foo patterns with weight 50 | - | ||||||||||||||||||
208 | // (which is most of them, so this optimization is definitely worth it) | - | ||||||||||||||||||
209 | const int lastDot = fileName.lastIndexOf(QLatin1Char('.')); | - | ||||||||||||||||||
210 | if (lastDot != -1) { // if no '.', skip the extension lookup
| 25-71 | ||||||||||||||||||
211 | const int ext_len = fileName.length() - lastDot - 1; | - | ||||||||||||||||||
212 | const QString simpleExtension = fileName.right(ext_len).toLower(); | - | ||||||||||||||||||
213 | // (toLower because fast patterns are always case-insensitive and saved as lowercase) | - | ||||||||||||||||||
214 | - | |||||||||||||||||||
215 | const QStringList matchingMimeTypes = m_fastPatterns.value(simpleExtension); | - | ||||||||||||||||||
216 | foreach (const QString &mime, matchingMimeTypes) { | - | ||||||||||||||||||
217 | result.addMatch(mime, 50, QLatin1String("*.") + simpleExtension); | - | ||||||||||||||||||
218 | } executed 57 times by 1 test: end of block Executed by:
| 57 | ||||||||||||||||||
219 | // Can't return yet; *.tar.bz2 has to win over *.bz2, so we need the low-weight mimetypes anyway, | - | ||||||||||||||||||
220 | // at least those with weight 50. | - | ||||||||||||||||||
221 | } executed 71 times by 1 test: end of block Executed by:
| 71 | ||||||||||||||||||
222 | - | |||||||||||||||||||
223 | // Finally, try the low weight matches (<=50) | - | ||||||||||||||||||
224 | m_lowWeightGlobs.match(result, fileName); | - | ||||||||||||||||||
225 | } executed 96 times by 1 test: end of block Executed by:
| 96 | ||||||||||||||||||
226 | if (foundSuffix)
| 8-88 | ||||||||||||||||||
227 | *foundSuffix = result.m_foundSuffix; executed 8 times by 1 test: *foundSuffix = result.m_foundSuffix; Executed by:
| 8 | ||||||||||||||||||
228 | return result.m_matchingMimeTypes; executed 96 times by 1 test: return result.m_matchingMimeTypes; Executed by:
| 96 | ||||||||||||||||||
229 | } | - | ||||||||||||||||||
230 | - | |||||||||||||||||||
231 | void QMimeAllGlobPatterns::clear() | - | ||||||||||||||||||
232 | { | - | ||||||||||||||||||
233 | m_fastPatterns.clear(); | - | ||||||||||||||||||
234 | m_highWeightGlobs.clear(); | - | ||||||||||||||||||
235 | m_lowWeightGlobs.clear(); | - | ||||||||||||||||||
236 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | QT_END_NAMESPACE | - | ||||||||||||||||||
239 | - | |||||||||||||||||||
240 | #endif // QT_NO_MIMETYPE | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |