33 Eskil Heyn Olsen, 2005, distributed under the GPL as part of mythtv.
44
55 Update July 2010 updated for Qt4 (Paul Harrison)
6-
6+ Update December 2012 updated to use QSettings for the pls parser
77*/
88
99// c
1515#include < QList>
1616#include < QMap>
1717#include < QStringList>
18+ #include < QFileInfo>
19+ #include < QSettings>
1820
1921// mythtv
2022#include < mythlogging.h>
2527
2628using namespace std ;
2729
28- class CfgReader
29- {
30- public:
31- CfgReader ()
32- {
33- }
34- ~CfgReader ()
35- {
36- }
37-
38- typedef QPair<QString,QString> KeyValue;
39- typedef QList<KeyValue> KeyValueList;
40- typedef QMap<QString, KeyValueList> ConfigMap;
41-
42- void parse (const char *d, int l)
43- {
44- const char *ptr = d;
45- int line = 1 ;
46- bool done = l <= 0 ;
47-
48- QString current_section = " " ;
49- KeyValueList keyvals;
50-
51- while (!done)
52- {
53- switch (*ptr)
54- {
55- case ' \0 ' :
56- done = true ;
57- break ;
58- case ' #' :
59- {
60- const char *end = strchr (ptr, ' \n ' );
61- if (!end) done = true ;
62- ptr = end;
63- break ;
64- }
65- case ' \n ' :
66- ptr ++;
67- line ++;
68- break ;
69- case ' [' :
70- {
71- ptr ++;
72- const char *nl = strchr (ptr, ' \n ' );
73- const char *end = strchr (ptr, ' ]' );
74-
75- if (!nl) nl = d + l;
76-
77- if (!end || nl < end)
78- {
79- LOG (VB_GENERAL, LOG_ERR,
80- QString (" CfgReader:: Badly formatted section, "
81- " line %1" ).arg (line));
82- done = true ;
83- }
84-
85- if (current_section.length () > 0 )
86- {
87- cfg[current_section] = keyvals;
88- keyvals = KeyValueList ();
89- }
90-
91- current_section = std::string (ptr, end - ptr).c_str ();
92- if (current_section.length () == 0 )
93- {
94- LOG (VB_GENERAL, LOG_ERR,
95- QString (" CfgReader:: Badly formatted section, "
96- " line %1" ).arg (line));
97- done = true ;
98- }
99- ptr = end + 1 ;
100- break ;
101- }
102- default :
103- {
104- if (current_section.length () > 0 )
105- {
106- const char *eq = strchr (ptr, ' =' );
107- const char *nl = strchr (ptr, ' \n ' );
108-
109- if (!nl) nl = d + l;
110-
111- if (!eq || nl < eq)
112- {
113- LOG (VB_GENERAL, LOG_ERR,
114- QString (" CfgReader:: Badly formatted line %1" )
115- .arg (line));
116- done = true ;
117- }
118- else
119- {
120- QString key = string (ptr, eq - ptr).c_str ();
121- QString val = string (eq + 1 , nl - eq - 1 ).c_str ();
122- keyvals.push_back (KeyValue (key, val));
123- ptr = nl;
124- }
125- }
126- else
127- {
128- LOG (VB_GENERAL, LOG_ERR,
129- QString (" CfgReader:: Badly formatted line %1" )
130- .arg (line));
131- done = true ;
132- }
133- break ;
134- }
135- }
136-
137- if (ptr - d == l)
138- done = true ;
139- }
140-
141- if (current_section.length () > 0 )
142- cfg[current_section] = keyvals;
143- }
144-
145- QList<QString> getSections (void )
146- {
147- QList<QString> res;
148- for (ConfigMap::iterator it = cfg.begin (); it != cfg.end (); ++it)
149- res.push_back (it.key ());
150- return res;
151- }
152-
153- QList<QString> getKeys (const QString §ion)
154- {
155- KeyValueList keylist = cfg[section];
156- QList<QString> res;
157- for (KeyValueList::iterator it = keylist.begin ();
158- it != keylist.end (); ++it)
159- {
160- res.push_back ((*it).first );
161- }
162- return res;
163- }
164-
165- QString getStrVal (const QString §ion, const QString &key,
166- const QString &def = " " )
167- {
168- KeyValueList keylist = cfg[section];
169- QString res = def;
170- for (KeyValueList::iterator it = keylist.begin ();
171- it != keylist.end (); ++it)
172- {
173- if ((*it).first == key)
174- {
175- res =(*it).second ;
176- break ;
177- }
178- }
179- return res;
180- }
181-
182- int getIntVal (const QString §ion, const QString &key, int def=0 )
183- {
184- QString def_str;
185- def_str.setNum (def);
186- return getStrVal (section, key, def_str).toInt ();
187- }
188-
189- private:
190- ConfigMap cfg;
191- };
192-
193- /* ***************************************************************************/
19430
19531PlayListFile::PlayListFile (void ) : m_version(0 )
19632{
@@ -201,30 +37,29 @@ PlayListFile::~PlayListFile(void)
20137 clear ();
20238}
20339
204- int PlayListFile::parse (PlayListFile *pls, QTextStream *stream, const QString &extension )
40+ int PlayListFile::parse (PlayListFile *pls, const QString &filename )
20541{
20642 int result = 0 ;
43+ QString extension = QFileInfo (filename).suffix ().toLower ();
20744
20845 if (extension == " pls" )
209- result = PlayListFile::parsePLS (pls, stream );
46+ result = PlayListFile::parsePLS (pls, filename );
21047 else if (extension == " m3u" )
211- result = PlayListFile::parseM3U (pls, stream );
48+ result = PlayListFile::parseM3U (pls, filename );
21249
21350 return result;
21451}
21552
216- int PlayListFile::parsePLS (PlayListFile *pls, QTextStream *stream )
53+ int PlayListFile::parsePLS (PlayListFile *pls, const QString &filename )
21754{
218- int parsed = 0 ;
219- QString d = stream->readAll ();
220- CfgReader cfg;
221- cfg.parse (d.toAscii (), d.length ());
55+ QSettings settings (filename, QSettings::IniFormat);
56+ settings.beginGroup (" playlist" );
22257
223- int num_entries = cfg. getIntVal ( " playlist " , " numberofentries" , -1 );
58+ int num_entries = settings. value ( " numberofentries" , -1 ). toInt ( );
22459
22560 // Some pls files have "numberofentries", some has "NumberOfEntries".
226- if (num_entries == -1 )
227- num_entries = cfg. getIntVal ( " playlist " , " NumberOfEntries" , -1 );
61+ if (num_entries == -1 )
62+ num_entries = settings. value ( " NumberOfEntries" , -1 ). toInt ( );
22863
22964 for (int n = 1 ; n <= num_entries; n++)
23065 {
@@ -233,23 +68,27 @@ int PlayListFile::parsePLS(PlayListFile *pls, QTextStream *stream)
23368 QString f_key = QString (" File%1" ).arg (n);
23469 QString l_key = QString (" Length%1" ).arg (n);
23570
236- e->setFile (cfg. getStrVal ( " playlist " , f_key));
237- e->setTitle (cfg. getStrVal ( " playlist " , t_key));
238- e->setLength (cfg. getIntVal ( " playlist " , l_key));
71+ e->setFile (settings. value ( f_key). toString ( ));
72+ e->setTitle (settings. value ( t_key). toString ( ));
73+ e->setLength (settings. value ( l_key). toInt ( ));
23974
24075 pls->add (e);
241- parsed++;
24276 }
24377
244- return parsed ;
78+ return pls-> size () ;
24579}
24680
24781#define M3U_HEADER " #EXTM3U"
24882#define M3U_INFO " #EXTINF"
24983
250- int PlayListFile::parseM3U (PlayListFile *pls, QTextStream *stream )
84+ int PlayListFile::parseM3U (PlayListFile *pls, const QString &filename )
25185{
252- QString data = stream->readAll ();
86+ QFile f (filename);
87+ if (!f.open (QIODevice::ReadOnly))
88+ return 0 ;
89+
90+ QTextStream stream (&f);
91+ QString data = stream.readAll ();
25392 QStringList lines = data.split (QRegExp (" [\r\n ]" ));
25493
25594 QStringList::iterator it;
0 commit comments