-
Notifications
You must be signed in to change notification settings - Fork 341
/
pls.cpp
119 lines (90 loc) · 2.68 KB
/
pls.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
/*
playlistfile (.pls) parser
Eskil Heyn Olsen, 2005, distributed under the GPL as part of mythtv.
Update July 2010 updated for Qt4 (Paul Harrison)
Update December 2012 updated to use QSettings for the pls parser
*/
// c
//#include "iostream"
#include <string>
// qt
#include <QPair>
#include <QList>
#include <QMap>
#include <QStringList>
#include <QFileInfo>
#include <QSettings>
// mythtv
#include <mythlogging.h>
// mythmusic
#include "pls.h"
using namespace std;
PlayListFile::PlayListFile(void) : m_version(0)
{
}
PlayListFile::~PlayListFile(void)
{
clear();
}
int PlayListFile::parse(PlayListFile *pls, const QString &filename)
{
int result = 0;
QString extension = QFileInfo(filename).suffix().toLower();
if (extension == "pls")
result = PlayListFile::parsePLS(pls, filename);
else if (extension == "m3u")
result = PlayListFile::parseM3U(pls, filename);
return result;
}
int PlayListFile::parsePLS(PlayListFile *pls, const QString &filename)
{
QSettings settings(filename, QSettings::IniFormat);
settings.beginGroup("playlist");
int num_entries = settings.value("numberofentries", -1).toInt();
// Some pls files have "numberofentries", some has "NumberOfEntries".
if (num_entries == -1)
num_entries = settings.value("NumberOfEntries", -1).toInt();
for (int n = 1; n <= num_entries; n++)
{
PlayListFileEntry *e = new PlayListFileEntry();
QString t_key = QString("Title%1").arg(n);
QString f_key = QString("File%1").arg(n);
QString l_key = QString("Length%1").arg(n);
e->setFile(settings.value(f_key).toString());
e->setTitle(settings.value(t_key).toString());
e->setLength(settings.value(l_key).toInt());
pls->add(e);
}
return pls->size();
}
#define M3U_HEADER "#EXTM3U"
#define M3U_INFO "#EXTINF"
int PlayListFile::parseM3U(PlayListFile *pls, const QString &filename)
{
QFile f(filename);
if (!f.open(QIODevice::ReadOnly))
return 0;
QTextStream stream(&f);
QString data = stream.readAll();
QStringList lines = data.split(QRegExp("[\r\n]"));
QStringList::iterator it;
for (it = lines.begin(); it != lines.end(); ++it)
{
// ignore empty lines
if (it->isEmpty())
continue;
// ignore the M3U header
if (it->startsWith(M3U_HEADER))
continue;
// for now ignore M3U info lines
if (it->startsWith(M3U_INFO))
continue;
// add to the playlist
PlayListFileEntry *e = new PlayListFileEntry();
e->setFile(*it);
e->setTitle(*it);
e->setLength(-1);
pls->add(e);
}
return pls->size();
}