qmimeglobpattern.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/mimetypes/qmimeglobpattern.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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-
42QT_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-
53void 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)
weight < m_weightDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 155 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
6-155
57 return;
executed 6 times by 1 test: return;
Executed by:
  • tst_QMimeDatabase
6
58 bool replace = weight > m_weight;-
59 if (!replace) {
!replaceDescription
TRUEevaluated 13 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 142 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
13-142
60 // Compare the length of the match-
61 if (pattern.length() < m_matchingPatternLength)
pattern.length...gPatternLengthDescription
TRUEnever evaluated
FALSEevaluated 13 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-13
62 return; // too short, ignore
never executed: return;
0
63 else if (pattern.length() > m_matchingPatternLength) {
pattern.length...gPatternLengthDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 7 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
6
67 }
executed 13 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
13
68 if (replace) {
replaceDescription
TRUEevaluated 148 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 7 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
148
74 if (!m_matchingMimeTypes.contains(mimeType)) {
!m_matchingMim...ains(mimeType)Description
TRUEevaluated 154 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QMimeDatabase
1-154
75 m_matchingMimeTypes.append(mimeType);-
76 if (pattern.startsWith(QLatin1String("*.")))
pattern.starts...1String("*."))Description
TRUEevaluated 128 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 26 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
26-128
77 m_foundSuffix = pattern.mid(2);
executed 128 times by 1 test: m_foundSuffix = pattern.mid(2);
Executed by:
  • tst_QMimeDatabase
128
78 }
executed 154 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
154
79}
executed 155 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
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-
90bool 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;
m_caseSensitiv...aseInsensitiveDescription
TRUEevaluated 6116 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 382 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
382-6116
96-
97 const int pattern_len = m_pattern.length();-
98 if (!pattern_len)
!pattern_lenDescription
TRUEnever evaluated
FALSEevaluated 6498 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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)
m_pattern[0] =...atin1Char('*')Description
TRUEevaluated 3454 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 3044 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
m_pattern.inde...ar('[')) == -1Description
TRUEevaluated 3264 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 190 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
starCount == 1Description
TRUEevaluated 3264 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
0-3454
106 {-
107 if (len + 1 < pattern_len) return false;
executed 686 times by 1 test: return false;
Executed by:
  • tst_QMimeDatabase
len + 1 < pattern_lenDescription
TRUEevaluated 686 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 2578 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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--)
cnt < pattern_lenDescription
TRUEevaluated 2907 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 10 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
*c1-- == *c2--Description
TRUEevaluated 339 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 2568 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
10-2907
113 ++cnt;
executed 339 times by 1 test: ++cnt;
Executed by:
  • tst_QMimeDatabase
339
114 return cnt == pattern_len;
executed 2578 times by 1 test: return cnt == pattern_len;
Executed by:
  • tst_QMimeDatabase
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('*')) {
starCount == 1Description
TRUEevaluated 570 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 2664 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
m_pattern.at(p...atin1Char('*')Description
TRUEevaluated 380 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 190 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
190-2664
119 if (len + 1 < pattern_len) return false;
executed 100 times by 1 test: return false;
Executed by:
  • tst_QMimeDatabase
len + 1 < pattern_lenDescription
TRUEevaluated 100 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 280 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
100-280
120 if (m_pattern.at(0) == QLatin1Char('*'))
m_pattern.at(0...atin1Char('*')Description
TRUEnever evaluated
FALSEevaluated 280 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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++)
cnt < pattern_lenDescription
TRUEevaluated 386 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 20 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
*c1++ == *c2++Description
TRUEevaluated 126 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 260 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
20-386
127 ++cnt;
executed 126 times by 1 test: ++cnt;
Executed by:
  • tst_QMimeDatabase
126
128 return cnt == pattern_len;
executed 280 times by 1 test: return cnt == pattern_len;
Executed by:
  • tst_QMimeDatabase
280
129 }-
130-
131 // Names without any wildcards like "README"-
132 if (m_pattern.indexOf(QLatin1Char('[')) == -1 && starCount == 0 && m_pattern.indexOf(QLatin1Char('?')))
m_pattern.inde...ar('[')) == -1Description
TRUEevaluated 2474 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 380 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
starCount == 0Description
TRUEevaluated 2474 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
m_pattern.inde...tin1Char('?'))Description
TRUEevaluated 2474 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
0-2474
133 return (m_pattern == filename);
executed 2474 times by 1 test: return (m_pattern == filename);
Executed by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
380
138}-
139-
140static 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:
  • tst_QMimeDatabase
pattern.lastIn...har('*')) == 0Description
TRUEevaluated 4056 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 70 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
pattern.lastIn...har('.')) == 1Description
TRUEevaluated 3896 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 160 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
!pattern.conta...tin1Char('?'))Description
TRUEevaluated 3896 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
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:
  • tst_QMimeDatabase
!pattern.conta...tin1Char('['))Description
TRUEevaluated 3891 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 5 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
4126
149}-
150-
151void 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()) {
glob.weight() == 50Description
TRUEevaluated 4126 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 10 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
isFastPattern(pattern)Description
TRUEevaluated 3891 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 235 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
!glob.isCaseSensitive()Description
TRUEevaluated 3881 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 10 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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()))
!patterns.cont...ob.mimeType())Description
TRUEevaluated 3869 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 12 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
12-3869
165 patterns.append(glob.mimeType());
executed 3869 times by 1 test: patterns.append(glob.mimeType());
Executed by:
  • tst_QMimeDatabase
3869
166 } else {
executed 3881 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
3881
167 if (glob.weight() > 50) {
glob.weight() > 50Description
TRUEnever evaluated
FALSEevaluated 255 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-255
168 if (!m_highWeightGlobs.hasPattern(glob.mimeType(), glob.pattern()))
!m_highWeightG...lob.pattern())Description
TRUEnever evaluated
FALSEnever evaluated
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()))
!m_lowWeightGl...lob.pattern())Description
TRUEevaluated 255 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
0-255
172 m_lowWeightGlobs.append(glob);
executed 255 times by 1 test: m_lowWeightGlobs.append(glob);
Executed by:
  • tst_QMimeDatabase
255
173 }
executed 255 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
255
174 }-
175}-
176-
177void QMimeAllGlobPatterns::removeMimeType(const QString &mimeType)-
178{-
179 QMutableHashIterator<QString, QStringList> it(m_fastPatterns);-
180 while (it.hasNext()) {
it.hasNext()Description
TRUEnever evaluated
FALSEnever evaluated
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-
187void 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) {
it != endItDescription
TRUEevaluated 4896 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 192 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
192-4896
194 const QMimeGlobPattern &glob = *it;-
195 if (glob.matchFileName(fileName))
glob.matchFileName(fileName)Description
TRUEevaluated 26 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 4870 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
26
197 }
executed 4896 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
4896
198}
executed 192 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
192
199-
200QStringList 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()) {
result.m_match...ypes.isEmpty()Description
TRUEevaluated 96 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
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
lastDot != -1Description
TRUEevaluated 71 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 25 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
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:
  • tst_QMimeDatabase
96
226 if (foundSuffix)
foundSuffixDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 88 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
8-88
227 *foundSuffix = result.m_foundSuffix;
executed 8 times by 1 test: *foundSuffix = result.m_foundSuffix;
Executed by:
  • tst_QMimeDatabase
8
228 return result.m_matchingMimeTypes;
executed 96 times by 1 test: return result.m_matchingMimeTypes;
Executed by:
  • tst_QMimeDatabase
96
229}-
230-
231void 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:
  • tst_QMimeDatabase
5
237-
238QT_END_NAMESPACE-
239-
240#endif // QT_NO_MIMETYPE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9