-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioDirectory.h
73 lines (68 loc) · 1.86 KB
/
AudioDirectory.h
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
#pragma once
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
class AudioDirectory
{
public:
void SetPath(std::string path)
{
m_path = path;
this->Parse();
}
std::vector<std::string> GetFiles()
{
return m_files;
}
private:
void Parse()
{
DIR* dp;
struct dirent *dirp;
std::vector<std::string> dirs;
struct stat st;
dirs.push_back(m_path);
while(dirs.size() > 0)
{
if((dp = opendir(dirs[0].c_str())) != NULL)
{
while((dirp = readdir(dp)) != NULL)
{
if(std::string(".").compare(dirp->d_name) == 0
|| std::string("..").compare(dirp->d_name) == 0)
{
continue;
}
std::string filename = dirs[0] + "/" + dirp->d_name;
stat(filename.c_str(), &st);
if(S_ISDIR(st.st_mode))
{
dirs.push_back(filename);
}
else
{
if(this->string_ends_with(filename, ".mp3"))
{
m_files.push_back(filename);
}
}
}
closedir(dp);
}
dirs.erase(dirs.begin());
}
}
// no camel case here, don't know why
bool string_ends_with(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
std::string m_path;
std::vector<std::string> m_files;
};