-
Notifications
You must be signed in to change notification settings - Fork 31
/
cuedata.cpp
236 lines (191 loc) · 6.46 KB
/
cuedata.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* Flacon - audio File Encoder
* https://github.com/flacon/flacon
*
* Copyright: 2020
* Alexander Sokoloff <sokoloff.a@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "cuedata.h"
#include <QFileInfo>
#include <QFile>
#include <QDebug>
#include "types.h"
/************************************************
*
************************************************/
static QByteArray unQuote(const QByteArray &line)
{
if (line.length() > 2 && (line.at(0) == '"' || line.at(0) == '\'') && line.at(0) == line.at(line.length() - 1)) {
return line.mid(1, line.length() - 2);
}
return line;
}
/************************************************
************************************************/
static QByteArray extractFileFromFileTag(const QByteArray &value)
{
int n = value.lastIndexOf(' ');
if (n > -1)
return unQuote(value.left(n));
return unQuote(value);
}
/************************************************
*
************************************************/
CueData::CueData(QIODevice *device) noexcept(false)
{
read(device);
}
/************************************************
*
************************************************/
CueData::CueData(const QString &fileName) :
mFileName(fileName)
{
QFileInfo fi(fileName);
if (!fi.exists()) {
throw FlaconError(QObject::tr("File <b>\"%1\"</b> does not exist").arg(fileName));
}
QFile file(fi.canonicalFilePath());
if (!file.open(QIODevice::ReadOnly)) {
throw FlaconError(file.errorString());
}
read(&file);
file.close();
}
/************************************************
Complete CUE sheet syntax documentation
https://github.com/flacon/flacon/blob/master/cuesheet_syntax.md
************************************************/
void CueData::read(QIODevice *file)
{
mBomCodec = detectBomCodec(file);
uint lineNum = 0;
QByteArray tag;
QByteArray value;
QByteArray audioFile;
// Read global tags ..............................
while (!file->atEnd()) {
lineNum++;
QByteArray line = file->readLine().trimmed();
if (line.isEmpty()) {
continue;
}
parseLine(line, tag, value, lineNum);
if (tag.isEmpty()) {
continue;
}
if (tag == TRACK_TAG) {
break;
}
if (tag == FILE_TAG) {
audioFile = extractFileFromFileTag(value);
continue;
}
mGlobalTags.insert(tag, value);
}
while (!file->atEnd()) {
bool ok;
leftPart(value, ' ').toInt(&ok);
if (!ok)
throw FlaconError(QObject::tr("<b>%1</b> is not a valid CUE file. Incorrect track number on line %2.", "Cue parser error.")
.arg(mFileName)
.arg(lineNum));
Tags track;
track.insert(TRACK_TAG, leftPart(value, ' '));
track.insert(FILE_TAG, audioFile);
while (!file->atEnd()) {
lineNum++;
QByteArray line = file->readLine().trimmed();
if (line.isEmpty())
continue;
parseLine(line, tag, value, lineNum);
if (tag.isEmpty())
continue;
if (tag == TRACK_TAG)
break;
if (tag == FILE_TAG) {
audioFile = extractFileFromFileTag(value);
continue;
}
track.insert(tag, value);
if (tag.startsWith(INDEX_TAG)) {
track.insert(tag + " FILE", audioFile);
if (tag.contains(" 01")) {
track.insert(FILE_TAG, audioFile);
}
}
}
mTracks << track;
}
}
/************************************************
* Detect codepage and skip BOM
************************************************/
TextCodec::BomCodec CueData::detectBomCodec(QIODevice *file)
{
QByteArray magic = file->read(3);
if (magic.startsWith("\xEF\xBB\xBF")) {
file->seek(3);
return TextCodec::BomCodec::UTF_8;
}
if (magic.startsWith("\xFE\xFF")) {
file->seek(2);
return TextCodec::BomCodec::UTF_16BE;
}
if (magic.startsWith("\xFF\xFE")) {
file->seek(2);
return TextCodec::BomCodec::UTF_16LE;
}
file->seek(0);
return TextCodec::BomCodec::Unknown;
}
/************************************************
*
************************************************/
void CueData::parseLine(const QByteArray &line, QByteArray &tag, QByteArray &value, uint lineNum) const
{
QByteArray l = line.trimmed();
if (l.isEmpty()) {
tag = "";
value = "";
return;
}
tag = leftPart(l, ' ').toUpper();
value = rightPart(l, ' ').trimmed();
if (tag == "REM") {
tag = leftPart(value, ' ').toUpper();
value = rightPart(value, ' ').trimmed();
}
value = unQuote(value);
//=============================
if (tag == INDEX_TAG) {
bool ok;
int num = leftPart(value, ' ').toInt(&ok);
if (!ok)
throw FlaconError(QObject::tr("<b>%1</b> is not a valid CUE file. Incorrect track index on line %2.", "Cue parser error.")
.arg(mFileName)
.arg(lineNum));
if (num < 0 || num > 99)
throw FlaconError(QObject::tr("<b>%1</b> is not a valid CUE file. Incorrect track index on line %2.", "Cue parser error.")
.arg(mFileName)
.arg(lineNum));
tag = QString("%1 %2").arg(INDEX_TAG).arg(num, 2, 10, QChar('0')).toLatin1();
value = rightPart(value, ' ').trimmed();
}
}