1 /* PLSParser.cpp */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "PLSParser.h"
22 #include "Utils/MetaData/MetaData.h"
23 
24 #include <QRegExp>
25 #include <QStringList>
26 
27 #include <algorithm>
28 
29 struct LineEntry
30 {
31 	QString key;
32 	QString value;
33 	int trackIdx;
34 
LineEntryLineEntry35 	LineEntry()
36 	{
37 		trackIdx = -1;
38 	}
39 };
40 
41 
split_line(const QString & line)42 static LineEntry split_line(const QString& line)
43 {
44 	LineEntry ret;
45 
46 	int pos_idx;
47 	QRegExp re_idx("(\\S+)([0-9]+)");
48 	QStringList splitted = line.split("=");
49 
50 	if(splitted.size() < 2){
51 		return ret;
52 	}
53 
54 	pos_idx = re_idx.indexIn(splitted[0]);
55 	if(pos_idx < 0){
56 		ret.key = splitted[0];
57 		ret.value = splitted[1];
58 		ret.trackIdx = 1;
59 	}
60 
61 	else {
62 		ret.key = re_idx.cap(1).toLower();
63 		ret.value = splitted[1];
64 		ret.trackIdx = re_idx.cap(2).toInt();
65 	}
66 
67 	return ret;
68 }
69 
70 
PLSParser(const QString & filename)71 PLSParser::PLSParser(const QString& filename) :
72 	AbstractPlaylistParser(filename) {}
73 
74 PLSParser::~PLSParser() = default;
75 
parse()76 void PLSParser::parse()
77 {
78 	QStringList lines = content().split("\n");
79 
80 	MetaData md;
81 	int cur_trackIdx = -1;
82 
83 	for(QString line : lines) {
84 		line = line.trimmed();
85 		if(line.isEmpty() || line.startsWith("#")){
86 			continue;
87 		}
88 
89 		LineEntry line_entry = split_line(line);
90 
91 		if(line_entry.trackIdx < 0){
92 			continue;
93 		}
94 
95 		if(line_entry.trackIdx != cur_trackIdx){
96 
97 			if(cur_trackIdx > 0){
98 				addTrack(md);
99 			}
100 
101 			md = MetaData();
102 			cur_trackIdx = line_entry.trackIdx;
103 		}
104 
105 
106 		md.setTrackNumber(TrackNum(line_entry.trackIdx));
107 
108 		if(line_entry.key.startsWith("file", Qt::CaseInsensitive))
109 		{
110 			QString filepath = getAbsoluteFilename(line_entry.value);
111 			md.setFilepath(filepath);
112 			md.setArtist(filepath);
113 		}
114 
115 		else if(line_entry.key.startsWith("title", Qt::CaseInsensitive))
116 		{
117 			md.setTitle(line_entry.value);
118 		}
119 
120 		else if(line_entry.key.startsWith("length", Qt::CaseInsensitive))
121 		{
122 			int len = line_entry.value.toInt();
123 
124 			len = std::max(0, len);
125 			md.setDurationMs(len * 1000);
126 		}
127 	}
128 
129 	if(!md.filepath().isEmpty()){
130 		addTrack(md);
131 	}
132 }
133 
134