mimetypes/qmimetypeparser.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8static const char mimeInfoTagC[] = "mime-info"; -
9static const char mimeTypeTagC[] = "mime-type"; -
10static const char mimeTypeAttributeC[] = "type"; -
11static const char subClassTagC[] = "sub-class-of"; -
12static const char commentTagC[] = "comment"; -
13static const char genericIconTagC[] = "generic-icon"; -
14static const char iconTagC[] = "icon"; -
15static const char nameAttributeC[] = "name"; -
16static const char globTagC[] = "glob"; -
17static const char aliasTagC[] = "alias"; -
18static const char patternAttributeC[] = "pattern"; -
19static const char weightAttributeC[] = "weight"; -
20static const char caseSensitiveAttributeC[] = "case-sensitive"; -
21static const char localeAttributeC[] = "xml:lang"; -
22 -
23static const char magicTagC[] = "magic"; -
24static const char priorityAttributeC[] = "priority"; -
25 -
26static const char matchTagC[] = "match"; -
27static const char matchValueAttributeC[] = "value"; -
28static const char matchTypeAttributeC[] = "type"; -
29static const char matchOffsetAttributeC[] = "offset"; -
30static const char matchMaskAttributeC[] = "mask"; -
31QMimeTypeParserBase::ParseState QMimeTypeParserBase::nextState(ParseState currentState, const QStringRef &startElement) -
32{ -
33 switch (currentState) { -
34 case ParseBeginning: -
35 if (startElement == QLatin1String(mimeInfoTagC))
never evaluated: startElement == QLatin1String(mimeInfoTagC)
0
36 return ParseMimeInfo;
never executed: return ParseMimeInfo;
0
37 if (startElement == QLatin1String(mimeTypeTagC))
never evaluated: startElement == QLatin1String(mimeTypeTagC)
0
38 return ParseMimeType;
never executed: return ParseMimeType;
0
39 return ParseError;
never executed: return ParseError;
0
40 case ParseMimeInfo: -
41 return startElement == QLatin1String(mimeTypeTagC) ? ParseMimeType : ParseError;
never executed: return startElement == QLatin1String(mimeTypeTagC) ? ParseMimeType : ParseError;
0
42 case ParseMimeType: -
43 case ParseComment: -
44 case ParseGenericIcon: -
45 case ParseIcon: -
46 case ParseGlobPattern: -
47 case ParseSubClass: -
48 case ParseAlias: -
49 case ParseOtherMimeTypeSubTag: -
50 case ParseMagicMatchRule: -
51 if (startElement == QLatin1String(mimeTypeTagC))
never evaluated: startElement == QLatin1String(mimeTypeTagC)
0
52 return ParseMimeType;
never executed: return ParseMimeType;
0
53 if (startElement == QLatin1String(commentTagC ))
never evaluated: startElement == QLatin1String(commentTagC )
0
54 return ParseComment;
never executed: return ParseComment;
0
55 if (startElement == QLatin1String(genericIconTagC))
never evaluated: startElement == QLatin1String(genericIconTagC)
0
56 return ParseGenericIcon;
never executed: return ParseGenericIcon;
0
57 if (startElement == QLatin1String(iconTagC))
never evaluated: startElement == QLatin1String(iconTagC)
0
58 return ParseIcon;
never executed: return ParseIcon;
0
59 if (startElement == QLatin1String(globTagC))
never evaluated: startElement == QLatin1String(globTagC)
0
60 return ParseGlobPattern;
never executed: return ParseGlobPattern;
0
61 if (startElement == QLatin1String(subClassTagC))
never evaluated: startElement == QLatin1String(subClassTagC)
0
62 return ParseSubClass;
never executed: return ParseSubClass;
0
63 if (startElement == QLatin1String(aliasTagC))
never evaluated: startElement == QLatin1String(aliasTagC)
0
64 return ParseAlias;
never executed: return ParseAlias;
0
65 if (startElement == QLatin1String(magicTagC))
never evaluated: startElement == QLatin1String(magicTagC)
0
66 return ParseMagic;
never executed: return ParseMagic;
0
67 if (startElement == QLatin1String(matchTagC))
never evaluated: startElement == QLatin1String(matchTagC)
0
68 return ParseMagicMatchRule;
never executed: return ParseMagicMatchRule;
0
69 return ParseOtherMimeTypeSubTag;
never executed: return ParseOtherMimeTypeSubTag;
0
70 case ParseMagic: -
71 if (startElement == QLatin1String(matchTagC))
never evaluated: startElement == QLatin1String(matchTagC)
0
72 return ParseMagicMatchRule;
never executed: return ParseMagicMatchRule;
0
73 break;
never executed: break;
0
74 case ParseError: -
75 break;
never executed: break;
0
76 } -
77 return ParseError;
never executed: return ParseError;
0
78} -
79 -
80 -
81static bool parseNumber(const QString &n, int *target, QString *errorMessage) -
82{ -
83 bool ok; -
84 *target = n.toInt(&ok); -
85 if (!ok) {
never evaluated: !ok
0
86 *errorMessage = QString::fromLatin1("Not a number '%1'.").arg(n); -
87 return false;
never executed: return false;
0
88 } -
89 return true;
never executed: return true;
0
90} -
91 -
92 -
93 -
94 -
95 -
96static bool createMagicMatchRule(const QXmlStreamAttributes &atts, -
97 QString *errorMessage, QMimeMagicRule *&rule) -
98{ -
99 const QString type = atts.value(QLatin1String(matchTypeAttributeC)).toString(); -
100 QMimeMagicRule::Type magicType = QMimeMagicRule::type(type.toLatin1()); -
101 if (magicType == QMimeMagicRule::Invalid) {
never evaluated: magicType == QMimeMagicRule::Invalid
0
102 QMessageLogger("mimetypes/qmimetypeparser.cpp", 184, __PRETTY_FUNCTION__).warning("%s: match type %s is not supported.", __PRETTY_FUNCTION__, type.toUtf8().constData()); -
103 return true;
never executed: return true;
0
104 } -
105 const QString value = atts.value(QLatin1String(matchValueAttributeC)).toString(); -
106 if (value.isEmpty()) {
never evaluated: value.isEmpty()
0
107 *errorMessage = QString::fromLatin1("Empty match value detected."); -
108 return false;
never executed: return false;
0
109 } -
110 -
111 int startPos, endPos; -
112 const QString offsetS = atts.value(QLatin1String(matchOffsetAttributeC)).toString(); -
113 const int colonIndex = offsetS.indexOf(QLatin1Char(':')); -
114 const QString startPosS = colonIndex == -1 ? offsetS : offsetS.mid(0, colonIndex);
never evaluated: colonIndex == -1
0
115 const QString endPosS = colonIndex == -1 ? offsetS : offsetS.mid(colonIndex + 1);
never evaluated: colonIndex == -1
0
116 if (!parseNumber(startPosS, &startPos, errorMessage) || !parseNumber(endPosS, &endPos, errorMessage))
never evaluated: !parseNumber(startPosS, &startPos, errorMessage)
never evaluated: !parseNumber(endPosS, &endPos, errorMessage)
0
117 return false;
never executed: return false;
0
118 const QString mask = atts.value(QLatin1String(matchMaskAttributeC)).toString(); -
119 -
120 rule = new QMimeMagicRule(magicType, value.toUtf8(), startPos, endPos, mask.toLatin1()); -
121 -
122 return true;
never executed: return true;
0
123} -
124 -
125 -
126bool QMimeTypeParserBase::parse(QIODevice *dev, const QString &fileName, QString *errorMessage) -
127{ -
128 -
129 -
130 -
131 -
132 -
133 QMimeTypePrivate data; -
134 int priority = 50; -
135 QStack<QMimeMagicRule *> currentRules; -
136 QList<QMimeMagicRule> rules; -
137 QXmlStreamReader reader(dev); -
138 ParseState ps = ParseBeginning; -
139 QXmlStreamAttributes atts; -
140 while (!reader.atEnd()) {
never evaluated: !reader.atEnd()
0
141 switch (reader.readNext()) { -
142 case QXmlStreamReader::StartElement: -
143 ps = nextState(ps, reader.name()); -
144 atts = reader.attributes(); -
145 switch (ps) { -
146 case ParseMimeType: { -
147 const QString name = atts.value(QLatin1String(mimeTypeAttributeC)).toString(); -
148 if (name.isEmpty()) {
never evaluated: name.isEmpty()
0
149 reader.raiseError(QString::fromLatin1("Missing '%1'-attribute").arg(QString::fromLatin1(mimeTypeAttributeC))); -
150 } else {
never executed: }
0
151 data.name = name; -
152 }
never executed: }
0
153 } -
154 break;
never executed: break;
0
155 case ParseGenericIcon: -
156 data.genericIconName = atts.value(QLatin1String(nameAttributeC)).toString(); -
157 break;
never executed: break;
0
158 case ParseIcon: -
159 data.iconName = atts.value(QLatin1String(nameAttributeC)).toString(); -
160 break;
never executed: break;
0
161 case ParseGlobPattern: { -
162 const QString pattern = atts.value(QLatin1String(patternAttributeC)).toString(); -
163 unsigned weight = atts.value(QLatin1String(weightAttributeC)).toString().toInt(); -
164 const bool caseSensitive = atts.value(QLatin1String(caseSensitiveAttributeC)).toString() == QLatin1String("true"); -
165 -
166 if (weight == 0)
never evaluated: weight == 0
0
167 weight = QMimeGlobPattern::DefaultWeight;
never executed: weight = QMimeGlobPattern::DefaultWeight;
0
168 -
169 qt_noop(); -
170 const QMimeGlobPattern glob(pattern, data.name, weight, caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); -
171 if (!process(glob, errorMessage))
never evaluated: !process(glob, errorMessage)
0
172 return false;
never executed: return false;
0
173 data.addGlobPattern(pattern); -
174 } -
175 break;
never executed: break;
0
176 case ParseSubClass: { -
177 const QString inheritsFrom = atts.value(QLatin1String(mimeTypeAttributeC)).toString(); -
178 if (!inheritsFrom.isEmpty())
never evaluated: !inheritsFrom.isEmpty()
0
179 processParent(data.name, inheritsFrom);
never executed: processParent(data.name, inheritsFrom);
0
180 } -
181 break;
never executed: break;
0
182 case ParseComment: { -
183 -
184 QString locale = atts.value(QLatin1String(localeAttributeC)).toString(); -
185 const QString comment = reader.readElementText(); -
186 if (locale.isEmpty())
never evaluated: locale.isEmpty()
0
187 locale = QString::fromLatin1("en_US");
never executed: locale = QString::fromLatin1("en_US");
0
188 data.localeComments.insert(locale, comment); -
189 } -
190 break;
never executed: break;
0
191 case ParseAlias: { -
192 const QString alias = atts.value(QLatin1String(mimeTypeAttributeC)).toString(); -
193 if (!alias.isEmpty())
never evaluated: !alias.isEmpty()
0
194 processAlias(alias, data.name);
never executed: processAlias(alias, data.name);
0
195 } -
196 break;
never executed: break;
0
197 case ParseMagic: { -
198 priority = 50; -
199 const QString priorityS = atts.value(QLatin1String(priorityAttributeC)).toString(); -
200 if (!priorityS.isEmpty()) {
never evaluated: !priorityS.isEmpty()
0
201 if (!parseNumber(priorityS, &priority, errorMessage))
never evaluated: !parseNumber(priorityS, &priority, errorMessage)
0
202 return false;
never executed: return false;
0
203 -
204 }
never executed: }
0
205 currentRules.clear(); -
206 -
207 } -
208 break;
never executed: break;
0
209 case ParseMagicMatchRule: { -
210 QMimeMagicRule *rule = 0; -
211 if (!createMagicMatchRule(atts, errorMessage, rule))
never evaluated: !createMagicMatchRule(atts, errorMessage, rule)
0
212 return false;
never executed: return false;
0
213 QList<QMimeMagicRule> *ruleList; -
214 if (currentRules.isEmpty())
never evaluated: currentRules.isEmpty()
0
215 ruleList = &rules;
never executed: ruleList = &rules;
0
216 else -
217 ruleList = &currentRules.top()->m_subMatches;
never executed: ruleList = &currentRules.top()->m_subMatches;
0
218 ruleList->append(*rule); -
219 -
220 currentRules.push(&ruleList->last()); -
221 delete rule; -
222 break;
never executed: break;
0
223 } -
224 case ParseError: -
225 reader.raiseError(QString::fromLatin1("Unexpected element <%1>"). -
226 arg(reader.name().toString())); -
227 break;
never executed: break;
0
228 default: -
229 break;
never executed: break;
0
230 } -
231 break;
never executed: break;
0
232 -
233 case QXmlStreamReader::EndElement: -
234 { -
235 const QStringRef elementName = reader.name(); -
236 if (elementName == QLatin1String(mimeTypeTagC)) {
never evaluated: elementName == QLatin1String(mimeTypeTagC)
0
237 if (!process(QMimeType(data), errorMessage))
never evaluated: !process(QMimeType(data), errorMessage)
0
238 return false;
never executed: return false;
0
239 data.clear(); -
240 } else if (elementName == QLatin1String(matchTagC)) {
never executed: }
never evaluated: elementName == QLatin1String(matchTagC)
0
241 -
242 currentRules.pop(); -
243 -
244 } else if (elementName == QLatin1String(magicTagC)) {
never executed: }
never evaluated: elementName == QLatin1String(magicTagC)
0
245 -
246 -
247 QMimeMagicRuleMatcher ruleMatcher(data.name, priority); -
248 ruleMatcher.addRules(rules); -
249 processMagicMatcher(ruleMatcher); -
250 rules.clear(); -
251 }
never executed: }
0
252 break;
never executed: break;
0
253 } -
254 default: -
255 break;
never executed: break;
0
256 } -
257 }
never executed: }
0
258 -
259 if (reader.hasError()) {
never evaluated: reader.hasError()
0
260 if (errorMessage)
never evaluated: errorMessage
0
261 *errorMessage = QString::fromLatin1("An error has been encountered at line %1 of %2: %3:").arg(reader.lineNumber()).arg(fileName, reader.errorString());
never executed: *errorMessage = QString::fromLatin1("An error has been encountered at line %1 of %2: %3:").arg(reader.lineNumber()).arg(fileName, reader.errorString());
0
262 return false;
never executed: return false;
0
263 } -
264 -
265 return true;
never executed: return true;
0
266 -
267} -
268 -
269 -
270 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial