-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcdrutil.cpp
More file actions
140 lines (126 loc) · 3.79 KB
/
cdrutil.cpp
File metadata and controls
140 lines (126 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (C) 2021 Jo2003 (olenka.joerg@gmail.com)
* This file is part of cd2netmd_gui
*
* cd2netmd 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 3 of the License, or
* (at your option) any later version.
*
* cd2netmd 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
*/
#include <QFileInfo>
#include <QApplication>
#include "cdrutil.h"
#include "helpers.h"
#include <cmath>
#include <QXmlStreamReader>
CDRUtil::CDRUtil(QObject *parent)
: CCliProcess(parent)
{
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &CDRUtil::finish);
}
int CDRUtil::start()
{
QStringList params;
mLog.clear();
params << "cdtext";
qInfo() << DRUTIL_CLI << params;
run(DRUTIL_CLI, params);
return 0;
}
void CDRUtil::finish(int exitCode, ExitStatus exitStatus)
{
CDTextData cdtdata;
if ((exitCode == 0) && (exitStatus == ExitStatus::NormalExit))
{
mLog += QString::fromUtf8(readAllStandardOutput());
// find xml start- and end marker
int start = mLog.indexOf("<?xml");
int end = mLog.lastIndexOf("</plist>");
if ((start > -1) && (end > -1))
{
QString content = mLog.mid(start, 8 + end - start);
parseXml(content, cdtdata);
}
}
if (!mLog.isEmpty())
{
qDebug() << static_cast<const char*>(mLog.toUtf8());
}
emit fileDone(cdtdata);
}
//--------------------------------------------------------------------------
//! @brief parse output of drutil (xml)
//!
//! @param[in] xmlData xml data string
//! @param[in] cdtdata buffer for CD-TEXT data
//!
//! @return 0 -> ok; -1 -> error
//--------------------------------------------------------------------------
int CDRUtil::parseXml(const QString &xmlData, CDTextData &cdtdata)
{
SCDText cdt;
QXmlStreamReader xml;
xml.addData(xmlData);
enum Cap {
ARTIST,
TITLE,
NONE
} capNext = NONE;
cdtdata.clear();
while(!xml.atEnd() && !xml.hasError())
{
xml.readNext();
if (xml.isStartElement() && (xml.name() == "key"))
{
if (xml.readNext() == QXmlStreamReader::Characters)
{
if (xml.text() == "DRCDTextPerformerKey")
{
capNext = Cap::ARTIST;
}
else if (xml.text() == "DRCDTextTitleKey")
{
capNext = Cap::TITLE;
}
else
{
capNext = Cap::NONE;
}
}
}
else if ((xml.isStartElement() && (xml.name() == "string")) && (capNext != Cap::NONE))
{
if (xml.readNext() == QXmlStreamReader::Characters)
{
switch(capNext)
{
case Cap::ARTIST:
cdt.mArtist = xml.text().toString();
break;
case Cap::TITLE:
cdt.mTitle = xml.text().toString();
break;
default:
break;
}
}
capNext = Cap::NONE;
}
else if (xml.isEndElement() && (xml.name() == "dict"))
{
if (!cdt.mArtist.isEmpty() || !cdt.mTitle.isEmpty())
{
cdtdata.append(cdt);
cdt = {"", ""};
}
}
}
return cdtdata.isEmpty() ? -1 : 0;
}