1 Star 0 Fork 0

dou/YUView

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
plistparser.cpp 3.48 KB
一键复制 编辑 原始数据 按行查看 历史
/* YUView - YUV player with advanced analytics toolset
* Copyright (C) 2015 Institut für Nachrichtentechnik
* RWTH Aachen University, GERMANY
*
* YUView is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* YUView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YUView. If not, see <http://www.gnu.org/licenses/>.
*/
// Own includes
#include "plistparser.h"
// Qt includes
#include <QDomNode>
#include <QDomDocument>
#include <QDateTime>
#include <QDebug>
QVariant PListParser::parsePList(QIODevice *device) {
QVariantMap result;
QDomDocument doc;
QString errorMessage;
int errorLine;
int errorColumn;
bool success = doc.setContent(device, false, &errorMessage, &errorLine, &errorColumn);
if (!success) {
qDebug() << "PListParser Warning: Could not parse PList file!";
qDebug() << "Error message: " << errorMessage;
qDebug() << "Error line: " << errorLine;
qDebug() << "Error column: " << errorColumn;
return result;
}
QDomElement root = doc.documentElement();
if (root.attribute(QStringLiteral("version"), QStringLiteral("1.0")) != QLatin1String("1.0")) {
qDebug() << "PListParser Warning: plist is using an unknown format version, parsing might fail unexpectedly";
}
return parseElement(root.firstChild().toElement());
}
QVariant PListParser::parseElement(const QDomElement &e) {
QString tagName = e.tagName();
QVariant result;
if (tagName == QLatin1String("dict")) {
result = parseDictElement(e);
}
else if (tagName == QLatin1String("array")) {
result = parseArrayElement(e);
}
else if (tagName == QLatin1String("string")) {
result = e.text();
}
else if (tagName == QLatin1String("data")) {
result = QByteArray::fromBase64(e.text().toUtf8());
}
else if (tagName == QLatin1String("integer")) {
result = e.text().toInt();
}
else if (tagName == QLatin1String("real")) {
result = e.text().toFloat();
}
else if (tagName == QLatin1String("true")) {
result = true;
}
else if (tagName == QLatin1String("false")) {
result = false;
}
else if (tagName == QLatin1String("date")) {
result = QDateTime::fromString(e.text(), Qt::ISODate);
}
else {
qDebug() << "PListParser Warning: Invalid tag found: " << e.tagName() << e.text();
}
return result;
}
QVariantList PListParser::parseArrayElement(const QDomElement& element) {
QVariantList result;
QDomNodeList children = element.childNodes();
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
result.append(parseElement(e));
}
}
return result;
}
QVariantMap PListParser::parseDictElement(const QDomElement& element) {
QVariantMap result;
QDomNodeList children = element.childNodes();
QString currentKey;
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
QString tagName = e.tagName();
if (tagName == QLatin1String("key")) {
currentKey = e.text();
}
else if (!currentKey.isEmpty()) {
result[currentKey] = parseElement(e);
}
}
}
return result;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hengq/YUView.git
git@gitee.com:hengq/YUView.git
hengq
YUView
YUView
TikzExport

搜索帮助

0d507c66 1850385 C8b1a773 1850385