forked from popcornmix/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAutoPlaylist.cpp
100 lines (82 loc) · 2.67 KB
/
AutoPlaylist.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
/*
*
* Copyright (C) 2020 Michael J. Walsh
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string>
#include <vector>
#include <algorithm>
#include <locale>
#include <dirent.h>
#include "utils/RegExp.h"
#include "AutoPlaylist.h"
using namespace std;
void AutoPlaylist::readPlaylist(string &filename)
{
// reset object
playlist_pos = -1;
playlist.clear();
dirname.clear();
int pos = filename.find_last_of('/');
dirname = filename.substr(0, pos+1); // including trailing slash
string basename = filename.substr(pos+1);
DIR *dir = opendir(dirname.c_str());
if (!dir) {
puts("Failed to open playlist directory for reading");
return;
}
// re for filename match
CRegExp fnameext_match("\\.(3g2|3gp|amv|asf|avi|drc|f4a|f4b|f4p|f4v|flv|"
"m2ts|m2v|m4p|m4v|mkv|mov|mp2|mp4|mpe|mpeg|mpg|mpv|mts|mxf|nsv|ogg|"
"ogv|qt|rm|rmvb|roq|svi|ts|vob|webm|wmv|yuv|iso|dmg)$");
// Quit if file being played doesn't have one of the above filename extensions
if(fnameext_match.RegFind(basename, 0) == -1) {
puts("Disabling playlist as filename extension not recognised");
return;
}
struct dirent *ent;
while ((ent = readdir(dir))) {
if(ent->d_type != DT_DIR && ent->d_name[0] != '.' &&
fnameext_match.RegFind(ent->d_name, 0) > -1) {
playlist.push_back(ent->d_name);
}
}
closedir(dir);
// In English and most other European langauges, this should sort by lower case without
// regard to diacritics
const locale loc = locale("");
sort(playlist.begin(), playlist.end(), loc);
// search for file we started with
for(uint i = 0; i < playlist.size(); i++) {
if(playlist[i] == basename) {
playlist_pos = i;
return;
}
}
// strange error
puts("Weird error");
playlist.clear();
}
bool AutoPlaylist::ChangeFile(int delta, string &filename)
{
int npos = playlist_pos + delta;
int last_index = playlist.size() - 1;
if(npos < 0 || npos > last_index)
return false;
playlist_pos = npos;
filename = dirname + playlist[playlist_pos];
return true;
}